//MPX Living JavaScript


$(document).ready(function() {

	//Fix IE bug where menu displays behind the widget
	if ($.browser.msie) {
		var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	}
	ie6FrameExpander();
	var htmlModal = '<div id="modal" class="hidden"><a href="javascript:dynamicEntry.cancel();" id="cancel" class="modalCloseButton">X</a><div id="dynamicEntry"></div></div>';
	$('body').append(htmlModal);
	//all links marked as ".popupLink" should have popupWindow function attached to them
	$('#popup').parent('body').attr('style', 'overflow-x:hidden');
	$(".popupLink").click(function() {
		popupWindow($(this).attr('href'));
		return false;
	})
	//  if (isIE6OrLess() && document.getElementById("staticEntryWidget")) {
	//    h = document.getElementById("staticEntryWidget").innerHTML;
	//    $("#staticEntryWidget").remove();
	//    $('#frame').prepend("<div id='staticEntryWidget'></div>");
	//  }

	mc = new MediaCentre();
	mc.init();
});


function popupWindow(URL, width, height) {
	OverlayPage(URL);
}
function popupWindow_old(URL, width, height) {
	if (empty(width)) {
		width = 650;
	}
	if (empty(height)) {
		height = 600;
	}
	window.open(URL, 'Page', 'toolbar=0,statusbar=0,scrollbars=1,resize=0,width=' + width + ',height=' + height);
}

function resizePage(w, h) {
	window.resizeTo(w, h)
}

function empty(data) {
	if (typeof data == 'undefined' || data === null) { return true; }
	else if (typeof data == 'string' && (data === '0' || data.replace(/^\s+|\s+$/g, '') === '')) { return true; }
	else if (typeof data == 'number' && data === 0) { return true; }
	else if (typeof data == 'boolean' && data === false) { return true; }
	else if (typeof data == 'object') {
		if (data instanceof Array && data.length == 0) { return true; }
		else {
			var n = 0;
			for (var i in data) {
				if (!data.hasOwnProperty(i)) { continue; }
				n++;
			}
			if (n == 0) { return true; }
		}
	}
	return false;
}

/** isIE6OrLess()
*  for some of our methods its neccesary to know if we're dealing with an old browser
*
*  @returns {bool}
*/
function isIE6OrLess() {
	//Detect < IE6
	version = 0
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		temp = navigator.appVersion.split("MSIE");
		version = parseFloat(temp[1]);
	}
	if (version <= 6.0 && version != 0) { //NON IE browser will return 0
		return true;
	}
	return false;
}


function ie6FrameExpander() {
	if ($.browser.msie && ($.browser.version.substring(0, 3) == '6.0') && ($('#popup').is('div'))) {
		var theFrame = $("#contentFrame", parent.document.body);
		h = $(document).height() + 40;
		h = h < 490 ? 490 : h;
		theFrame.height(h);
	}
}

function OverlayPage(url) {
	overlayBg = 'overlayBg';
	context = 'body';
	theOverlay = '#theOverlay';
	theContent = '#theOverlayContent';
	//destroy any overlays that might be present
	$('.' + overlayBg).remove();
	$(theOverlay).remove();
	//console.log('creating overlay and background');
	markup = '<div id="' + overlayBg + '"></div>';
	markup += '<div id="theOverlay"><div class="oTop"></div><div id="theOverlayContent"></div></div>'
	$c = $(context);
	//$c.append('<div id="' + overlayBg + '"></div>');
	//$c.append('<div id="theOverlay"><div id="theOverlayContent"></div></div>')
	$c.append(markup);
	$bg = $('#' + overlayBg);
	$o = $(theOverlay);
	$o.addClass('overlay');
	$bg.css('opacity', 0);
	$o.append('<div class="close"><a href="#"><span>Close</span></a></div>');

	$o.hide();
	$o.fadeTo(0, 0);
	$c = $(theContent);
	bgOpacity = 0.6;

	iID = 'contentFrame';
	var html = '<iframe id="' + iID + '" name="' + iID + '" height="490" ' +
			'width="940" frameborder="0" marginwidth="0" marginheight="0" ' +
			'scrolling="auto" ';
	rand = '';
	if ($.browser.msie) {
		// prevent brief whiteout while loading iframe source
		html += ' allowtransparency="true"';

		// prevent "secure content" warning for https on IE6
		// see http://www.zachleat.com/web/2007/04/24/adventures-in-i-frame-shims-or-how-i-learned-to-love-the-bomb/
		//if(S.client.isIE6)
		//		html += ' src="javascript:false;document.write(\'\');"';
		//if ($.browser.version.substring(0,3)=='6.0') {
		q = '';
		if (url.indexOf('?') == -1) { q = '?' }
		d = new Date();
		r = d.getMilliseconds() * d.getUTCMilliseconds();
		rand = q + '&' + (Math.random() * r);
		//}

	}

	html += '></iframe>';
	$(theContent).html(html);
	$f = $('#' + iID);


	$f.attr('src', url + rand);
	$o.fadeTo(300, 1);

	$o.find('.close a').click(function() {
		$o.fadeOut('fast')
		$bg.fadeTo(300, 0).hide();
	});
	$o.appendTo(context);

	$(theOverlay).fadeIn('fast');
	$bg.show().fadeTo(300, bgOpacity);
}




function MediaCentre() {
	this.s = {
		list: "#videoList",
		player: "#videoPlayer",
		videoplayer: "#videoPlayerContent"
	};
	this.pageSize = 4;

	this.init = function() {
		var that = this;
		$(this.s.player).find(".description").append("<h3></h3><p></p>");
		$list = $(this.s.list);
		$list.find("ul").quickPager({ pageSize: this.pageSize });
		$list.find(".thumb a[rel], .description h3 a[rel]").click(function() { that.loadVideo(this); return false; });
		$list.find("li:first .thumb a").trigger("click");
	};

	this.loadVideo = function(el) {
		this.setDetails(el);
		vId = $(el).attr("rel");
		this.flashPlayVideo(vId, $(this.s.videoplayer).attr('id'));
	};
	this.loadFirst = function() {
		this.setDetails($(this.s.list).find("li:first .thumb a"));
	}
	this.setDetails = function(el) {
		$el = $(el).closest("li");
		lDesc = $el.find("p").attr("title");
		lTitle = $el.find("h3 a").attr("title");
		sTitle = $el.find("h3 a").text();
		$theDesc = $(this.s.player).find(".description");
		$theDesc.find("h3").html(lTitle);
		$theDesc.find("p").html(lDesc);
		$(this.s.player).find("h2").html(sTitle);

	}

	/**
	* 	Return the flash object dom element
	* 	@param movieName {string} id of the of the carousel flash
	*	@returns {DOMElement}
	*/
	this.getFlashMovie = function(movieName) {
		return document.getElementById(movieName);
	};

	/**
	* 	Tell the flash carousel to move to a panel.
	* 	@param id {integer} The id of the panel
	* 	@param flashObjId {string} The selector of the flash object holding the carousel
	* 	@returns {void}
	*/
	this.flashPlayVideo = function(id, movieObjId) {
		var flash = this.getFlashMovie(movieObjId);
		if (flash.playVideo) {
			flash.playVideo(id);
		}
		return;
	};


}