$(document).ready(function(){

	/* Display hide activities */
	$(".content .activities .act_body a.show").css("display","block");
	$(".content .activities .act_body h5").css("width","500px");
	$(".content .activities .act_body .act_content:gt(0)").css("display","none");
	$('.content .activities .act_body a.show:eq(0)').html("Hide details");
	
	$('.content .activities .act_body a.show').click(function() {
		$(this).siblings("div.act_content").slideToggle(500, function() {});
		if ($(this).html() == "Show details"){
			$(this).html("Hide details");
		} else {
			$(this).html("Show details");
		}
		return false;
	});
	
	/* Display hide Intro */
	$(".content .activities .intro a.show").css("display","block");
	$(".content .activities .intro h5").css("width","500px");
	$(".content .activities .intro .hide_show").css("display","none");
	
	$('.content .activities .intro a.show').click(function() {
		$(".content .activities .intro .hide_show").slideToggle(500, function() {});
		if ($(".content .activities .intro a.show").html() == "Show curriculum details"){
			$(".content .activities .intro a.show").html("Hide curriculum details");
		} else {
			$(".content .activities .intro a.show").html("Show curriculum details");
		}
		return false;
	});
	
	// ************************************************************************
	// SEARCH box - display default text, remove when input field has the focus
	// ************************************************************************
	// TOP LHS SEARCH BOX
	var cSEARCH_DEFAULT_TEXT = "";
	
	// SEARCH paging buttons - add hover effect
	$(".SearchSummaryBottom .SearchSummaryBottomItem").hover(
		function(){
			if ( $("a",this).size()  ) {
				$(this).addClass("hover");		
				$("a",this).addClass("hover");		
			}
		},
		function(){
			if ( $("a",this).size()  ) {
				$(this).removeClass("hover");		
				$("a",this).removeClass("hover");		
			}									
		}
	); 
	
	// Make whole button clickable
	$(".SearchSummaryBottom .SearchSummaryBottomItem").click(
		function(){
		 	if ( $("a",this).size()  && $("a",this).attr("href").search("http://") == -1) {
				document.location.href = $("a",this).attr("href");		
			}
		}
	);
	
	// Remove UNDERLINE and ADD CURSOR = HAND on paging links if JQuery is active
	$(".SearchSummaryBottom .SearchSummaryBottomItem").css("cursor","pointer").css("text-decoration","none");
	// Remove CURSOR = HAND on currently selected page link
	$(".SearchSummaryBottom .selected").css("cursor","default");
	
	// ************************************************************************
	// ************************************************************************
	
	
	$('.stills_list a').click(function(){
	});	
	
	$('.right_col .stills_list a').bind("click",bindStillsPopup);	
	$('.middle .content ul.stills_gallery a').bind("click",bindStillsPopup);	
	$('.right_col .interviews .thumbList a').bind("click",bindMoviePopup);	
	$('.middle .content ul.clip_bank a').bind("click",bindMoviePopup);		
	$('.middle .right_col .watch a').bind("click",bindMoviePopup);	
	$('.middle .top #vidspace #download_movies a').bind("click",bindDownloadPopup);	
	
	
	function googleStats(){
		try {
			var pageTracker = _gat._getTracker("UA-7566215-10");
			pageTracker._trackPageview($(this).attr('href'));
		} catch(err) {}
	}
	
	function bindDownloadPopup(){
		var popupContent = $.ajax({
		url: $(this).attr('href') + "&ajaxAction=getDownloadPopup",
		async: false,
		cache: false
		}).responseText;			
				
		$("#stills_popup").html(popupContent);
		$("#stills_popup").fadeIn(500);
	
		/* Bind close link */
		$('#stills_popup p.agree a.cancel').click(function(){
			$("#stills_popup").fadeOut(500);
			return false;
		});	
		/* Bind Agree link */
		$('#stills_popup p.agree a.agree').click(function(){
									
			var popupContent = $.ajax({
			url: $(this).attr('href') + "&ajaxAction=getDownloadPopup",
			async: false,
			cache: false
			}).responseText;	
			
			$("#stills_popup .download_popup").fadeOut(500,function(){
				$("#stills_popup .download_popup").html(popupContent);
				$("#stills_popup .download_popup").fadeIn(500);	
				/* Google stats for downloads */
				$('.download_popup td a').bind("click",googleStats);	
				
				/* Bind close link */
				$('#stills_popup p.agree a.cancel').click(function(){
					$("#stills_popup").fadeOut(500);
					return false;
				});	
			});
														  
			return false;
		});	
		
		return false;
		
	}
	function bindStillsPopup(){
		var popupContent = $.ajax({
		url: $(this).attr('href') + "&ajaxAction=getStillsPopup",
		async: false,
		cache: false
		}).responseText;			
				
		$("#stills_popup").html(popupContent);
		$("#stills_popup").fadeIn(500);
		$('#stills_popup .heading a.previous').bind("click",initStillsPrevNext);
		$('#stills_popup .heading a.next').bind("click",initStillsPrevNext);
	
		/* Bind close link */
		$('#stills_popup .heading a.closeStills').click(function(){
			$("#stills_popup").fadeOut(500);
			return false;
		});	
		
		return false;
		
	}
	
	function bindMoviePopup(){
		var popupContent = $.ajax({
		url: $(this).attr('href') + "&ajaxAction=getMoviePopup",
		async: false,
		cache: false
		}).responseText;	
		
		/* Google stats for downloads */
		try {
			var pageTracker = _gat._getTracker("UA-7566215-10");
			pageTracker._trackPageview($(this).attr('href'));
		} catch(err) {}
				
		$("#stills_popup").html(popupContent);
		$("#swfplaceholder").css("visibility","visible");
		$("#stills_popup").fadeIn(500);
	
		/* Bind close link */
		$('#stills_popup .heading a.closeStills').click(function(){
			$("#stills_popup").fadeOut(500);
			return false;
		});	
		
		return false;
		
	}
	
	function initStillsPrevNext(){
		
		unInitLinks();
		
		/* Change the title */
		var stillNewTitle = extractAjaxDatas($.ajax({
		url: $(this).attr('href') + "&ajaxAction=getStillNewTitle",
		async: false,
		cache: false
		}).responseText);			
		
		$("#stills_popup .heading h1").animate({opacity:0},300,function(){
			$("#stills_popup .heading h1").html(stillNewTitle);
			$("#stills_popup .heading h1").animate({opacity:1},300);	
		});
		
		/* Get new image */
		var stillNewImgUrl = extractAjaxDatas($.ajax({
		url: $(this).attr('href') + "&ajaxAction=getStillNewImgUrl",
		async: false,
		cache: false
		}).responseText);
		var stillNewImgAltText = extractAjaxDatas($.ajax({
		url: $(this).attr('href') + "&ajaxAction=getStillNewImgAltText",
		async: false,
		cache: false
		}).responseText);
		$("#stills_popup .still_content img").fadeOut(500,function () {
			$("#stills_popup .still_content img").attr("alt",stillNewImgAltText);
			$("#stills_popup .still_content img").attr("title",stillNewImgAltText);
			$("#stills_popup .still_content img").attr("src",stillNewImgUrl).load(function(){
			
				$("#stills_popup .still_content img").fadeIn(500,function(){
					$('#stills_popup .heading a.previous').bind("click",initStillsPrevNext);
					$('#stills_popup .heading a.next').bind("click",initStillsPrevNext);
					/* Bind close link */
					$('#stills_popup .heading a.closeStills').click(function(){
						$("#stills_popup").fadeOut(500);
						return false;
					});	
				});
			} );
	    });
		
		
		/* Update prev/next link */
		var stillNewPrevImgUrl = extractAjaxDatas($.ajax({
		url: $(this).attr('href') + "&ajaxAction=getStillNewPrevImgUrl",
		async: false,
		cache: false
		}).responseText);
		var stillNewNextImgUrl = extractAjaxDatas($.ajax({
		url: $(this).attr('href') + "&ajaxAction=getStillNewNextImgUrl",
		async: false,
		cache: false
		}).responseText);
		
		if (stillNewPrevImgUrl != ""){
			$("#stills_popup .heading a.previous").attr("href",stillNewPrevImgUrl);
			if ($("#stills_popup .heading a.previous").css("display") == "none"){
				$("#stills_popup .heading a.previous").css("display","block");
			}
		} else {
			$("#stills_popup .heading a.previous").fadeOut(300);			
		}
		
		if (stillNewNextImgUrl != ""){
			$("#stills_popup .heading a.next").attr("href",stillNewNextImgUrl);
			if ($("#stills_popup .heading a.next").css("display") == "none"){
				$("#stills_popup .heading a.next").css("display","block");
			}
		} else {
			$("#stills_popup .heading a.next").fadeOut(300);			
		}
		
		
		return false;
	}
	
	function extractAjaxDatas (htmlContent){
		return 	htmlContent.substring( htmlContent.indexOf("<datas>") + 12 , htmlContent.indexOf("</datas>") ).replace('&amp;', '&') ;
	}
	function unInitLinks(){
		$("#stills_popup .heading a").each(function () {
			$(this).unbind("click");
			$(this).click(function(){return false;});	
		});	
	}	
	
	
	$(window).load( function (){
							  
		
		//if (jQuery.browser.msie && jQuery.browser.version.substr(0,3) != "6.0") {
		//	$("body.hidebody").css("display","block");
		//} else if (!jQuery.browser.msie) {
		//	$("body.hidebody").fadeIn(1000);
		//}		  						  
							  
	});

});
