﻿$(document).ready(function()
{
	PodsButtonsAdd();
	CharacterScroll();
	
	if($("li.current").length > 0)
	{
		$(".programme-list .contain-scroll").scrollTo("li.current");
	}
});

function CharacterScroll(){
	//Controls bind
	$(".controls a").click(function(){
		//Add loading animation
		$("#page .programme .content-pri").html("<img class=\"loading\" alt=\"loading\" src=\"/images/Global/loadingAnimation.gif\" />");
		//Get url target
		var target = $(this).attr("href").split("?")[0] +"AjaxCharacter.aspx?"+ $(this).attr("href").split("?")[1];
		target = target.replace(" ", "%20").replace("#top-goo","");
		$.ajax(
		{
			url: target,
			dataType: 'html',
			success: function(html){
				//Hide the container untill populated with the html code (makes the fade work)
				$("#page .programme .content-pri").hide();
				$("#page .programme .content-pri").html(html);
				//Re bind the controls to this function
				CharacterScroll();
				$("#page .programme h1").html($("#page .programme .content-pri h2").html());
				$("#page .programme h1").html($("#page .programme .content-pri img").attr("alt"));
				$("#page .programme .content-pri").fadeIn("fast");
			}
		});
		
		return false;
	});
}


// Add Buttons to Pods that have more than 1 list item
function PodsButtonsAdd() {
	
	// Add left and right classes to pods
	$("#page .programme .content-sec > ul > li:odd").addClass("right");
	$("#page .programme .content-sec > ul > li:even").addClass("left");
	
	// If number of list items is greater than 1 - add traverse buttons
	$("#page .programme .content-sec > ul > li").each(function(){
		if($(this).find("li").length > 1){
			
			$(this).append("<a class=\"next\" href=\"#\">Next</a>"
				+ "<a class=\"prev\" href=\"#\">Previous</a>");
		}
	});
	
	$("#page .programme .content-sec li a.next").bind('click',function(event){
		var PodItemSelected = 0;
		var PodItemNum = $(this).siblings("ul").children().length;
		
		$(this).siblings("ul").children().each(function(){
			PodItemSelected++;
			if($(this).is(".selected")){
				return false;
			}
		});
		
		if(PodItemSelected < PodItemNum){
			$(this).siblings("ul").children(".selected").removeClass().next().addClass("selected");
			return false;
		} else {
			$(this).siblings("ul").children(".selected").removeClass().parent().find("li:first").addClass("selected");
			return false;
		}
		
	});
		
	$("#page .programme .content-sec li a.prev").bind('click',function(event){
		
		var PodItemSelected = 0;
		var PodItemNum = $(this).siblings("ul").children().length;
		
		$(this).siblings("ul").children().each(function(){
			PodItemSelected++;
			if($(this).is(".selected")){
				return false;
			}
		});
		
		if(PodItemSelected > 1){
			$(this).siblings("ul").children(".selected").removeClass().prev().addClass("selected");
			return false;
		} else {
			$(this).siblings("ul").children(".selected").removeClass().parent().find("li:last").addClass("selected");
			return false;
		}
		
	});

}