// Brightcove API stuff, needs to be declared at top of javascript
// Protocol check to include the secure or non-secure brightcove script
var securePg = false,
    protocol = "http:",
    adminProtocol = "admin";
if(window.location.protocol === "https:"){
    securePg = true;
    protocol = "https:";
    adminProtocol = "s"+adminProtocol;
}
$.getScript(protocol+"//"+adminProtocol+".brightcove.com/js/BrightcoveExperiences_all.js");
var bcExp, modVP;
var bcArray = new Array();
function onTemplateLoaded(experienceID) {// called when template loads, this function stores a reference to the player and modules.
	bcExp = brightcove.getExperience(experienceID);
	modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
	bcArray.push(modVP);
}

willow.ready(function($){
	//setting height of marketing items to space evenly
	var allHeight = 0;
	$(".marketingSelectors").each(function(){
		allHeight = allHeight + $(this).height();
	});
	var lastMarkHeight = $(".marketingSelectors:last-child").height();
	var spaceLeft = 352 - allHeight - lastMarkHeight;
	var marginTop = Math.floor(spaceLeft/$numberMarks);
	spaceLeft = spaceLeft - marginTop;
	marginTop = Math.floor(spaceLeft/$numberMarks);
	$(".marketingSelectors").css("margin-top",marginTop+"px");
	$(".marketingSelectors a").animate({color: "#ffffff"},700);
	
	//emergency bulletin
	$('#Form1').bulletin({emButton: false,data: [{type:"announcement",id:4042}]});
	
	//menu
	var menuOptions = {
		directionL2:"down",
		directionL3:"right",
		directionL4:"right",
		showL3s:true,
		showL4s:true
	};
	
	willow.getMenu("130440|130442|130457|130473|130479|130481",function(data){
		$("#L1").menu_wh(data.menu,menuOptions);
	});
	//call news ticker 
	$('.newsTicker').SetScroller({});
	//podium search
	$('#search').pdSearch({showButton:true, showButtonClass:'searchSubmit',showButtonText:''});
	setTimeout('brightcove.createExperiences();',1000);
//**********showcase code **********
	//cycle news showcase
	
	$("#newsShowcase").cycle({
		timeout: 5500,
		pager: ".articlePager"
	});
	
	//start cycle on image galleries
	imageGallery("newsShowcase");
	imageGallery("marketing");
	//showing and hiding news showcase content
	$("#newsShowcase .newsArticle").hide();
	$("#newsShowcase .photoGallery").hide();
	$("#newsShowcase .videoPlayer").hide();
	$("#newsShowcase .showcaseLink").click(function(e){
		e.preventDefault();
		$("#newsShowcase .photoGallery").hide();
		$("#newsShowcase .videoPlayer").hide();
		$("#newsShowcase .mainPhoto").hide("fade",750);
		setTimeout("$('#newsShowcase .newsArticle').show('fade',750);",1000);		
		$("#newsShowcase").cycle('pause');
		$("#marketing .marketingSelectors a").removeClass("onMarketing");
	});
	$("#newsShowcase .newsArticle .closeButton").click(function(e){
		e.preventDefault();		
		$("#newsShowcase .newsArticle").hide("fade",750);
		$("#newsShowcase .newsArticle").hide();
		$("#newsShowcase .photoGallery").hide();
		$("#newsShowcase .videoPlayer").hide();
		setTimeout("$('#newsShowcase .mainPhoto').show('fade',750);",1000);	
		$("#newsShowcase").cycle('resume');
	});
	$("#newsShowcase .photoGallery .closeButton").click(function(e){
		e.preventDefault();
		$("#newsShowcase .photoGallery").hide("fade",750);
		setTimeout("$('#newsShowcase .newsArticle').show('fade',750);",1000);
	});
	$("#newsShowcase .videoPlayer .closeButton").click(function(e){
		e.preventDefault();
		$("#newsShowcase .videoPlayer").hide("fade",750);
		//$("#newsShowcase .showFirst").hide();
		setTimeout("$('#newsShowcase .newsArticle').show('fade',750);",1000);	
		willow.stopVideo();
	});
	$("#newsShowcase .photos.preview a").click(function(e){
		e.preventDefault();
		$("#newsShowcase .newsArticle").hide("fade",750);
		setTimeout("$('#newsShowcase .photoGallery').show('fade',750);",1000);
	});
	$("#newsShowcase .videos.preview a").click(function(e){
		willow.stopVideo();
		e.preventDefault();
		$("#newsShowcase .newsArticle").hide("fade",750);
		setTimeout("$('#newsShowcase .videoPlayer').show('fade',750);",1000);
	});
	
	//showing and hiding marketing content
	$("#marketing .moveMarketing").hide();
	$("#marketing .marketingSelectors a").click(function(e){
		e.preventDefault();
		var nid = $(this).attr("nid"),
			vid = $(this).attr("vid");
		$("#marketing .marketingSelectors a").removeClass("onMarketing");
		$(this).addClass("onMarketing");
		$("#newsShowcase").hide("fade",750,callback);
		$("#marketing .moveMarketing").hide();
		$("#marketing .newsArticle").hide();
		$("#marketing .moveMarketing .photoGallery").hide();
		$("#marketing .moveMarketing .videoPlayer").hide();		
		function callback() {
			setTimeout(function() {
				$("#marketing .moveMarketing.item_"+nid).show();
				$('#marketing .moveMarketing.item_'+nid+' .showFirst').show("fade",750);
			}, 500 );
		};
		willow.stopVideo();
	});
	$("#marketing .newsArticle .closeButton").click(function(e){
		e.preventDefault();
		$("#marketing .moveMarketing").hide("fade",1250);
		$("#newsShowcase").show("fade",1250);
		$("#marketing .marketingSelectors a").removeClass("onMarketing");
	});
	$("#marketing .photos.preview a").click(function(e){
		e.preventDefault();
		$("#marketing .newsArticle").hide("fade",750);
		$("#marketing .photoGallery").show("fade",1250);
	});
	$("#marketing .photoGallery .closeButton").click(function(e){
		e.preventDefault();
		$("#marketing .photoGallery").hide("fade",750);
		//$("#marketing .newsArticle").show("fade",750); //client asked to jump right back to showcase
		$("#newsShowcase").show("fade",1250);
		$("#marketing .marketingSelectors a").removeClass("onMarketing");
	});
	$("#marketing .videos.preview a").click(function(e){
		willow.stopVideo();
		e.preventDefault();
		$("#marketing .newsArticle").hide("fade",750);
		$("#marketing .videoPlayer").show("fade",1250);
	});
	$("#marketing .videoPlayer .closeButton").click(function(e){
		e.preventDefault();
		$("#marketing .videoPlayer").hide("fade",750);
		//$("#marketing .newsArticle").show("fade",750); //client asked to jump right back to showcase
		$("#newsShowcase").show("fade",1250);
		$("#marketing .marketingSelectors a").removeClass("onMarketing");
		willow.stopVideo();
	});
	
});	

function imageGallery(container){
		var $photoGallery = $("#"+container+' .photoGallery');	
		if ($photoGallery.length > 0){
			for(i=0;i<$photoGallery.length;i++){
			var $galleryID = $photoGallery.eq(i).attr("id");
			$('#'+container+' #'+$galleryID+' .largeImages').cycle({  
				pause: 1,
				pager:  '#'+container+' #'+$galleryID+' .pager',
				pagerAnchorBuilder: function(idx, slide) { 
					return '<li><a href="#"><img src="' + slide.src + '" width="80" height=45" /></a></li>'; 
				} 
			});
			}
		}
	}
//stopping brightcove video
willow.stopVideo = function(){
	for(var i = 0; i < bcArray.length; i++){
		bcArray[i].stop();
	}
}
