$(document).ready(function() {	
	
	//dropdown menu 
	$("#nav .produits_ul")
	.superfish({
		animation : { opacity:"show", height:"show" },
		delay : 100
	});
	
	// remove align attributes & add class instead to style floating images
	$('img[align="left"]').removeAttr('align').addClass('alignleft');
	$('img[align="right"]').removeAttr('align').addClass('alignright');

	// feature list
	$.featureList(
		$(".feature-links a"),
		$(".feature-img li"), {
			start_item : 0
		}
	);

	// xhtml valid external links
	$('a[rel="external"]').attr('target', '_blank');
	
	// toggle message cadeau
	$("input[name='papier_cadeau']").change(function() {
		$(this).nextAll('p').slideToggle();	
	});
	
	// livraison
	$('.panier_mode_prix_title').empty().append('Délai moyen de livraison de 48h après traitement de votre commande');	

	// add class to inputs
	 $(":checkbox").addClass('checkbox');
	 $(":radio").addClass('radio');
	 $(":submit").addClass('submit');

	// clear search field on click
	$("#login-form-front #password, #login-form-front #email, #recherche, #email-newsletter").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});

	// check email syntax on signup
	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}

	// check required fields on signup
	$('.modify_user').each(function() {
		$(this).find('#email, #civilite, #nom, #prenom, #adresse, #zip, #ville').addClass('required').after('<span></span>');
	});
	$('.inscription_user').each(function() {
		$(this).find('#email, #civilite, #nom, #prenom, #adresse, #zip, #ville, #password').addClass('required').after('<span></span>');
	});
	$('#register-form .required').blur(function() {
		var value = $(this).attr('value');
		if(!value) {
			$(this).addClass('empty');
			$(this).next('span').removeClass('input-on').addClass('input-off');
		} else {
			$(this).removeClass('empty');
			$(this).next('span').removeClass('input-off').addClass('input-on');
		}
	});
	$('#register-form').submit(function() {
		var ok = true;
		$('.required').each(function() {
			var value = $(this).attr('value');
			if (!value) {
				$(this).addClass('empty');
				$(this).next('span').removeClass('input-on').addClass('input-off');
				ok = false; 
			}
		});
		return ok;
	});
	// check if fields are empty when adding secondary addresses to avoid no name&address commands
	$('table.adresse_table').each(function() {
		$(this).find('input[type=text], textarea').addClass('required').after('<span></span>');
		$('table.adresse_table .required').blur(function() {
			var value = $(this).attr('value');
			if(!value) {
				$(this).addClass('empty');
				$(this).next('span').removeClass('input-on').addClass('input-off');
			} else {
				$(this).removeClass('empty');
				$(this).next('span').removeClass('input-off').addClass('input-on');
			}
		});
	});
	$('table.adresse_table').parents('form').submit(function() {
		var ok = true;
		$('.required').each(function() {
			var value = $(this).attr('value');
			if (!value) {
				$(this).addClass('empty');
				$(this).next('span').removeClass('input-on').addClass('input-off');
				ok = false; 
			}
		});
		return ok;
	});

	// Remove style attribute in Address page
	$("strong.adresse_title").nextAll("div").removeAttr("style").addClass("address-buttons");
	
	// animation for formSuccess & formError
	$('.formSuccess, .formError, .panier_chronopost').fadeOut(800).fadeIn(800);
	
	// Add class to the selected path
	$("#path a:last").addClass("selected-path");
	if ($("#path a:last:contains('Accueil')").length) {
		$("#path").hide();
	}
	
	// promo texte "au lieu de"
	try {
		$(".panier_prix_barre").prepend("au lieu de ");
	} catch(e){}

});

