$(document).ready(function() {
	
	posSouris = 0;
	fiche_affichee = false;
	current_fiche = false;	clic_lien = false;
	
	initFiche();

});

function initFiche(){
	$("#contentwrap")
	// Au déplacemenet de la souris, on stocke la position du curseur
	.mousemove(function(e){ posSouris = e.pageX - this.offsetLeft; });
	//.bind("click", function() { cacheFiche() } ); 
	
	$(".produit_liste_photo a").filter(function(){  
		elm = $(this).parents("#coeurnav_produits");
		if(elm.length) return false;
		else return true;
	})
	// Au survol de la vignette produit : on affiche la fiche
	.bind("mouseenter",function() { 
		$(this).parent().parent().parent().addClass('infiche');
		if ( fiche_affichee == false )
		{
			// En fonction de la position du curseur de la souris, on cale la fiche à gauche ou à droite 
			if ( posSouris < 580 )
				margeFiche = "-280px 0px 0px 0px";
			else
				margeFiche = "-280px 0px 0px -225px";
			
			lien = $(this);
			self.setTimeout(function(){montreFiche(lien, margeFiche)},1000);
		}
		
    })
	.bind("mouseleave", function() {
		$(this).parent().parent().parent().removeClass('infiche');
	});
	
	
	$(".win_fiche")
	// Quand on clique sur la fiche ou quand on "sort" de la fiche produit : on cache la fiche
	.bind("mouseleave", function() { cacheFiche() });
	
	$('.win_fiche_content a').click(function(){
		clic_lien = true;
	});
	$('.win_fiche_content').click(function(){
		if(clic_lien){
			clic_lien = false;
		} else {
			url = $(this).find('.winfiche_lienfiche a').attr('href');
			if(url.substr(0,7)!='http://'){
				url = 'http://www.lemanegeabijoux.com/'+url;
			}
			window.location = url;
		}
	});
}

function cacheFiche() {
	if ( fiche_affichee != false )
	{
		fiche_affichee.animate({opacity: "hide"}, "fast");
		fiche_affichee = false;
	}
	$('.win_fiche').animate({opacity: "hide"}, "fast");
}


function montreFiche(lien, margeFiche) {
	if(lien.parent().parent().parent().hasClass('infiche')){
		fiche_affichee = lien.parent().parent().next("div.win_fiche");
		fiche_affichee.css({margin: margeFiche});
		fiche_affichee.animate({opacity: "show"}, "slow");
	} else {
		fiche_affichee = false;
	}
}
