// *****************************************************************
//  Copyright (c) 2004 ORISA Software GmbH All Rights Reserved.
// ORISA Software GmbH
// www.orisa.de
// *****************************************************************

	//*****************************************************************
	// Frame handling
	//*****************************************************************
	
	//*****************************************************************
	// Definition of class FrameDefinition
	//*****************************************************************
	function FrameDefinition(_name, _path)
	{
		// properties
		this.name = _name;
		this.path = _path;
		this.isLoaded = false;
		this.idx = 0;
	}
	
	// return FrameDefinition
	// *****************************************************************
	function getFrameDef(fName)
	{
		if(fDefinitions[fName])
		{
			return fDefinitions[fName];
		}
		//alert("getFrameDef: " + fName);
		return null;
		
	}
	
	// return frame obj
	// *****************************************************************
	function getFrameObj(fName)
	{
		var f = fDefinitions[fName];
		try
		{
			if(f && f.isLoaded)
				return eval(f.path);
		}
		catch (e)
		{}
		//alert("getFrameObj: " + fName);
		return null;	
	}
	
	// return true if success
	// *****************************************************************
	function setUrl(fName, url)
	{
		try
		{
			fDefinitions[fName].isLoaded = false;
			eval(fDefinitions[fName].path).location.href = url;
			return true;
		}
		catch(e)
		{}
		//alert("setUrl: " + fName);
		return false;
	}
	
	// register module frame
	//*****************************************************************
	function registerFrame(frameName)
	{
		if(frameName && fDefinitions[frameName])
		{
			fDefinitions[frameName].isLoaded = true;
			
			/*if(eventQueue && eventQueue.runningEvent != null)
				eventQueue.finishedEvent();*/
		}
	}

	// unregister module frame
	//*****************************************************************
	function unregisterFrame(frameName)
	{
		if(frameName && fDefinitions[frameName])
		{
			fDefinitions[frameName].isLoaded = false;
		}
	}

	//*****************************************************************
	// Event handling
	//*****************************************************************
	var thisGlasspane= null;
	var cancelWait = null;
	
	// handle incoming client event
	//*****************************************************************
	function fireEvent(actId, parameter)
	{
		if(!eventQueue || !eventQueue.elements)
			setTimeout('fireEvent("' + actId + '", "' + parameter + '")', 10);
		else if (parameter)
		{
			// show glass pane
			showGlasspane();
			eventQueue.addQueueEvent(actId, parameter);
		}
	
	}

	// set running action
	//*****************************************************************
	function setPendingAction(act)
	{
		if(eventQueue)
		{
			eventQueue.pendingAction= act;
		}
	}
	
	// get running action
	//*****************************************************************
	function getPendingAction(act)
	{
		if(eventQueue && eventQueue.pendingAction)
		{
			return eventQueue.pendingAction;
		}
	}
	
	// finish javaScript events?
	//*****************************************************************
	function finishJSEvent(actName)
	{
		if(eventQueue.runningEvent && eventQueue.runningEvent.actionName == actName)
		{
			// stack entkoppeln!
			setTimeout("window.eventQueue.finishedEvent()",2);
		}
	
	}
	
	// abort by user, if pending action
	//*****************************************************************
	function abortCurrentAction()
	{
		if(eventQueue)
		{
			if(eventQueue.pendingAction)
			{
				// reset pending action
				eventQueue.pendingAction = null;
			}
		}
		
	}

	//*****************************************************************
	// Definition of class EventQueueElement
	//*****************************************************************
	function EventQueueElement(aName, aType, aParameterString)
	{
		this.actionName = aName;
		this.actionType = aType;
		this.actionParameterString = aParameterString;
	}
	
	//*****************************************************************
	// Definition of class EventQueue
	//*****************************************************************
	function EventQueue(_codeFrame, _baseURL)
	{
		this.runningEvent = null;
		this.elements = new Array();
		this.codeFrame = _codeFrame;
		this.baseURL = _baseURL;
		this.pendingAction = null;
	
		this.addQueueEvent = addQueueEvent;
		this.launchEvent = launchEvent;
		this.finishedEvent = finishedEvent;
	}
	
	// add client event information to event queue
	//*****************************************************************
	function addQueueEvent(action, parameters) //objectId,
	{
		var i = 0;
		var qE = null;
		if(action == null)
			qE = new EventQueueElement(action, "request", parameters);
		else
			qE = new EventQueueElement(action, "javascript", parameters);
		
		this.elements[this.elements.length] = qE;
		//top.status = "Eventqueue: \nL= " + this.elements.length + "\nact= " + action + "\np= " + parameters;

		if(!this.runningEvent)
			this.launchEvent();
	}
	
	// launch next client event
	//*****************************************************************
	function launchEvent()
	{
		var rV = false;
		var pObj = null;
		while(!this.runningEvent && this.elements.length > 0)
		{
			// store the current event
			this.runningEvent = this.elements[0];
	
			// shift left
			this.elements = this.elements.slice(1, this.elements.length);
	
			// launch action with parameters
			switch(this.runningEvent.actionType)
			{
				case "javascript":
					//alert("call jsEvent: " + (typeof this.runningEvent.actionParameterString == "string"? this.runningEvent.actionName + "(\"" + this.runningEvent.actionParameterString + "\")": this.runningEvent.actionName + "(" + this.runningEvent.actionParameterString + ")"));
					if(typeof this.runningEvent.actionParameterString == "string")
						rV = eval(this.runningEvent.actionName + "(\"" + this.runningEvent.actionParameterString + "\")");
					else
					{
						var p = "";
						pObj = this.runningEvent.actionParameterString;
						for(var i in pObj)
						{
							if(p.length > 0)
								p += ", ";
							p += "\"" + pObj[i] + "\"";
						}
						//alert("call clientEvent: " + this.runningEvent.actionName + "({" + p + "})");
						eval(this.runningEvent.actionName + "(" + p + ")");
					}
					break;
				case "request":
					pObj = this.runningEvent.actionParameterString;
					//alert(this.baseURL + pObj + "&RT=" + new Date().getTime());
					if(typeof(pObj) != "string")
					{
						pObj.submit();
					}
					else
					{
					    this.codeFrame.location.href = this.baseURL + pObj + "&RT=" + new Date().getTime();
					}
					break;
			}
		}
	}
	
	// reset running state of event queue
	//*****************************************************************
	function finishedEvent()
	{
		// check frames
		if(topFrame.view != "START")
		{
			for(i in fDefinitions)
			{
				if(fDefinitions[i].isLoaded != true)
				{
					//setTimeout("eventQueue.finishedEvent()", 100);
					top.status = "Not loaded: " + i;
					return;
				}
			}
		}
		this.runningEvent = null;
		if(this.elements.length > 0)
			this.launchEvent();
		else
		{
			// hide glass pane
			hideGlasspane();
		}
	}
	
	//*****************************************************************
	function showGlasspane(isDragging)
	{
		isGlasspaneActive = true;
		if(!thisGlasspane)
		{
			thisGlasspane = document.getElementById("glasspane");
		}

		if(thisGlasspane)
		{
			with (thisGlasspane.style)
			{
				zIndex = 1000;
				visibility = "visible";
				cursor = isDragging?"w-resize":"wait";
			}
		}
	}
	
	//*****************************************************************
	function hideGlasspane()
	{
		isGlasspaneActive = false;
		if (thisGlasspane)
		{
			thisGlasspane.style.zIndex = -3;
			thisGlasspane.style.visibility = "hidden";
	    }
	}
		
	// Wir nehmen an, dass diese Seite unter web-directory steht:
	// ******************************************
	function getApplicationPath()
	{
		var appPath = window.location.href;
		var idx = -1;

		// Parameter abschneiden
		if((idx = appPath.indexOf("?"))> -1)
			appPath = appPath.substring(0, idx);
			
		var pLength = 0;
		var s = appPath;
		while(s.indexOf("/") > -1)
		{
			idx = s.indexOf("/");
			pLength += idx + 1;
			s = s.substring(idx+1);
		}
		return appPath.substring(0, pLength);
	}

	// return object with window dimensions in offsetWidth and offsetHeight
	// *****************************************************************
	function getFrameInfo(win) 
	{
		doc = win.document;
		var myWidth = 0, myHeight = 0;
	  	if(typeof( win.innerWidth ) == 'number' ) 
	  	{
	    	//Non-IE
	    	myWidth = win.innerWidth;
	    	myHeight = win.innerHeight;
	  	}
	  	else if( doc.documentElement && ( doc.documentElement.clientWidth || doc.documentElement.clientHeight))
	  	{
	    	//IE 6+ in 'standards compliant mode'
	    	myWidth = doc.documentElement.clientWidth;
	    	myHeight = doc.documentElement.clientHeight;
	  	}
	  	else if( doc.body && ( doc.body.clientWidth || doc.body.clientHeight ) ) 
	  	{
		    //IE 4 compatible
		    myWidth = doc.body.clientWidth;
		    myHeight = doc.body.clientHeight;
	  	}
	  	return {offsetWidth:myWidth, offsetHeight:myHeight};
	}
