var highlights = {
	blockManagers: 		{},
	init: 				function()
	{
		highlights.initBlocks();
		
		highlights.initBlockNav();
		highlights.startBlockAnimation();
	},
	initBlocks: 		function()
	{
		$('.highlights_wrapper').each(function(n){
			if (! $(this).attr('id')){
				$(this).attr('id','highlight_wrapper_'+n);
			}
		});
		
		$('.highlights_wrapper .highlight:first').addClass('selected block0');
	},
	initBlockNav: 		function()
	{
		$('.highlights_wrapper .nav').css({zIndex: 200});
		$('.highlights_wrapper .nav li:first').addClass('selected');
		
		$('.highlights_wrapper .nav li a').each(function(n){
			var highlightWrapperId	= $(this).parents('.highlights_wrapper:first').attr('id');
			$(this)
				.hover(
					function(){
						highlights.stopBlockAnimation(highlightWrapperId)
					}, 
					function(){
						highlights.startBlockAnimation(highlightWrapperId)
					}
				)
				.click(function(){ 
					highlights.cycleBlocks(highlightWrapperId, parseInt( $(this).text()) );
					return false;
				});
		});
	},
	
	startBlockAnimation: function(highlightWrapperId)
	{
		if (! highlightWrapperId){
			$('.highlights_wrapper').each(function(){
				highlights.startBlockAnimation( $(this).attr('id') );
			});
		} else {
			highlights.blockManagers[ highlightWrapperId ] = setInterval(function(){ highlights.cycleBlocks(highlightWrapperId) }, 5000);
		}
	},
	stopBlockAnimation: function(highlightWrapperId)
	{
		clearInterval(highlights.blockManagers[ highlightWrapperId ]);
	},
	cycleBlocks: 		function(highlightWrapperId, targetBlock)
	{
		if (! $('#'+highlightWrapperId)){
			return;
		}
		
		var noOfBlocks	= $('#'+highlightWrapperId+' .highlight').length;
		if (noOfBlocks <= 1){
			return;
		} 
		
		// Bring current block to the front
		$('#'+highlightWrapperId+' > .selected').css({
			zIndex: 20
		}).addClass('wasSelected').removeClass('selected');
		
		$('#'+highlightWrapperId+' .nav li.selected').removeClass('selected');
		
		if (targetBlock){
			// queueBlock needs targetBlock to be 0 based
			highlights.queueBlock(highlightWrapperId, targetBlock-1);
		} else {
			// Bring new block into second position
			for (var a = 0; a < noOfBlocks; a++){
				if ( $('#'+highlightWrapperId+' > .wasSelected').hasClass('block'+a) ){
					var nextBlock	= ((a+1) >= noOfBlocks) ? 0 : a+1;
				
					highlights.queueBlock(highlightWrapperId, nextBlock);
					break;
				}
			}
		}
		
		// fade out .wasSelected block and drop down the queue
		$('#'+highlightWrapperId+' > .wasSelected').animate({opacity: 0},500, function(){
			$(this).css({
				zIndex: 0
			}).removeClass('wasSelected').css({opacity: 1});
		}); // end fadeout
	},
	queueBlock: 			function(highlightWrapperId, blockNo)
	{
		$('#'+highlightWrapperId+' .highlight:nth-child('+(blockNo+1)+')').css({
			zIndex: 10,
			position: 'absolute',
			top: 	0,
			left: 	0
		}).addClass('selected block'+blockNo);
		
		$('#'+highlightWrapperId+' .nav li:nth-child('+(blockNo+1)+')').addClass('selected');
	}
}
	
$().ready(function() {
	highlights.init();
});
