/* Author: Stuart Steel.
 * Created: 30/12/2010
 */


$(document).ready(function(){
	configureWiiCTAs();
	//configureLightBox();
	configurePopups();
	$("#outerwrapper").prepend("<div id='lightboxScreen'></div>");
});

function configureWiiCTAs() {
	$("#wii-cta a").click(function(){
		$(this.hash).addClass("active-popup");
		displayLightBox(true);
		displayVideo($(this.hash+" div.popup-content.active"));
		return false;
	});
	$("#wifi-popups a.close_popup").click(function(){
		$(this.parentNode).removeClass("active-popup");
		killVideo(this.parentNode);
		displayLightBox(false);
		return false;
	});
}

function displayLightBox(action) {
	var lightbox=document.getElementById("lightboxScreen");
	if (action) {
		$(lightbox).addClass("active");
		killExternalVideo();
	} else {
		$(lightbox).removeClass("active");
		buildExternalVideo()
	}
}

function configurePopups() {
	$("#wifi-popups ul.popup-menu a").click(function(){
		if ($(this.parentNode).hasClass("active")) {
			return false;
		}
		$(".active",this.parentNode.parentNode.parentNode).removeClass("active");
		killVideo(this.parentNode.parentNode.parentNode);
		$(this.parentNode).addClass("active");
		var target=$(this.hash).addClass("active");
		$(".popup-wrapper",this.parentNode.parentNode.parentNode).animate({left:"-"+target[0].offsetLeft+"px"},800);
		displayVideo(target);
		return false;
	}); 
}

function displayVideo(target) {
	var flashparams = { wmode:"transparent" };
	var flashvars={};
	var videoStage=$(".movie-stage",target).each(function(){
			$(this).append("<div id='"+this.id+"_target'>");
		});
	if (videoStage.length>0) {
		swfobject.embedSWF(videoStage.attr("rel"), videoStage.attr("id")+"_target", "385", "250", "7", "#FFFFFF",flashvars,flashparams);
	}
}

function killVideo(target) {
	$("div.movie-stage",target).html("");
}

//These are just for publishing 
function killExternalVideo(){
	$("#wifi-wii-video_controller").html('<span id="wii-wifi-movie"><img src="/template_images/wifi/fake_wii_movie.gif" alt="replace this later" width="470" title="replace this later"/></span>');
}

function buildExternalVideo(){
	var flashparams = { wmode:"transparent", allowFullScreen: "true" };
	var flashvars={};
	var attributes={wmode:"transparent", allowFullScreen: "true"};
	swfobject.embedSWF("/template_images/wifi/wii-wifi-video.swf", "wii-wifi-movie", "460", "300", "7", "#FFFFFF",flashvars,flashparams,attributes);
}

