/************** CHARGEMENT PAGE *****************************/
jQuery(document).ready(function(){
	rolloverPanier();
	loadAddNewsletter('#frmNewsletter');
	initMenu();
});
/************** CHARGEMENT PAGE *****************************/


// PENDING : a normaliser
function rolloverPanier(){
	jQuery("#monpanier").mouseenter(function(event){
		if(jQuery("#nb-prod-panier").text() > 0){
			jQuery("#monpanier").css('cursor', 'pointer');
			var pos = jQuery("#monpanier").position();

			jQuery.ajax({
				type: "POST",
				url: '/js/ajax/show_panier.php',
				success: function(html){
					// création de la div
					if (!jQuery("#rollover_panier").attr('id')) {
						jQuery("<div></div>").hide().attr("id", "rollover_panier").css({
							'left': (pos.left+190)+'px',
							'position': 'absolute',
							'top': (pos.top+60)+'px',
							'z-index': '3000'
						}).html(html).appendTo("body").show();
					}else{
						jQuery("#rollover_panier").html(html);
					}

					jQuery("#rollover_panier").mouseenter(function(event){
						jQuery("#rollover_panier").css('display','block');
					});
					jQuery("#rollover_panier").mouseleave(function(event){
						jQuery("#rollover_panier").css('display','none');
					});
					jQuery("#monpanier").mouseleave(function(event){
						jQuery("#rollover_panier").css('display','none');
					});
				}
			});
			jQuery("#rollover_panier").show();
		}
	});
	
	jQuery("monpanier").mouseleave(function(event){
		jQuery("#rollover_panier").css('display','none');
	});
}

function showMonCompte(etat) {
	if(etat==1) {
		jQuery('#rollovercompte').css('display','block');
	}else{
		if(!locklogin) {
			jQuery('#rollovercompte').css('display','none');
		}
	}
}

var locklogin = false;
function lockLogin(etat) {
	if(etat) {
		locklogin = true;
	}else{
	 	locklogin = false;
	}
}


//Affichage du sous menu au rollover
function initMenu(){

	jQuery("#menu_header a.ongletrollover").each(function(i){

		jQuery(this).mouseenter(function(event){
			jQuery("#sous-" + jQuery(this).attr("id")).css('display', 'block');
		});

		jQuery(this).mouseleave(function(event){
			jQuery("#sous-" + jQuery(this).attr("id")).css('display', 'none');
		});
	});

	jQuery(".rollovermenu").each(function(i){
		jQuery(this).mouseenter(function(event){
			jQuery("#" + jQuery(this).attr("id").substr(5)).addClass('onrollover');
			jQuery(this).css('display', 'block');
		});

		jQuery(this).mouseleave(function(event){
			jQuery(this).removeClass('hover');
			jQuery("#" + jQuery(this).attr("id").substr(5)).removeClass('onrollover');
			jQuery(this).css('display', 'none');
		});
	});
}







function loadPopupAttr(){
	jQuery(".liste-opts .img-attr").each(function(i){
		if(jQuery("#popup-"+this.id).attr('id')){
			jQuery(this).css('cursor', 'pointer').click(function(){
				jQuery("body").popup({
					popup:{
						css:{
							'width':'700px',
							'height':'320px',
							'border':'1px solid #C6C6C6'
						},
						removeOnClick:{
							0:'#popup'
						}
					},
					content: jQuery("#popup-"+this.id).html()
				});
			});
		}
	});
}

function loadAddNewsletter(node){
	alterneText("#newsletter_email", 'Votre email ici');

	
	jQuery(node).submit(function(){
		jQuery("body").popup({
			popup:{
				removeOnClick:{
					0:'#btn-close-popup',
					1:'#close'
				}
			},
			ajax:'/js/ajax/addNewsletter.php',
			data:jQuery(node).serialize()
		});
		return false;
	});
}

