/*
 * Product Slider Animation
 */
var speed = 450;
var showdir;

// id du 1er produit
var idShowcase;
var varmode;

var items;
var current = 0;

var new0 = 0;
var new1 = 1;
var new2 = 2;
var new3 = 3;
var timer;

var isLoading = false;
var debug = false;
$(init);

function init(){
	if (debug) console.log('Initialising product slider');
	timer = setInterval("divSlide('left')", 5000);
	items = products.length;
	
	$("a#arrow_left").click(function () {
		clearInterval(timer);
		if(!isLoading){
			divSlide('left');
		}
		return false;
	});
	
	$("a#arrow_right").click(function () {
		clearInterval(timer);
		if(!isLoading){
			divSlide('right');
		}
		return false;
	});
	initForm();
	//loadSlider();
}
function initForm(){
	/*$('#spread_word').submit(
		function(){
		
			submitform();
			return false;
		}
	);*/
	
	$('#email_ad').focus(
	
		function(){
			var val = $('#email_ad').val();
			
			if(val=='Enter your email adress here'){
				$('#email_ad').val('');
			}
		}
	);
}

function submitform(){

	var val = $('#email_ad').val();
			
	if(val=='Enter your email adress here' || val ==''){
		$('#email_ad').focus();
		return false;
	}else{
	
		$.ajax({
			url: ('/library/php/mailinglist.php'),
			data: "mail="+$('#email_ad').val(),
			success: redirect,
			error: erreur
		})
	}
}

function redirect(r){
	document.location.href= r;
}

function loadSlider() {
	if (debug) console.log('Loading product slider');
	$('#showcase #i_1 a').attr('href', products[0].url);
	$('#showcase #i_1 h3').text(products[0].title);
	$('#showcase #i_1 div p').html(products[0].text);
	
	$('#miniatures #i_0 #s_0 a').attr('href', products[1].url);
	$('#miniatures #i_0 #s_0 a img').attr('src', '/themes/dermavia/images/animation/'+products[1].img);
	$('#miniatures #i_0 div p').html(products[1].intro);
	
	$('#miniatures #i_2 #s_2 a').attr('href', products[2].url);
	$('#miniatures #i_2 #s_2 a img').attr('src', '/themes/dermavia/images/animation/'+products[2].img);
	$('#miniatures #i_2 div p').html(products[2].intro);
	if (debug) console.log('Finished Loading product slider');
}

function divSlide(dir){
	if (debug) console.log('Sliding to the '+dir);
	isLoading = true;
	
	idShowcase = getFirstId() * 1;
	
	switch(dir){
		case 'right':
			new0 = new0 + 1;
			if ( new0 == items )
				new0 = 0;
			new1 = new1 + 1;
			if ( new1 == items )
				new1 = 0;
			new2 = new2 + 1;
			if ( new2 == items )
				new2 = 0;
			new3 = new3 + 1;
			if ( new3 == items )
				new3 = 0;				
		
		break;
		
		case 'left':
			new0 = new0 - 1;
			if ( new0 == -1 )
				new0 = (items-1);
			new1 = new1 - 1;
			if ( new1 == -1 )
				new1 = (items-1);
			new2 = new2 - 1;
			if ( new2 == -1 )
				new2 = (items-1);
			new3 = new3 - 1;
			if ( new3 == -1 )
				new3 = (items-1);				
		
		break;
	}
	if (debug) console.log('New order => '+ "\nShowcase: "+new0+"\nFirst: "+new1+"\nSecond: "+new2);

	slideAndLoad('showcase', new0, dir);
	slideAndLoad('un', new2, dir);
	slideAndLoad('deux', new3, dir);
	
}

function slideAndLoad(idslide, idload, dir) {
	if (debug) console.log("Start Loading Slide id "+idslide+"\nLoad id: "+idload+"\nto the "+dir);

	switch (idslide) {
		case "showcase":
			$('#showcase #i_1').hide("slide", { direction: dir }, speed);
			setTimeout(function(){
				showdir = (dir == 'right') ? 'left' : 'right';
				$('#showcase #i_1 a').attr('href', products[idload].url);
				$('#showcase #i_1 a img').attr('src', '/themes/dermavia/images/animation/'+products[idload].img);
				$('#showcase #i_1 h3').text(products[idload].title);
				$('#showcase #i_1 div p').html(products[idload].text);
				$('#showcase #i_1').show("slide", { direction: showdir }, speed);}, speed);
		break;
		
		case "un":
			$('#miniatures #i_0').hide("slide", { direction: dir }, speed);
			setTimeout(function(){
				showdir = (dir == 'right') ? 'left' : 'right';
				$('#miniatures #i_0 #s_0 a').attr('href', products[idload].url);
				$('#miniatures #i_0 #s_0 a img').attr('src', '/themes/dermavia/images/animation/'+products[idload].img);
				$('#miniatures #i_0 div p').html(products[idload].intro);
				$('#miniatures #i_0').show("slide", { direction: showdir }, speed);}, speed);
		break;
		
		case "deux":
			$('#miniatures #i_2').hide("slide", { direction: dir }, speed);
			setTimeout(function(){			
				showdir = (dir == 'right') ? 'left' : 'right';
				$('#miniatures #i_2 #s_2 a').attr('href', products[idload].url);
				$('#miniatures #i_2 #s_2 a img').attr('src', '/themes/dermavia/images/animation/'+products[idload].img);
				$('#miniatures #i_2 div p').html(products[idload].intro);
				$('#miniatures #i_2').show("slide", { direction: showdir }, speed);}, speed);
		break;
	}
	
	isLoading = false;
	if (debug) console.log("Finished SlideAndLoad for "+ idslide);
}

function getFirstId() {
	var first = $('#showcase > div');
	
	var firstid = getId(first.attr('id'));
	if (debug) console.log('First: '+first+' and id: ' + firstid);
	return firstid;
}

function getId(id) {
	var i = id.slice(2);
	//console.log('i='+i);
	return i;
}

function erreur() {
	//alert('erreur de chargement, please try again');
}
