/************** CHARGEMENT PAGE *****************************/
jQuery(document).ready(function(){
    rolloverPanier();
    loadAddNewsletter('#inscription_newsletter');
});
/************** CHARGEMENT PAGE *****************************/


// PENDING : a normaliser
function rolloverPanier(){
    jQuery("#panier").mouseenter(function(event){
        if(jQuery("#nb-prod-panier").text() > 0){
            jQuery("#panier").css('cursor', 'pointer');
            var pos = jQuery("#panier").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-255)+'px',
                            'position': 'absolute',
                            'top': (pos.top+80)+'px',
                            'z-index': '9000'
                        }).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("#panier").mouseleave(function(event){
                        jQuery("#rollover_panier").css('display','none');
                    });
                }
            });
            jQuery("#rollover_panier").show();
        }
    });
    
    jQuery("panier").mouseleave(function(event){
        jQuery("#rollover_panier").css('display','none');
    });
}

function savepanier(){
    jQuery("body").popup({
        popup:{
            removeOnClick:{
                0:'#close'
            }
        },
        ajax:'/js/ajax/savepanier.php'
    });
}
function savepanier2(idform) {
		jQuery.ajax({
			type: "POST",
		  url: '/js/ajax/savepanier2.php',
		  data:jQuery('#'+idform).serialize(),
		  success: function(req) {
				jQuery('#popup_ajout_panier').html(req);
		  }
		});			
}
function addWishlist(wishid) {
	
	jQuery.ajax({
		type: "POST",
		url: '/js/ajax/addWishlist.php',
		data:"wishlist_id="+wishid,
		success: function(req) {
			if(req != "OK") {
				jQuery('#err').html(req);
			} else {
				window.location.href = '/shopping_cart.html';
			}
		}
	});	
}

function deleteWishlist(wishid) {
	
	jQuery.ajax({
		type: "POST",
		url: '/js/ajax/deleteWishlist.php',
		data:"wishlist_id="+wishid,
		success: function(req) {
				window.location.href = '/precommande.html';
		}
	});	
}



function changestatutclient(statut_id,clientid) {
	
	jQuery.ajax({
		type: "POST",
		url: '/js/ajax/changestatutclient.php',
		data:"client_id="+clientid+"&client_statut_id="+statut_id,
		success: function(req) {

		}
	});	
}

function changestatutwishlist(wishid, statut_id) {
	jQuery.ajax({
		type: "POST",
		url: '/js/ajax/changestatutwishlist.php',
		data:"wishlist_id="+wishid+"&wishlist_statut_id="+statut_id
	});
}



function addmultipre() {
	
	jQuery.ajax({
		type: "POST",
		url: '/js/ajax/addmultipre.php',
		data:jQuery('#addmultiprecom').serialize(),
		success: function(req) {
			window.location.href = '/shopping_cart.html';
		}
	});	
}


//Affichage du sous menu au rollover
function initMenu(){

    jQuery("#menu a.lien_menu_principal").each(function(i){

        jQuery(this).mouseenter(function(event){
            jQuery("#sous-" + jQuery(this).attr("id")).css('visibility', 'visible');
        });

        jQuery(this).mouseleave(function(event){
            jQuery("#sous-" + jQuery(this).attr("id")).css('visibility', 'hidden');
            jQuery(".sous-" + jQuery(this).attr("id")).css('visibility', 'hidden');
        });
    });

    jQuery(".sous-categorie").each(function(i){
        jQuery(this).mouseenter(function(event){
            jQuery("#" + jQuery(this).attr("id").substr(5)).addClass('onrollover');
            jQuery(this).css('visibility', 'visible');
        });

        jQuery(this).mouseleave(function(event){
            jQuery(this).removeClass('hover');
            jQuery("#" + jQuery(this).attr("id").substr(5)).removeClass('onrollover');
            jQuery(this).css('visibility', 'hidden');
        });
    });
}





function loadChangeImage(){
    jQuery("#liste-mini-visuel .mini-visuel").each(function(i){
        jQuery(this).css('cursor', 'pointer').click(function(){
            var idVisu = jQuery(this).attr('id');
            jQuery("#grand-visuel img:first").attr('src', jQuery("#grand-"+idVisu+" img").attr('src'));
            jQuery("#grand-visuel img:first").parents('a.jqzoom').attr('href', jQuery("#popup-"+idVisu+" img").attr('src'));     // pour le zoom produit
            jQuery("#loupe-visuel").unbind('click');
            jQuery("#loupe-visuel").click(function(){
                jQuery("body").popup({
                    popup:{
                        css:{
                            'width':'700px',
                            'height':'320px',
                            'border':'1px solid #C6C6C6'
                        },
                        removeOnClick:{
                            0:'#popup'
                        }
                    },
                    content: jQuery("#popup-"+idVisu).html()
                });
            });
        });
    });
	
    jQuery("#loupe-visuel").css('cursor', 'pointer').click(function(){
        jQuery("body").popup({
            popup:{
                removeOnClick:{
                    0:'#popup'
                },
                css:{
                    'width':'700px',
                    'height':'320px',
                    'border':'1px solid #C6C6C6'
                }
            },
            content: jQuery("#popup-"+jQuery("#liste-mini-visuel .mini-visuel:first").attr('id')).html()
        });
    });
}

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:{
            removeOnClick:{
                0:'#btn-close-popup',
                1:'#btn-continue-achat',
                2:'#close',
                3:'#continuer',
                4:'#resaligne'
            }
        },
        ajax:'/js/ajax/update-panier.php',
        data:jQuery("#"+idForm).serialize(),
        callback:'updateProdPanier()'
    });
}

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 result = msg.split('|');
            jQuery("#nb-prod-panier").html(result[0]);
            jQuery("#prix_panier").html(result[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');
        });

    });
    
    jQuery(".infos-institut").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');
        });

    });
    
    jQuery(".input_livraison_zone").each(function(){
        jQuery(this).click(function(){
        	if(jQuery(this).attr('value') == "9"){
        		jQuery("#liste_instituts").css('display', 'block');
        	} else {
        		jQuery("#liste_instituts").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 clickRDV(idInstitut,ajoutpanier){
    jQuery("body").popup({
        popup:{
            removeOnClick:{
                0:'#bout_resa'
            }
        },
        ajax:'/js/ajax/choix-institut.php',
        data:'id='+idInstitut+"&ajoutpanier="+ajoutpanier
    });
}

/***********************/
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;
}
/***********************/