function loadAjoutPanier(){

	jQuery(".plus").css('cursor', 'pointer').click(function(){
		jQuery("#case-"+this.id).attr('value',(parseInt(jQuery("#case-"+this.id).attr('value'))+1));
		jQuery("#qte-"+jQuery(this).attr('id')).attr('value', parseInt(jQuery("#case-"+jQuery(this).attr('id')).attr('value')));
	});
	
	jQuery(".moin").css('cursor', 'pointer').click(function(){
		if ((parseInt(jQuery("#case-"+this.id).attr('value'))-1) > 0) {
			jQuery("#case-"+this.id).attr('value',(parseInt(jQuery("#case-"+this.id).attr('value'))-1));
			jQuery("#qte-"+jQuery(this).attr('id')).attr('value', parseInt(jQuery("#case-"+jQuery(this).attr('id')).attr('value')));
		}
	});
	
	jQuery("#btn-ajout-fprod").css('cursor', 'pointer').click(function(){
		jQuery("body").popup({
			popup:{
				removeOnClick:{
					0:'#close',
					1:'#continuer'
				}
			},
			ajax:'/js/ajax/update-panier.php',
			data:jQuery("#add-panier").serialize(),
			callback:'updateProdPanier()'
		});
	});
}

function addFastPanier(idForm){
	jQuery("body").popup({
		popup:{
			css:{
			'width':'582px',
			 'height':'314px'
			},
			removeOnClick:{
				0:'#btn-close-popup',
				1:'#btn-continue-achat',
				2:'#close',
				3:'#continuer'
			}
		},
		ajax:'/js/ajax/update-panier.php',
		data:jQuery("#"+idForm).serialize(),
		callback:'updateProdPanier()'
	});
}

function selectTaille(optiontailleid,nboption){
	jQuery('.labeltaille').removeClass('selected');
	jQuery('#labeltaille-'+optiontailleid).addClass('selected');
	jQuery('#taille-'+optiontailleid).attr('checked','checked');
	
	var produit_id = jQuery('#produit_id').attr('value');
	if(nboption>1) {
		var optioncouleurid = jQuery('input[type=radio][name=couleur]:checked').attr('value');
	} else {
		var optioncouleurid = "";
	}
	jQuery.ajax({
		type: "POST",
		url: "/js/ajax/update-attribut.php",
		data:"produit_id=" + produit_id + "&couleur="+ optioncouleurid +"&taille="+optiontailleid,
		success: function(msg){
					jQuery('#bloc_prix').html(msg);
		}
	});
}

function selectCouleur(optioncouleurid,nboption){
	jQuery('.labelcouleur').removeClass('selected');
	jQuery('#labelcouleur-'+optioncouleurid).addClass('selected');
	jQuery('#couleur-'+optioncouleurid).attr('checked','checked');
	
	var produit_id = jQuery('#produit_id').attr('value');
	if(nboption>1) {
		var optiontailleid = jQuery('input[type=radio][name=taille]:checked').attr('value');
	} else {
		var optiontailleid = "";
	}
	
	jQuery.ajax({
		type: "POST",
		url: "/js/ajax/update-attribut.php",
		data:"produit_id=" + produit_id + "&couleur="+ optioncouleurid +"&taille="+optiontailleid,
		success: function(msg){
					jQuery('#bloc_prix').html(msg);
		}
	});
}

function changeprixdegressif(prodid,attribid,qte){

	jQuery.ajax({
		type: "POST",
		url: "/js/ajax/update-prix.php",
		data:"produit_id=" + prodid + "&produit_attribut_id="+ attribid +"&qte="+qte,
		success: function(msg){
					jQuery('#bloc_prix').html(msg);
		}
	});
}

