// Wigzy: Client library object

// [1] Create the Wigzy class
var Wigzy = Class.create({

	// [1a] Construct function
	initialize: function(dir,current_page) {

		this.head = $$("head")[0];
		this.errorLog = new Array();

		this.dir = dir;
		this.framework = this.dir+"framework/";
		this.lib = this.dir+"lib/";
		this.dummy = this.dir+"dummy/";

		this.currentPage = current_page;

		this.libJS = new Array(
//			"blackbox",
			"img.rollover"
//			"popup",
//			"tabs",
//			"valid",
//			"swf/swfaddress",
//			"swf/swfaddress-optimizer",
//			"swf/swfobject"
		);

		this.libCSS = new Array(
			"blackbox",
			"popup"
		);


		this.libJS.each(function(libFunction) {
		
			document.write('<script type="text/javascript" src="'+this.lib+libFunction+'.js"><\/script>');		

		}.bind(this));

		this.libCSS.each(function(libFunction) {
		
			document.write('<link type="text/css" rel="stylesheet" href="'+this.lib+'css/'+libFunction+'.css" />');				

		}.bind(this));

	},

	// [1b] Logs errors
	logError: function(functionName,errorMessage) {
		var template = new Template("#{functionName} Error: #{errorMessage}\n");
		var new_error = template.evaluate({ functionName: functionName, errorMessage: errorMessage });
		this.errorLog.push(new_error);
	},

	showErrors: function() {
		return this.errorLog;
	},

	// [1c] Creates a script element and appends it to the head
	createJS: function(src) {
		var NewJS = new Element("script", {
			type: "text/javascript",
			src: src
		});
		this.head.appendChild(NewJS);
	},

	// [1d] Creates a link element and appends it to the head
	createCSS: function(href) {
		var NewJS = new Element("link", {
			rel: "stylesheet",
			type: "text/css",
			href: href
		});
		this.head.appendChild(NewJS);
	},

	// [1e] Loads an XML and stores it as this.currentXML
	loadXML: function(file) {
		xmlDoc = document.implementation.createDocument("","",null);
		xmlDoc.async=false;
		xmlDoc.load(file);
		return xmlDoc;
	},

	selectNode: function(parent,nodeToSelect) {
		var selected_node = parent.getElementsByTagName(nodeToSelect);
		return $(selected_node);	
	},

	getNodeValue: function(parent,nodeValueOf) { 
		XMLNode = parent.getElementsByTagName(nodeValueOf);
		XMLValue = XMLNode[0].childNodes[0].nodeValue;
		return XMLValue;  
	},
	
	
	// [1g] Processes XSLT files with XML and appends output to the receptacle var	
	processXSLT: function(xsltFile,receptacle) {
		if(this.currentXML && xsltFile) {
			var xml_file = this.loadXML(this.currentXML.path);
			var xslt_file = this.loadXML(this.dummy+xsltFile);
			xsltProcessor = new XSLTProcessor();
			xsltProcessor.importStylesheet(xslt_file);
			resultDocument = xsltProcessor.transformToFragment(xml_file,document);
			document.observe('dom:loaded',function() {
				$(receptacle).appendChild(resultDocument);
			});
			

		} else {
			this.logError("Wigzy.processXSLT","XSLT could not be processed");
		}
	}

}); // [1] END Class
