$(document).ready(function(){
	initSlideShow();	
});

var itemsBlocksWidth = 0;
var itemsBlocksCount = 0;

var previousActivBlock = -1;
var activBlock = 0;
var nextActivBlock = 0;

var activItem = -1;
var nextActivItem = 0;

var showDuration = 1000;
var blockDuration = 12000;
var restartDuration = 15000;
var itemDuration = 3000;
var playTimer = 0;
var playItemTimer = 0;

var playing = 0;

var pointNumber = 0;


function initSlideShow(){
	$('#blockBigSell').each(function(){
		var blockBigSell = $(this);
		var itemsBlocks = blockBigSell.find('.items-blocks');
		var nav = blockBigSell.find('.nav');
		var points = nav.find('.points a');
		var prev = blockBigSell.find('.nav .previous');
		var next = blockBigSell.find('.nav .next');
		
		// ---------------------------
		// IE FIX 
		nav.pngFix();
		var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
		var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
		if (jQuery.browser.msie && (ie55 || ie6)) {
			itemsBlocks.find('.bubble img').attr('src','/skins/gamecash/blockBigSell_bubble_bgd.gif');
		}
		itemsBlocks.find('.bubble').css('display','block');
		// ---------------------------
		
		// ---------------------------
		// Initialisation 
		itemsBlocksWidth = itemsBlocks.width();
		itemsBlocksCount = itemsBlocks.find('.block').length;

		itemsBlocks.find('.block').hide();
		itemsBlocks.find('.block:first').show(); // AFFICHAGE DES 5 PREMIERS JEUX PAR DEFAUT 
		
		activBlock = 0;
		// ---------------------------
		
		// ---------------------------
		// animation automatique
		blockBigSell.find('.play:first').each(function(){				
			$(this).unbind('click').click(function(){					
				points.removeClass('activ');
				nextActivBlock = activBlock + 1;
				if(nextActivBlock > ( itemsBlocksCount - 1 ) ) nextActivBlock = 0;			
				nav.find('.points a:eq('+nextActivBlock+')').addClass('activ');				
				animateBlock();			
				play();
				activItem = -1;
				$('#blockBigSell .play').trigger('dblclick');		
				return false;
			});
			$(this).unbind('dblclick').dblclick(function(){
				nextActivItem = activItem + 1;
				if(nextActivItem <= 3){
					itemsBlocks.find('.item').removeClass('activ');
					itemsBlocks.find('.block:eq('+activBlock+') .item:eq('+nextActivItem+')').addClass('activ');
					activItem = nextActivItem;
					if(playItemTimer != 0){ clearTimeout(playItemTimer); }
					if(activItem == 0){
						playItemTimer = window.setTimeout("$('#blockBigSell .play').trigger('dblclick')", itemDuration + ( showDuration / 3 ) );
					}else{
						playItemTimer = window.setTimeout("$('#blockBigSell .play').trigger('dblclick')", itemDuration);
					}
					
				}
			});
		});		
		
		function play(){
			playing = 1;
			playTimer = window.setTimeout("$('#blockBigSell .play').trigger('click')", blockDuration);
			$('#blockBigSell .play').trigger('dblclick');
		}
		
		function pause(){
			playing = 0;
			if(playTimer != 0){ clearTimeout(playTimer); }
			if(playItemTimer != 0){ clearTimeout(playItemTimer); }
			
			playTimer = window.setTimeout("$('#blockBigSell .play').trigger('click')", restartDuration);
		}
		// ---------------------------
		
		// ---------------------------
		// Animation des blocks
		function animateBlock(){
		
			var actualBlock = itemsBlocks.find('.block:eq('+activBlock+')');
			var nextBlock = itemsBlocks.find('.block:eq('+nextActivBlock+')');
			var previousBlock = itemsBlocks.find('.block:eq('+previousActivBlock+')');
			actualBlock.stop();
			nextBlock.stop().hide();	
			if( nextActivBlock < activBlock ){
				if(activBlock == (itemsBlocksCount - 1) && nextActivBlock == 0){
					previousBlock.stop().hide();
				}
				nextBlock.removeClass('toHide').css('margin-left', -(itemsBlocksWidth)).css('display','block');
				actualBlock.addClass('toHide');

				nextBlock.animate({
					marginLeft: 0
				},showDuration,'easeInOutQuint',function(){ return true ; });	

				actualBlock.animate({
					marginLeft: 0
				},showDuration,function(){ $(this).hide() });
			}else{			
				previousBlock.stop().hide();
				nextBlock.css('margin-left', 0).css('display','block');
				actualBlock.animate({
					marginLeft: (-(itemsBlocksWidth))			
				},showDuration,'easeInOutQuint',function(){
					$(this).hide()
				});
			}	
			previousActivBlock = activBlock;				
			activBlock = nextActivBlock;
		}
		// ---------------------------
		
		// ---------------------------
		// Events
		next.unbind('click').click(function(){
			pause();
			//itemsBlocks.find('.block').hide();
			points.removeClass('activ');
			nextActivBlock = activBlock + 1;
			if(nextActivBlock > (itemsBlocksCount - 1) ) nextActivBlock = 0;			
			nav.find('.points a:eq('+nextActivBlock+')').addClass('activ');
			
			animateBlock();
			return false;
		});
				
		prev.unbind('click').click(function(){
			pause();
			//itemsBlocks.find('.block').hide();
			points.removeClass('activ');
			
			nextActivBlock = activBlock - 1;
			if(nextActivBlock < 0) nextActivBlock = (itemsBlocksCount - 1) ;			
			nav.find('.points a:eq('+nextActivBlock+')').addClass('activ');
			
			animateBlock();			
			return false;
		});
		
		points.each(function(){			
			var pointEl = $(this);
			var pointElNumber = pointNumber;
			pointEl.unbind('click').click(function(){
				pause();
				if(pointElNumber > activBlock || pointElNumber < activBlock){
					nextActivBlock = pointElNumber;
					points.removeClass('activ');
					nav.find('.points a:eq('+nextActivBlock+')').addClass('activ');					
					animateBlock();
				}
				return false;
			});
			pointNumber++;
		});
		// ---------------------------
		
		play(); // LANCEMENT DE L'ANIMATION
	});
}

