/**************************************
File:	browser.js
Author:	Matt Meyer
Date:	2007.05.24

Description:
	Detects browser information and 
	features.
**************************************/

//extend the string to support trimming
if(!String.prototype.trim){
	String.prototype.trim = function(){
		return this.replace(/^\s+|\s+$/gi,"");
	}
}

//Very basic html escaping function
function html_escape(arg){
	if(!arg)return "";
	return arg.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;");
}

//Owner object
Browser.prototype.Owner = false;

//Browser Types
Browser.prototype.IsIE = false;
Browser.prototype.IsOpera = false;
Browser.prototype.IsFirefox = false;
Browser.prototype.IsNetscape = false;
Browser.prototype.IsSafari = false;
Browser.prototype.IsOther = false;

//Capabilities
Browser.prototype.HasDOM = false;
Browser.prototype.HasJava = false;
Browser.prototype.HasCookies = false;
Browser.prototype.HasActiveX = false;
//Common Plugins
Browser.prototype.HasFlash = false;
Browser.prototype.HasAcrobat = false;
Browser.prototype.HasQuickTime = false;
Browser.prototype.HasMediaPlayer = false;

Browser.prototype.IsLoaded = false;

//Browser/OS Version
Browser.prototype.Version = 0.0;
Browser.prototype.MajorVersion = 0;
Browser.prototype.MinorVersion = 0;
Browser.prototype.OperatingSystem = "";

function Browser(Owner){
	//so I can access config and what not
	if(Owner){
		this.Owner = Owner;
	}
	
	var info = this.GetUserAgentInfo();

	var v;	//used in version detection
	var b = false;	//flag for detecting browse
	
	//Attempts to parse the info in the user agent
	//segments.  
	for(var i = 0; i < info.length; i++){
		if(info[i].indexOf("MSIE")!=-1){	//IE
			b = true;
			this.IsIE = true;
			v = info[i].split(" ")[1];
			this.Version = v;
			v = v.split(".");
			this.MajorVersion = v[0];
			this.MinorVersion = v[1];
		}else if(info[i].indexOf("Opera")!=-1){	//Opera
			b = true;
			this.IsOpera = true;
			v = info[i].split("/")[1];
			this.Version = v;
			v = v.split(".");
			this.MajorVersion = v[0];
			this.MinorVersion = v[1];
		}else if(info[i].indexOf("Firefox")!=-1){	//Firefox
			b = true;
			this.IsFirefox = true;
			v = info[i].split("/")[1];
			this.Version = v;
			v = v.split(".");
			this.MajorVersion = v[0];
			this.MinorVersion = v[1];
		}else if(info[i].indexOf("Safari")!=-1){	//Safari
			b = true;
			this.IsSafari = true;
			v = info[i].split("/")[1];
			this.Version = v;
			v = v.split(".");
			this.MajorVersion = v[0];
			this.MinorVersion = v[1];
		}else if(info[i].indexOf("Netscape")!=-1){	//Navigator
			b = true;
			this.IsNetscape = true;
			v = info[i].split("/")[1];
			this.Version = v;
			v = v.split(".");
			this.MajorVersion = v[0];
			this.MinorVersion = v[1];
		}else if(info[i].indexOf("Windows")!=-1){	//Windows (OS)
			this.OperatingSystem = info[i];
		}else if(info[i].indexOf("OS X")!=-1){		//Mac (OS)
			this.OperatingSystem = info[i];
		}
	}
	//Check if we've detected what browser
	if(!b){
		this.IsOther = true;
	}

	//Capability detection
	this.HasJava = navigator.javaEnabled();
	this.HasCookies = navigator.cookieEnabled;
	this.HasActiveX = window.ActiveXObject ? true : false;
	if(document.getElementById)
		this.HasDOM = true;
	if(this.IsIE){
		//Use special function for detecting IE plugins
		//since it doesn't use the plugins array.
		this.HasFlash = this.DetectIePlugin("ShockwaveFlash.ShockwaveFlash");
		//this.HasAcrobat = this.DetectIePlugin("PDF.PdfCtrl");
		//this.HasQuickTime = this.DetectIePlugin("QuickTimeCheckObject.QuickTimeCheck");
		//this.HasMediaPlayer = this.DetectIePlugin("MediaPlayer.MediaPlayer");
	}else{
		if(navigator.plugins){
			for(var i = 0; i < navigator.plugins.length; i++){
				if(navigator.plugins[i].name.indexOf("Flash")!=-1)
					this.HasFlash = true;
				else if(navigator.plugins[i].name.indexOf("Acrobat")!=-1)
					this.HasAcrobat = true;
				else if(navigator.plugins[i].name.indexOf("QuickTime")!=-1)
					this.HasQuickTime = true;
				else if(navigator.plugins[i].name.indexOf("Windows Media Player")!=-1)
					this.HasMediaPlayer = true;
			}
		}
	}
	//just to be stateful
	this.IsLoaded = true;
}

//Function specifically for detecting pluins in IE by
//created an ActiveX object for them.
Browser.prototype.DetectIePlugin = function(name){
	var t;
	for(var i = 1; i < 10; i++){
		try{
			t = new ActiveXObject(name + "." + i);
			return true;
		}catch(e){
		}
	}
	return false;
}

//Retreives the UserAgent string and makes a best 
//attempt to split up into relevant chunks
Browser.prototype.GetUserAgentInfo = function(){
	var ua = navigator.userAgent;
	var uainf = [];	//info

	var s = 0,	//start
		i = 0;	//index
	var sc = " (",	//split characters
		v = "";	//value

	while(i < ua.length){
		if(sc.indexOf(ua.charAt(i))!=-1){
			if(s!=i){
				v = ua.substr(s,i-s).trim();
				if(v.length>0){
					uainf.push(v);
				}
			}
			s = i+1;
			if(ua.charAt(i)=="("){
				sc = ";)";
			}else if(ua.charAt(i)==")"){
				sc = " (";
			}
		}
		i++;
	}
	if(s!=i){	//last value
		v = ua.substr(s,i-s).trim();
		if(v.length>0){
			uainf.push(v);
		}
	}
	return uainf;
}

//Create a browser object in the document model
document.Browser = new Browser();