/*--FUNCTIONS.JS--*/
/*Développements spécifique au site*/

/*--- MÊME HAUTEUR POUR CHAQUE ARTICLE D'UNE MÊME LIGNE ---*/
function init_height_li()
{
	var length = $('#category li').length;

	for(ctp=0; ctp < length; ctp++)
	{
		var height = $('#category li').eq(ctp).height();
		var height_sibling = $('#category li').eq(ctp+1).height();
		
		if(height > height_sibling)
		{
			$('#category li').eq(ctp+1).css('height', height);
		}
		else if(height < height_sibling)
		{
			$('#category li').eq(ctp).css('height', height_sibling);
		}
		ctp++;
	}
}
/*--- MÊME HAUTEUR POUR CHAQUE ARTICLE D'UNE MÊME LIGNE ---*/

/*--- MENU DEROULANT ---*/
function menu_deroulant(target)
{		
	$('.leftnav ul:eq(0) li').find('ul').slideUp();
	$('.leftnav ul:eq(0) li a').removeClass('current');
	
	$(target).parent().find('ul').slideDown();
	$(target).addClass('current');

	return false;
}
/*--- MENU DEROULANT ---*/
function fieldFocus(element){
	if(element.value == element.defaultValue){
		element.value = '';
	}
}
function fieldBlur(element){
	if(element.value == ''){
		element.value = element.defaultValue;
	}
}

