/*
	Basic Javascript Functions for the Siranos system

	© Copyright Gerwood Stewart 2003
	Version: 1.0.1
	Date: 30-07-2003
*/


//Determine Browser Type.
// not interested in older browsers. If you use them its your problem
var N = (document.all) ? 0 : 1;

var MozVer = 0;
if(navigator)
{
	MozVer = navigator.appVersion.replace(/\s\(.*\)/g,'');
}

// OB is the currently active object.
var OB = null;

var MX = MY = 0;
var OID = false;
var ddActive = false;
//System Activity Codes. These indicate particular actions.
//Old code. Here for temporary compatibility
var rm = false;

//Find the X position of an element to the document window.
function findX (tob)
{
	var X = 0;

	while(tob.offsetParent)
	{
		X += tob.offsetLeft;
		tob = tob.offsetParent;
	}
	
	return X;
}

//Find the Y position of an element to the document window.
function findY (tob)
{
	var Y = 0;
	while(tob.offsetParent)
	{
		Y += tob.offsetTop;
		tob = tob.offsetParent;
	}
	
	return Y;
}

//get node will read backwards through the nodeTree
//starting from startingNode and continue until it finds a node of nodeTag.
//if it reaches the body node before finding a match it will return 0.
//it will not include the startingNode in it's search (only it's parents).
//it also won't accept 'body' as the nodeTag
function getNode (startingNode,nodeTag)
{
	nodeTag = nodeTag.toLowerCase();
	var n = startingNode.parentNode;

	while(n.tagName.toLowerCase() != nodeTag && n.tagName.toLowerCase() != 'body' && n.nodeType == 1)
	{
		n = n.parentNode;
	}
	
	if(n.nodeType == 1 && n.tagName.toLowerCase() == nodeTag)
	{
		return n;
	}

	//this is a failure.
	return 0;	
}

function _addEvent (addTo,eAction,func)
{
	if(addTo.nodeType == 1 || addTo.nodeType == 9 || addTo.nodeType == -1)
	{
		if(N)
		{
			addTo.addEventListener(eAction,func,false);
		}
		else
		{
			addTo.attachEvent('on'+eAction,func);
		}
	}
}

//close the current window (called closepopup because it is generally used by the popups).
function closepopup(myLink,closeonly)
{
	if (! (window.focus && window.opener))return true;
	window.opener.focus();
	if (! closeonly)window.opener.location.href=myLink.href;
//  if (closeme)window.close();
	return false;
}

//open a popup window.
function popup(myURL,windowName,width,height,features,pos,resize)
{
	if(!windowName) { windowName = 'temp_window';}
	if(!height) { height = 400;}
	if(!width) { width = 500;}
	if(!window.focus)
		return;
	var myWin = window.open(myURL,windowName,"height="+height+",width="+width+","+features);
	if ((document.window != null) && (!myWin.opener))
		myWin.opener = document.window;
	
	if(pos == 1)
	{
			window.focus;
			myWin.moveTo((document.body.clientWidth/2)-(width/2),(document.body.clientHeight/2)-(height/2));
	}

	myWin.focus;

	return myWin;
}

function centerDialog ()
{
	window.moveTo(
		(screen.availWidth/2)-(document.body.offsetWidth/2),
		(screen.availHeight/2)-(document.body.offsetHeight/2)
	);
//	setTimeout(function () {
//		alert(document.body.offsetWidth+"-"+screen.availWidth+"-"+window.offsetWidth+"-"+window.pageXoffset);
//		},300);
}

function fixSize (Element,Padding)
{
	var El = document.getElementById(Element);
	var body = document.body;
	window.resizeTo(1,1);
	if(Padding)
	{
		window.resizeTo(
			El.offsetWidth+25,
			El.offsetHeight+50
		);
	}
	else
	{
		if(N)
		{
			resizeTo(El.offsetWidth,window.innerHeight);
		}
		else
		{
			window.resizeBy(
				El.offsetWidth-body.offsetWidth,
				El.offsetHeight-body.offsetHeight
			);
		}
	}
}

function bringToFront ()
{
	window.focus();
}

//Panel needs to be expanded into it's own file.
function GSPanel (title,panel,anchortype)
{
	this.title = title;
	this.panel = obj;
	this.anchortype = anchortype;
}

//goes with the GSPanel object
function toggle (action)
{
	//since another window command is being passed clear the timeout
	//this prevents a stack of unclearable timeouts
	window.clearTimeout(currentMenu.timeout);
	//first deal with the previous menu.
	if(currentMenu.title && (currentMenu.title != sentMenu.title))
	{
		window.clearTimeout(currentMenu.timeout);
		hideMenu();
	}
	else if(currentMenu.title && action == true)
	{
		window.status = ("Failed:"+currentMenu.title+":"+action);
		window.clearTimeout(currentMenu.timeout);
		return false;
	}

	currentMenu = this;
	window.status = ("Status:"+currentMenu.title+":"+action+":"+currentMenu.menu.style.visibility);
	if(action)
	{
		window.clearTimeout(currentMenu.timeout);
		currentMenu.menu.style.visibility = 'visible';
		if(currentMenu.up && currentMenu.up != '')
		{
			currentMenu.button.src = currentMenu.up;
		}
		currentMenu.status = true;
	}
	else
	{
		currentMenu.timeout = window.setTimeout("hideMenu()",200);
	}
}



function goTo(url)
{
	window.location = url;
}

function save(myForm)
{
	myForm.submit();
}

function embed (path,ext,key) 
{
	ext = ext.toLowerCase();
	if(ext == 'gif' || ext == 'jpg' || ext == 'png')
	{
		key = document.getElementById(key);
		key.value = path;
	}
}

//Used by the admin system to highlight menu items
// the colours really shouldn't be here but that will be fixed later.
var highcolor = '#E2EAF2';
var lowcolor = '#446688';
var normcolor = '#99B0C7';

function high (item)
{
	item.style.background = highcolor;
}

function low (item)
{
	item.style.background = lowcolor;
}

function norm (item)
{
	item.style.background = normcolor;
}



// These functions are more like registries for actions that need to happen on
// a particular event.

function MD (e)
{
	if(rm)
	{
		rm.hide();
	}
}

function MM (e)
{
	if(N)
	{
		//set the mouse position
		MX = e.pageX;
		MY = e.pageY;

		if(OB == null)
		{
		}
		else if(OB.type == 'dragdrop')
		{
			OB.MM(MX,MY);
		}
	}
	else
	{
		//set the mouse position
		MX = event.clientX;
		MY = event.clientY;

		if(OB == null)
		{
		}
		else if(OB.type == 'dragdrop')
		{
			OB.MM(MX,MY);
		}
	}
	
	if(!ddActive && OID)
	{
		OID = false;
	} 

	if(!N && (ddActive || OID))
	{
		return false;
	}	

}

function MU (e)
{
	if(OB == null)
	{
	}
	else if(OB.type == 'dragdrop')
	{
		OB.MU()
	}
}

function MR (e)
{
}

function MOV (e)
{
}

function MOU (e)
{
}

//Here we set the functions for each event.
if (N)
{
	document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP | Event.MOUSEOVER | Event.MOUSEOUT | Event.CLICK);
}

document.onmousedown = MD;
document.onmousemove = MM;
document.onmouseup = MU;
document.oncontextmenu = MR;
document.onmouseover = MOV;
document.onmouseout = MOU;
