/*
* Our custom Jquery scripts go here
*/

// When the DOM is loaded run this
$(document).ready(function() {

	// Open video automatically set?
	var auto_open_video = 0;
	
	// auto_open_video set on page as hidden field
	if (document.getElementById('auto_open_video') != null)
	{
		auto_open_video = document.getElementById('auto_open_video').value;
	}
	
	if (auto_open_video == 1)
	{
		// Setup Flash Video Dialog links
		$("a.video_dialog").fancybox({
			'hideOnContentClick': false,
			'frameWidth': 620,
			'frameHeight': 650,
			'overlayOpacity': 0.87,
			'callbackOnClose': closeVideoDialog
		}).trigger('click');
	}
	else
	{
		// Setup Flash Video Dialog links
		$("a.video_dialog").fancybox({
			'hideOnContentClick': false,
			'frameWidth': 620,
			'frameHeight': 650,
			'overlayOpacity': 0.87,
			'callbackOnClose': closeVideoDialog
		});
	}
});

/*
* Callback function run when fancybox dialog is closed
*/
function closeVideoDialog()
{
	// Fix for IE
	// IE kept the video playing in the iframe which kept the sound going. This is a fix for that issue. 
	if (document.getElementById('fancy_frame') != null)
	{
		document.getElementById('fancy_frame').src = '/flash_videos/blank.html';
	}
}



/*
* detects version of flash user is using. This code is taken from Adobe Flash Player detection kit:
* http://www.adobe.com/products/flashplayer/download/detection_kit/
* added by 4mation 22-07-2009
*/
function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") {
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == "r") {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) {
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
			//alert("flashVer="+flashVer);
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}


/**
* tracks user's browser details etc and records in database table.
* This function was created to solve how/why a user from Denmark (IP address: 85.82.169.162)
* used 21GB of site traffic within 24 hours!
* @author Jonathan Stucken, 4mation Technologies, created 22/07/2009
*/
function trackUser()
{
	var flash_version = GetSwfVer();
	
	// Track the user's details silently and unobtrusively behind the scenes using Jquery ajax
	var data = $.ajax({
		url: "/includes/ajax_user_tracker.php?flash_version="+flash_version,
		async: true
	}).responseText;
}