
// JavaScript Document

//----------------------------------------------------------------------------------------------------
// Fonctions onClick des propriétés collections
//----------------------------------------------------------------------------------------------------
function ShowPropertieTab (itemEl){
    HidePropertiesTab ();
    if (document.getElementById("propertie_"+itemEl)){
        $("#propertie_"+itemEl).removeClass("propertie-off");
        $("#propertie_"+itemEl).addClass("propertie-on");
        $("#panel-instance_"+itemEl).removeClass("hide");
        jScrollPane (itemEl);
    }
    return false;
}
function HidePropertiesTab (){
    var tabIndex = 0;
    while (document.getElementById("propertie_"+tabIndex)){
        $("#propertie_"+tabIndex).removeClass("propertie-on");
        $("#propertie_"+tabIndex).removeClass("propertie-off");
        $("#panel-instance_"+tabIndex).removeClass("hide");
        $("#propertie_"+tabIndex).addClass("propertie-off");
        $("#panel-instance_"+tabIndex).addClass("hide");
        tabIndex++;
    }
    return false;
}

//----------------------------------------------------------------------------------------------------
// Fonction pour mettre à jour la quantité produit lorsqu'il y à plusieurs couleurs'
//----------------------------------------------------------------------------------------------------
function updateQuantityProduct (form_name, ref){
    if( document[form_name] != null ){
        if (document[form_name]["quantity_"+ref]){
            var new_quantity = document[form_name]["quantity_"+ref].options[document[form_name]["quantity_"+ref].selectedIndex].value;
            if( document[form_name].quantity != null )
                document[form_name].quantity.value = new_quantity;
        }
    }
}

//----------------------------------------------------------------------------------------------------
// Fonction pour scroller les contenus
//----------------------------------------------------------------------------------------------------
function jScrollPane (itemEl){
    if (document.getElementById("panel-property_"+itemEl)){
        var options = {
            scrollbarWidth:             13,         /* [int] - the width of the created scrollbar in pixels (defaults to 10) */
            scrollbarMargin:            2,          /* [int] - the margin to leave to the left of the scrollbar in pixels (defaults to 5) */
            wheelSpeed:                 18,         /* [int] - controls how fast the mouse wheel makes the content scroll in pixels (defaults to 18) */
            showArrows:                 true,       /* [boolean] - controls whether to display arrows for the user to scroll with (defaults to false) */
            arrowSize:                  13,          /* [int] - the height of the arrow buttons if showArrows=true (calculated from CSS if not provided) */
            animateTo:                  false,      /* [boolean] - whether to animate when calling scrollTo and scrollBy (defaults to false) */
            dragMinHeight:              0,          /* [int] - the minimum height to allow the drag bar to be (defaults to 0) */
            dragMaxHeight:              99999,      /* [int] - the maximum height to allow the drag bar to be (defaults to 99999!) */
            animateInterval:            100,        /* [int] - The interval in milliseconds to update an animating scrollPane (default 100) */
            animateStep:                3,          /* [int] - The amount to divide the remaining scroll distance by when animating (default 3) */
            maintainPosition:           true,       /* [boolean] - Whether you want the contents of the scroll pane to maintain it's position when you re-initialise it - so it doesn't scroll as you add more content (default true) */
            reinitialiseOnImageLoad:    false,       /* [boolean] - Whether the jScrollPane should automatically re-initialise itself when any contained images are loaded (defaults to false) */
            scrollbarOnLeft:true
        };
        if( $("#panel-property_"+itemEl).jScrollPane != null ){
            $("#panel-property_"+itemEl).jScrollPane(options);
        }
    }
}

//----------------------------------------------------------------------------------------------------
// Fonction pour gérer les variants
//----------------------------------------------------------------------------------------------------
/*
 * sku          : code produit
 */

