/** 
 * Instantiates a PengLoader object. ClientObject must be a string representation of a global accesible 
 * Javascript Variable to this object.
 */
pengag.CPengLoader = function()
{
	var my = this;
	
	this.loadedModules = new Array();
	var pengLoader;
	var isIE;
	var elementId;
	
	this.versionInfoObject;
	
	/**
	 * Represents the global access path to this
	 */
	this.clientObject = null;
	this.preventCaching = "always";
	
	/**
	 * Loads the PengLoader.swf form the swfUrl into the HTML with the specified ID.
	 */
	this.initPeng = function (id, width, height, version, skinUrl)
	{
		elementId = id;
		
		var att = { data:(pengag.basePath+pengag.versionPrefix+"PengLoader.swf"), width:width, height:height, bgcolor:"#FFFFFF" };
        var par = { allowScriptAccess: "always", flashvars:("clientObject=" + my.clientObject + "&preventCaching=" + my.preventCaching + "&skinUrl=" + skinUrl)};
        var idR = id;
        pengLoader = swfobject.createSWF(att, par, idR);
        
		if (navigator.appName.indexOf("Microsoft") != -1)
		{
			isIE = true;
		}
		else
		{
			isIE = false;
		}
	}
	
	this.createModule = function()
	{
		var cpmc = new pengag.CPengModuleClient();
		my.loadedModules.push(cpmc);
		cpmc.clientObject = my.clientObject + ".loadedModules[" + (my.loadedModules.length - 1) + "]";
		cpmc.pengLoader = pengLoader;
		return cpmc;
	}
	
	this.callFunction = function(id, parameters)
	{
		var functionName = String(id);
		if(typeof(parameters) == "undefined")
		{
			return pengLoader[functionName]();
		}
		else
		{
			return pengLoader[functionName](parameters);
		}
	}
	
	this.getVersionInfoObject = function()
	{
		//console.warn("VERSION INFO");
		return my.versionInfoObject;
	}
	
	this.internalLoaderReady = function()
	{
		pengag.log("INTERNAL LOADER READY");
		pengLoader["setBasePath"](pengag.basePath);
		my.loaderReady();
	}
	
	/**
	 * This function is called by PengLoader when he's ready. Can be overwritten.
	 */
	this.loaderReady = function()
	{
		
	}
	
	/**
	 * @private
	 * gets the document element with the flash content
	 */
	function getPlayer(elementName) 
	{
		if (navigator.appName.indexOf("Microsoft") != -1) 
		{
			return window[elementName];
		}
		else
		{
			return document[elementName];
		}
	}
}

/**
 * Class for communication with PengModules
 */
pengag.CPengModuleClient = function()
{
	var my = this;
	/**
	 * @private
	 * ID assinged to the Module by PengLoader
	 */
	this.pengId = "";
	
	/**
	 * Represents the global access path to this
	 */
	this.clientObject = "";
	
	/**
	 * The Loader which created this Module
	 */
	this.pengLoader = null;
	
	/**
	 * @private
	 * Called by the Module when it's ready
	 */
	this.internalModuleReady = function(uid)
	{
		pengag.log("INTERNAL MODULE READY " + uid);
		my.pengId = uid;
		my.moduleReady();
	}
	
	/**
	 * This function is called when PengLoader finishes loading the Module.
	 */
	this.moduleReady = function()
	{
		
	}
	
	/**
	 * Loads the Module with the specified name and version, client object must be a global path to this Object.
	 */
	this.loadModule = function(id, majorVersion, minorVersion, position)
	{
		if(typeof(position) == "undefined")
		{
			my.pengLoader.loadModule(id, my.clientObject, majorVersion, minorVersion, "center");
		}
		else
		{
			my.pengLoader.loadModule(id, my.clientObject, majorVersion, minorVersion, position);
		}
	}
	
	/**
	 * Calls a function of the Module
	 */
	this.callFunction = function(id, parameters)
	{
		pengag.log("CALL " + id + " WITH " + parameters);
		pengag.log(my.pengLoader[functionName]);
		var functionName = String(my.pengId + "_" + id);
		if(typeof(parameters) == "undefined")
		{
			pengag.log("WITHOUT PARAM");
			return my.pengLoader[functionName]();
		}
		else
		{
			pengag.log("WITH PARAM");
//			if(id=="setInfo")
//			{
//				pengag.log("SET INFO ZEUG");
//				pengag.log(my.pengLoader[functionName]());
//			}
			pengag.log("WITH PARAM 2");
			return my.pengLoader[functionName](parameters);
		}
	}
}
//The purpose of the following script is to circumvent a bug in the Flex Compiler depending on the Browser History integration
BrowserHistoryUtils = {addEvent: function(elm, evType, fn, useCapture){return true;}}
BrowserHistory = (function() {
	function getHistoryFrame(){return null;}
    function getAnchorElement(){return null;}
    function getFormElement(){return null;}
    function getRememberElement(){return null;}
    function getPlayer(id){return undefined;}
    function getPlayers(){return {};}
	function getIframeHash(){}
    function getHash(){return '';}
    function setHash(hash){}
    function createState(baseUrl, newUrl, flexAppUrl){}
    function addHistoryEntry(baseUrl, newUrl, flexAppUrl){}
    function _storeStates(){}
    function handleBackButton(){}
    function handleForwardButton(){}
    function handleArbitraryUrl(){}
    function checkForUrlChange(){}
    function addAnchor(flexAppUrl){}
	return {
        historyHash: null,
        backStack: function(){return null;},
        forwardStack: function(){return null;},
        getPlayer: null,
        initialize: function(src){},
        setURL: function(url){},
        getURL: function(){return '';},
        getTitle: function(){return '';},
        setTitle: function(title){},
        setDefaultURL: function(def){},
        setBrowserURL: function(flexAppUrl, objectId){},
        browserURLChange: function(flexAppUrl){},
        getUserAgent: function(){return '';},
        getPlatform: function(){return '';}
    }
})();
function setURL(url){}
function backButton(){}
function forwardButton(){}
function goForwardOrBackInHistory(step){}
