/* CONFIG */
/* CAROUSEL */
var displayShows = 6; 		/* Number of shows in the carousel that are visible without scrolling it */
var currentItemCount = 0;


/* GLOBAL VARS */
var currentItemIndex = 1;	/* which item is showing in the carousel */
var iWasClicked = false;	/* did the user click the carousel or let it scroll? */
var iHaveTooFew = false;	/* flag when no of items is fewer than display count */
var itemClicked = 0;		/* only used when too few items AND one was clicked */

$(document).ready(function(){
   	

	
	
	/* NAV ROLLOVERS */
	$("#navigation ul li").mouseover(function()
	{
		/* clear hover states*/
		$("#navigation ul li").removeClass("hover");
		$("div.column-box h2").removeClass("hover");
		
		$(this).addClass("hover");
		
	}).mouseout(function()
	{
		$(this).removeClass("hover");	
	});
	
	/* END NAV ROLLOVERS */
	
	
	
	
	
	/* COLUMN BOX ROLLOVERS */
	$("div.column-box h2").mouseover(function()
	{
		/* clear hover states*/
		$("#navigation ul li").removeClass("hover");
		$("div.column-box h2").removeClass("hover");

		$(this).addClass("hover");
		
	}).mouseout(function()
	{
		$(this).removeClass("hover");	
	});
	
	$("div.column-box-invert h2").mouseover(function()
	{
		/* clear hover states*/
		$("#navigation ul li").removeClass("hover");
		$("div.column-box-invert h2").removeClass("hover");

		$(this).addClass("hover");
		
	}).mouseout(function()
	{
		$(this).removeClass("hover");	
	});
	
	/* END COLUMN BOX ROLLOVERS */
		
	
	
	
	/* TIMELINE EFFECTS */
	
	$("body.timeline div#content-text ul li:first").addClass( "first" );
	$("body.timeline div#content-text ul li:last").addClass( "last" );
	
	/* END TIMELINE EFFECTS */
	
	
	
	
	
	
	/* CAROUSEL */	
	
	/* Innit carousel buttons */
	
	/* if too few items sto scroll, turn off auto scroll and nav buttons */
	currentItemCount = $("#carousel-items").children(".carousel-item").length;
	if( currentItemCount <= displayShows )
	{
		iHaveTooFew = true;
		$(".prev").hide();
		$(".next").hide();
		autoValue = 0;
		speedValue = 0;
		goItems = [".next"]; /* breaks the carousel click when too few items */
		
	}
	
	/* TURN OFF ANYWAY AS NOT NEEDED */
	$(".prev").hide();
	$(".next").hide();
	
	$(function() {
		$("#carousel-menu").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			visible: displayShows,
			auto: autoValue,
			speed: speedValue,
			easing: "bounceout",
			beforeStart: function()
			{
				beforeCallBack();	
			},
			afterEnd: function()
			{
				afterCallBack();
			},
			btnGo: goItems /* GENERATED AUTOMATICALLY IN HTML SOURCE */
		});
	});
	
	/* Innit Carousel first item */
	$(".carousel-item").hide();
	$(".carousel-item:first").show().addClass("showing");
	$("#carousel-menu img.1").addClass("selected");
		
	/* Handle Carousel Clicks */
	$("#carousel-menu img").click(function()
	{
		iWasClicked = true;
				
		$(".carousel-item").removeClass("showing");
		var clickedItem = $(this).attr("class");
		
		/*************/
		//alert( "OnClick - item was " + clickedItem ); /* clicked item is correct at this point */
		/************/
		
		$(".carousel-item[class*='" + clickedItem + "']").addClass("showing");
		itemClicked = clickedItem;
		
		if( iHaveTooFew )
		{
			updateItemIndex( );
			updateItemInView( );
			updateSelectedInMenu( );
		}
	});

});

/* CALL BACK FUNCTIONS */
function beforeCallBack()
{
	
}

function afterCallBack()
{
	updateItemIndex();	/* manage the counter */
	updateItemInView();	/* manage the current viewed item */
	updateSelectedInMenu(); /* sets the currently selected carousel item t be highlighted in the menu */
	iWasClicked = false;	/* aleways remember to reset this to false when done handling the call back*/
}


/* CAROUSEL UTILIS FUNCTIONS */
function updateItemIndex()
{
	
	
	/* Did the user click? */
	if(iWasClicked)
	{
		/* the user user clicked an item directly - update index */
		if( iHaveTooFew )
		{
			currentItemIndex = itemClicked;
		}
		else
		{
			/* Get number of items */
			/*var itemCount = $("#carousel-items").children(".carousel-item").length;
			/* check which one is showing as a result of the click */
			/*for( var i = 1; i <= itemCount; i++ )
			{
				if($("#carousel-items .carousel-item[class*='" + i + "']").hasClass("showing") )
				{
					/*************/
					//alert( "CurrentItemIndex = " + itemClicked ); /* clicked item is incorrect at this point */
					/*************/
					currentItemIndex = itemClicked;
					/*break;
				}
			}*/
		}
		
	}
	else
	{
		/* the user let it scroll automatically - update index */
		
		/* check if current item is the last*/
		if(currentItemIndex == currentItemCount )
		{
			/* it is the last item, cycle */
			currentItemIndex = 1;
			
		}
		else
		{
			/* not last item, increment */
			currentItemIndex++;
		}
		
	}
}

function updateItemInView()
{
	/* hide all big items */
	$(".carousel-item").removeClass("showing");
	$(".carousel-item").fadeOut(500);
	
	/* grabs large item that has class matching currentItemIndex and show it */
	
	$(".carousel-item").each(function(){
		if($(this).hasClass(currentItemIndex))
		{
			$(this).addClass("showing");
			$(this).fadeIn(500);
		}
	});
	
	/*$(".carousel-item[class*='" + currentItemIndex + "']").addClass("showing");
	$(".carousel-item[class*='" + currentItemIndex + "']").fadeIn(1000);*/
}

function updateSelectedInMenu()
{
	var itemCount = $("#carousel-items").children(".carousel-item").length;
	$("#carousel-menu img").removeClass("selected");
	$("#carousel-menu img[class*='" + currentItemIndex + "']").addClass("selected");
	
	/* check which one is showing as a result of the click */
	/*for( var i = 1; i <= itemCount; i++ )
	{
		if($("#carousel-items .carousel-item[class*='" + i + "']").hasClass("showing"))
		{
			$("#carousel-menu img").removeClass("selected");
			$("#carousel-menu img[class*='" + i + "']").addClass("selected");
			break;
		}
	}*/
}

/* END CAROUSEL CODE */