function doViewVariant ( sku, container, el ){
    /* Gestion des pictos variants */
    elementID 	= document.getElementById(container);
    if (elementID){
        var nbNodes = elementID.childNodes.length;
        for (var i=0; i<nbNodes; i++){
            var node = elementID.childNodes[i];
            if ( node.nodeName.toLowerCase() == "div" && node.id.match(el) ){
                if (document.getElementById(node.id)){
                    $("#"+node.id).removeClass("hide");
                    if (node.id!=el+sku)
                        $("#"+node.id).addClass("hide");
                }
            }
        }
    }
}
function viewVariant ( sku ){

    /* Gestion des pictos variants */
    doViewVariant( sku, 'product-code', 'code_' );
    /* Gestion des illustrations variants */
    doViewVariant( sku, 'illustration', 'illustration_' );
    /* Gestion des matières variants */
    doViewVariant( sku, 'product-mea', 'matiere_' );
    
    /* Gestion des prix et pictos variants */
    elementID 	= document.getElementById('product-price');
    if (elementID){
        nbNodes = elementID.childNodes.length;
        for (i=0; i<nbNodes; i++){
            node = elementID.childNodes[i];
            if ( node.nodeName.toLowerCase() == "div"
                && (node.id.match("price_")
                || node.id.match("picto_")) ){
                if (document.getElementById(node.id)){
                    $("#"+node.id).removeClass("hide");
                    $("#"+node.id).addClass("hide");
                    if (node.id=="price_"+sku || node.id=="picto_"+sku)
                        $("#"+node.id).removeClass("hide");
                }
            }
        }
    }
    /* Gestion de Select Box */
    if (document.f_article.reference.nodeName.toLowerCase()=="select"){
        var select = document.f_article.reference;
        for (i=0;i<select.options.length;i++){
            if (select.options[i].value==sku)
                select.options[i].selected = true;
        }
    }
    /* Gestion de la zone d'achat */
    if ($("input[name="+sku+"]").length>0){
        var zoneAchat = $("input[name="+sku+"]").get(0).value;
        $("#product-buy").removeClass("hide");
        $("#product-indispostock").removeClass("hide");
        $("#product-indispo").removeClass("hide");
        if (zoneAchat!="product-buy")
            $("#product-buy").addClass("hide");
        if (zoneAchat!="product-indispostock")
            $("#product-indispostock").addClass("hide");
        if (zoneAchat!="product-indispo")
            $("#product-indispo").addClass("hide");
    }

    if( document.getElementById("code_produit_text") )
        document.getElementById("code_produit_text").innerHTML = sku;
    $(".variance_teinte").removeClass("variance_teinteon");
    $("#teinte_"+sku).addClass("variance_teinteon");
}

//----------------------------------------------------------------------------------------------------
// Fonction pour ajouter un OpenSet au panier
//----------------------------------------------------------------------------------------------------
function OpenSet_AddToShopper ( form_id, form_name ){
    return AddOpenSetToShopper (form_id, form_name );
}

//----------------------------------------------------------------------------------------------------
// Fonctions relatives aux testimoniaux
//----------------------------------------------------------------------------------------------------
/*
* Affiche tous les commentaires
*/
function viewAllTestimonials (){
    var tabIndex = 0;
    while (document.getElementById('testimonial_'+tabIndex)){
        $("#testimonial_"+tabIndex).removeClass("hide");
        tabIndex++;
    }
    $("*[name=bt-show-testimonials]").addClass("hide");
    $("*[name=bt-give-testimonials]").addClass("mt10_real");
}
/*
* sku          : code_produit
*/
function GiveATestimonial (sku){
    PopInCreate ("Déposer un commentaire");
    var AjaxShopperFile = "/fr/webservices/services-client.htm";
    $.post(AjaxShopperFile, {
        form_action:"avis.deposer",
        code_produit:sku
    }, function(data, textStatus){
        PopInRender (data, "330");
    });
}
/*
* Exécute le dépot du commentaire
*/
function doGiveATestimonial (){
    if (document.f_deposerunavis.commentaire.value == ''){
        alert('Vous ne pouvez pas déposer un commentaire vide.');
    }else{
        var AjaxShopperFile = "/fr/webservices/services-client.htm";
        var code_produit_   = document.f_deposerunavis.code_produit.value;
        var commentaire     = document.f_deposerunavis.commentaire.value;
        PopInReset ();
        $.post(AjaxShopperFile, {
            form_action:"avis.ajouter",
            code_produit:code_produit_,
            message:commentaire
        }, function(data, textStatus){
            PopInRender (data, "330");
        });
    }
}
/*
 * Exécute l'e dépot du commentaire'identification de l'utilisateur
 */
