/**
	Browser Info - browser information retrieval methods.
	
	@author	Brian Grayless
			Copyright 2000-2005 (c) Sunergize Inc.
			Visit http://www.sunergize.com
 	 				
 	@license	This library is free software; you can redistribute it and/or
			modify it under the terms of the GNU Lesser General Public
			License as published by the Free Software Foundation; either
			version 2.1 of the License, or (at your option) any later version.
			
			This library is distributed in the hope that it will be useful,
			but WITHOUT ANY WARRANTY; without even the implied warranty of
			MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
			Lesser General Public License for more details.
			
			If you did not receive a copy of the GNU Lesser General Public
			License along with this library, write to the Free Software
			Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
function Browser_Info()
{
	this.detect_string = navigator.userAgent.toLowerCase();
	// set default browser values
	this.os = null;
	this.name = null;
	this.version = null;
	this.full_version = null;
	
	// used for name
	this.name_index = -1;
	this.name_string = '';
};

Browser_Info.prototype.get_Browser_Object = function()
{
	var browser_obj = new Object();
	
	browser_obj.name = this.get_Name();
	browser_obj.version = this.get_Version();
	browser_obj.full_version = this.get_Full_Version();
	browser_obj.os = this.get_Os();
	
	return browser_obj;
};

Browser_Info.prototype.get_Name = function()
{
	if(this.name == null)
	{
		// look for browser name and set if found
		switch (true)
		{
			case this._check_Browser_String('konqueror', 'name'):
				this.name = 'Konqueror';
				break;
			case this._check_Browser_String('safari', 'name'):
				this.name = 'Safari';
				break;
			case this._check_Browser_String('firefox', 'name'):
				this.name = 'Firefox';
				break;
			case this._check_Browser_String('omniweb', 'name'):
				this.name = 'OmniWeb';
				break;
			case this._check_Browser_String('opera', 'name'):
				this.name = 'Opera';
				break;
			case this._check_Browser_String('webtv', 'name'):
				this.name = 'WebTV';
				break;
			case this._check_Browser_String('icab', 'name'):
				this.name = 'iCab';
				break;
			case this._check_Browser_String('msie', 'name'):
				this.name = 'Internet Explorer';
				break;
			case !this._check_Browser_String('compatible', 'name'):
				// if another browser hasn't been found at this point, assume Netscape (Netscape doesn't have "compatible" in the details)
				this.name = 'Netscape Navigator';
				break;
			default:
				this.name = 'An unknown browser';
				break;
		}
	}

	return this.name;	
};

Browser_Info.prototype.get_Version = function()
{
	// define characters allowed in version number
	var allowed_chars = '0123456789.';
	
	// run if it doesn't exist
	if(this.version == null)
	{
		// set name incase it's needed to determine version
		this.get_Name();
		
		// Netscape's version is always at position 8
		if(this.name == 'Netscape Navigator')
		{
			var version_string = this.detect_string.substr(8);
		}
		else
		{
			// get substring from end of name to end
			var version_string = this.detect_string.substr(this.name_index + this.name_string.length);
		}

		// find ending version character index
		var ver_str_length = version_string.length;
		var end_index = ver_str_length;
		for(var i = 0; i < ver_str_length; i++)
		{
			// attempt to split allowed characters with given char from version string (if illegal char found, it won't split)
			var char_split = allowed_chars.split(version_string.charAt(i));

			// wont split, illegal character found, set end_index
			if(char_split.length == 1)
			{
				end_index = i;
				break;
			}
		}
		
		this.full_version = version_string.substring(0, end_index);
		// extract version from full version to the first decimal
		this.version = this.full_version.substring(0, this.full_version.indexOf('.'));
		
		// if version is not a full version, substitute with full version
		if(this.version < 1)
		{
			this.version = this.full_version;	
		}
	}
	
	return this.version;
};

Browser_Info.prototype.get_Full_Version = function()
{	
	// if full version doesn't exist, run get_Version which creates it
	if(this.full_version == null)
	{
		this.get_Version();
	}
	
	return this.full_version;
};

Browser_Info.prototype.get_Os = function()
{
	if(this.os == null)
	{
		switch(true)
		{
			case this._check_Browser_String('linux'):
				this.os = 'Linux';
				break;
			case this._check_Browser_String('x11'):
				this.os = 'Unix';
				break;
			case this._check_Browser_String('mac'):
				this.os = 'Mac';
				break;
			case this._check_Browser_String('win'):
				this.os = 'Windows';
				break;
			default:
				this.os = 'an unknown operating system';
				break;
		}
	}
	
	return this.os;
};

Browser_Info.prototype._check_Browser_String = function(needle, type)
{	
	var found = false;
	
	// look for string in details
	var place_index = this.detect_string.indexOf(needle);
	
	if(place_index > -1)
	{
		found = true;
	}
	
	switch(type)
	{
		case 'name':
			// if name type, set where string is found and it's value
			this.name_index = place_index + 1;
			this.name_string = needle;
			break;
		default:
			break;
	}

	return found;
};