// 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)
{
	var myForm = $("#"+id_form);
	myForm.find("input[@type=text]").each(function(){
		if( $(this).attr('readonly') != true ){
			$(this).attr({ title : $(this).val() });
			
			$(this).bind("click",function(){
				if( $(this).attr("title") == $(this).val() ){
					$(this).val("");
				}
			});
			
			$(this).bind("blur",function(){
				if( $(this).attr("title") == $(this).val() || $(this).val() == ""){
					$(this).val( $(this).attr("title") );
				}
			});
		}
	});
	
	
	myForm.find('input[@type=submit]').click(function(){
		var error = 0;
		myForm.find('.obligatoireInput').each(function(){
			if( $(this).attr("title") == $(this).val() ){
				error +=1;
			}
		});
		
		if( error > 0 ){
			alert( $("#"+id_message).val() );
			return (false);
		}
	});
}

// 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 : 0.85 });
		$(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',12,6,'||',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();
	});
});