Task = function ()
{
	this.type = "";
	this.parameters = "";
}

Tasks = function ()
{
	this.taskList = new Array();
}
Tasks.prototype.clear = function()
{
	this.id++;
	this.taskList.length = 0;
}  
Tasks.prototype.pushTask = function(taskType, taskParameters)
{
	var theTask = new Task();
	theTask.type = taskType;
	theTask.parameters = taskParameters;
	this.taskList.push(theTask);
}
Tasks.prototype.fetchTask = function()
{
	return this.taskList.shift();
}

var tasks = new Tasks();
var lastCallBackId = 0; 

function LoadFrame_Loaded()
{
	tasks.clear();
	var i=0; 
	for (i=0;i<jsFiles.length;i++)
	{
		tasks.pushTask("loadJsFile",jsFiles[i]);
	}
	for (i=0;i<cssFiles.length;i++)
	{
		tasks.pushTask("loadCssFile",cssFiles[i]);
	}
	for (i=0;i<moduleNames.length;i++)
	{
		tasks.pushTask("loadModule",moduleNames[i]);
	}
	tasks.pushTask("loadJavaScript","");
	Fetch_A_Task();
}

function Fetch_A_Task()
{
	if (tasks.taskList.length < 1)	 return;
	
	theTask = tasks.fetchTask();

	switch(theTask.type)
	{
		case "loadJsFile":
			Load_Js_File(theTask.parameters);
			break;
		case "loadCssFile":
			Load_Css_File(theTask.parameters);
			break;
		case "loadModule":
			Load_Module(theTask.parameters);
			break;
		case "loadJavaScript":
			Load_JavaScript();
			break;
	}
}

function Js_File_Is_Loaded()
{
	var jsFile = this.src;
	var mustLoad = true;
	for(var i=0;i<parent.loadedScripts.length;i++)
	{
		if (parent.loadedScripts[i] == jsFile)
		{
			mustLoad = false;
			break;
		}
	}
	if (mustLoad)
		parent.loadedScripts.push(jsFile);
	Fetch_A_Task();
}

function Load_Js_File(jsFile)
{
	for(var i=0;i<parent.loadedScripts.length;i++)
	{
		if (parent.loadedScripts[i] == jsFile)
		{
			Fetch_A_Task();
			return;
		}
	}
    callBackId = lastCallBackId;
    lastCallBackId++;
	var theHead = parent.document.getElementsByTagName("head")[0];
    var theScript = parent.document.createElement("script");
    theScript.type = "text/javascript";
    theScript.src = jsFile;
    
    window["Js_File_Is_Loaded_"+callBackId] = function()
    {
		Js_File_Is_Loaded(jsFile);
    }
    
    if (navigator.appName != 'Microsoft Internet Explorer')
    {
		theScript.onload = Js_File_Is_Loaded;
    }
    else
    {
		theScript.onreadystatechange = function()
		{
			if ((this.readyState == 'complete') || (this.readyState == 'loaded'))
				 Js_File_Is_Loaded.call(this);
		}
    }
    theHead.appendChild(theScript);
}

function Load_Css_File(cssFile)
{
	for(var i=0;i<loadedStyleSheets.length;i++)
	{
		if (loadedStyleSheets[i] == cssFile)
		{
			Fetch_A_Task();
			return;
		}
	}
    var theHead = parent.document.getElementsByTagName("head")[0];
    var theLink = parent.document.createElement("link");
    theLink.type = "text/css";
    theLink.rel = "stylesheet";
    theLink.href = cssFile;
    theHead.appendChild(theScript);
    loadedStyleSheets.push(cssFile);
    Fetch_A_Task();
}

function Load_Module(moduleName)
{
	var toReplaceModule = moduleName;
	if ((theMainModule != '') && (moduleName == 'main'))
		toReplaceModule = theMainModule;

	parent.document.getElementById(toReplaceModule+"_module").innerHTML = 
	document.getElementById(moduleName+"_module").innerHTML;
	Fetch_A_Task();
}

function Load_JavaScript()
{
	Add_Page_Scripts();
	parent.scroll(0,0);
	Fetch_A_Task();
}

