// 

/* window 'load' attachment */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function doLogout(){
	window.open(".");
	this.close();
}
// 
function openDialogWindow(strURL,intWIDTH,intHEIGHT,boolMODAL,vArguments){
	strWidth = "dialogWidth:"+intWIDTH+"px;";
	strHeight = "dialogHeight:"+intHEIGHT+"px;";
	strFeatures =	strWidth +
					strHeight +
					"center: yes;" + 
					"status: no;" + 
					"resizable: no;"
					
	switch( boolMODAL ){
		case true:
			var result = window.showModalDialog(strURL,vArguments,strFeatures);
			break
		case false:
			var result = window.showModelessDialog(strURL,vArguments,strFeatures);
			break
		default:
			alert("TBD: boolMODAL = null")
			break
	}
	return result
	// Note:
	// Modal dialog returns "returnValue" value from dialog
	// Modeless dialog returns reference to dialog window
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function inheritFrom(/* Object */ aThis, /* Object */ aParent)
{
	var excp;

	for (var property in aParent)
	{
		try
		{
			aThis[property] = aParent[property];
		}
		catch(excp)
		{
		}
	}
}
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

/* GUI Element Handeling */
function guiObject()
{
	/* Generic Methods */
	this.getStyle = function (Obj,strCssRule)
	{
		var strValue = "";
		if(document.defaultView && document.defaultView.getComputedStyle){
			strValue = document.defaultView.getComputedStyle(Obj, "").getPropertyValue(strCssRule);
		}
		else if(Obj.currentStyle){
			strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
				return p1.toUpperCase();
			});
			strValue = Obj.currentStyle[strCssRule];
		}
		return strValue;
	}
	this.swapBGs = function( _oldState, _newState )
	{
		this.style.backgroundImage = getStyle(this,"background-image").replace(_oldState,_newState);
	}
	/* Event Handlers */
	this.onmouseover = function ()
	{
		this.swapBGs("_idle","_hover");
	}
	this.onmouseout = function ()
	{
		this.swapBGs("_hover","_idle");
	}
	this.onmousedown = function ()
	{
		this.swapBGs("_hover","_down");
	}
	this.onmouseup = function ()
	{
		this.swapBGs("_down","_hover");
	}
	
	/* Methods */
	this.disable = function()
	{
		this.disabled = true;
		
	}
	this.enable = function()
	{
		this.disabled = false;
		
	}
}

function loadScripts()
{
	this.loadScript = function(strPath)
	{
		oScript = document.createElement("SCRIPT");
		oScript.id = "Script_"+strPath.replace(/\/()/g,"_");
		oScript.type = "text/javascript";
		oScript.src = strPath;
		document.getElementsByTagName("HEAD")[0].appendChild(oScript);
	}
	var Scriptlist = 
	{
		"ButtonSet_Button"		: "css/buttons/buttons.js",
		"Button3Parts"			: "css/buttons/buttons.js",
		"MenuButtonSet"			: "css/toolbar/toolbarMenu.js",
		"ImageBtn"				: "css/buttons/buttons.js",
		"ImageBtn_WithHover"	: "css/buttons/buttons.js",
		"ImageBtn_OnlyHover"	: "css/buttons/buttons.js",
		"Grid"					: "css/grid/grid.js",
		"Tab"					: "css/tabCtrl/tabCtrl.js",
		"SidePanel_Item"		: "css/leftPane/leftPanel.js",
		"SidePanel"				: "css/leftPane/leftPanel.js"
	}
	for(className in Scriptlist){
		if(getElementsByClass(className).length > 0)
		{
			if( !document.getElementById("Script_"+Scriptlist[className].replace(/\/()/g,"_") ) )
			{
				this.loadScript(Scriptlist[className]);
			}
		}
	}
	
	
}

addLoadEvent(loadScripts);
