/*	DETECT.JS  -> Deteccao do browser do cliente
   Ver documentacao em ./doc/detect.txt
*/
function Browser() {
	this.name = "Unknown";
	this.platform = "Unknown";
	this.version = "";
	this.majorver = "";
	this.minorver = "";
	this.init= Init;
	this.getName= GetName;
	this.getMinorver= GetMinorver;
	this.getMajorver= GetMajorver;
	this.getVersion= GetVersion;
	this.getPlatform= GetPlatform; 
	this.isIE= IsIE;
	this.isNetscape= IsNetscape;
	this.isWindows= IsWindows;
	this.isWinNT= IsWinNT;
	this.isWin95= IsWin95;
	this.isWin98= IsWin98;
	this.isLinux= IsLinux;
	this.isMac= IsMac;
	this.init();
}

function Init() {
	var ua=navigator.userAgent;
	
	uaLen = ua.length;

	//separa o que vem antes do parentesis do que esta
	//dentro do parentesis
	var preparens = new String("");
	var parenthesized = new String("");
	
	i = ua.indexOf("(");

	if (i >= 0){
		preparens = removeSpc(ua.substring(0,i));
		parenthesized = ua.substring(i+1, uaLen);
		j = parenthesized.indexOf(")");
		if (j >= 0)
			parenthesized = parenthesized.substring(0, j);
	}
	else
		preparens = ua;
	//primeiro assume que a versao e o browser estao em preparens
	//depois, sobrescrevemos se encontrarmos em parenthesized
	var browVer = preparens;
	var s= new String(parenthesized);
	var tokens= new Array;
	tokens= s.split_ITE(";");
	var token = "";
	//Tratamento dos Tokens em parenthesized
	for (var i=0; i < tokens.length; i++) {
		token = removeSpc(tokens[i]);
		//compatible - nao aparece no netscape
	    	if (token == "compatible"){
			//se este string esta na primeira posicao de tokens, 
			//entao a versao do browser aparecera em uma posicao
			//subsequente, dessa forma ignoramos este token.
		}
		else if (token.indexOf("MSIE") >= 0) {
			browVer = token;
		}else if (token.indexOf("Opera") >= 0) {
			browVer = token;
		}
		//plataforma: testamos para X11, SunOS, Win, Mac, PPC
		else if ((token.indexOf("X11") >= 0) || (token.indexOf("SunOS") >= 0) || (token.indexOf("Linux") >= 0)) {
			this.platform = "Unix";
		}else if (token.indexOf("Win") >= 0) {
			this.platform = token;
		}else if ((token.indexOf("Mac") >= 0) || (token.indexOf("PPC") >= 0)) {
			this.platform = token;
		}
	}
	var msieIndex = browVer.indexOf("MSIE");
	if (msieIndex >= 0) {
		browVer = browVer.substring(msieIndex, browVer.length);
	}
	var leftover = "";
	if (browVer.substring(0, "Mozilla".length) == "Mozilla") {
		this.name = "Netscape";
		leftover = browVer.substring("Mozilla".length+1, browVer.length);
	}
	else if (browVer.substring(0, "Lynx".length) == "Lynx") {
		this.name = "Lynx";
		leftover = browVer.substring("Lynx".length+1, browVer.length);
	}
	else if (browVer.substring(0, "MSIE".length) == "MSIE") {
		this.name = "IE";
		leftover = browVer.substring("MSIE".length+1, browVer.length);
	}
	else if (browVer.substring(0, "Microsoft Internet Explorer".length) == "Microsoft Internet Explorer") {
		this.name = "IE"
		leftover = browVer.substring("Microsoft Internet Explorer".length+1,
		browVer.length);
	}
	else if (browVer.substring(0, "Opera".length) == "Opera") {
		this.name = "Opera"
		leftover = browVer.substring("Opera".length+1, browVer.length);
	}
	leftover = removeSpc(leftover);
	//tentamos pegar a versao fora do leftover
	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 = this.version.substring(0,j);
		this.minorver = this.version.substring(j+1, this.version.length);
	}else {
		this.majorver = this.version;
	}
}//Fim de Browser

function GetName()
{
	return this.name;
}

function GetMajorver()
{
	return this.majorver;
}

function GetMinorver()
{
	return this.minorver;
}

function GetVersion()
{
	return parseFloat(this.version,10);
}

function GetPlatform()
{
	return this.platform;
}

function IsIE()
{
	return (this.name == 'IE');
}

function IsNetscape()
{
	return (this.name == 'Netscape');
}

function IsWinNT()
{
	return( (this.platform.indexOf('WinNT')!= -1) || (this.platform.indexOf('Windows NT')!= -1) );
}

function IsWindows()
{
	return( (this.platform.indexOf('Windows')!= -1) || (this.platform.indexOf('WinNT')!= -1) );
}

function IsWin95()
{
	return (this.platform.indexOf('Windows 95')!= -1);
}

function IsWin98()
{
	return (this.platform.indexOf('Windows 98')!= -1);
}

function IsLinux()
{
	return (this.platform.indexOf('Linux')!= -1);
}

function IsMac()
{
	return (this.platform.substring(0,3) == 'Mac');
}

//----- funcoes internas auxiliares -----//

function push(item)
{
	this[this.length] = item;
	return this.length;
}
Array.prototype.push_ITE = push;

function split(separador)
{
   var retorno= new Array;
   var temp= new String("");
   if( this.length == 0) return null;
   for( var i=0; i < this.length; i++) {
      var ch= this.substring(i,i+1);
      if( ch == separador) {
         retorno.push_ITE(temp);
         temp= "";
      }else{
         temp+= ch;
      }
   }
   retorno.push_ITE(temp);
   return retorno;
}
String.prototype.split_ITE = split;
//---------------------------------------//

function removeSpc(s)
{
   var retVal = "";
   var start = 0;
   var str= String(s);
   if( str== null || str.length == 0)
      return "";
   while ((start < str.length) && (str.charAt(start) == ' ')) {
   ++start;
   }
   var end = str.length;
   while ((end > 0) && (str.charAt(end - 1) == ' ')) {
   --end;
   }
   retVal = str.substring(start, end);
   return retVal;
}
