var isMSIE = document.attachEvent != null;
var isGecko = !document.attachEvent && document.addEventListener;

var DraggingItem = new Object();

function StartDrag (event, _this, _afteraction)
{
	st = 1;
	DraggingItem.This = _this;
	DraggingItem.AfterAction = _afteraction;

	var position = new Object();
	if (isMSIE)
	{
		position.x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		position.y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (isGecko)
	{
		position.x = event.clientX + window.scrollX;
		position.y = event.clientY + window.scrollY;
	}

	DraggingItem.cursorStartX = position.x;
	DraggingItem.cursorStartY = position.y;

	DraggingItem.StartLeft = parseInt (DraggingItem.This.style.left);
	DraggingItem.StartTop = parseInt (DraggingItem.This.style.top);

	if (isNaN (DraggingItem.StartLeft)) DraggingItem.StartLeft = 0;
	if (isNaN (DraggingItem.StartTop)) DraggingItem.StartTop = 0;

	if (isMSIE)
	{
		document.attachEvent ("onmousemove", ProceedDrag);
		document.attachEvent ("onmouseup", StopDrag);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (isGecko)
	{
		document.addEventListener ("mousemove", ProceedDrag, true);
		document.addEventListener ("mouseup", StopDrag, true);
		event.preventDefault();
	}
}

function ProceedDrag (event)
{
	var position = new Object();

	if (isMSIE) {
		position.x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		position.y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (isGecko)
	{
		position.x = event.clientX + window.scrollX;
		position.y = event.clientY + window.scrollY;
	}	

	var nextX = DraggingItem.StartLeft + position.x - DraggingItem.cursorStartX;

	DraggingItem.This.style.left = nextX + "px";


	DraggingItem.This.style.left = nextX + "px";

	var nextY = DraggingItem.StartTop  + position.y - DraggingItem.cursorStartY;

	DraggingItem.This.style.top = nextY + "px";


	DraggingItem.This.style.top = nextY + "px";
	
	if (isMSIE)
	{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (isGecko) event.preventDefault();
}

function StopDrag (event)
{	
	if (isMSIE)
	{
		document.detachEvent ("onmousemove", ProceedDrag);
		document.detachEvent ("onmouseup", StopDrag);
	}
	if (isGecko)
	{
		document.removeEventListener ("mousemove", ProceedDrag, true);
		document.removeEventListener ("mouseup", StopDrag, true);
	}

	if (DraggingItem.AfterAction) DraggingItem.AfterAction (DraggingItem.This);

	SaveDesktop();
}

function SaveDesktop()
{
	var draggables = document.getElementsByTagName ('div');
	var desktopLayout = '';
	
	for (var c = 0; c != draggables.length; c++)
	{
		var current = draggables[c];
		if (current.className == 'met')
		{
			desktopLayout += current.id + '<' + parseInt (current.style.left) + ',' + parseInt (current.style.top) + '>;';
		}
	}

new Ajax('/fresh.php?x='+xglob+'&y='+yglob+'&cols='+cols+'&rows='+rows+'&zoom='+zoom+'&layoutn='+desktopLayout, { method: 'get', update: $('frameinsite1')}).request();
	st = 0;

}

function PutBack (item)
{
	item.style.zIndex = 2;;
}

function setCookie (name, value, path)
{

	document.cookie = name + '=' + escape (value)
		+ ((path == null)? '' : ('; path=' + path));
}
