// Scripts Vente en Or
var countdown = 0;
var $countdown;
$(document).ready(function() {
	$("div#panier_paiement div.titreEtape").hide();
	$('<h2 class="h2_modifier">Modifier Mon Profil</h2>').insertBefore('form.modify_user');
	$('<h2 class="h2_modifier">Ajouter une adresse</h2>').insertBefore('input[value="add_adresse"]');
	var sexe = $("#sexe").val();
	$('label[for="sexe"]').remove();
	$('input#sexe').remove();
	$('<br /><label for="sexe">Sexe: </label><input type="text" name="sexe" id="sexe" value="'+sexe+'" /><br />').insertAfter('form.modify_user input#prenom');
//zoom produits
	jQuery(".zoom").jqzoom({
		xzoom:250, 
		yzoom:150,
		offset:10
	});


	// mon compte
	$('#content_mon-compte').each(function() {
	   var html = $(this).html();
		$(this)
		.find('.home_title')
		.click(function() {
			var current = this;
			$('#content_mon-compte .home_title').each(function() {
				if(this == current) {
					$(this).addClass('current')
					.next().addClass('current-content').slideDown();
				}
				else {
					$(this).removeClass('current')
					.next().removeClass('current-content').slideUp();
				}
			});
		})
		.eq(0).trigger('click');
	});
	
	// Onglets fiche produit
	$('#tabs h3 a').each(function(i){
			$(this).click(function(){
				$('#tabs h3').removeClass('current');
				$(this).parent().addClass('current');
				$('.tabsContent').css('display','none').eq(i).css('display','block');
				return false;
			});
	}).eq(0).trigger('click');
	
	// menu campagne
	$('#home-campagne-nav .produits_ul > li, #campagne-listing-nav .produits_ul > li').each(function() {
		$(this).find('> a').before('<a href="#" class="expand" title="Ouvrir">+</a>');
		$(this).find('> .expand').click(function() {
			var ul = $(this).next().next();
			if(ul.css('display') == 'none') {
				ul.css('display', 'block');
				$(this).removeClass('expand').addClass('collapse').html("-").attr('title','Fermer');
			}
			else {
				ul.css('display', 'none');
				$(this).removeClass('collapse').addClass('expand').html("+").attr('title','Ouvrir');
			}
			return false;
		});
	});//.eq(0).find('> .expand').trigger('click');
	
	// Panier
	$('#checkout-buttons .continue-shopping').addClass('button');

	// xhtml valid external links
	$('a[rel="external"]').attr('target', '_blank');

	// animation for formSuccess & formError
	$('.formSuccess, .formError, .panier_chronopost').fadeOut(500).fadeIn(500);

	// clear search field on click
	$("#sidebar #email, #sidebar #password").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	// refresh count down
	$countdown = $('.campagne-countdown');
	if($countdown.length == 1) {
		countdown = parseInt($countdown.html());
		countdownInterval();
		window.setInterval(countdownInterval, 1000);
	}
	
	// color box
	$('.fiche-recommand a').colorbox({href:"/?call=send_friend"});
	$('.list-addcart, .select-addcart').colorbox({width:720, height:500});
	$('#continuer_achats').colorbox.close()

	// highlight current path
	$("#breadcrumb a:last").addClass("current");

	// add classes to inputs
	 $(":checkbox").addClass('checkbox');
	 $(":radio").addClass('radio');
	 $(":submit").addClass('submit');
	 $(":reset").addClass('reset');
	 $(":image").addClass('imgsubmit');

	// check required fields on forms
	$('.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>');
	});
	$('#contact-form').each(function() {
		$(this).find('#name, #email, #subject, #message').addClass('required').after('<span></span>');
	});
	$('table.adresse_table').each(function() {
		$(this).find('input[type="text"], textarea').addClass('required').after('<span></span>');
	});
	
	check_field_onblur('#signup-form .required, #contact-form .required, table.adresse_table .required');
	var address_form = $('table.adresse_table').parents('form');
	check_form_onsubmit(address_form);
	check_form_onsubmit('#signup-form, #contact-form, table.adresse_table .required');
});

function check_field_onblur(required) {
	$(required).blur(function() {
		var value = $(this).attr('value').replace(/^\s+/,'');
		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');
		}
	});
}
function check_form_onsubmit(form) {
	$(form).submit(function() {
		var ok = true;
		$('.required').each(function() {
			var value = $(this).attr('value').replace(/^\s+/,'');
			if (!value) {
				$(this).addClass('empty');
				$(this).next('span').removeClass('input-on').addClass('input-off');
				ok = false;
			}
		});
		return ok;
	});
}
function countdownInterval() {
	countdown--;
	var html = '';
	var j = Math.floor(countdown/86400);
	var h = Math.floor((countdown - j*86400)/3600);
	var m = Math.floor((countdown - j*86400 - h*3600)/60);
	var s = countdown - j*86400 - h*3600 - m*60;
	if(j == 1) {
		html += j + ' jour, ';
	}
	else if (j > 1) {
		html += j + ' jours, ';
	}
	html += h + ':';
	html += Math.floor(m/10).toString()+(m%10)+':';
	html += Math.floor(s/10).toString()+(s%10);
	$countdown.html(html);
}	
Cufon.replace('div.panier_header strong, div.titreEtape, div.panier_mode_title, .adresse_title, .h2_bons, .h2_commandes, .h2_modifier, .h2_filleuls, p strong font, #passdor' );
