/*
* A Simplified version of the http://www.quirksmode.org/js/detect.html browser detection
* Stripped out uncommon browsers and just put what I belive to be the most widely used
* @Author Andrew Powell, Copyright &copy; 2008
* @Version 1.0
* @Date 25/07/2008
*/

/* 
* The String of names that will be returned for the certain browser
*/
var SAFARI = "Safari";
var OPERA = "Opera";
var NAVIGATOR = "Navigator";
var FIREFOX = "Firefox";
var EXPLORER = "Internet Explorer";

/*
* Main Entry point into creating the BrowserDetect object
*/
function BrowserDetect() {
	this.userAgentString = navigator.userAgent.toLowerCase();	
	
	/*
	* Defines the list of browsers
	* ----------------------------
	* subString : Is the name to look for in the userAgentString
	* identity : Is the name that will be returned into the this.browser variable
	* versionSearch : Is the string that appears before the version number, that is used to search the userAgentString to find the version number.
	*/
	this.browsers = [
		{
			subString: "Apple",
			identity: SAFARI,
			versionSearch: "version/"
		},
		{
			subString: "Opera",
			identity: OPERA,
			versionSearch: "Opera/"
		},
		{
			subString: "Navigator",
			identity: NAVIGATOR,
			versionSearch: "navigator/"
		},
		{
			subString: "Firefox",
			identity: FIREFOX,
			versionSearch: "Firefox/"
		},
		{
			subString: "MSIE",
			identity: EXPLORER,
			versionSearch: "MSIE "
		}
	];
	
	this.browser = this.findBrowser(this.browsers);
	if(this.browser == undefined) {
		throw "Browser Not Recognized";
	}
	this.version = this.findVersion();
	if(this.version == undefined) {
		throw "Browser Version Number Not Recognized";
	}
}

/*
* Get the browsers major version number
*/
BrowserDetect.prototype.getVersion = function() {
	return this.version;
}

/*
* Get the browsers name
*/
BrowserDetect.prototype.getBrowser = function() {
	return this.browser;
}

/*
* Search for compatible browser
*/
BrowserDetect.prototype.findBrowser = function(data) {
	for(var i = 0; i < data.length; i++) {
		// Get the String to search for the subString of the browser
		if(this.userAgentString.indexOf(data[i].subString.toLowerCase()) != -1) {
			this.versionSearchString = data[i].versionSearch.toLowerCase();
			return data[i].identity;
		}
	}
	return undefined;
}

/*
* Find the browsers majour version using the versionSearchString to find it
*/
BrowserDetect.prototype.findVersion = function() {
	var pos = this.userAgentString.indexOf(this.versionSearchString);
	if(pos == -1) {
		return undefined;
	}
	return parseFloat(this.userAgentString.substring(pos + this.versionSearchString.length, pos + this.versionSearchString.length + 1));
}

var Detector;

try {
	Detector = new BrowserDetect();
	// alert(detector.getBrowser() + ", " + detector.getVersion());
} catch(err) {
	//alert(err);
}

var isIE = false;
var isFirefox = false;
var isSafari = false;
var isOpera = false;
var isNavigator = false;

if(Detector) {
	isIE = Detector.getBrowser() == EXPLORER;
	isFirefox = Detector.getBrowser() == FIREFOX;
	isSafari =Detector.getBrowser() == SAFARI;
	isOpera = Detector.getBrowser() == OPERA;
	isNavigator = Detector.getBrowser() == NAVIGATOR;
}
