/* ---------------------------------------- */
/* FreeWalk Diaporama v1.0                  */
/* Copyleft (c) 2009 Sébastien BEIRNAERT	*/
/* licensed under the GPL licence           */
/*											*/
/* Date: 2009-10-25							*/
/* http://www.preprod-sbeirnaert.fr 		*/
/* ---------------------------------------- */
	
function FreeWalkDiaporama(container, options)
{
	this.currentX = 0;
	this.currentY = 0;
	this.container = container;
	this.widthMove = options.widthMove;
	this.heightMove = options.heightMove;
	this.maxAbcissElements = options.maxAbcissElements;
	this.duration = options.duration;
	this.delay = options.delay;
	this.type = options.type;
	this.auto_play = options.auto_play;
	this.loop = options.loop;

	this.initialize = function(){		
		switch(this.type)
		{
			case 'multi-dim' :
				var obj = this;
			
				$('#' + obj.container)
					.css("width", (obj.widthMove * obj.maxAbcissElements) );
					
				$('#diapo-y1').click(function(event){ obj.currentY = 0; $('#block-fiches-produits').animate({ marginLeft : - obj.widthMove*0, marginTop : - obj.currentY*obj.heightMove}, {duration : obj.duration}); $('#menu-produit li a').removeClass('over'); $('#diapo-y1').addClass('over'); return false; });
				$('#diapo-y2').click(function(event){ obj.currentY = 1; $('#block-fiches-produits').animate({ marginLeft : - obj.widthMove*0, marginTop : - obj.currentY*obj.heightMove}, {duration : obj.duration}); $('#menu-produit li a').removeClass('over'); $('#diapo-y2').addClass('over'); return false; });
				$('#diapo-y3').click(function(event){ obj.currentY = 2; $('#block-fiches-produits').animate({ marginLeft : - obj.widthMove*0, marginTop : - obj.currentY*obj.heightMove}, {duration : obj.duration}); $('#menu-produit li a').removeClass('over'); $('#diapo-y3').addClass('over'); return false; });
				$('#diapo-y4').click(function(event){ obj.currentY = 3; $('#block-fiches-produits').animate({ marginLeft : - obj.widthMove*0, marginTop : - obj.currentY*obj.heightMove}, {duration : obj.duration}); $('#menu-produit li a').removeClass('over'); $('#diapo-y4').addClass('over'); return false; });
				
				$('.diapo-x1').click(function(event){ $('#block-fiches-produits').animate({ marginLeft : - obj.widthMove*0, marginTop : - obj.currentY*obj.heightMove}, {duration : obj.duration}); return false; });
				$('.diapo-x2').click(function(event){ $('#block-fiches-produits').animate({ marginLeft : - obj.widthMove*1, marginTop : - obj.currentY*obj.heightMove}, {duration : obj.duration}); return false; });
				$('.diapo-x3').click(function(event){ $('#block-fiches-produits').animate({ marginLeft : - obj.widthMove*2, marginTop : - obj.currentY*obj.heightMove}, {duration : obj.duration}); return false; });
				$('.diapo-x4').click(function(event){ $('#block-fiches-produits').animate({ marginLeft : - obj.widthMove*3, marginTop : - obj.currentY*obj.heightMove}, {duration : obj.duration}); return false; });
				$('.diapo-x5').click(function(event){ $('#block-fiches-produits').animate({ marginLeft : - obj.widthMove*4, marginTop : - obj.currentY*obj.heightMove}, {duration : obj.duration}); return false; });
				break;
				
			case 'diapo-h' :
				var obj2 = this;
				
				// On regarde s'il y a assez d'image pour justifier le diapo auto
				var posLastElement = 0;
				var i = 0;
				var tab = getEl(obj2.container).getElementsByTagName('li');
				while(posLastElement >= 0 && i != tab.length)
				{
					// On defini la position de l'element qui depasse à droite de l'overflow 
					var posLastElement = (GetDomOffset(getEl('diapo_bottom'), 'offsetLeft') + obj2.widthMove) - (GetDomOffset(tab[i], 'offsetLeft') + tab[i].offsetWidth);
					i++;
				}
				
				// Si il y a assez d'image pour justifier le diaporama
				if(posLastElement < 0)
				{
					var width_diapo = 0;
					var nb_thumbs = $('#' + obj2.container + ' li').length;
					for(i = 0; i < nb_thumbs; i++)
					{
						$('#' + obj2.container + ' li').eq(i).clone().appendTo($('#' + obj2.container + ' ul'));
						width_diapo += $('#' + obj2.container + ' li').eq(i).get(0).offsetWidth + parseInt($('#' + obj2.container + ' li').eq(i).css('margin-right'));
					}
					for(i = 0; i < nb_thumbs; i++)
						$('#' + obj2.container + ' li').eq(i).clone().appendTo($('#' + obj2.container + ' ul'));
						
					setTimeout('moveH("' + obj2.container + '", ' + obj2.widthMove + ', ' + obj2.duration + ', ' + obj2.delay + ', ' + 0 + ', ' + 0 + ', ' + nb_thumbs + ', ' + width_diapo + ')', obj2.delay);
				}
				break;
				
			case 'diapo-v' :
				var obj3 = this;
				var nb_thumbs = $('#' + obj3.container + ' div div').length;
				
				// Dédoublement du diapo pour assurer une transition fluide
				$('#' + obj3.container + ' div div').clone().appendTo($('#' + obj3.container + ' div#block-diapo-news'));
				
				setTimeout('moveV("' + obj3.container + '", ' + obj3.heightMove + ', ' + obj3.duration + ', ' + 0 + ', ' + obj3.delay +  ', ' + nb_thumbs + ')', obj3.delay);
				break;
		}
	}
};