function AllTemoignage(){

	jQuery(".temoignage").css('cursor', 'pointer').click(function(){
		jQuery("body").popup({
			type: "POST",
			popup:{
				removeOnClick:{
					0:'#close'
				}
			},
			ajax:'/js/ajax/temoignage.php',
			data: 'id='+jQuery(this).attr('id')
		});
	});

	jQuery("#ajouter_un_temoignage").css('cursor', 'pointer').click(function(){
		jQuery("body").popup({
			popup:{
				removeOnClick:{
					0:'#close'
				}
			},
			ajax:'/js/ajax/ajout-temoignage.php'
		});
	});
}

function updateProdPanier(){
	jQuery.ajax({
		type: "POST",
		url: "/js/ajax/update_prod_panier.php",
		success: function(msg){
			var tab = msg.split('|');
			jQuery("#nbprodpanier").html(tab[0]);
			jQuery("#prixtotpanier").html(tab[1]);
		}
	});
}

function loadPagePanier(){
	jQuery(".btn-qte-plus").css('cursor', 'pointer').click(function(){
		jQuery("#new-"+jQuery(this).attr('id')).attr('value', parseInt(jQuery("#value-"+jQuery(this).attr('id')).attr('value'))+1);
		jQuery("#form-"+jQuery(this).attr('id')).submit();
	});

	jQuery(".btn-qte-moins").css('cursor', 'pointer').click(function(){
		jQuery("#new-"+jQuery(this).attr('id')).attr('value', parseInt(jQuery("#value-"+jQuery(this).attr('id')).attr('value'))-1);
		jQuery("#form-"+jQuery(this).attr('id')).submit();
	});

	jQuery(".delete-produit").css('cursor', 'pointer').click(function(){
		jQuery("#form-"+jQuery(this).attr('id')).submit();
	});

	jQuery("#port select").each(function(){
		jQuery(this).change(function(){
			jQuery("#port").submit();
		});
	});

	toolTipLivraison();
}

function loadPageCoord(){
	
	// jQuery("#new_adresse_livraison_id").change(function(){
	//	jQuery("#flag").attr('value', '4');
	//	 jQuery('#coordonnees').submit();
	//  });
	
	jQuery("#pays_id").change(function(){
		jQuery("#flag").attr('value', '3');
		jQuery('#coordonnees').submit();
	});

	

	toolTipLivraison();
}



function loadPageRecap(){
	jQuery("#liste-paiement .paiement").css('cursor', 'pointer').click(function(){
		jQuery("#"+jQuery(this).attr('id')+" input:radio").attr('checked', 'checked');
	});

	toolTipLivraison();
}

function toolTipLivraison(){
	jQuery(".infos-livraison").each(function(){
		jQuery(this).css('cursor','help');
		jQuery(this).mouseenter(function(event){

			if(jQuery("#popup-"+jQuery(this).attr('id')).attr('id')){
				jQuery("#popup-"+jQuery(this).attr('id')).css({
					'display':'block',
					'top':event.pageY + 10,
					'left':event.pageX + 10
				});
			}else{
				jQuery("<div></div>")
				.html(jQuery("#"+jQuery(this).attr('id')+"-contenu").html())
				.attr('id', "popup-"+jQuery(this).attr('id'))
				.addClass('tool-tip-livraison')
				.css({
					'top':event.pageY + 10,
					'left':event.pageX + 10,
					'position':'absolute'
				})
				.appendTo("body");
			}
		});

		jQuery(this).mouseleave(function(){
			jQuery("#popup-"+jQuery(this).attr('id')).css('display', 'none');
		});

	});
}

function loadSendMail(){
	jQuery("#contact").submit(function(){
		jQuery.ajax({
			type: "POST",
			url: "/js/ajax/contact.php",
			data: jQuery(this).serialize(),
			success: function(msg){
				jQuery("#contenu-contact").html(msg);
				loadSendMail();
			}
		});
		return false;
	});
}
function loadInscNewsletter(){
	jQuery("#newsletter").submit(function(){
		jQuery.ajax({
			type: "POST",
			url: "/js/ajax/addNewsletter.php",
			data: jQuery(this).serialize(),
			success: function(msg){
				jQuery("#contenu-contact").html(msg);
			}
		});
		
		return false;
	});
}

