var gDragObj = null;
var gDragGhost = null;
var gMouseOffset = null;
var gGridSize = 5;
//var gGrid = null;

$(document).mousemove(function(evt) { 
  if(gDragObj)
  {
    var dragPos = {x: evt.pageX - gMouseOffset.left, y: evt.pageY - gMouseOffset.top};
    
    gDragGhost.css("left", dragPos.x);
    gDragGhost.css("top", dragPos.y);
    
    if(Math.round(dragPos.x % gGridSize) == 0)
    {
      gDragObj.css("left", Math.round(dragPos.x/10)*10);
    }
    if(Math.round(dragPos.y % gGridSize) == 0)
    {          
      gDragObj.css("top", Math.round(dragPos.y/10)*10);
    }
  }
});
$(document).mouseup(function() { 
  if(gDragObj)
  {
    gDragGhost.remove();
    gDragGhost = null;
    
    SetCookie(gDragObj.attr("id") + "_x", gDragObj.position().left, 30);
    SetCookie(gDragObj.attr("id") + "_y", gDragObj.position().top, 30);
    gDragObj = null;
    //gGrid.fadeOut(100);
  }
});
  
function DragInitialize()
{
  $(".drag-handle").mousedown(function(evt) 
  { 
    evt.preventDefault();
    var block_pos = $(this).closest(".drag").position();
    gMouseOffset =  {left: evt.pageX - block_pos.left, top: evt.pageY - block_pos.top};
    gDragObj = $(this).closest(".drag");
    gDragGhost = $(this).closest(".block").clone();
    gDragGhost.css("opacity", 0.5);
    gDragObj.parent().append(gDragGhost);
    //gGrid.fadeIn(100);
  });
  $(".drag").each(function(){ 
    var x = Number(GetCookie($(this).attr("id") + "_x"));
    if(!isNaN(x) && x != 0) $(this).css("left", x);
    var y = Number(GetCookie($(this).attr("id") + "_y"));
    if(!isNaN(y) && y != 0) $(this).css("top", y); 
  });

  //BuildGrid();
}

function SetCookie(key, val, days)
{
  var date = new Date();
  date.setTime(date.getTime()+(days*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();

  document.cookie = key + "=" + val + expires;
}

function UnsetCookie(key)
{
  var date = new Date();
  date.setTime(date.getTime()-(99*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
  
  var val = GetCookie(key);
  
  document.cookie = key + "=" + val + expires;
}

function GetCookie(key)
{
  var keypart = key + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(keypart) == 0) return c.substring(keypart.length, c.length);
  }
  return null;
}

function ResetLayout()
{
  $(".drag").each(function() {
      var id = $(this).attr('id');
      UnsetCookie(id + "_x");
      UnsetCookie(id + "_y");
    });
  location.reload();
}
      
/*function BuildGrid()
{
  var dWidth = document.width > window.outerWidth ? document.width : window.outerWidth;
  var dHeight = document.height > window.outerHeight ? document.height : window.outerHeight;
  var w = Math.ceil(dWidth / gGridSize);
  var h = Math.ceil(dHeight / gGridSize);
  
  gGrid = $("<div id=\"grid\">&nbsp;</div>").appendTo("body").css("display", "none");
  gGrid.css("width", dWidth);
  gGrid.css("height", dHeight);
  /*for(var y = 0; y < h; y++)
  {
    for(var x = 0; x < w; x++)
    {
      var cell = $("<div />").appendTo(gridBase);
      cell.css("width", gGridSize-2);
      cell.css("height", gGridSize-2);
    }
  }/
}*/
