var animate = 1;
var player = null;

function stateListener(e){
	animate = 0;
}
function stopplayer(){
	if (navigator.appName=="Microsoft Internet Explorer"){
		if (player){
			player.sendEvent('STOP');
		}
	}
	return true;
}
function playerReady(obj) {
	player = window.document[obj.id];
	player.addModelListener("STATE", "stateListener");
	player.sendEvent('STOP');
};


function showhideslides(show, hide){
	changeclickselector(show)
	//$.jGrowl("Showing " + show + " Hiding " + hide );
	
	stopplayer(hide);
	$("#mySlides li .video").hide();	
	$("#mySlides li:eq("+ hide +")").css({zIndex:"2"});
	$("#mySlides li:eq("+ show +")").css({zIndex:"1"});
	$("#mySlides li:eq("+ show +")").show();
	$("#mySlides li:eq("+ hide +")").fadeOut(1000, function(){
		$("#mySlides li .video").show();	
	});
	
}

function changeclickselector(slide){
	//$.jGrowl(slide);
	$('#PromoController a').removeClass("selected");
	$('#PromoController a:eq('+ (slide) +')').addClass("selected");

}

function animateSlides(slide){
	//Change back to animate==1 to turn the slider back on!
	if (animate == 1){
		if (slide == $("#mySlides li").length ) { 
			showhideslides(0, slide-1)
			setTimeout("animateSlides(1)", 9000);
		} else {
			showhideslides(slide, slide-1)
			setTimeout("animateSlides("+ (slide+1) +")", 9000);
		}
	}
}

function showclick(slide){
	animate = 0;
	changeclickselector(slide)
	//mark the slide on the click selector
	//$.jGrowl( $("#mySlides li:eq("+ slide +")").css("z-index") );
	/*
		if slide is active then do nothing (ie its on zindex 1)
		move all slides to z2 and mark as inactive
		move selected slide to z1
		fade out all slides marked inactive
	*/
	if ( $("#mySlides li:eq("+ slide +")").css("z-index") != 1 ){
		stopplayer();
		$("#mySlides li .video").hide();	
		$("#mySlides li").addClass("inactive").css({zIndex:"2"});
		$("#mySlides li:eq("+ slide +")").removeClass("inactive").css({zIndex:"1"}).show();
		$("#mySlides .inactive").fadeOut(1000, function(){
			$("#mySlides li:eq("+ slide +") .video").show();
		});
	}
}	

$(document).ready(function(){
	//only if there is more then one item
	if ($("#mySlides li").length == 2){
		slide = Math.floor(Math.random()*2);
		$('#PromoController a:eq('+slide+')').addClass("selected");
		$('#mySlides li:eq('+slide+')').show().css({zIndex:"2"});
	}
	else if($("#mySlides li").length > 2){
		//make the slide selector
		$("#Promo").append("<div id='PromoController'></div>");
			//$("#PromoController").append("<a href='#'>Pervious</a>");
			var slides = $("#mySlides li");
			for (x=0;x<slides.length;x++){
				$("#PromoController").append("<a href='#'>"+ (x+1) +"</a>");
			}
			//$("#PromoController").append("<a href='#'>Next</a>");
	
		//append functions to the links 
		$('#PromoController a').click(function(e) {
			e.preventDefault();
			if ( parseInt($(this).text()) > 0){
				//$.jGrowl( "Clicked "+ parseInt($(this).text()) );	
				showclick( parseInt($(this).text())-1 )
			}
	
		});
		$('#PromoController a:eq(0)').addClass("selected");
		$("#mySlides li:eq(0)").show().css({zIndex:"2"});
	
		$("#mySlides li").click(function(){
			animate=0;
		});
		
		setTimeout("animateSlides(1)", 6000);
		
	} else {
		$('#PromoController a:eq(0)').addClass("selected");
		$("#mySlides li:eq(0)").show().css({zIndex:"2"});
	}
});

$(document).ready(function(){
	$("#ctl00_ListSummary1").appendTo("#ENS");
});
