var isMSIE = document.attachEvent != null;
var isGecko = !document.attachEvent && document.addEventListener;

var DraggingItem = new Object();

currentWin = null;

function StartDrag (event, _caller, _window) {

	DraggingItem.Caller = _caller;
	DraggingItem.Window = _window;
	
	DraggingItem.Window.style.zIndex = 100;
	if (currentWin) currentWin.style.zIndex = 2;
	currentWin = DraggingItem.Window;

	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.Window.style.left);
	DraggingItem.StartTop = parseInt(DraggingItem.Window.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;
//	if (nextX < -150) nextX = -150;
	DraggingItem.Window.style.left = nextX + "px";

	var nextY = DraggingItem.StartTop + position.y - DraggingItem.cursorStartY;
//	if (nextY > 360) nextY = 360;
	DraggingItem.Window.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);
	}

//	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 == 'draggable')
		{
			desktopLayout += current.id + '(' + parseInt (current.style.left) + ',' + parseInt (current.style.top) + ');';
		}
	}
	
	var expires = new Date();
	expires = new Date (expires.getYear() + 1901, expires.getMonth(), 1);
	
	setCookie ('layoutN', desktopLayout, expires, '/');
//	CountItems();
}

function CountItems()
{
	var draggables = document.getElementsByTagName ('div');
	var desktopLayout = '';
	
	var count = 0;
	for (var c = 0; c != draggables.length; c++)
	{
		var current = draggables[c];
		if (current.className == 'draggable')
		{
			var left = parseInt (current.style.left);
			var top = parseInt (current.style.top);
			if (left > -20 && left < 480 && top > -20 && top < 390) count++;
		}
	}
	
	var ItemsCounter = document.getElementById ('ItemsCounter');
	if (ItemsCounter)
	{
		var flexia = '';
		switch (count)
		{
			case 1: 
				break;
			case 2: 
			case 3: 
			case 4: 
				flexia = 'а'; break;
			default:
				flexia = 'ов'; break;	
		}

		ItemsCounter.innerHTML = count ? count + ' предмет' + flexia : ' ничего нет';
	}
}

function PutBack (item)
{
	item.style.zIndex = 2;;
}

function setCookie (name, value, expire, path)
{
	document.cookie = name + '=' + escape (value)
		+ ((expire == null)? '' : ('; expires=' + expire.toGMTString()))
		+ ((path == null)? '' : ('; path=' + path));
}

