/**
 * File containing block releated functions.
 */

/******************************
 * Context menu related stuff *
 ******************************/

var currentContextMenu=null;

/** 
 * Context menu object.
 */
function ContextMenuClass()
{
	this.childes=Array();
	this.orgFunc=Array();
	// We need to have a "universal" click that will remove the popup again.
	// This click will then not do ANYTHING else than remove the popup.
	// This is faked with a layer that is behind the context menu and invisible
	// but clickable.
	this.background=null;
	this.xPos=0;
	this.yPos=0;
	
	this.SetPos = function (x,y)
	{
		var scrollbars=getScrollXY();
		this.xPos=x+scrollbars[0];
		this.yPos=y+scrollbars[1];
	};
	
	this.SetBackground = function()
	{
		this.background=document.createElement("div");
		this.background.className="sysContextBg";
		this.background.style.zIndex=currentPopupZIndex+10;
		this.background.onclick=RemoveContextMenu;
		this.background.oncontextmenu=RemoveContextMenu;
		this.background.style.height=getDocHeight()+"px";
		document.getElementById("popUpContainer").appendChild(this.background);
	};
	this.SetBackground();
	
	this.ShowMenu = function (obj,id)
	{
		if (this.childes.length==0) return;
		var scrollbars=getScrollXY();
		var x=this.xPos, y=this.yPos;
		var screen=GetScreenSize();
		if (obj)
		{
			var xy=GetXyCordForObj(obj);
			x=xy[0];
			y=xy[1];
			x+=obj.offsetWidth+3;
		}
		this.childes[id].style.display='block';

		// Need to check if it is outside visible...
		if (x+this.childes[id].offsetWidth>screen.w+scrollbars[0])
		{
			if (obj)
				x=x-obj.offsetWidth-this.childes[id].offsetWidth-3;
			else
				x=screen.w-this.childes[id].offsetWidth+scrollbars[0];
		}
		
		if (y+this.childes[id].offsetHeight>screen.h+scrollbars[1])
			y=screen.h-this.childes[id].offsetHeight+scrollbars[1];
		
		this.childes[id].style.left=x+"px";
		this.childes[id].style.top=y+"px";
		if (obj)
			obj.parentNode.parentNode.parentNode.lastShowed=this.childes[id];
	};
	
	this.AddMenu = function (element,html,index)
	{
		var len=this.childes.length;
		var obj=document.createElement("div");
		obj.className="sysContextMenu";
		obj.id=element;
		// They are created in reverse order and we can assume that not more
		// than 100 submenues are created to one menu.
		obj.style.zIndex=currentPopupZIndex+(111-index);
		obj.innerHTML=html;
		
		document.getElementById("popUpContainer").appendChild(obj);
		this.childes[len]=obj;
		// Now we like to hook up some functions.
		var elements=obj.getElementsByTagName("tr");
		var len2=elements.length;
		for(var i=0;i<len2;i++)
		{
			// First we keep the onclick.
			elements[i].orgFunc=elements[i].onclick;
			elements[i].onclick=DoContextClick;
			// Now we need to do mostly the same with onMouseOver
			if (elements[i].onmouseover)
				elements[i].orgOnmouseover=elements[i].onmouseover;
			elements[i].onmouseover=DoContextMouseOver; 
		}
		// For debug:
//		if (index!=len) alert("Error in childes. Got: "+len+", expected: "+index);
	};
	
	this.Destroy = function ()
	{
		// Remove background.
		if (this.background)
		{
			this.background.parentNode.removeChild(this.background);
			this.background=null;
		}
		var len=this.childes.length;
		for (var i=0; i<len; i++)
		{
			// Need to null this so we dont get a loop the garbage collector cant handle.
			this.childes[i].lastShowed=null;
			this.childes[i].parentNode.removeChild(this.childes[i]);
			delete this.childes[i];
		}
	};
}

/**
 * 
 */
function DoContextClick()
{
	f=this.orgFunc;
	RemoveContextMenu();
	if (!f) return ;
	f();
}

function ContextRecursiveHide(obj)
{
	if (obj.lastShowed) ContextRecursiveHide(obj.lastShowed);
	obj.lastShowed=null;
	obj.style.display='none';
}

