$(document).ready(function()
{
	
	var clearQueue = true;
	var gotoEnd = true;
	
	//// Initialize the slidelist ////
	$('.slidesList').each (function(index)
		{
			//RESET
			// Show first page
			$('.slidesList:eq('+index+')').children('ul:first').show();
			// Opacity for the thumbs
			//$('.slidesList:eq('+index+') ul.slideShow li label img').animate({opacity:.5});
			//Show the first div from the first page
			$('.slidesList:eq('+index+') ul.slideShow:first li div:first').show();
			
			//SET
			$('.slidesList:eq('+index+') ul.slideShow:first li:first label img')
				.animate({opacity:1},1)
				.addClass('selectedThumb');
			// Hide the previous arrow
			$('.slidesList:eq('+index+') div.slideShowPager img.slidePagerPrev').animate({opacity:.5});
			
			// Hide the next arrow if it's necessary
			var iTotalPages = $('.slidesList:eq('+index+') ul').size()-1; // PAGER MAX VALUE
			if ( iTotalPages < 1 )
				$('.slidesList:eq('+index+') div.slideShowPager img.slidePagerNext').animate({opacity:.5});
			
			// Add the current page number
			$('.slidesList:eq('+index+')').data ( 'iCurrentPage', 0 );
			$('.slidesList:eq('+index+')').data ( 'iTotalPages', iTotalPages );
		}
	); // end of $('.slidesList').each (function(index)
	
	
	// Register pager customized event
	$('.slidesList').bind('goToPage', function(event, page)
		{
			//alert(param1 + "\n" + param2);
			var iCurrentPage = $(this).data ('iCurrentPage');
			var iTotalPages = $(this).data ('iTotalPages');
			
			if ( iCurrentPage != page && page >= 0 && page <= iTotalPages )
			{
				$(this).children ('ul.slideShow').hide();
				
				// Save current page
				iCurrentPage = page;
				$(this).data ('iCurrentPage', iCurrentPage);
				
				//RESET
				// Opacity for the thumbs (imgs)
				$(this).children ('ul.slideShow:eq('+iCurrentPage+')').children('li').children('label').children('img')
					//.animate({opacity:0.5},1)
					.removeClass('selectedThumb');
				// Hide all the divs
				$(this).children ('ul.slideShow:eq('+iCurrentPage+')').children('li').children('div').hide();
				
				//SET
				// Select first thumb(imgs) & div
				$(this).children ('ul.slideShow:eq('+iCurrentPage+')').children ('li:first').children('label').children ('img')
					//.animate({opacity:1},1)
					.addClass('selectedThumb');
				$(this).children ('ul:eq('+iCurrentPage+')').children('li:first').children('div').show();
				
				
				// Show current div and update the arrows
				$(this).children ('ul.slideShow:eq('+iCurrentPage+')').fadeIn(250);
				
				// Change arrows
				if ( iCurrentPage < 1 )
					$(this).children('div.slideShowPager').children('img.slidePagerPrev').animate({opacity:.5});
				else
					$(this).children('div.slideShowPager').children('img.slidePagerPrev').animate({opacity:1});

				if ( iCurrentPage >= iTotalPages )
					$(this).children('div.slideShowPager').children('img.slidePagerNext').animate({opacity:.5});
				else
					$(this).children('div.slideShowPager').children('img.slidePagerNext').animate({opacity:1});

			}
		}
	);
	// end of $('.slidesList').bind('goToPage', function(event, page)
	
	
	//// Register the onClick handler on the thumbs ////
	$('.slidesList ul li label').click (function(index)
		{	
			//RESET
			$(this).parent('li').parent('.slidesList').children('ul.slideShow').hide();
			$(this).parent('li').parent('ul.slideShow').show();
			$(this).parent('li').parent('ul.slideShow').children('li').children('label').children('img')
				//.animate({opacity:0.5},100)
				.removeClass('selectedThumb');
			$(this).parent('li').parent('ul.slideShow').children('li').children('div').hide();
			//SET
			$(this).children('img')
				.animate({opacity:1},100)
				.addClass('selectedThumb');
			$(this).parent('li').children('div').fadeIn(150);
		}
	); // end of $('.slidesList ul li label').click (function(index)
	
	$('.slidesList ul li label img').hover(
		function(){
			if ($(this).hasClass('selectedThumb'))
			{
				$(this).animate({opacity:1},100);
			}
			else
			{
				$(this).animate({opacity:.5},200);
			}
		},
		function(){
			$(this).stop(clearQueue,gotoEnd);
			$(this).animate({opacity:1},200);
		}
	);
	
	//// Register the onClick handler for the previous arrow ////
	$('.slidePagerPrev').click ( function(index)
		{		
			var iCurrentPage = $(this).parent('div.slideShowPager').parent('div.slidesList').data ('iCurrentPage');
			if ( iCurrentPage > 0 )
			{
				$(this).parent('div.slideShowPager').parent('div.slidesList').trigger('goToPage', [iCurrentPage-1]);
			}
		}
	); // end of $('.slidePagerPrev').click ( function(index)

	//// Register the onClick handler for the next arrow ////
	$('.slidePagerNext').click(function()
		{
			var iCurrentPage = $(this).parent('div.slideShowPager').parent('div.slidesList').data ('iCurrentPage');
			var iTotalPages = $(this).parent('div.slideShowPager').parent('div.slidesList').data ('iTotalPages');
			if (iCurrentPage < iTotalPages)
			{
				$(this).parent('div.slideShowPager').parent('div.slidesList').trigger('goToPage', [iCurrentPage+1]);				
			}
		}
	);	// end of $('.slidePagerNext').click(function()
});
