/*
	Interface class file

	This class is suppost to bring into the GUI Section of the webpage , living parts, along with Javascript / CSS and HTML

	Started at : 07:07 11th of  July , 2007
*/

getInstance = function(aName){
	/*
		This function is a common function resposible for getting the interface for you
		it will check if the interface is loaded , if it is , it will return the instance , otherwise it will return false
	*/
	var aType,anInstance = null;
	
	eval ("aType = typeof(" + aName + ");");
	
	if (aType == "object"){
		eval("anInstance = " + aName + ";");
		return anInstance;
	} else {
		return false;
	}
};

interfaceClass = function(anInterfaceName){
	/*
		Interface Constructor
		Recieves basically the path to the first file and then does all the rest :P
	*/
	this.interfaceName = anInterfaceName;
	this.jsObjectName  = "o" + anInterfaceName;
	this.nL = "../";
};

interfaceClass.prototype.setNestingLevel = function(sz){
	this.nL = sz;
	return true;
};

interfaceClass.prototype.getJavascript = function(){
	new Ajax.Request(this.nL+"include/php/interface.php" ,{
		method:"post",
		asynchronous: false,
		parameters:{
			interfaceName:this.interfaceName,
			interfacePart:"js",
			interfacesPath:this.getInterfacesPath()
		},
		onComplete: function(transport){
			eval(transport.responseText);
		}
	});
	
	return true;
};

interfaceClass.prototype.setInterfacesPath = function(aPath){
	this.interfaceDirectory = aPath;
};

interfaceClass.prototype.getInterfacesPath = function(){
	return this.interfaceDirectory;
};

interfaceClass.prototype.getHTMLCode = function(){
	var returnString = "";
	new Ajax.Request(this.nL+"include/php/interface.php" , {
		method:"post",
		asynchronous: false,
		parameters:{
			interfaceName:this.interfaceName,
			interfacePart:"html",
			interfacesPath:this.getInterfacesPath()
		},
		onComplete: function(transport){
			returnString += transport.responseText;
		}
	});
	
	return returnString;
};

interfaceClass.prototype.getCSSStyles = function(){
	
	var o = document.createElement("link");
	o.setAttribute("rel","stylesheet");
	o.setAttribute("type","text/css");
	o.setAttribute("href","interfaces/" + this.interfaceName + ".css");
	
	this.cssBlock = o;
	document.getElementsByTagName("head").item(0).appendChild(o);
	//document.body.insertBefore(o, document.body.firstChild);
	return true;
};

interfaceClass.prototype.getModule = function(){
	/*
		This method will start getting the module parts onto the page.
	*/
	
	eval("aType = typeof(" + this.jsObjectName +");");
	
	if ( aType == "undefined" ){
		return this.createModule();
	} else {
		/*
			the object might be alive or might be unused (if this interface was loaded once , and removed then the object
			would exist , but be null)
			this next branching is to understand which case is referred to in this situation
		*/
		eval("this.instance = anInstance = " + this.jsObjectName);
		if (anInstance == null){
			return this.createModule();
		}
		return anInstance;
	}
};

interfaceClass.prototype.createModule = function(){
	//Load the CSS stylesheet for this specific module
	this.getCSSStyles();
	
	var theHTML = this.getHTMLCode();
	var aTemporaryElement = document.createElement("div");
	document.body.appendChild(aTemporaryElement);
	aTemporaryElement.style.width = "1px";
	aTemporaryElement.style.height = "1px";
	aTemporaryElement.style.overflow = "hidden";
	aTemporaryElement.innerHTML = theHTML;
	
	var theHTML = aTemporaryElement.getElementsByTagName("div").item(0);
	document.body.removeChild(aTemporaryElement);
	document.body.appendChild(theHTML);
	this.getJavascript();
	
	eval("this.instance = " + this.jsObjectName + ";");
	this.instance.awakeFromLoad(this);
	return this.instance;
};

interfaceClass.prototype.removeModule = function(){
	/*
		Removes the entire interface from the DOM completly
		to free up resources
	*/
	
	var theHTML = document.body.removeChild($(this.interfaceName));
	theHTML = null;
	
	this.instance = null;
	eval(this.jsObjectName + " = null;");
	
	var theCSS = document.getElementsByTagName("head").item(0).removeChild(this.cssBlock);
	theCSS = null;
};
