/*********************************************************************************

	"Apply CSS Fixes"  -  4 functions:
	
	    * Apply_CSS_Fixes()			// the starting-point
			
			* BrowserDetect()				// Defines a JavaScript object with properties
															// which return the name, version, and platform
															// (OS) of the browser.
			
			* GlobalBrowserFixes()	// Applies CSS fixes for site-wide template
			
			* PageBrowserFixes()		// Detects and applies CSS fixes for current page,
															//if any
	
**********************************************************************************/

Apply_CSS_Fixes();


function Apply_CSS_Fixes() {
	var myBrowser = new BrowserDetect();
	if(typeof GlobalBrowserFixes=="function") GlobalBrowserFixes(myBrowser);
	if(typeof PageBrowserFixes=="function")   PageBrowserFixes(myBrowser);
}


function BrowserDetect() {
	
	////////////////////////////////
	// BROWSER DETECTION PATTERNS //
	////////////////////////////////
	//Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.68 Safari/534.24
	this.dataBrowser = [
		{
			string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{
			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"
		}
	];
	
	
	/////////////////////////////////
	// PLATFORM DETECTION PATTERNS //
	/////////////////////////////////
	this.dataOS = [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	];
	

	///////////////////////////////
	// HELPER FUNCTIONS (SEARCH) //
	///////////////////////////////
	this.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;
		}
	}

	this.searchVersion = function(dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	}


	////////////////////////////////
	// PRIVATE INSTANCE VARIABLES //
	////////////////////////////////
	var appName;
	var appVersion;
	var os;
	
	
	/////////////////////////////
	// PUBLIC INSTANCE METHODS //
	/////////////////////////////
	this.getAppName = function() {
		return appName;
	}
	
	this.getAppVersion = function() {
		return appVersion;
	}
	
	this.getOS = function() {
		return os;
	}
	
	
	/////////////////////////
	// INITIALIZATION CODE //
	/////////////////////////
	appName    = this.searchString(this.dataBrowser) || "An unknown browser";
	appVersion = this.searchVersion(navigator.userAgent) ||
			         this.searchVersion(navigator.appVersion) ||
			         "an unknown version";
  os         = this.searchString(this.dataOS) || "an unknown OS";
}





/**************************************

	GLOBAL BROWSER FIXES
	
	Selects the correct CSS stylesheet
	appropriate for the user's browser;
	
	Requires the BrowserDetect object
	in browser_detect.js
	
***************************************/

function GlobalBrowserFixes(bd) {
	var stylesheetPath="/site/_css/";
	var stylesheetDefault="layout_unk_browser.css";
	var whichStylesheet=stylesheetDefault;
	

	if (bd.getAppName()=="Firefox") {
		whichStylesheet="layout_firefox.css";
	} else if (bd.getAppName()=="Safari") {
		whichStylesheet="layout_safari.css";
	} else if (bd.getAppName()=="Opera") {
		whichStylesheet="layout_opera.css";
	} else if (bd.getAppName()=="Chrome") {
		whichStylesheet="layout_chrome.css";
	} else if (bd.getAppName()=="Explorer") {
		if (bd.getAppVersion() < 6) {
			whichStylesheet="layout_msie_5.css";
		} else if (bd.getAppVersion()==6) {
			whichStylesheet="layout_msie_6.css";
		} else if (bd.getAppVersion()==7) {
			whichStylesheet="layout_msie_7.css";
		} else {
			whichStylesheet="layout_msie_8.css";
		}
	}
	
	document.write("<link rel='stylesheet' type='text/css' href='");
	document.write(stylesheetPath + whichStylesheet);
	document.write("' />\n\n");
	
}







/**************************************

	PAGE BROWSER FIXES
	
	Selects the correct CSS stylesheet
	appropriate for the user's browser
	to apply fix code for this particular
	page;
	
	Requires the BrowserDetect object
	in browser_detect.js
	
***************************************/

function  PageBrowserFixes(bd) {
	var whichStylesheet="";

	if (bd.getAppName()=="Firefox") {
		whichStylesheet="fix_firefox.css";
	} else if (bd.getAppName()=="Safari") {
		whichStylesheet="fix_safari.css";
	} else if (bd.getAppName()=="Opera") {
		whichStylesheet="fix_opera.css";
	} else if (bd.getAppName()=="Chrome") {
		whichStylesheet="fix_chrome.css";
	} else if (bd.getAppName()=="Explorer") {
		if (bd.getAppVersion() < 6) {
			whichStylesheet="fix_msie_5.css";
		} else if (bd.getAppVersion()==6) {
			whichStylesheet="fix_msie_6.css";
		} else if (bd.getAppVersion()==7) {
			whichStylesheet="fix_msie_7.css";
		}	else {
			whichStylesheet="fix_msie_8.css";
		}
	}
	
	if(whichStylesheet != "") {
		document.write("<link rel='stylesheet' type='text/css' href='");
		document.write(whichStylesheet);
		document.write("' />\n\n");
	}
	
}
