 /** 
* @projectDescription L'Evenement VIP
* @author	Guyllaume Doyer
*/


// ---------------------------------------------------------------------------------------------- \\
// Jquery init event
// ---------------------------------------------------------------------------------------------- \\
$(document).ready(function()
{
	handleTraiteurProducts();
});


function handleTraiteurProducts()
{
	
	
	var ttprod = {
		
		context: $("#TraiteurProductsBlock"),
		
		init: function()
		{
			// Show Pics Diaporama Block (hidden by default for accessibility issue)
			$(".diaporamaBlk", ttprod.context).show();
			
			// Hide pics (displayed by default for accessibility issue)
			$("div.itemData .itemPics", ttprod.context).hide();
			
			this.listeners()
			
			$("div.itemTitle:first", ttprod.context).click();
			
			if ( $("div.itemList", ttprod.context).hasClass("noPicsCat") === true )
			{
				var cutIndex = Math.ceil( $("div.itemTitle", ttprod.context).length / 2 );
				var itemsTotalNb = $("div.itemList div.itemTitle", ttprod.context).length;

				if ( cutIndex > 1 )
				{				
					$("div.itemList", ttprod.context).after( $("div.itemList", ttprod.context).clone(true) );
					$("div.itemList", ttprod.context).slice(0,1).find("div.itemTitle").slice(cutIndex, itemsTotalNb).each(function()
					{
						$(this).next("div.itemData").remove().end().remove();
					});
					
					$("div.itemList", ttprod.context).slice(1,2).find("div.itemTitle").slice(0, cutIndex).each(function()
					{
						$(this).next("div.itemData").remove().end().remove();
					});
					
				}
				
				$("div.product_group_data").each(function()
				{
					if ( $(this).next().is("div.product_group_data") || $(this).next().length == 0 ) { $(this).remove(); };
				});
			};
			
			return this;
		},
		
		
		listeners: function()
		{
			$("div.itemTitle", ttprod.context).css("cursor", "pointer").click( function() { ttprod.showItemPics($(this)); } );
			
			$(".diaporamaBlk span.goNextLnk", ttprod.context).click( function(){ ttprod.next(); } );
			
			$(".diaporamaBlk span.goPreviousLnk", ttprod.context).click( function(){ ttprod.previous(); } );
			
			return this;
		},
		
		
		showItemPics: function(clickedItem)
		{
				// Remove "current" class of every DT and DD (quicklier than finding the current ones and the remove class)
				$("div.itemTitle, div.itemData", ttprod.context).removeClass("current");
				
				// Add "current" class on clicked item DT
				clickedItem.addClass("current");
			
				// Get clicked item related pics attributes (and add "current" class on clicked item DD)
				var itemPics = clickedItem.next("div.itemData").addClass("current").find("img.itemPics");
				var itemPicsSRC = itemPics.attr("src");
				var itemPicsAlt = itemPics.attr("alt");
				var itemPicsTitle = itemPics.attr("title");
				
				// Set Diaporama Pics new attributes
				$("#diaporamaPics", ttprod.context).attr({src:itemPicsSRC, alt:itemPicsAlt, title:itemPicsTitle});
				
			
			return this;
		},
		
		
		getCurrent: function()
		{
			return $("div.itemTitle.current" ,ttprod.context);
		},
		
		
		next: function()
		{
			var current = ttprod.getCurrent();
			
			// If The current is the last item, the next item has to be the first
			if ( $("div.itemTitle", ttprod.context).index(current[0]) == $("div.itemTitle", ttprod.context).length - 1 )
			{
					$("div.itemTitle:first", ttprod.context).click();
			}
			else
			{
				ttprod.getCurrent().prev("div.itemData").prev("div.itemTitle").click();
			}
			
			current.next("div.itemData").next("div.itemTitle").click();
			
			return this;
		},
		
		
		previous: function()
		{
			var current = ttprod.getCurrent();
			
			// If The current is the first item, the previous item has to be the last
			if ( $("div.itemTitle", ttprod.context).index(current[0]) == 0 )
			{
					$("d.itemTitle:last", ttprod.context).click();
			}
			else
			{
				ttprod.getCurrent().prev("div.itemData").prev("div.itemTitle").click();
			}
			
			return this;
		}
		
	}
	
	// Initialise function
	ttprod.init();
	
}
