$(document).ready(function (){
	// Writing link to install Flash player for users with JS (This will be swapped out by swfobject if flash is already present)
	var playerVersion = swfobject.getFlashPlayerVersion();
	if ((playerVersion.major == 0) || (playerVersion.major < 8)) {
	 	$(".video-alerts").html('To view the video version of this content you will need to  <a href="http://get.adobe.com/flashplayer/">install Adobe Flash Player</a>.');
	} else {
		// Javascript and Flash are installed so hide the alerts
		$(".video-alerts").hide();
		// Add share video 
		$(".video-player span").each(function (i) {
			// Grab url, width, height, id into values array
			var values = unescape($(this).attr("id").replace(/:/g, "%")).split(" ");
			// Hide 'Share this video' box
			var videoFields = $("#video-fields-" + values[3]);
			$(videoFields).hide();
			// Fill in 'Link to this video' field
			$("#video-url-" + values[3]).val(values[0]);
			// Fill in 'Share this this video' field
			$("#video-html-" + values[3]).val('<object width="' + values[1] + '" height="' + values[2] + '"><param name="movie"value="' + values[0] + '"><\/param><param name="allowFullScreen" value="true"><\/param><param name="allowscriptaccess" value="always"><\/param><embed src="' + values[0] + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + values[1] + '" height="' + values[2] + '"><\/embed><\/object>');
			// If this video is visible to everyone...
			if (values[4] == '1') {
				// Insert 'Share this video' link and set toggle event
				$("#video-caption-" + values[3]).after($('<p class="video-link"><\/p>').append($('<a>Share this video<\/a>').click(function () {
					shareVideo(videoFields);
					} )) );
				// Insert share video close button
				$(".video-fields fieldset").after($("<a><img src='/assets/images/buttons/btn-close-video.gif' alt='Close' /></a>").click(function () {$(videoFields).slideUp(); }));
			}
			
			// Insert Flash video object
			swfobject.embedSWF(values[0], "video-" + values[3], values[1], values[2], "9.0.0", "/assets/flash/expressInstall.swf", "false", {allowFullScreen: "true", wmode:"transparent", allowScriptAccess:"always"}, "false" );
		});
	}
});

$(document).ready(function (){
		$("#video-content.lightbox #video-lightbox h2").prepend("<span id='help-close'><a href='#' class='close-player'>X</a></span>");
		$("#video-content.lightbox").hide();
		$("#video-content .video-container").hide();
		$("#product-gallery a.thickbox").each(function(){
			var currentHref = this.href;
            var productId = $(this).attr("id").replace(/video-link[-a-z]+/g, "");
			{
				currentHref = "#TB_inline?inlineId=video-content&amp;modal=true";
				$(this).attr("href", currentHref).click(function() {
				    $("#video-content.lightbox").show();
                    $("#video-container-" + productId).show();
				});
			}				
		});
		$("#video-content.lightbox a.close-player").click(function() {
			$("#video-content.lightbox").hide();
            $(".video-container").hide();
			tb_remove();
			return false;
		});
	});


function shareVideo(videoFields) {
	if ($(videoFields).is(":hidden")) {
		$(videoFields).slideDown();
	} else {
		$(videoFields).slideUp();
	}
}

