﻿/*
Copyright (c) 2010 by 5 Limes Pty Limited. All rights reserved.
This material may not be duplicated for any purpose without the
express written consent of 5 Limes Pty Limited of Sydney, Australia.
http://www.5Limes.com.au
*/
 
function CVideoPopup() {}
CVideoPopup.prototype =
{
	/**  Private Fields **/
	_fadeDuration: 300, 		// Fade in/out duration in milliseconds (for window shade and videoPopup)
	_videoPopupOpen: false, 	// true: open, false: closed

	/** Public Methods **/
	OpenVideoPopup: function (anchor) {
		//Open videoPopup only if it is currently closed
		if (!gVideoPopup._videoPopupOpen) {
			jQuery("#divVideoPopupContainer").load(encodeURI(anchor.attr("href")) + " .embeddedVideo", 	function (data, status, xhr) {
				if (status == "error") {
					var msg = "Sorry but there was an error: ";
					alert(msg + xhr.status + " " + xhr.statusText);
					return;
				}

				//Centre with CSS
				gVideoPopup.PositionVideoPopup();

				//Fade in the video shade then the video popup
				jQuery("#divVideoShade").css("opacity", "0").show().animate({ opacity: 0.8 }, gVideoPopup._fadeDuration, function () {
					jQuery("#divVideoPopup").fadeIn(gVideoPopup._fadeDuration);
				});

				gVideoPopup._videoPopupOpen = true;

				//Set click event for close button or window shade clicked
				jQuery("#divVideoPopup .closeLink, #divVideoShade").click(gVideoPopup.CloseVideoPopup);
			});
		}
	},
	CloseVideoPopup: function () {
		//Close videoPopup (and video shade) only if it is open
		if (gVideoPopup._videoPopupOpen) {
			jQuery("#divVideoPopup").fadeOut(gVideoPopup._fadeDuration, function () {
				jQuery("#divVideoPopup").hide();
				jQuery("#divVideoShade").fadeOut(gVideoPopup._fadeDuration, function () {
					jQuery("#divVideoShade").hide();
				});
			});

			gVideoPopup._videoPopupOpen = false;
		}
	},
	PositionVideoPopup: function () {
		//Attempt to vertically centre the videoPopup in the browser window
		//using a notional height for the videoPopup (since the videoPopup can
		//actually be of varying height, depening on the amount of text).
		//the videoPopup is horizontally aligned with the main body div
		var win = jQuery(window);
		var windowHeight = win.height();
		var videoPopupHeight = jQuery("#divVideoPopup").height();
		jQuery("#divVideoPopup").css({
			"position": "absolute",
			"top": windowHeight / 2 - videoPopupHeight / 2 + win.scrollTop()
		});
	},
	Initialise: function () {
		jQuery(document).keypress(function (e) {
			if (gVideoPopup._videoPopupOpen) switch (e.keyCode) {
				case 27: gVideoPopup.CloseVideoPopup(); break; // Escape
			}
		});
		//Video link clicked
		var links = jQuery(".videoPopupLink");
		links.click(function (e) {
			e.preventDefault();
			//Load videoPopup, passing the clicked anchor element
			gVideoPopup.OpenVideoPopup(jQuery(this));
		});
		//Create the popup container and window shade div
		if (links.length > 0) {
			jQuery("#divBodyContainer").append(
					'<div id="divVideoShade"></div>' +
					'<div id="divVideoPopup">' +
						'<div class="closeLink"><a>Close</a></div>' +
						'<div id="divVideoPopupContainer"></div>' +
					'</div>');

			jQuery("#divBodyContainerWhite").append(
					'<div id="divVideoShade"></div>' +
					'<div id="divVideoPopup">' +
						'<div class="closeLink"><a>Close</a></div>' +
						'<div id="divVideoPopupContainer"></div>' +
					'</div>');

}
	}

}

// Global video popup object
var gVideoPopup = null;

if (jQuery) {
	jQuery(function () {
		gVideoPopup = new CVideoPopup();
		gVideoPopup.Initialise();
	});
}