function moveH(container, widthMove, duration, delay, marginL, j, nb_thumbs, width_diapo)
{
	var posLastElement = 0;
	
	var i = 0;
	var tab = $('#' + container + ' li');
	var tab_length = tab.length;
	var diapo_left = GetDomOffset(getEl('diapo_bottom'), 'offsetLeft');
	
	while(posLastElement >= 0 && i != tab_length)
	{
		// On defini la position de l'element qui depasse à droite de l'overflow 
		var posLastElement = (diapo_left + widthMove) - (GetDomOffset(tab.eq(i).get(0), 'offsetLeft') + tab.eq(i).get(0).offsetWidth);
		i++;
	}
	
	// On redefini la position, mais cette fois ci à gauche pour caler le deplacement à venir sur cet element
	posLastElement = (diapo_left + widthMove) - (GetDomOffset(tab.eq(i-1).get(0), 'offsetLeft'));
	
	
	var posFinalElement = (diapo_left + widthMove) - (GetDomOffset(tab.eq(tab_length - 1).get(0), 'offsetLeft') + tab.eq(tab_length - 1).get(0).offsetWidth) + widthMove - posLastElement;
	if(posFinalElement >= 0)
	{
		marginL -= width_diapo;
		$('#diapo_bottom ul').css('margin-left', - marginL);
	}
	marginL += widthMove - posLastElement;
	$('#' + container + ' ul').animate({marginLeft : - marginL}, {duration : duration});
	
	setTimeout('moveH("' + container + '", ' + widthMove + ', ' + duration + ', ' + delay + ', ' + marginL + ', ' + j + ', ' + nb_thumbs + ', ' + width_diapo + ')', delay);
}

function moveV(container, heightMove, duration, j, delay, nb_thumbs)
{	
	if(j == (2*nb_thumbs - 1))
	{
		$('#block-diapo-news').css('margin-top', - heightMove*(nb_thumbs-1));
		j = nb_thumbs-1;
	}
	
	$('#block-diapo-news').animate({marginTop : - heightMove*(j+1)}, {duration : duration});
		
	j++;
	setTimeout('moveV("' + container + '", ' + heightMove + ', ' + duration + ', ' + j + ', ' + delay + ', ' + nb_thumbs + ')', delay);
}
