/***
 * mootilities.js contains helper functions based on mootools 1.11 library 
 */

/**************************************************************************************/

/**
 * runLater runs a generic function until that function returns 
 * true.
 * 
 * @param someFunction the function to execute until success.
 * it is expected to return true on success and false otherwise
 * and does not any args
 * @param timeOut the time between retry
 * @return
 */
function runLater(someFunction,timeOut){

	var run=function(){
		if(!someFunction())
		{
			setTimeout(run,timeOut);
		}
	};

	this.start=function(){
		setTimeout(run,timeOut);
	}();	
}
/**
 * runOnce initiates a runLater thread and also returns a fucntion that can be called to 
 * manually execute, as well as kill the thread. the intended purpose of this method is 
 * to generate onload fucntions that can be executed after injecting a new asset, but might 
 * also fail on certian browsers... the runlater thread will eventually notice that no onload 
 * event has occured and execute its function. however proper exection of the onload event 
 * kills the thread aswell preventing duplicate function calls
 * 
 * @param fn - some useful fucntion (ie access a method from an injected asset)
 * @param requirementsFn helper function (return true if requiered fucntion is defined)
 * @param delay time between timeouts
 * @return a manual fucntion that can be passed to the onload parameter
 */
function runOnce(fn, requirementsFn, delay){
	var myFunction=fn;
	var myRequirement=requirementsFn;

	runLater(function(){
		if(myRequirement()){
			var m=myFunction;
			myFunction=function(){}; //make empty in case other source calls it
			m();
			return true;
		}
		return false;
	},delay);

	return function(){
		var m=myFunction;
		myFunction=function(){};	//make empty for other source
		myRequirement=function(){return true;}; //make other source die on next check
		m();

	};
}


/**
 *  checks an array for a specified entry
 * @param a item
 * @param b array
 * @return true if item is in array, otherwise false
 */
function inArray(a, b){
	var i;
	for(i=0;i<b.length;i++){
		if(a==b[i])
			return true;
	}
	return false;
}





/**
 * BrowserDectect - dectects client browser information and javascript engines
 */	
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: "Chrome",
            	  identity: "Chrome"
              },
              { 	string: navigator.userAgent,
            	  subString: "OmniWeb",
            	  versionSearch: "OmniWeb/",
            	  identity: "OmniWeb"
              },
              {
            	  string: navigator.vendor,
            	  subString: "Apple",
            	  identity: "Safari",
            	  versionSearch: "Version"
              },
              {
            	  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.userAgent,
                        	subString: "iPhone",
                        	identity: "iPhone/iPod"
                        },
                        {
                        	string: navigator.platform,
                        	subString: "Linux",
                        	identity: "Linux"
                        }
                        ]

};
BrowserDetect.init();

function runOnLoad(f){
	var wrapper=function(){
		window.removeEvent('load',wrapper);
		f();
	};
	window.addEvent('load', wrapper); 
	
}






