var showAlert = false ;
var byVersion = true ;
var byPlatform = false ;
var platformOnly = false ;
var nonLocalPath = false ;
var cssLocation = 'css/' ;
var jsLocation = 'Scripts/' ;
var mobileRedirect = false ;
var mobilePath = '/m' ;
var noIE6Redirect = false ;
var noIE6Path = '/noIE6.html' ;

function cssObject(theCSS) {
this.headID = document.getElementsByTagName("head")[0]; 
this.cssNode = document.createElement('link');
this.cssNode.type = 'text/css';
this.cssNode.rel = 'stylesheet';
this.cssNode.href = cssLocation + theCSS + '.css';
this.cssNode.media = 'screen';
this.headID.appendChild(cssNode);
if (showAlert == true) {
	alert(theCSS) ;
	alert(this.cssNode.href) ;
	}

}

function cssByBrowser() {
// 
var browser = BrowserDetect.browser ;
var version = String(BrowserDetect.version) ;
var platform = BrowserDetect.OS ;
var vp = "" ;
var subVerPOS = -1 ;

	var items = cssByBrowser.arguments.length
	var theArg ;
	for (i = 0; i < items;i++)
	{
		theArg = cssByBrowser.arguments[i] ;
		procArgs(theArg) ;
	}
	
	// check for mobile redirect
	if ((ifPlatform("iPhone")) && (mobileRedirect == true)) {
		window.location.href = window.location.protocol + "//" + window.location.host + mobilePath + window.location.pathname + "?" + location.search.split("?")[1];
	}
	
	// check for noIE6
//	if ((browser + version == "Explorer6") && (noIE6Redirect == true)) {
	if ((ifBrowser("Explorer","6")) && (noIE6Redirect == true)) {
		window.location.href = window.location.protocol + "//" + window.location.host + noIE6Path ;
	}
	
	// location.hostname

	if (byVersion == true) {
		// check if we're getting sub-versions like
		//  we do in Safari and strip the extra data
		subVerPOS = version.indexOf(".") ;
		if (subVerPOS > 0) {
			version = version.substr(0, subVerPOS) ;
		}
		
		vp = "_" + version ;
	}

	if (byPlatform == true) {
		vp += "_" + platform ;
	}

//  old version always called by browser first
//	cssObject(browser) ;

	// platform only means no browser & no version at all
	if (platformOnly == true) {
		cssObject(platform) ;
	} else {
		// browser specific changes first
		cssObject(browser) ;
		
		// if we need version and/or platform specific changes we do them here
		if ((byVersion == true) || (byPlatform == true)) {
	//		if ((browser == "Explorer") || (browser == "Firefox"))
				cssObject(browser + vp) ;
		}
	}
}

function ifBrowser(ifB, ifV, ifP) {
var ignore = "ignore" ;
ifB = ifB || ignore ;
ifV = ifV || ignore ;
ifP = ifP || ignore ;

// we set this to true for a little 'and' logic ;-)
result = true ;

	if (ifB != ignore) {
		result = (result && (ifB == BrowserDetect.browser)) ;
	}

	if (ifV != ignore) {
		result = (result && (ifV == String(BrowserDetect.version))) ;
	}

	if (ifP != ignore) {
		result = (result && (ifP == BrowserDetect.OS)) ;
	}
//alert(result + "::" + BrowserDetect.browser + "::" + String(BrowserDetect.version)) ;
	return result ;
}

function ifVersion(ifV) {
	ifBrowser("",ifV) ;
}


function ifPlatform(ifP) {
	ifBrowser("","",ifP) ;
}


function setTheme(value) {
	cssObject(value) ;
}


function procArgs(anArg)
{
var lowerArg = anArg.toLowerCase()
	switch(lowerArg)
	{
	case "debug":
		showAlert = true ;
		break;    
	case "versions":
		byVersion = true ;
		break;
	case "noversions":
		byVersion = false ;
		break;
	case "platform":
		byPlatform = true ;
		break;
	case "platformonly":
	case "platform only":
		byPlatform = true ;
		byVersion = false ;
		platformOnly = true ;
		break;
	case "path":
		nonLocalPath = true ;
		break;
	case "mobile":
	case "iphone":
		mobileRedirect = true ; 
		break ;
	case "noie6":
		noIE6Redirect = true ;
		break ;
	default:
		if (nonLocalPath == true) 
			cssLocation = "http://www.mainstreetfaces.com/" + anArg + "/" ;
	}
}


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";
		this.displayAs = this.searchString(this.dataCompatability) || "an unknown Display As";
	},
	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: "Win"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	],
	dataCompatability : [
		{
			string: this.browser,
			subString: "Explorer",
			identity: "Safari"
		},
		{
			string: this.browser,
			subString: "Safari",
			identity: "Safari"
		},
		{
			string: this.browser,
			subString: "Firefox",
			identity: "Safari"
		}
	]

};
BrowserDetect.init();

function centerIE(force) {
force = force || "" ;
var widthPadding = 20, heightPadding = 20 ;
	if (ifBrowser("Explorer")) {
		var winW = document.documentElement.clientWidth ;
		var	winH = document.documentElement.clientHeight ;
		var theDiv = document.getElementById("page-wrap") ;
		var divW = theDiv.offsetWidth ;
		var divH = theDiv.offsetHeight ;
		if (force == "force") {
			if (winW < divW) winW = divW + widthPadding ;
			if (winH < divH) winH = divH + heightPadding ;
// 				window.resizeTo(winW, winH) ;						
		} 
		var divDeltaW = winW - divW ;
		var divDeltaH = winH - divH ;
		if (divDeltaW > 0) theDiv.style['left'] = divDeltaW / 2 ;			
		if (divDeltaH > 0) theDiv.style['top'] = divDeltaH / 2 ;	
		
		var divs = document.getElementsByTagName("div");
        for (var i=0; i<divs.length; i++) {
        alert(this.id)
            divs[i].onmouseover = function () { return true;} ; 
            divs[i].onmouseover() ; 
        }
	}
}
