﻿var BrowserCompatibility = {
	init: function() {
		this.popups = this.allows_popups();
		this.cookies = this.allows_cookies();
		this.flash_version = this.get_flash_version();
		this.flash = this.min_version('8.0.0', this.flash_version);
//		this.svg_version = this.get_svg_version();
//		this.svg = this.min_version('3.03', this.svg_version);
		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";
		this.valid_browser = this.valid_browser(this.browser, this.version);
		this.valid = this.popups && this.cookies && this.flash ? this.valid_browser : 0;
	},

	allows_popups: function() {
		// open a popup to see if popups are allowed
		var result = window.open("about:blank", "popup", "width=1, height=1");
		if(result == null) {
			return 0;
		}
		try {
		    result.close();  // close the popup
		} catch(e) {
		    // opera tells us the window opened even though it was blocked
		    // but it fails when trying to close the window, so we can detect that
		    return 0;
		}
		return 1;
	},

	allows_cookies: function() {
		// check if session cookies are enabled,
		// by setting a cookie and reading back it's value
		document.cookie = "test=42;";
		if(document.cookie.indexOf('test=42') != -1) {
			// cookie found
			// erase the cookie
			document.cookie = "test=42;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
			return 1;
		}
		return 0;
	},

	get_flash_version: function() {
		// detect the flash plugin version
		if(navigator.plugins && navigator.plugins.length) {
			// do it the standard way
			var plugin = navigator.plugins['Shockwave Flash'];
			if(plugin) {
				var flash_version = plugin.description.substring(16, plugin.description.length);
				return flash_version.replace(/\s*r/i, ".");
			}
		}
		// attempt to detect the plugin in IE using activeX
		for(var i=10; i>0; i--) {
			try {
				new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);
				// only the major version can be detected this way
				return i+".0.0";
			} catch(e) {
			}
		}
		// unable to detect any flash plugin
		return 0;
	},

	get_svg_version: function() {
		// detect the Adobe SVG plugin version
		if(navigator.plugins && navigator.plugins.length) {
			// do it the standard way
			var plugin = navigator.plugins['Adobe SVG Viewer Plugin'];
			if(plugin) {
				return plugin.description.substring(11, plugin.description.length);
			}
		}
		// attempt to detect the plugin in IE using activeX
		for(var i=4; i>0; i--) {
			try {
				new ActiveXObject("Adobe.SVGCtl."+i);
				// only the major version can be detected this way
				return i+".03";
			} catch(e) {
			}
		}
		// unable to detect any svg plugin
		return 0;
	},

	valid_browser: function(browser, version) {
		if(browser == 'Explorer' && version == 7) return 2;
		if(browser == 'Firefox' && (version == 2 || version == 3)) return 2;
		if (browser == 'Firefox' && version > 3) return 2;
		if (browser == 'Explorer' && version > 7) return 2;
		
		if(browser == 'Explorer' && version == 6) return 1;
		
		if(browser == 'Safari') return 1;
		if(browser == 'Opera') return 1;
		if(browser == 'Chrome') return 1;
		
		return 0;
	},
	
	min_version: function(min_version, version) {
	    if(!version.split || !min_version.split) return false;  // version needs to be a string
	    var m_version = min_version.split('.');
	    var t_version = version.split('.');
	    for(var i=0; i<m_version.length && i<t_version.length; i++) {
	        if(parseInt(m_version[i], 10) > parseInt(t_version[i], 10)) return false;
	        if(parseInt(t_version[i], 10) > parseInt(m_version[i], 10)) return true;
            // else their equal, so check the next digit
	    }
	    // the versions are equal, so the requirement is met
	    return true;
	},

	// browser version and OS detection code taken from
	// http://www.quirksmode.org/js/detect.html
	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.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};