/*--Function check Form--*/
function checkForm(form, config){

	//Vérification de la config
	if(typeof(config.offset) == 'undefined'){
		config.offset = {
			left: 0,
			top: 10
		};
	}
	else{
		if(typeof(config.offset.left) == 'undefined'){config.offset.left = 0;}
		if(typeof(config.offset.top) == 'undefined'){config.offset.top = 10;}
	}

	//Initialisation de la validation
	if(typeof($(form).attr('status')) == 'undefined' && $(form).attr('status') != 'initialized'){
		$(form).attr('status', 'initialized');
	
		//On stocke les champs uniques dans un tableau
		var uniqueElements = new Array();
		//Si on a des champs requis
		if(typeof(config.required) != 'undefined'){
			for(var i=0; i<config.required.elements.length; i++){
				if(jQuery.inArray(config.required.elements[i], uniqueElements) == -1){
					uniqueElements.push(config.required.elements[i]);
				}
			}
		}
		//Si on a des champs à comparer
		if(typeof(config.compare) != 'undefined'){
			for(var i=0; i<config.compare.elements.length; i++){
				if(jQuery.inArray(config.compare.elements[i][1], uniqueElements) == -1){
					uniqueElements.push(config.compare.elements[i][1]);
				}
			}
		}
		//Si on a des champs radio
		if(typeof(config.radio) != 'undefined'){
			for(var i=0; i<config.radio.elements.length; i++){
				if(jQuery.inArray(config.radio.elements[i], uniqueElements) == -1){
					uniqueElements.push(config.radio.elements[i]);
				}
			}
		}
		//Si on a des champs email à vérifier
		if(typeof(config.email) != 'undefined'){
			for(var i=0; i<config.email.elements.length; i++){
				if(jQuery.inArray(config.email.elements[i], uniqueElements) == -1){
					uniqueElements.push(config.email.elements[i]);
				}
			}
		}
		//Si on a des dates à vérifier
		if(typeof(config.date) != 'undefined'){
			for(var i=0; i<config.date.elements.length; i++){
				if(jQuery.inArray(config.date.elements[i], uniqueElements) == -1){
					uniqueElements.push(config.date.elements[i]);
				}
			}
		}
		//Si on a des champs customisés
		if(typeof(config.custom) != 'undefined'){
			for(var i=0; i<config.custom.length; i++){
				if(jQuery.inArray(config.custom[i].element, uniqueElements) == -1){
					uniqueElements.push(config.custom[i].element);
				}
			}
		}
		//On crée les infos en fonction des champs uniques
		for(var i=0; i<uniqueElements.length; i++){
			var target = $('#'+uniqueElements[i]);
			checkFormFeedback(target, {
				id: i,
				offset: config.offset,
				message: '',
				position: target.offset()
			});
		}
	}
	
	//Statut
	var errors = 0;
	/*** Checker les adresses email ***/
	if(typeof(config.email) != 'undefined'){
		for(var i=0; i<config.email.elements.length; i++){
			var target = $('#'+config.email.elements[i]);
			updateFeedback(target, config.offset, config.email.message);
			
			var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/;
			if(reg.exec(target.val()) == null){
				$(target).parent().addClass('error');
				errors++;
			}
		}
	}
	/*** Vérifier une date ***/
	if(typeof(config.date) != 'undefined'){
		for(var i=0; i<config.date.elements.length; i++){
			var target = $('#'+config.date.elements[i]);
			updateFeedback(target, config.offset, config.date.message);
			
			var reg = /^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$/;
			if(reg.exec(target.val()) == null){
				$(target).parent().addClass('error');
				errors++;
			}
		}
	}
	/*** Checker la correspondance de 2 champs ***/
	if(typeof(config.compare) != 'undefined'){
		for(var i=0; i<config.compare.elements.length; i++){
			var target1 = $('#'+config.compare.elements[i][0]);
			var target2 = $('#'+config.compare.elements[i][1]);
			updateFeedback(target2, config.offset.top, config.compare.message);
			
			if(target2.val() != target1.val()){
				$(target2).addClass('error');
				errors++;
			}
		}
	}
	/*** Checker la sélection d'au moins 1 bouton radio ***/
	if(typeof(config.radio) != 'undefined'){
		for(var i=0; i<config.radio.elements.length; i++){
			var target = $('#'+config.radio.elements[i]);
			var ctp = 0;
			$('input[name="'+(target.attr('name'))+'"]').each(function(){
				if($(this).is(':checked')){
					ctp++;
				}
			});
			if(!ctp){
				target.parent().addClass('error');
				updateFeedback(target, config.offset, config.radio.message);
				errors++;
			}
		}
	}
	/*** Checking custom ***/
	if(typeof(config.custom) != 'undefined'){
		for(var i=0; i<config.custom.length; i++){
			var target = $('#'+config.custom[i].element);
			updateFeedback(target, config.offset, config.custom[i].message);
			
			if(config.custom[i].reg.exec(target.val()) == null){
				$(target).parent().addClass('error');
				errors++;
			}
		}
	}
	/*** Checking required ***/
	if(typeof(config.required) != 'undefined'){
		for(var i=0; i<config.required.elements.length; i++){
			
			var target = $('#'+config.required.elements[i]);
			var defaultValue = document.getElementById(config.required.elements[i]).defaultValue;
			if(target.is('input')){
				if(target.attr('type') == 'text' || target.attr('type') == 'password'){
					if(target.val() == '' || target.val() == defaultValue){
						target.parent().addClass('error');
						updateFeedback(target, config.offset, config.required.message);
						errors++;
					}
				}
				else if(target.attr('type') == 'checkbox' || target.attr('type') == 'radio'){
					if(!target.is(':checked')){
						target.parent().addClass('error');
						updateFeedback(target, config.offset, config.required.message);
						errors++;
					}
				}
			}
			else if(target.is('select')){
				if($('option:selected', target).val() == ''){
					target.parent().addClass('error');
					updateFeedback(target, config.offset, config.required.message);
					errors++;
				}
			}
			else if(target.is('textarea')){
				if(target.val() == '' || target.val() == defaultValue){
					target.parent().addClass('error');
					updateFeedback(target, config.offset, config.required.message);
					errors++;
				}
			}
			
		}
	}
	
	if(!errors){
		return true;
	}
	else{
		return false;
	}
	
};
function updateFeedback(target, offset, message){
	$('#'+target.attr('related_to')+' .message').html(message);
	if(target.attr('type') != 'checkbox' && target.attr('type') != 'radio'){
		if($('#'+target.attr('related_to')).outerWidth() > target.outerWidth()){
			var width = target.outerWidth() - parseFloat($('#'+target.attr('related_to')).css('paddingLeft')) - parseFloat($('#'+target.attr('related_to')).css('paddingRight'));
			$('#'+target.attr('related_to')).css('width', width+'px');
		}
		else{
			$('#'+target.attr('related_to')).css('width', 'auto');
		}
	}
	//Offset top
	var top =  Math.round(target.offset().top) - $('#'+target.attr('related_to')).outerHeight() + offset.top;
	$('#'+target.attr('related_to')).css('top', top+'px');
	//Offset left
	var left =  Math.round(target.offset().left) + offset.left;
	$('#'+target.attr('related_to')).css('left', left+'px');
}
function checkFormFeedback(target, data){
	//Plugged
	target.attr('related_to', 'checkFormFeedback_'+data.id);
	
	//Insertion de l'élément
	$('body').append('<div class="errorFeedback" id="checkFormFeedback_'+data.id+'" style="display:none;"><span class="message">'+data.message+'</span><span class="appendice"></span></div>');
	$('#checkFormFeedback_'+data.id).attr('form_element', target.attr('id'));
	
	//Offset top
	var top =  Math.round(data.position.top) - $('#checkFormFeedback_'+data.id).outerHeight() + data.offset.top;
	$('#checkFormFeedback_'+data.id).css('top', top+'px');
	//Offset left
	var left =  Math.round(data.position.left) + data.offset.left;
	$('#checkFormFeedback_'+data.id).css('left', left+'px');
	
	/*** Déclaration des évènements ***/
	//Si c'est un champ INPUT -> Checkbox ou Radio
	if(target.is('input') && (target.attr('type') == 'checkbox' || target.attr('type') == 'radio')){
		target.parent().find('label').hover(
			function(){
				if($(this).parent().hasClass('error')){
					$('#checkFormFeedback_'+data.id).fadeIn('fast');
				}
			},
			function(){
				if($(this).parent().hasClass('error')){
					$('#checkFormFeedback_'+data.id).fadeOut('fast');
				}
			}
		);
	}
	else{
		target.focus(function(){
			if($(this).parent().hasClass('error')){
				$('#checkFormFeedback_'+data.id).fadeIn('fast');
			}
		});
		target.blur(function(){
			if($(this).parent().hasClass('error')){
				$('#checkFormFeedback_'+data.id).fadeOut('fast');
			}
		});
	}
	
	//Si c'est un bouton radio
	if(target.is('input') && target.attr('type') == 'radio'){
		$('input[name="'+target.attr('name')+'"]').change(function(){
			if($(this).parent().hasClass('error')){
				$('#checkFormFeedback_'+data.id).fadeOut('fast', function(){
					var target = $(this).attr('form_element');
					$('#'+target).parent().removeClass('error');
				});
			}
		});
	}
	else{
		target.change(function(){
			if($(this).parent().hasClass('error')){
				$('#checkFormFeedback_'+data.id).fadeOut('fast', function(){
					var target = $(this).attr('form_element');
					$('#'+target).parent().removeClass('error');
				});
			}
		});
	}
}

function show_newsletter()
{
	$('#newsletter').toggle('medium', function(){		
	});
	return false;
}
