/*
	Advanced browser detection function/object (Used for the browser version parsing feature)
	http://www.quirksmode.org/js/detect.html
*/
var BrowserDetect = {

	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};BrowserDetect.init();
////////////////////////////////////////////////////////////

/*
	Single-instance sysinfo class
	Requires jQuery and flash_detection.js
	Provides a central interface for the environment info
*/
tfoSysInfo = function(){

	try {
	
		// Check that THEFOOT namespace is created
		if (!THEFOOT){throw('THEFOOT global namespace is not yet initialised.');}
	
		/*****************************************************
			Properties
		*****************************************************/
		this.browser = {'ff': null, 'ie': null, 'sa': null, 'op': null, 'ch': null, 'version': null, 'support': null}
		this.flash = {'enabled': null, 'version': {'maj': null, 'min': null, 'rev': null}}
		this.javascript = null;
		this.cookies = null;
		this.prototypejs = null;
		this.jquery = null;
		this.yui = null;
		this.screen = {
			'width': null,
			'height': null
		};
		this.viewport = {
			'width': null,
			'top': null,
			'height': null
		}
		this.page = {
			'width': null,
			'height': null
		}
		this.language = null;
		this.os = {'name': null}
		this.java = null;
		
		/*****************************************************
			Public Methods
		*****************************************************/
		
		// Query the env
		this.refresh = function(){

			// Browser object
			this.browser.name = BrowserDetect.browser;
			this.browser.ff = jQuery.browser.mozilla;
			this.browser.ie = jQuery.browser.msie;
			this.browser.sa = jQuery.browser.safari;
			this.browser.op = jQuery.browser.opera;
			this.browser.ch = (navigator.userAgent.indexOf('Chrome') > 0);
			this.browser.version = BrowserDetect.version;
			this.browser.support = jQuery.support; // See http://docs.jquery.com/Utilities/jQuery.support
			
			// Browser feature support (requires Adobe's flash-detection.js script)
			var v_version = GetSwfVer();
			if (parseInt(v_version) == -1){
				this.flash.version.maj = 0;
				this.flash.enabled = false;
			} else {
				var a_version = v_version.split('.');
				this.flash.version.maj = parseInt(a_version[0]);
				this.flash.version.min = parseInt(a_version[1]);
				this.flash.version.rev = parseInt(a_version[2]);
				this.flash.enabled = true;
			}
			
			// Javascript support :) here for the sake of brevity, and possible expansion with version number
			this.javascript = true;
			
			// JAVA support?
			this.java = navigator.javaEnabled();
			
			// Cookies?
			this.cookies = THEFOOT.cookie.enabled();
			
			// JS Frameworks
			this.prototypejs = !!window.Prototype;
			this.jquery = !!window.jQuery;
			this.yui = !!window.YAHOO;
			
			// Dimensions
			this.screen.width = screen.width;
			this.screen.height = screen.height;
			this.viewport.width = jQuery(window).width();
			this.viewport.top = jQuery(window).scrollTop();
			this.viewport.height = jQuery(window).height();
			this.page.width = jQuery(document).width();
			this.page.height = jQuery(document).height();
			
			// Language
			this.language = _getLanguage();
			
			// OS Name
			this.os.name = 'Unknown';
			if (navigator.appVersion.toLowerCase().indexOf("win")!=-1) this.os.name = 'Windows';
			if (navigator.appVersion.toLowerCase().indexOf("mac")!=-1) this.os.name = 'Mac';
			if (navigator.appVersion.toLowerCase().indexOf("x11")!=-1) this.os.name = 'UNIX';
			if (navigator.appVersion.toLowerCase().indexOf("linux")!=-1) this.os.name = 'Linux';
			
			return this;

		} // this.refresh()
		
		// Create a JSON object from the sysinfo properties
		this.toJson = function(v_as_string){
			
			var o_localcopy = {}
			jQuery.each(this, function(name, value) {
				if (typeof value != "function"){o_localcopy[name] = value;}
			});
		
			// Return data in requested format
			if (v_as_string){
				return JSON.stringify(o_localcopy);
			} else {
				return o_localcopy;
			}
			
		} // this.toJson()
		
		/*****************************************************
			Private Methods
		*****************************************************/
		var _getLanguage = function() {		
			if (navigator.userLanguage) {
				return(navigator.userLanguage);
			} else if (navigator.language) {
				return(navigator.language);
			} else {
				return("(Not supported)");
			}
		} // _getLanguage();
		
		/*****************************************************
			Constructor code
		*****************************************************/
		this.refresh();
		
		// Ensure that dimension objects are refreshed when window resizes
		$(window).resize(function(){
			this.screen.width = screen.width;
			this.screen.height = screen.height;
			this.viewport.width = jQuery(window).width();
			this.viewport.top = jQuery(window).scrollTop();
			this.viewport.height = jQuery(window).height();
			this.page.width = jQuery(document).width();
			this.page.height = jQuery(document).height();
		}.bind(this));
		$(window).scroll(function(){
			this.screen.width = screen.width;
			this.screen.height = screen.height;
			this.viewport.width = jQuery(window).width();
			this.viewport.top = jQuery(window).scrollTop();
			this.viewport.height = jQuery(window).height();
			this.page.width = jQuery(document).width();
			this.page.height = jQuery(document).height();
		}.bind(this));		
		return this;
		
	} catch(e) {
		alert('Error creating application sysinfo object: ' + e);
		return false;
	}
	
} // tfoSysInfo Constructor
