$(document).ready(function() 
{
	$(".block").bind("mousedown", function(e) 
	{
		mouseDown(this, e) 
	});

	$(".block").bind("mouseup", function() 
	{
		mouseUp(this) 
	}); 
	
	$(".block").bind("mouseout", function() 
	{
		mouseOut(this) 
	}); 
	
	$("body").mousemove(function(e) 
	{
		mouseMove(e) 
	}); 
	
	var cookievalue; 
	$(".block").each(function() 
	{
		cookievalue = readCookie(document.domain + ".left." + this.id); 
			
		if (cookievalue != null && cookievalue != 'null') 
		{
			$("#" + this.id).css("marginLeft", cookievalue + "px"); 
			$("#" + this.id).css("marginTop", readCookie(document.domain + ".top." + this.id) + "px"); 
		}
	}); 
});

var activeElement = null;
var posX = 0;
var posY = 0;
var mouseX = 0;
var mouseY = 0;

function falsefunc() 
{
   return false;
}
   
function mouseOut(element) 
{
   if (activeElement == null) return;
   // var parentPos = $(element).parent().offset();
   $(activeElement).css("marginLeft", mouseX - posX);
   $(activeElement).css("marginTop", mouseY - posY);
}
   
function mouseDown(element, coord) 
{
	document.onmousedown = falsefunc;
	if (activeElement != null) return;
	$(".block").css('zIndex', 50);
	$(element).css('zIndex', 100);
	$(element).css("cursor", "move");
	activeElement = element;
	var oldPosition = $(element).offset();
	var parentPos = $(element).parent().offset();
	posX = coord.clientX - oldPosition.left + parentPos.left;
	posY = coord.clientY - oldPosition.top + parentPos.top;
	document.onselectstart = function() 
	{
		return false;
	}
	return false;
}
   
function mouseUp(element) 
{
   $(element).css("cursor", "pointer");
   var pos = $(element).offset();
   var ppos = $(element).parent().offset();
   createCookie(document.domain + ".left." + element.id, pos.left - ppos.left, 7);
   createCookie(document.domain + ".top." + element.id, pos.top - ppos.top, 7);
   activeElement = null;
   document.onmouseup = null;
   document.onmousedown = null;
   document.onselectstart = null;
}
   
function mouseMove(coord) 
{
   if (activeElement == null) return;
   mouseX = coord.clientX;
   mouseY = coord.clientY;
   $(activeElement).css("marginLeft", mouseX - posX);
   $(activeElement).css("marginTop", mouseY - posY);
   return false;
}

function createCookie(name, value, days) 
{
   if (days) 
   {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      var expires = "; expires=" + date.toGMTString();
   }
   else var expires = "";
   document.cookie = name + "=" + value + expires + "; path=/; ";
}

function readCookie(name) 
{
	var nameEQ = name + "=";
	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(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function removeCookies()
{
	$(".block").each(function() 
	{
		createCookie(document.domain + ".left." + this.id, null, 0);
		createCookie(document.domain + ".top." + this.id, null, 0);
	});
	window.location.reload();
}