function loadFicheProduit(){
	loadChangeImage();
	loadPopupAttr();

	jQuery(".jqzoom").jqzoom({
		zoomType: 'reverse',
		title: false,
		showPreload: false,
		zoomWidth: 360,
		zoomHeight: 150,
		position: 'right',
		xOffset : 20,
		yOffset : 5
	});
}

function alterneText(id, text){
	jQuery(id).focus(function(){
		if (jQuery(this).attr('value') == text) {
			jQuery(this).attr('value', '');
		}
	});
	jQuery(id).blur(function(){
		if (jQuery(this).attr('value') == '') {
			jQuery(this).attr('value', text);
		}
	});
}

function loadMenu(){
	jQuery(".lien_menu_rayon").css('cursor', 'pointer').click(function(){
		jQuery(".lien_menu_rayon").removeClass('lien_menu_rayon_select');
		jQuery(this).addClass('lien_menu_rayon_select');
		jQuery(".sous_menu_rayon").removeClass('sous_menu_rayon_hidden');
		jQuery(".sous_menu_rayon").addClass('sous_menu_rayon_hidden');
		jQuery("#sous-"+jQuery(this).attr('id')).removeClass('sous_menu_rayon_hidden');
	});
}


function loadSlideShow(){

	jQuery("#promo").showcase({
		animation: {
			type: "horizontal-slider",
			stopOnHover: true,
			speed: 600
		},
		navigator:{
			position: "bottom-left",
			css: {
				top: "190px",
				height: "40px",
				"z-index": 1100
				
			},
			showNumber: true,
			item: {
				css: {
					height:"30px",
					"line-height":"28px",
					width:"50px",
					color: "#ffffff",
					"font-weight":"bold",
					"font-size":"18px",
					backgroundColor: "transparent",
					border: "none",
					background:"url(/styles/images/conteneur/left/btn_slideshow.png) no-repeat",
					margin: "0px 15px 0px 0px",
					"text-align": "center",
					"vertical-align": "middle"
				},
				cssHover: {
					color: "#333333",
					backgroundColor: "transparent",
					border: "none",
					background:"url(/styles/images/conteneur/left/btn_slideshow_select.png) no-repeat"
				},
				cssSelected: {
					color: "#333333",
					backgroundColor: "transparent",
					border: "none",
					background:"url(/styles/images/conteneur/left/btn_slideshow_select.png) no-repeat"
				}
			}
		},
		titleBar: {
			enabled: false
		}
	});
}


/***********************/
function in_array(value, tableau){
	var a=false;
	for(var i=0;i<tableau.length;i++){
		if(value == tableau[i]){
			a=true;
			break;
		}
	}
	return a;
}
/***********************/


function envoyerAmi(produit_id)
{
	jQuery("body").popup({
  	popup:{
  		
	  removeOnClick:{
		  0:'#ami_retour',
		  1:'#popup_valider',
		  2:'#close'
	  }, 		
  		
		css:{
		'width':'580px'
	  }
	},
	ajax:'/js/ajax/popupEnvoyerAmi.php',
	data:"produit_id=" + produit_id
  });		

}

function removePopup(){
	jQuery("#popup").fadeOut(function(){
		jQuery("#popup").remove();
		jQuery("#bg-popup").remove();
		jQuery("select").css('visibility', 'visible');
	});
}

function checkamis(on) {
		jQuery.ajax({
			type: "POST",
		  url: '/js/ajax/popupEnvoyerAmi.php',
		  data:jQuery('#formamis').serialize(),
		  success: function(req) {
				if(jQuery('#popup_ami')) jQuery('#popup').html(req);
		  }
		});			
}

function showprod(etat, idprod) {
	if(etat==1) {
		jQuery('#prodzoom'+idprod).css('display','block');
	}else{
		jQuery('#prodzoom'+idprod).css('display','none');
	}
}
