/*	
	Updated @ 2008-12-27
	License: GNU General Public License v3
	Developer: Ehsun Behravesh
	email: ehsun7b@gmail.com
	Tested on Firefox 3.0.3, Opera 9.52, Safari 3.1.2, Internet Explorer 7
	
	Thank to: Leandroh <leandroh@gmail.com>
*/

var AjaxCSSJS = Class.create({
	initialize: function(url, type, onLoadFunction) {
		try {
			this.url			= url;
			this.type			= type.toLowerCase(); 
			this.onLoadFunction = onLoadFunction;
			
			//	---------------------------------
			
			if (this.type == 'css') {
				this.removeAnExistingFile('link[href="' + this.url + '"]');
							
				var fileref = document.createElement("link");
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", this.url);			
			} else if (this.type == 'js') {
				this.removeAnExistingFile('script[src="' + this.url + '"]');
			
				var fileref = document.createElement('script');
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", this.url);				
				fileref.onload = this.onLoadFunction;
				fileref.onreadystatechange = this.onReadyChange.bind(this, fileref);											
			}
			
			if (typeof fileref != "undefined")
				document.getElementsByTagName("head")[0].appendChild(fileref);
				
		} catch (e) {
			alert("error:" + e.name + " - " + e.message);
		}
	},
	
	onReadyChange: function(fr) {	
		if (fr.readyState == 'complete' || fr.readyState == 'loaded')
			this.onLoadFunction();
	},
	
	removeAnExistingFile: function(xpath) {
		var tag = $$(xpath)[0];
		if (tag) {
			document.getElementsByTagName("head")[0].removeChild(tag);
		}
	},
	
	remove: function() {
		if (this.type == 'css') {
			this.removeAnExistingFile('link[href="' + this.url + '"]');
		} else if (this.type == 'js') {
			this.removeAnExistingFile('script[src="' + this.url + '"]');
		}
	}
});