// ==============================================================================================================
// ===                                     P U B L I C   F U N C T I O N S                                    ===
// ==============================================================================================================

// Show the compatibility dialog...

function TestCompatibility(functor)
{
    // 1: check abilities...
    
   	var test = new CompatibilityTest();
    if(test.passed)
    {
        functor();
        return;
    }

	// 2: add some code...

	$('body').append("<div id='compatibility'>" +
										 "<div id='comp-background'>" +
									   "</div>" +
										 "<table id='comp-table' align='center' valign='middle'><tr><td>" +
										 	 "<div id='comp-dialog'>" +
										 	   "<a id='comp-close' href='http://www.dyyno.com/support.php' title='Close'><img src='_compat/img/close.png' width='16' height='16' border='0' /></a>" +
										 	   "<h1>Dyyno Compatibility Test</h1>" +
												 "<ul>" +
												 	 "<li id='comp-os'>System must be Windows XP or better or Mac OS X (Intel)</li>" +
												 	 "<li id='comp-browser'>Browser has to be Internet Explorer 6+ or Firefox 2+</li>" +
												 	 "<li id='comp-resolution'>Screen resolution hast to be 640 &times; 480 or better</li>" +
												 	 "<li id='comp-depth'>Screen depth has to be 24 bit or better</li>" +
												 	 "<li id='comp-flash'>Flash version 10+ or better has to be installed</li>" +
												 	 "<li id='comp-cookie'>Cookies have to be enabled</li>" +
												 "</ul>" +
												 "<div id='comp-failed'>" +
												 	 "Your system failed in the Dyyno Compatibility Test. Please make sure that all the above requirements are met and repeat the test." +
												 "</div>" +
												 "<div id='comp-anyway'>" +
												 	 "<a href='#' title='Show video'>Try anyway!</a>" +
												 "</div>" +
											 "</div>" +
									   "</td></tr></table>" +
							  	 "</div>");

	// 3: check settings...

		// 3.1: check OS...

		if(test.os)
		{
			$('#comp-dialog #comp-os').addClass('comp-pass');
		}
		else
		{
			$('#comp-dialog #comp-os').addClass('comp-fail');
		}

		// 3.2: check browser...

		if(test.browser)
		{
			$('#comp-dialog #comp-browser').addClass('comp-pass');
		}
		else
		{
			$('#comp-dialog #comp-browser').addClass('comp-fail');
		}

		// 3.3: screen resolution...

		if(test.resolution)
		{
			$('#comp-dialog #comp-resolution').addClass('comp-pass');
		}
		else
		{
			$('#comp-dialog #comp-resolution').addClass('comp-fail');
			passed = false;
		}

		// 3.4: screen depth...

		if(test.depth)
		{
			$('#comp-dialog #comp-depth').addClass('comp-pass');
		}
		else
		{
			$('#comp-dialog #comp-depth').addClass('comp-fail');
			passed = false;
		}

		// 3.5: flash version...

		if(test.flash)
		{
			$('#comp-dialog #comp-flash').addClass('comp-pass');
		}
		else
		{
			$('#comp-dialog #comp-flash').addClass('comp-fail');
			passed = false;
		}
		
		// 3.6: cookies enabled?

		if(test.cookies)
		{
			$('#comp-dialog #comp-cookie').addClass('comp-pass');
		}
		else
		{
			$('#comp-dialog #comp-cookie').addClass('comp-fail');
			passed = false;
		}
		
	// 4: add code to close window...
	
	$('#comp-dialog #comp-anyway a').click(function(event)
	{
		event.preventDefault();

		$('#compatibility').remove();
        functor();
	});
}


// The actual detection functionality...

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();

function CompatibilityTest()
{
	// 1: check OS...
		
	this.os = false;

	if(RegExp('Win').test(navigator.platform))
	{
		var result = navigator.userAgent.match(RegExp('Windows\\sNT\\s+(\\d+\\.\\d+)'));
		if(typeof result != 'undefined')
		{
			var version = parseFloat(result[1]);
			
			if((version >= 5.1)) // && (version <= 6.0))
			{
				this.os = true;
			}
		}
	}

	if(BrowserDetect.OS == "Mac"){
		this.os = true;
	}

	if (!this.os)
	{
		var result = navigator.userAgent.match(RegExp('Intel Mac OS X'));
                this.os = (typeof result != 'undefined');
	}

	// 2: check browser...

	this.browser = true;

	if(RegExp('Firefox').test(navigator.userAgent))
	{
		// 2.1: firefox...

		var result = navigator.userAgent.match(RegExp('Firefox(?:\\s|\\/)(\\d+(?:\\.\\d+)+(?:(?:a|b)\\d*)?)'));
		if(typeof result != 'undefined')
		{
			var version = parseFloat(result[1]);
			
			if(version >= 2.0)
			{
				this.browser = true;
			}
		}
	}
	else if(RegExp('MSIE').test(navigator.userAgent))
	{
		// 2.2: internet explorer...

		var result = navigator.userAgent.match(RegExp('MSIE (\\d+(?:\\.\\d+)+(?:b\\d*)?)'));
		if(typeof result != 'undefined')
		{
			var version = parseFloat(result[1]);
			if(version >= 6.0)
			{
				this.browser = true;
			}
		}
	}

	// 3: screen resolution...

	this.resolution = (screen.width >= 640) && (screen.height >= 480);

	// 4: screen depth...

	this.depth			= (screen.colorDepth >= 24);

	// 5: flash version...

	this.flash			= DetectFlashVer(10, 0, 0);
	
	// 6: cookies enabled?

	this.cookies		= (navigator.cookieEnabled) ? true : false;
		
	if(typeof navigator.cookieEnabled == "undefined" && !are_cookies_enabled)
	{
		document.cookie	= "testcookie";
		this.cookies		= (document.cookie.indexOf("testcookie") != -1) ? true : false;
	}
	
	// 7: show appropriate final message...
	
	this.passed = this.os &&
								this.browser &&
								this.resolution &&
								this.depth &&
								this.flash &&
								this.cookies;
}

