// G�re le JS en mode Front End
// ***

// Objet SmallInput
// Permet d'ajouter une valeur par d�faut dans les inputs d'un formulaire

// @id_form
// 		id du formulaire
// @id_message
//		id du message d'alert quand les champs ne sont pas remplis

var SmallInput = function(id_form,id_message)
{
	myForm = $("#"+id_form);
	myForm.find("input[@type=text]").each(function(){
		
		$(this).attr({ defaut : $(this).val() });
		
		$(this).bind("click",function(){
			if( $(this).attr("defaut") == $(this).val() ){
				$(this).val("");
			}
		});
		
		$(this).bind("blur",function(){
			if( $(this).attr("defaut") == $(this).val() || $(this).val() == ""){
				$(this).val( $(this).attr("defaut") );
			}
		});
		
	});
}

// Objet Citation
// Affiche un bandeau sur la banni�re

// @Jq
// 		id ou class du bloc contenant le slogan
// @FontSize
//		Hauteur de police pour un caract�re
// @LetterWidth
//		Largeur d'un caract�re
// @StringSeparator
//		Marqueur de separation pour le retour a la ligne
// @Speed
//		Vitesse de l'animation

var Citation = function(Jq,FontSize,LetterWidth,StringSeparator,speed)
{
	if( $(Jq).find('div').text() != '' )
	{
		Phrases 	= $(Jq).find('div').text().split(StringSeparator);
		NbLignes 	= Phrases.length;
		
		NbLetters	= 0;
		PhraseNew	= '';
		
		for( i=0; i<NbLignes; i++){
			if( Phrases[i].length > NbLetters ) NbLetters = Phrases[i].length;
			if( PhraseNew != '' ) PhraseNew += '<br/>';
			PhraseNew += Phrases[i];
		}
		
		WordsWidth	= ( NbLetters * LetterWidth ) * 1.1;
		SloganLineHeight = $(Jq).css('lineHeight').replace('px','');
		LineHeight = FontSize < SloganLineHeight ? SloganLineHeight : Math.ceil(FontSize * 1.5);
		SloganHeight = LineHeight * NbLignes;
		$(Jq).find('div').html(PhraseNew).hide();
		$(Jq).css({height : SloganHeight+'px', lineHeight : LineHeight+'px' , opacity : 1 });
		$(Jq).animate({ width : WordsWidth },speed,function(){$(Jq).find('div').fadeIn('slow'); });
	}
	else{
		$(Jq).hide();
	}
}



// Si le DOM est charg�
$(function(){

	/* Valeur par d�faut pour le champ de recherche */
	SmallInput('recherche','message_recherche');
	
	// Lancer la fonction citation
	Citation('.index_slogan_fr',12,7,'||',1000);
	Citation('.index_slogan_nl',12,7,'||',1000);
	Citation('.index_slogan_en',12,7,'||',1000);
	Citation('.index_slogan_de',12,7,'||',1000);
	Citation('#slogan',12,6,'||',1000);
	
	/* Catalogue */
	SmallInput('afficherAcheter','emptyFields');
	SmallInput('afficherDemandePrix','');
	
	$('#acheter').bind('click',function(){
		$('#afficherDemandePrix').hide();
		$('#afficherAcheter').slideToggle();
	});
	
	$('#btn_demande_prix').bind('click',function(){
		$('#afficherAcheter').hide();
		$('#afficherDemandePrix').slideToggle();
	});

	// Change la phrase en fonction du custom-recycled
	$("li[id^=custom-recycled-] select").change( function() {
		
		var format = $(this).parent().attr('id').split('-')[2];
		var value = $(this).val();
		
		$("#custom-text-" + format + " span").hide();
		$("#custom-text-" + format + " span." + value).show();
			
	});

	// Génération de l'url du logo en fonction de la chaine et du logo choisi dans l'intranet
	// + Changement de l'image d'apercu
	$("li[id^=custom-text-] input, li[id^=custom-text-] select, li[id^=custom-recycled-] select").change( function() {
		
        //$("input").change( function() {
        var id_format = $(this).parent().attr('id');
		var format;
		if( id_format.length < 1 ) {
			id_format = $(this).parent().parent().attr('id');			
		}
		format = id_format.split('-')[2];
		
		var type = $("#custom-recycled-" + format + " select").val(); 
		var lang = $("#cur-lang").val();
		var num_logo = $('#num-logo').val();
		var custom_text = $('#custom-text-'+format+' input.custom-text').val();
		var issu = $('#custom-text-'+format+' span.'+type+' select.issu').val();
		
		
		var url = "/logo.php?lang="+lang+"&type="+type+"&format="+format+"&num_logo="+encodeURIComponent(num_logo)+"&custom_text="+encodeURIComponent(custom_text)+'&issu='+encodeURIComponent(issu);
		
		$("#custom-logo-"+format+' a.logo').attr('href', url);
		
		// Image apercu
		var selectorLogo = '#logo-preview-' + format;
		
		if( type.length < 1 ) {
			$(selectorLogo).hide();
			$(selectorLogo).attr('src', '');
		}
		else {
			var urlLogo = '/images/logos/jpg/' + lang + '-' + format + '-' + type + '.jpg';
			$(selectorLogo).show();
			$(selectorLogo).attr('src', urlLogo);
		}
		
		
		
		
	});

});