function DoContextMouseOver(e)
{
	if (this.parentNode.parentNode.parentNode.lastShowed) ContextRecursiveHide(this.parentNode.parentNode.parentNode.lastShowed);
	if (this.orgOnmouseover) this.orgOnmouseover();
}

function RemoveContextMenu(e)
{
	if (currentContextMenu)
	{
		currentContextMenu.Destroy();
		delete currentContextMenu;
		currentContextMenu=null;
	}
	// This can also be called as a event so therefore we want to cancle any
	// bouble down 
	return false;
}

function doContextMenu(e,ident, className,data,fixedUnderCM)
{
	if (!e) e = window.event;
	if (e) e.cancelBubble=true;
	if (e && e.stopPropagation) e.stopPropagation();
	RemoveContextMenu();
	currentContextMenu=new ContextMenuClass();
	if (fixedUnderCM!=null)
	{
		var obj=document.getElementById(className+"_"+fixedUnderCM);
		var xy=GetXyCordForObj(obj);
		currentContextMenu.SetPos(xy[0],xy[1]+obj.offsetHeight);
	}
	CreateContextMenu(ident,className,data);
}

function eventContextMenu(event, ident, className, data)
{
	if (!event) event = window.event;
	if (event) event.cancelBubble=true;
	if (event && event.stopPropagation) event.stopPropagation();
	doContextMenu(event, ident, className, data);
	currentContextMenu.SetPos(event.clientX, event.clientY);
	return false;
}

function MapContextMenu(left,top, ident, className, data)
{
	doContextMenu(null, ident, className, data);
	
	currentContextMenu.SetPos(left,top);
	//currentContextMenu.SetPos(10,30);
	return false;
}

/**
 * Sets a timout on a function and run this function if the spessified object still exist.
 * 
 * Strange function? Yes. Well, point is: If a "element" has not been refreshed in "time" millisecond
 * we like to run function "func". But we dont want it runned if it has been refreshed becouse then 
 * it might have set a new timer and "this" timer shall be cancled.
 * 
 * What we do is: We set the "ident" on "element". If "Ident" still exist after "time" on "element"
 * we know that elemnt has not been replaced and we can run "func"
 * @param func The func to be runned·
 * @param time Time in millisecond.
 * @param element The ID of the element.
 * @param ident The ident that is "uniqe"
 */