function identifierUtilisateurTestimonial (){
    var AjaxShopperFile = "/fr/webservices/services-client.htm";
    var code_produit_   = document.f_mainform_connexion.code_produit.value;
    var login_          = document.f_mainform_connexion.login.value;
    var mot_de_passe_   = document.f_mainform_connexion.mot_de_passe.value;
    PopInReset ();
    $.post(AjaxShopperFile, {
        form_action:"avis.deposer",
        code_produit:code_produit_,
        login:login_,
        mot_de_passe:mot_de_passe_,
        identifier:"1"
    }, function(data, textStatus){
        PopInRender (data, "330");
    });
}

//----------------------------------------------------------------------------------------------------
// Fonctions relatives au Marketing Viral
//----------------------------------------------------------------------------------------------------
/*
 * sku          : id produit
 */
function Forward (sku){
    PopInCreate ("Faire suivre à un(e) ami(e)");
    var AjaxShopperFile = "/fr/webservices/services-client.htm";
    $.post(AjaxShopperFile, {
        form_action:"forward",
        code_produit:sku
    }, function(data, textStatus){
        PopInRender (data, "200");
    });
}
/*
 * Exécute le dépot du commentaire
 */
function doForward (){
    var AjaxShopperFile = "/fr/webservices/services-client.htm";
    var code_produit_   = document.f_forward.code_produit.value;
    var nom_            = document.f_forward.nom.value;
    var prenom_         = document.f_forward.prenom.value;
    var email_from_     = document.f_forward.email_from.value;
    var email_to_       = document.f_forward.email_to.value;
    PopInReset ();
    $.post(AjaxShopperFile, {
        form_action:"forward",
        execute:1,
        code_produit:code_produit_,
        nom:nom_,
        prenom:prenom_,
        email_from:email_from_,
        email_to:email_to_
    }, function(data, textStatus){
        PopInRender (data, "200");
    });
}

//----------------------------------------------------------------------------------------------------
// Fonction d'ajoût aux favoris
//----------------------------------------------------------------------------------------------------
/*
* sku          : code_produit
*/
function AddToFavorite( sku ){
    PopInCreate ("Ajouter à mes produits favoris");
    var AjaxShopperFile = "/fr/webservices/services-client.htm";
    $.post(AjaxShopperFile,
    {
        form_action:"ajouter.favori",
        code_produit:sku,
        enregistrer:"ok"
    }, function(data, textStatus){
        PopInRender (data, "100");
        autoShowHide("ac-favorite","hide");
    });
}

//----------------------------------------------------------------------------------------------------
// Fonction panier de Quick View du produit
//----------------------------------------------------------------------------------------------------
/*
 * sku          : référence article
 */
function Quickview (sku){
    PopInCreate ("Aperçu de l'article");
    var AjaxShopperFile = "/fr/webservices/apercu-produit.htm";
    $.post(AjaxShopperFile, {
        form_action:"",
        code_produit:sku
    }, function(data, textStatus){
        PopInRender (data, "380");
    });
}
function Zoomview (sku){
    PopInCreate ("", "popin_zoom");
    var AjaxShopperFile = "/fr/webservices/apercu-produit.htm";
    $.post(AjaxShopperFile, {
        form_action:"zoom",
        code_produit:sku
    }, function(data, textStatus){
        PopInRender (data, "428");
    });
}

//----------------------------------------------------------------------------------------------------
// Exécution automatique des fonctions
//----------------------------------------------------------------------------------------------------
$(document).ready(function () {
    jScrollPane(0);
});
