/*
BrowserDetector()
Parses User-Agent string into useful info.
Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)
Author: Richard Blaylock
Author Email: blaylock@wired.com

Usage: var bd = new BrowserDetector(navigator.userAgent);

Major mods for narrowstep player - feb 03 by jb
050712	jb	Adding GeckoActiveX bits
051230	sd	Updated detection to support Opera properly
*/

function BrowserDetector(ua) {

// Defaults
	this.browser = "Unknown";
	this.platform = "Unknown";
	this.version = "";
	this.majorver = "";
	this.minorver = "";
	// jb added a few others
	
	this.cookies = false;
	this.is_win = false;
	this.is_mac = false;
	this.is_ie = false;
	this.is_ns = false;
	this.browser_ok = false;
	this.wmp_ver = 0;
	this.plugin_ok = false;
	this.applet_ok = false;
	this.activex = false;
	
	this.ua = ua;
	this.errors = new Array();
	this.smooth = false;
	// set by wmp-lib
	this.wmp7 = false;
	this.flip4mac = false;

	ua = ua.toLowerCase();
	var uaLen = ua.length;

// ##### Split into stuff before parens and stuff in parens
	var preparens = "";
	var parenthesized = "";

	var k = ua.indexOf("(");
	if (k >= 0) {
		preparens = Trim(ua.substring(0,k));
		parenthesized = ua.substring(k+1, uaLen);
		var j = parenthesized.indexOf(")");
		if (j >= 0) {
			parenthesized = parenthesized.substring(0, j);
		}
	} else {
		preparens = ua;
	}

// ##### First assume browser and version are in preparens
// ##### override later if we find them in the parenthesized stuff
	var browVer = preparens;

	var tokens = parenthesized.split(";");
	var token = "";
// # Now go through parenthesized tokens
	for (var i=0; i < tokens.length; i++) {
		token = Trim(tokens[i]);
		//## compatible - might want to reset from Netscape

		if (token == "compatible") {
			//## One might want to reset browVer to a null string
			//## here, but instead, we'll assume that if we don't
			//## find out otherwise, then it really is Mozilla
			//## (or whatever showed up before the parens).
			//## browser - try for Opera or IE
		} else if (token.indexOf("msie") >= 0) {
			browVer = token;
//		} else if (token.indexOf("opera") >= 0) {
//			browVer = token;

		}  else if ((token.indexOf("x11") >= 0) || (token.indexOf("sunos") >= 0) || (token.indexOf("linux") >= 0)) {
//'## platform - try for X11, SunOS, Win, Mac, PPC
			this.platform = "unix";
		} else if (token.indexOf("win") >= 0) {
			this.platform = token;
			this.is_win = true;
		} else if ((token.indexOf("mac") >= 0) || (token.indexOf("ppc") >= 0)) {
			this.platform = token;
			this.is_mac = true;
		}
	}
	// Opera pretends to be msie in the parenthesized stuff - thanks!
if (ua.toLowerCase().indexOf("opera") >= 0) {
//			this.browser = "Opera"
//			this.is_ns = true;
			browVer = "Opera";
		}
	var msieIndex = browVer.indexOf("msie");
	if (msieIndex >= 0) {
		browVer = browVer.substring(msieIndex, browVer.length);
	}

	var leftover = "";
	if (browVer.substring(0, "mozilla".length) == "mozilla") {
		this.browser = "netscape";
		this.is_ns = true;
		leftover = browVer.substring("mozilla".length+1, browVer.length);
	} else if (browVer.substring(0, "lynx".length) == "lynx") {
		this.browser = "lynx";
		leftover = browVer.substring("lynx".length+1, browVer.length);
	} else if (browVer.substring(0, "msie".length) == "msie") {
		this.browser = "IE";
		this.is_ie = true;
		leftover = browVer.substring("msie".length+1, browVer.length);
	} else if (browVer.substring(0, "microsoft internet explorer".length) == "microsoft internet explorer") {
		this.browser = "IE";
		this.is_ie = true;
		leftover = browVer.substring("microsoft internet explorer".length+1, browVer.length);
	} else if (browVer.toLowerCase().substring(0, "opera".length) == "opera") {
		this.browser = "Opera";
		this.is_ns = true;
		leftover = browVer.substring("opera".length+1, browVer.length);
	}
	if (this.is_ns) { //
		// browser name
		//alert(ua);
		if (ua.indexOf('konqueror') != - 1) {this.browser = "Konqueror";}
		if (ua.indexOf('safari') != - 1) {this.browser = "Safari";}
		if (ua.indexOf('omniweb') != - 1) {this.browser = "Omniweb";}
		if (ua.toLowerCase().indexOf('opera') != - 1) {this.browser = "Opera";}
		if (ua.indexOf('icab') != - 1) {this.browser = "Icab";}
		if (ua.indexOf('aol') != - 1) {this.browser = "AOL";}
		if (ua.indexOf('firebird') != - 1) {this.browser = "Firebird";}
		if (ua.indexOf('firefox') != - 1) {this.browser = "FireFox";}
		if (ua.indexOf('gecko') != -1 && (ua.indexOf('gecko/') + 14) == ua.length){
			this.browser = "Mozilla";
		}
 		if (ua.indexOf('netscape') != -1) {this.browser = "Netscape";}

	}
	leftover = Trim(leftover);

	// # Try to get version info out of leftover stuff
	i = leftover.indexOf(" ");
	if (i >= 0) {
		this.version = leftover.substring(0, i);
	} else {
		this.version = leftover;
	}
	j = this.version.indexOf(".");
	if (j >= 0) {
		this.majorver = parseInt(this.version.substring(0,j),10);
		this.minorver = parseInt(this.version.substring(j+1, this.version.length),10);
	} else {
		this.majorver = parseInt(this.version,10);
	}
	
// jb bits
	var obj;
	if (window.GeckoActiveXObject) {
		try {
			obj = new GeckoActiveXObject("WMPlayer.OCX.7");	// was MediaPlayer.MediaPlayer.1
			this.activex =  true;
		} catch (e) {
			this.activex =  false;
		}
	} else if (window.ActiveXObject && this.is_win) {
		try {
			obj = new ActiveXObject("WMPlayer.OCX.7");	// was MediaPlayer.MediaPlayer.1
			this.activex =  true;
		} catch (e) {
			this.activex =  false;
		}
	} else if (this.is_ie && this.is_win) {	// just in case
			this.activex =  true;				// bit of an assumption+
	} else {
		this.activex =  false;
	}
if ($.browser.opera) {
			this.activex =  false;				// bit of an assumption+
}
// get cookie details
	this.cookies = checkCookie();
	
	if(typeof gViewerID != "undefined") {userPrefs(gViewerID);} // Create User Prefs Cookie 

//	this.cookies = false;

	this.browser_ok = true;
	//alert(location + " " + gRequireCookies);
	if (!this.cookies && typeof gRequireCookies != "undefined" && gRequireCookies) {
		this.errors[this.errors.length] = "Cookies are disabled";
		this.browser_ok = false;
	}
	if (!this.is_ie && !this.is_ns) {	//
//alert("Browser not Microsoft Internet Explorer");
		this.errors[this.errors.length] = "Browser not Microsoft Internet Explorer or \"Mozilla\" compatible";	//
		this.browser_ok = false;
	} else if (this.is_win && this.majorver < 5) {
		this.errors[this.errors.length] = "Browser (Internet Explorer) older than version 5";
		this.browser_ok = false;
	} else if (this.is_mac && this.majorver < 5) {
		this.errors[this.errors.length] = "Browser (Internet Explorer) older than version 5";
		this.browser_ok = false;
	}
	if (!this.is_win && !this.is_mac) { // now really only Win compatible //) {
		this.errors[this.errors.length] = "Unsupported OS (only Microsoft Windows and Mac OS X fully supported)";
		this.browser_ok = false;
	}

} // function BrowserCap