function TimeoutOnID(func,time,element,ident)
{
	var obj=document.getElementById(element);
	if (!obj) alert(element);
	
	obj.timeOutID=ident;
	setTimeout("DoTimeoutOnID('"+func.replace(/'/g,"\\'")/* " */+"','"+element+"',"+ident+')',time);
}

function DoTimeoutOnID(func,element,ident)
{
	var obj=document.getElementById(element);
	if (!obj) return;
	if (obj.timeOutID==ident)
		eval(func);
}

/*
 * Some menu stuff:
 */
function SetUpMenuFunctions()
{
	var elements;
	var obj=document.getElementById("sysMenuBlock");
	if (!obj) return;
	elements=obj.getElementsByTagName("li");
	var len2=elements.length;
	for(var i=0;i<len2;i++)
	{
		elements[i].orgClass=elements[i].className;
		elements[i].onmouseover=OnMenuMouseOver;
		elements[i].onmouseout=OnMenuMouseOut;
		// Also need to cancle bubble:
		elements[i].orgOnClick=elements[i].onclick;
		elements[i].onclick=OnMenuClick;
	}
}

function OnMenuClick(e)
{
	if (!e) e = window.event;
	e.cancelBubble=true;
	if (e && e.stopPropagation) e.stopPropagation();
	if (this.orgOnClick) this.orgOnClick(); 
}

function OnMenuMouseOver(e)
{
	if (!e) e = window.event;
	this.className="sysMenuOver";
	e.cancelBubble=true;
	if (e && e.stopPropagation) e.stopPropagation();
}

function OnMenuMouseOut(e)
{
	if (!e) e = window.event;
	this.className=this.orgClass;
	e.cancelBubble=true;
	if (e && e.stopPropagation) e.stopPropagation();
}

function OnDBListRollOver(e)
{
	if (!e) e = window.event;
	this.className=this.DBListOrgClass+"Hover"+(this.ctrlClicked?"Selected":"");
	e.cancelBubble=true;
	if (e && e.stopPropagation) e.stopPropagation();
}

function OnDBListRollOut(e)
{
	if (!e) e = window.event;
	this.className=this.DBListOrgClass+(this.ctrlClicked?"Selected":"");
	e.cancelBubble=true;
	if (e && e.stopPropagation) e.stopPropagation();
}

function OnDBListOnCTRLClick(e)
{
	// We want be here if we dont have CTRL click enabled.
	if (!e) e = window.event;
	if (e.ctrlKey)
		this.ctrlClicked=!this.ctrlClicked;
	else
	{
		// Remove all:
		var obj=document.getElementById(this.parentBlock);
		elements=obj.getElementsByTagName("tr");
		var len2=elements.length;
		for(var i=0;i<len2;i++)
		{
			if (elements[i]!=this)
			{
				elements[i].ctrlClicked=false;
				elements[i].className=elements[i].DBListOrgClass;
			}
		}
		this.ctrlClicked=true;
	}
	// We assume hover when CTRL clicked (How can you not if you manage to click. ;-)
	this.className=this.DBListOrgClass+"Hover"+(this.ctrlClicked?"Selected":"");
	if (this.orgonclick)
		this.orgonclick(e);
}

function OnDBListSetCTRLClick(item)
{
	if (!item) return;
	item.ctrlClicked=true;
	if (!item.DBListOrgClass) return;
	item.className=item.DBListOrgClass+"Selected";
}

function DBListEnableCTRLClick(elm)
{
	var obj=document.getElementById(elm);
	if (!obj) return;
	obj.CTRLClickEnabled=true;
}

function EnableDBListRollOver(element)
{
	var obj=document.getElementById(element);
	if (!obj) return;
	elements=obj.getElementsByTagName("tr");
	var len2=elements.length;
	for(var i=0;i<len2;i++) //len2;i++)
	{
//		alert(elements[i].className);
		if (elements[i].className!="" && elements[i].onclick!=null)
		{
			elements[i].parentBlock=element;
			elements[i].DBListOrgClass=elements[i].className;
			elements[i].orgonmouseover=elements[i].onmouseover;
			elements[i].orgonmouseout=elements[i].onmouseout;
			elements[i].onmouseover=OnDBListRollOver;
			elements[i].onmouseout=OnDBListRollOut;
			if (elements[i].CTRLClickEnabled)
			{
				elements[i].ctrlClicked=false;
				elements[i].orgonclick=elements[i].onclick;
				elements[i].onclick=OnDBListOnCTRLClick;
			}
		}
	}
}

/**
 * This code is not directly related to a block, but more to the system as is. 
 * Its part of the "Get location progress bar stuff"
 */

var getLocationsElements=null;
var lastGetLocationIdent=0;
var getLocationsPopupElm="";
function GetLocationUpdateProgText(curr,max)
{
	var obj=document.getElementById("sysGetLocProgText");
	if (!obj) return;
	obj.innerHTML=curr+"/"+max;
	obj=document.getElementById("sysGetLocProgBar");
	if (!obj) return;
	obj.style.width=Math.round(200*curr/max)+"px";
}

function doGetLocations(elements,popupElm)
{
	getLocationsPopupElm=popupElm;
	lastGetLocationIdent++;
	getLocationsElements=elements;
	getLocationsElements.num=parseInt(getLocationsElements.num);
	GetLocationUpdateProgText(0,getLocationsElements.num);
	ajaxDoGetLocation(lastGetLocationIdent,1,getLocationsElements["loc1"]);
}

function doGetLocationUpdate(ident,curr,elm,location)
{
	if (ident!=lastGetLocationIdent) return;
	var obj=document.getElementById(elm);
	if (!obj) return;
	if (getLocationsElements.callBack)
		eval(getLocationsElements.callBack+"('"+elm+"','"+location+"');");
	obj.innerHTML=location;
	curr=parseInt(curr);
	GetLocationUpdateProgText(curr,getLocationsElements.num);
	if (curr<getLocationsElements.num)
		ajaxDoGetLocation(ident,curr+1,getLocationsElements["loc"+(curr+1)]);
	else
		closeGetLocation();
}

function closeGetLocation()
{
	RemovePopup(getLocationsPopupElm);
	// Just to stop any action running:
	lastGetLocationIdent++;
}

