jQuery(function( $ ){
	$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};
	$.scrollTo.defaults.axis = 'xy'; 			
	$.scrollTo( 0 );
});

$(document).ready(function(){
	$('#defileur')
		.css('overflow','hidden');
	
	$('#defileur_sum')
		.each(function(){
			$(this).find('a').mouseover(function(){
				var num;
				num=$(this).attr('id').split('_');
				defileTo(num[2]);
			})
	});

	$('#defileur_gauche').mouseover(function(){
		$('#defileur_droite').show();
		// On n'appele pas la fonction defileTo pour éviter de masquer les div trop top
		$('#defileur').stop().scrollTo((250)-110, 5000, {'easing':'swing'} );
	});
	$('#defileur_gauche').mouseout(function(){
		$('#defileur').stop();
		if(Math.abs($('#defileur ul').offset().left) <= 20){
			//On est à gauche : on masque la fleche de gauche et affiche la fleche de droite
			$('#defileur_gauche').hide();
		};
	});
	$('#defileur_droite').mouseover(function(){
		$('#defileur_gauche').show();
		// Il y a 6 elements vides
		// On n'appele pas la fonction defileTo pour éviter de masquer les div trop top
		$('#defileur').stop().scrollTo((250*($('#defileur ul li').length - 6))-110, 5000, {'easing':'swing'} );
	});
	$('#defileur_droite').mouseout(function(){
		$('#defileur').stop();
		//Si le ul contient onze li on a 11 - 6 = 5 elements soit 4 intervalles de 250px = 1000px
		// donc (250*($('#defileur ul li').length - 6 - 1)
		var offset = Math.abs($('#defileur ul').offset().left);
		var positionMax = 250*($('#defileur ul li').length - 6 - 1);
		if(offset >= positionMax-20 && offset <= positionMax+20){
			//On est à droite : on masque la fleche de droite et affiche la fleche de gauche
			$('#defileur_droite').hide();
		}
	});
	
	$('.colorpick').click(function(){
		//recuperation du code couleur
		$('#colorpicktitle').text('1\350re moiti\351 : ' + $(this).find("#colorname").val());
		//
		$('.colorpick a').attr('class','');
		$(this).find('#colorlink').attr('class','colorsel');
		//memorisation ajax
		var martialcolorid = $(this).find('#martialcolorid').val();
		$('#previewcontainer').load(context+"index.php?portlet=jdb_drageesmartial&template=noborder&type=setdragee1&mode=previewcontainer&item=" + martialcolorid);
		return false;
	});
	
	$('.colorpick2').click(function(){
		//recuperation du code couleur	
		$('#colorpicktitle2').text('2\350me moiti\351 : ' + $(this).find("#colorname").val());
		//
		$('.colorpick2 a').attr('class','');
		$(this).find('#colorlink').attr('class','colorsel');
		//memorisation ajax
		var martialcolorid = $(this).find('#martialcolorid').val();
		$('#previewcontainer').load(context+"index.php?portlet=jdb_drageesmartial&template=noborder&type=setdragee2&mode=previewcontainer&item=" + martialcolorid);
		return false; 
	});
	
	$('.rubanlink').click(function(){
		var ctrlid = $(this).attr('id').split('_');
		if(ctrlid[0]=="defileur")
			var rubanid = ctrlid[3];
		else	
			var rubanid = ctrlid[1];
		//alert(rubanid);
		$('#previewcontainer').load(context+"index.php?portlet=jdb_drageesmartial&template=noborder&type=setruban&mode=previewcontainer&item=" + rubanid);
		return false;
	});
	
	$('.decolink').click(function(){
		var ctrlid = $(this).attr('id').split('_');
		if(ctrlid[0]=="defileur")
			var rubanid = ctrlid[3];
		else	
			var rubanid = ctrlid[1];
		$('#previewcontainer').load(context+"index.php?portlet=jdb_drageesmartial&template=noborder&type=setdeco&mode=previewcontainer&item=" + rubanid);
		return false;
	});
	
	$('#rubaninput1').keyup(function(){
		$('#ruban_preview1').text($(this).val().toUpperCase());
	});
	
	$('#rubaninput1').blur(function(){
		$.get(context+"index.php?portlet=jdb_drageesmartial&template=noborder&type=setrubantext1&item='"+$(this).val()+"'");
	});
	
	$('#rubaninput2').keyup(function(){
		$('#ruban_preview2').text($(this).val().toUpperCase());
	});
	
	$('#rubaninput2').blur(function(){
		$.get(context+"index.php?portlet=jdb_drageesmartial&template=noborder&type=setrubantext2&item='"+$(this).val()+"'");
	});
	
	$('#norubantext').click(function(){
		$.get(context+"index.php?portlet=jdb_drageesmartial&template=noborder&type=setnotext");
	});
	
	$('#btn_martiasuivant').click(function(){
		if(customstep=='22'){
			if($('#colorpicktitle').text()==''){
				alert("Merci de s\351lectionnez une couleur pour la premi\350re drag\351e");
				return false;
			}	
			
			if(seldragee2==1 && $('#colorpicktitle2').text()==''){
				alert("Merci de s\351lectionnez une couleur pour la seconde drag\351e");
				return false;
			}
		}
		//selection du texte sur le ruban
		if(customstep=='31'){
			var link = $(this).attr('href');

			if($('#rubaninput1').val()=="" && $('#rubaninput2').val()==""){
				$.get(context+"index.php?portlet=jdb_drageesmartial&template=noborder&type=setnotext");
				$(this).attr('href', link.substring(0,link.indexOf('&customstep=')+12)+'40');
			}
		}
	});

	$('#btn_sendmail').click(function(){
		var nt = $('#nameto').val();
		var mt = $('#mailto').val();
		var nf = $('#namefrom').val();
		var mf = $('#mailfrom').val();	
		
		if(nt==""){
			alert("Veuillez renseigner le nom de votre ami");
			return false;
		}
		if(mt==""){
			alert("Veuillez renseigner l'adresse e-mail de votre ami");
			return false;
		}
		if(nf==""){
			alert("Veuillez renseigner votre nom");
			return false;
		}
		if(mf==""){
			alert("Veuillez renseigner votre adresse e-mail");
			return false;
		}			

		//alert(context+"index.php?portlet=jdb_drageesmartial&template=noborder&type=sendmail&nameto="+nt+"&mailto="+mt+"&namefrom="+nf+"&mailfrom="+mf);						
		$.get(context+"genPdf.php?sendmail=1&nameto="+nt+"&mailto="+mt+"&namefrom="+nf+"&mailfrom="+mf, function(data){
   			$("#messagesendmail").text(data);
   			$("#messagesendmail").show();
 		});
		return false;
	});
	
	$('.rubantextcolorpick').click(function(){
		var color = $(this).attr("style");
		color = color.split(":"); 
		$(this).parents("ul").find('a').removeClass('colorsel');
		$(this).find("a").addClass('colorsel');
		$('#rubanzone_title').html( $(this).find("input[name='colorname']").val() );
		$('#rubanzone .preview').attr('style','text-transform:uppercase;color:'+color[1]);
		$.get(context+"index.php?portlet=jdb_drageesmartial&template=noborder&type=settextcolor&item="+$(this).find("#martialcolorid").val());
		return false;
	});
	
	$('input.autoremainingcount').keyup(function(){
		var	remains =  $(this).attr('maxlength')  - $(this).val().length;
		if(remains==0)
		{
			$(this).siblings('.remainingcount').html( 'Taille maximale atteinte' );
			$(this).siblings('.remainingcount').addClass('max');
		}
		else if(remains==1)
		{
			$(this).siblings('.remainingcount').html( remains +' car. restant' );
			$(this).siblings('.remainingcount').removeClass('max');
		}
		else
		{
			$(this).siblings('.remainingcount').html( remains +' car. restants' );
			$(this).siblings('.remainingcount').removeClass('max');
		}
	});
});
$(window).load(function(){ $('#defileur').not('.mini').stop().scrollTo(490, 1900, {'easing':'swing'}); $('#defileur.mini').stop().scrollTo(370, 1900, {'easing':'swing'}); } );

function defileTo(num)
{
	$('#defileur').stop().scrollTo((250*num)-110, 1900, {'easing':'swing'} );
	if(num == 1){
		//On est à gauche : on masque la fleche de gauche et affiche la fleche de droite
		$('#defileur_gauche').hide();
		$('#defileur_droite').show();
	}
	else if(num == $('#defileur ul li').length - 6){
		//On est à droite : on masque la fleche de droite et affiche la fleche de gauche
		$('#defileur_droite').hide();
		$('#defileur_gauche').show();
	}else{
		//On est ni à droite ni à gauche : on affiche les 2 fleches
		$('#defileur_droite').show();
		$('#defileur_gauche').show();
	}
}

