/**
* The contents of this file is protected by Copyright, Rom&Ram, Mikael Larsson 2005
* Please feel free to read and learn from this as I have from others
* But contact me before using it as is. mailto:mikael.larsson@romram.se
**/

var moveObj = null;
var x = 0; // mouseX
var y = 0; // mouseY

function getObject(id)
{
	obj = window.document.getElementById(id);
	if (obj==null)
	{
		throw "objectcontrol.js::getObject()::There is no object with id " + id;
	}
	return obj;
}
function getElementStyle(id, IEStyleProp, CSSStyleProp)
{
    var obj = getObject(id);
    if (obj.currentStyle)
    {
        return obj.currentStyle[IEStyleProp];
    }
    else
    if (window.getComputedStyle)
    {
        var compStyle = window.getComputedStyle(obj, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}
function saveCurrentObjectPos()
{
	saveObjectPos(moveObj.id, moveObj.id);
}
function saveObjectPos(id, name)
{
	try
	{
		obj = getObject(id);
		setCookie(name, obj.style.left + ":" + obj.style.top + ":" + obj.style.visibility);
	}catch(er){}
}
function loadObjectPos(id)
{
	pos = getCookie(id);
	if (pos!=null)
	{
		obj = getObject(id);
		if (obj!=null)
		{
			obj.style.left = pos.split(":")[0];
			obj.style.top = pos.split(":")[1];
			obj.style.visibility = pos.split(":")[2];
		}
	}
}
function selectObject(id, e)
{
	getObject("viewportworkarea").style.zIndex=9; // Make viewportworkarea the working area for moving
	getMouseXY(e);
	moveObj = getObject(id);
	var marginLeft = getElementStyle(id, "marginLeft", "margin-left");
	var marginTop = getElementStyle(id, "marginTop", "margin-top");
	moveObj.diffX = x-moveObj.offsetLeft+parseInt(marginLeft);
	moveObj.diffY = y-moveObj.offsetTop+parseInt((marginTop=="auto")?0:marginTop);
//	window.status = "X:" + moveObj.diffX + " Y:" + moveObj.diffY + " L:" + parseFloat(marginLeft);
//	getObject("viewportworkarea").style.cursor='move';
	move(e);
}
function releaseObject(id)
{
	if (moveObj !=null)
	{
		if (moveObj.id == id || id == 'all')
		{
			saveCurrentObjectPos();
			moveObj = null;
		}
		getObject("viewportworkarea").style.zIndex=0; // Move viewportworkarea down again
//		getObject("viewportworkarea").style.cursor='default';
		showObject('empty');
		getObject('empty').select();
		hideObject('empty');
		focusFirst();
	}
}
function getMouseXY(e)
{
	e = (e) ? e : ((event) ? event : null);
	if (e)
	{
		x=e.x;
		if (!x)
		{
			x=e.pageX;
		}
		y=e.y;
		if (!y)
		{
			y=e.pageY;
		}
	}
}
function move(e)
{
	if (moveObj!=null)
	{
		getMouseXY(e);
		{
			moveObj.style.visibility = "visible";
//			window.status="x:" + x + " y:" + y + " moveObj.diffX:" + moveObj.diffX + " moveObj.diffY:" + moveObj.diffY;
			moveObj.style.left = (x-moveObj.diffX) + "px";
			moveObj.style.top = (y-moveObj.diffY) + "px";
			return false;
		}
	}
	return true;
}

function showObject(id)
{
	try
	{
		getObject(id).style.visibility = "visible";
	}
	catch(er)
	{}
}

function hideObject(id)
{
	getObject(id).style.visibility = "hidden";
}

function toggleObject(id)
{
	var v = getObject(id).style.visibility;
	if (getObject(id).style.visibility=='hidden')
	{
		getObject(id).style.visibility='inherit';
	}
	else
	{
		getObject(id).style.visibility='hidden';
	}
}

function setPxTop(obj, newPxTop)
{
	if (obj!=null && newPxTop>-1)
	{
		obj.style.top = newPxTop + "px";
	}
}
function setPxWidth(obj, newPxWidth)
{
	if (obj!=null && newPxWidth>-1)
	{
		obj.style.width = newPxWidth + "px";
	}
}
function setPxHeight(obj, newPxHeight)
{
	if (obj!=null && newPxHeight>-1)
	{
		obj.style.height = newPxHeight + "px";
	}
}
function setPxSize(obj, newPxWidth, newPxHeight)
{
	setPxWidth(obj, newPxWidth);
	setPxHeight(obj, newPxHeight);
}
function setDimension(id, top, width, height, left)
{
	obj = getObject(id);
	if (obj!=null)
	{
		try
		{
			obj.style.top = top;
			obj.style.width = width;
			obj.style.height = height;
			obj.style.left = left;
		}
		catch (e)
		{
		}
	}
}
function focusFirst()
{
	var button = null;
	var form = document.forms[0]
	if (form!=null)
	{
		for (i=0; i < form.length; i++)
		{
			var element = form.elements[i];
			if (element.type=='text')
			{
				break;
			}
			if (element.type=='submit')
			{
				button = element;
			}
		}
		if (element==null && button!=null) element = button;
		if (element!=null && (element.type=='text') || (element.type=='submit'))
		{
			try
			{
				element.focus();
				if (element.type=='text')
				{
					element.select();
				}
			}
			catch(er)
			{
			}
		}
	}
}
function setCookie(name, value)
{
	document.cookie = name+"="+value;
//	window.status = document.cookie;
}
function getCookie(name)
{
	var value = null;
	var split = document.cookie.split("; ");
	for (i=0;i<split.length;i++)
	{
		var aCookie = split[i];
		var cookiesplit = aCookie.split("=");
		if (cookiesplit[0]==name)
		{
			value=cookiesplit[1];
//			window.status = aCookie;
		}
//		alert(split[i]);
	}
	return value;
}
