/**
* First attempt to a completely new codebase for the WPS_javascript-clients
* In general the idea is to refactor most of the functionality needed right now to a more clever object-based structure.
* This structure is bound to a wps-namespace, to ensure functions will not clash.
* Libraries themselves must strive to be as standalone as possible, to make individual scripts available to the public.
* 
* 3rd party project which we intend to use: 
*	* prototype 1.50
*	* peterned-inheritance script
*	* scriptaculous library
*
* This page will try to set paths, instantiate the autoloader and load the most basic libraries
* @version 1.00
*/
	/**
	* set's the absolute path from the domain to the src of the scripts
	*/
	//var gRootPath = '/wpsRepos/';
	//  
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
	
	/**
	* Declares namespace
	*/
	var wps = function(){
		this.instID = 'wps_' + parseInt(Math.random()*1000000);
	}

	/**
	* helper to load libraries after checking their availability. 
	* This function might be usefull for calling object without actually including them first.
	* If catched to this function it might be able to recursively find the script.
	*/
	wps._require = function(srcArr){ 
		//srcArr.reverse();
		for (var cnt=0;cnt<srcArr.length;cnt++){
			if (!wps._scriptExists(srcArr[cnt])){
				wps._injectScript(srcArr[cnt]);
			}
		}
		//document.write('<input type="button" value="DebugRequire" onClick="debugRequire();"/>');		
	}
	
	/*
	* Checks whether a script was already required
	*/ 
	wps._scriptExists = function(sScriptSrc){
		var scripts = document.getElementsByTagName("script");
		//alert(scripts.length);
		for (var y=0;y<scripts.length;y++){
			if (scripts[y].src){
				var scriptSrc = new String(scripts[y].src);
				if (scriptSrc.indexOf(sScriptSrc) > -1){
					return true;
				}
			}
		}		
		return false;
	}
	
	/**
	* Actually inject the script into the head-node of the page
	*/
	wps._injectScript = function(src){
			if ((BrowserDetect.browser == 'Safari') ){
				document.write('<script type="text/javascript" src="'+src+'"></script>');
				return;
			}
            try {
	            var dingDong = new ActiveXObject('Msxml2.XMLHTTP');
	            document.write('<script type="text/javascript" src="'+src+'"></script>');
            }catch(e){
            	var theHead = document.getElementsByTagName("head");
				if (!theHead) {alert("no head?");return false;}
				theHead = theHead.item(0);
            	var oScriptElem = document.createElement('script');
				oScriptElem.setAttribute("src",src);
				oScriptElem.setAttribute("language","Javascript");
				oScriptElem.setAttribute("type","text/javascript");
				theHead.insertBefore(oScriptElem, theHead.childNodes.item(0));
				//theHead.appendChild(oScriptElem);
            }
	}
	
			// GOOGLE xslt-library, not ready for real-world implementation
			//gRootPath+'lib/wps/heartbeart.js', 
			//gRootPath+'lib/3rd/ajaxslt/misc.js',
			//gRootPath+'lib/3rd/ajaxslt/dom.js',
			//gRootPath+'lib/3rd/ajaxslt/xpath.js',
			//gRootPath+'lib/3rd/ajaxslt/xslt.js',
	/**
	* Set's requirement for the prototype-library, before doing anything else (like creating classes).
	*/
	//DEVELOPMENT INC
	/*
	wps._require(
		[
			gRootPath+'lib/3rd/peterned_inheritance.js',
			gRootPath+'lib/3rd/quirksmode/browserDetect.js',
			gRootPath+'lib/3rd/prototype.js',
			gRootPath+'lib/3rd/scriptaculous/beta/builder.js',
			gRootPath+'lib/3rd/scriptaculous/beta/effects.js',
			gRootPath+'lib/3rd/scriptaculous/beta/dragdrop.js',
			gRootPath+'lib/3rd/scriptaculous/beta/controls.js',
			gRootPath+'lib/3rd/scriptaculous/beta/slider.js',
			gRootPath+'lib/3rd/eventHandler.js',
			gRootPath+'lib/3rd/tiny_mce/tiny_mce.js',
			gRootPath+'lib/wps.debug.js',
			gRootPath+'lib/wps.base.js',
			gRootPath+'lib/wps.base.cache.js',
			gRootPath+'lib/imageBrowser/wps.imageViewer.js',
			gRootPath+'lib/wps.rpc.js',
			gRootPath+'lib/wps.file.js',
			gRootPath+'lib/wps.dom.js',
			gRootPath+'lib/wps.base.stringbuffer.js'
		]
	);
	*/
	
	wps._require(
	[
			gRootPath+'lib/3rd/peterned_inheritance.js',
			gRootPath+'lib/3rd/quirksmode/browserDetect.js',
			gRootPath+'lib/3rd/prototype.js',
			gRootPath+'compressedLite.js',
			gRootPath+'lib/3rd/tiny_mce/tiny_mce.js',
			gRootPath+'lib/3rd/scriptaculous/builder.js',
			gRootPath+'lib/3rd/scriptaculous/effects.js',
			gRootPath+'lib/3rd/scriptaculous/dragdrop.js',
			gRootPath+'lib/3rd/scriptaculous/controls.js',
			gRootPath+'lib/3rd/scriptaculous/slider.js',
			gRootPath+'lib/3rd/eventHandler.js',
			// MOVE THIS TO COMPRESSED/development!!
			gRootPath+'lib/mixins/eventstack.js'
		]
	);
	
