function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}


addLoadEvent(urlRewrite);
addLoadEvent(checkWindowSize);


function urlRewrite(){
	//console.log('urlRewrite()');
	var currenturl = location.href;
	var currenthost = "http://"+location.host;
	var currenthostendslash = "http://"+location.host+"/";
	var path = location.pathname;
	
	if(currenturl.indexOf('/xtr/') < 0) {
		
		if(currenturl.indexOf('#') < 0) {
			//Injects # in the url.
			//Check if path is larger then 1
			if(1 < path.length){
				location.href = currenthost+"#/"+path.substring(1,location.pathname.length)+"/";
			}
			
		} else {
			// Checks if # is in the right place
			if(currenthostendslash.length != currenturl.indexOf('#')){
				//Removes the # form the string an injects i at the right place.
				location.href = currenthost+"#/"+path.substring(1,location.pathname.length)+"/";		
			}
			
		}
		
	}
	
}


function activateJobSearch(iframeUrl) {
	var flashcontent = document.getElementById('flashcontent');
	var popup = document.getElementById('popup');
	var popupIframe = document.getElementById('iframepopup');

	//Hide flash and show activateJobSearch div for all but IE
	if(BrowserDetect.browser != "Explorer") {
		
		flashcontent.style.visibility = 'hidden';
	}
	popup.style.display = 'block';
	popupIframe.src = iframeUrl;
}


function deactivateJobSearch() {
	var flashcontent = document.getElementById('flashcontent');
	var popup = document.getElementById('popup');

	//Show flash and hide activateJobSearch div
	popup.style.display = 'none';
	flashcontent.style.visibility = 'visible';
}


function hideIframe(deepLinkUrl) {
	var flashcontent = document.getElementById('flashcontent');
	var popup = document.getElementById('popup');
	flashcontent.style.visibility = 'visible';
	popup.style.display = 'none';
	if(deepLinkUrl != null){
		window.parent.location = "http://"+window.parent.location.host+"/#"+deepLinkUrl+"/";
	}
}


function hideIframeOnHistoryBack(supressSafari) {
	
	if(BrowserDetect.browser == "Safari" && supressSafari == true) {
		return;
		
	} else {
		var flashcontent = document.getElementById('flashcontent');
		var popup = document.getElementById('popup');
		if(popup.style.display != 'none'){
			flashcontent.style.visibility = 'visible';
			popup.style.display = 'none';
		}
		
	}
	
}


function checkWindowSize(){
	var flashcontent = document.getElementById('flashcontent');
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	
	if(x < 981){
		flashcontent.style.width = '981px';
	} else if (x > 981)	{
		flashcontent.style.width = '100%';
	}
	
	if(y < 680){
		flashcontent.style.height = '680px';
	} else if (y > 680)	{
		flashcontent.style.height = '100%';
	}
	
}


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";
		detectChrome(this.browser);
	},
	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",
			versionSearch: "Chrome/",
			identity: "Chrome"
		},
		{ 	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();

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function detectChrome(currentBrowser) {
	var chromeOverride = readCookie('chromeOverride');
	var hash = '';
	if(window.location.hash.length>1)
	{
		hash = window.location.hash.substring(1, window.location.hash.length);
	}
	var myLocation = String(window.location);
	var myPosition = myLocation.indexOf('#');
	var myLocation = myLocation.substring(0, myPosition);
	if(currentBrowser=='Chrome' && chromeOverride!=1)
	{
		window.location = '/chrome.php?target=' + myLocation + '&hash=' + hash;	
	}	
}

