
// Prevent multiple instances firing...
if(chookDynamicPlayer == null)
{
	var chookDynamicPlayer = {};
	//alert("create");
	chookDynamicPlayer.PlayerInit = function()
	{
		var counter = 0;
		//alert("init");
		var players = $("chook\\:dynamicPlayer");
		if ($(players).size() == 0) players = $("chookDynamicPlayer");
		
		$(players).each(function()
		{
			// Flashvars
			var flashvars = {
				videoURL:$(this).attr("videoURL"), 
				thumbnailURL:$(this).attr("thumbnailURL"),
				webservice:$(this).attr("webservice"),
				advertising:$(this).attr("advertising"),
				quality:$(this).attr("quality"),
				videoID:$(this).attr("videoID"), 
				colour:$(this).attr("colour"),
				autoPlay:$(this).attr("autoPlay"),
				showTimer:$(this).attr("showTimer"),
				width:Number($(this).attr("width")),
				height:Number($(this).attr("height"))
			}
			
			// If function exists, get colour via function instead. Used on Lifestyle.com.au /lifestyle/pods/videos.ascx to allow dynamic colour theming.
			if(typeof window.getBackgroundColour == "function") flashvars.colour = getBackgroundColour();
			
			// Container Div
			var target = $(this).attr("target");
			var initialContent = $("#" + target).html();
			var divID;
			if (target == "")
			{
				divID = "VideoPlayer" + counter ;
				$(this).after('<div class="VidePlayerContainer" id="VideoPlayerContainer' + counter + '"><div class="PlayerInner" id="' + divID + '"></div></div>');
			}
			else
			{
				$("#" + target).html('<div class="PlayerInner" id="PlayerInner' + counter + '"></div>');
				divID = "PlayerInner" + counter;
			}
			
			// HTML5 Video Player (mp4 only, webkit or ie9+ only)
			var version = Number($.browser.version.slice(0, 1));
			var webkit = $.browser.webkit;
			var msie = $.browser.msie;
			if (flashvars.videoURL.match(/.mp4/) && ((webkit && version >= 3) || (msie && version >= 9)))
			{
				var fallbackVideoURL = flashvars.videoURL.substr(0,4) == "http" ? flashvars.videoURL : $(this).attr("fallbackURL");
				var html = '';
				html += '<video id="HtmlVideoPlayer' + counter + '" width="' + flashvars.width + '" height="' + flashvars.height + '" poster="' + flashvars.thumbnailURL + '" controls="">';
				html += '<source width="' + flashvars.width + '" height="' + flashvars.height + '" src="' + fallbackVideoURL + '" type="video/mp4" />';
				html += '<img src="' + flashvars.thumbnailURL + '" width="' + flashvars.width + '" height="' + flashvars.height + '" />';
				html += '</video>';
				$("#" + divID).html(html);
			}
			else
			{
				$("#" + divID).html(initialContent);
			}
			
			// Toggle between dev server and live site
			var domain = (document.domain == "internal.idstudios.net") ? 'http://internal.idstudios.net/lifestyle.com.au/' : "http://"+document.domain+"/";
			
			// Flash Player
			var params = {menu:"false", wmode:"transparent", bgcolor:"#000000", allowfullscreen:"true"};
			var attributes = {id:"FlashVideoPlayer" + counter};
			swfobject.embedSWF(domain+"includes/flash/dynamic.player.v6.swf", divID, String(flashvars.width), String(flashvars.height), "10.0.0", "flash/expressInstall.swf", flashvars, params, attributes);
			$("#FlashVideoPlayer" + counter).css({'outline':'none'}); // Remove FF object outline on focus
			counter ++;
		});
	}
	
	// Embed videos on page ready...
	$(document).ready(chookDynamicPlayer.PlayerInit);
}
