﻿
jQuery(document).ready(function() {
    InitWidgetPanier();
    InitWidgetCompte();
    InitQuantitePanier();
    InitCompositionProduit();
    InitTypeClient();
    InitBulleAide();
    InitMessage();
    InitCompositionMenu();
});

/*************************/
/* JQuery dans UpdatePanel */

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_endRequest(function(sender, args) {
    //var panelToUpdate = $get(sender._postBackSettings.panelsToUpdate[0].replace(/\$/g, '_'));
    InitWidgetCompteExpendable();
    InitQuantitePanier();
    InitCompositionProduit();
    InitTypeClient();
    InitBulleAide();
    InitMessage();
    InitCompositionMenu();
});

/*************************/
/* WIDGET */
/* Widget Panier */
function InitWidgetPanier() {
    jQuery('#header #panier .expendable').hide();

    jQuery('#header #panier.widget .fixe').click(function() {
        jQuery(this).parent().find('.expendable').slideToggle("fast");
    });
    jQuery('#header #panier.widget .expendable .btn-collapse').click(function() {
        jQuery(this).parent().slideToggle("fast");
    });
}

/* Widget Compte */
function InitWidgetCompte() {
    jQuery('#header #moncompte .expendable').hide();

    jQuery('#header #moncompte.widget #liSidentifier').click(function() {
        jQuery(this).parent().parent().parent().find('.expendable').slideToggle("fast");
        changePuceSidentifier();
    });
    jQuery('#header #moncompte.widget h3').click(function() {
        jQuery(this).parent().parent().find('.expendable').slideToggle("fast");
        changePuceSidentifier();
    });
    jQuery('#header #moncompte.widget .login').click(function() {
        jQuery(this).parent().parent().find('.expendable').slideToggle("fast");
        changePuceSidentifier();
    });
    InitWidgetCompteExpendable();
}

function InitWidgetCompteExpendable() {
    jQuery('#header #moncompte.widget .expendable.logged .btn-collapse').click(function() {
        jQuery(this).parent().slideToggle("fast");
        changePuceSidentifier();
    });
    jQuery('#header #moncompte.widget .expendable.notlogged .btn-collapse').click(function() {
        jQuery(this).parent().parent().slideToggle("fast");
        changePuceSidentifier();
    });
}

function changePuceSidentifier() {
    if (jQuery('#header #moncompte #picto-sidentifier').hasClass('picto-puce-grise')) {
        jQuery('#header #moncompte #picto-sidentifier').addClass('picto-puce-grise-selected');
        jQuery('#header #moncompte #picto-sidentifier').removeClass('picto-puce-grise');
    }
    else if (jQuery('#header #moncompte #picto-sidentifier').hasClass('picto-puce-grise-selected')) {
        jQuery('#header #moncompte #picto-sidentifier').addClass('picto-puce-grise');
        jQuery('#header #moncompte #picto-sidentifier').removeClass('picto-puce-grise-selected');
    }
};

/*************************/
/* BULLE */
function InitBulleAide() {
    jQuery('.aide a').mouseenter(function(e) {
        bulle = $(this).parent().find('.bulle');
        bulle.css('top', '-' + (bulle.height() + 2) + 'px');
        bulle.css('left', '-4px');
        bulle.fadeIn('fast');
    });
    jQuery('.aide a').mouseleave(function(e) {
        $(this).parent().find('.bulle').fadeOut('fast');
    });
};

/*************************/
/* QUANTITE PANIER */
function InitQuantitePanier() {
    jQuery('input.quantite').keyup(function(e) {
        $this = jQuery(this);
        if (e.keyCode == '13') {
            e.preventDefault();
        }
        if (isNaN($this.val())) {
            $this.val('');
        }
    });
    jQuery('.btn').click(function(e) {
        $this = jQuery(this);
        var qteToAdd = 0;
        if ($this.hasClass('btnmoins')) {
            qteToAdd = -1;
        }
        else if ($this.hasClass('btnplus')) {
            qteToAdd = 1;
        }
        $txtQte = $this.parent().find('input[type=text]');
        if ($txtQte.val() == '') {
            $txtQte.val('0');
        }
        if ((($txtQte.val() != '0' && qteToAdd == -1) || qteToAdd == 1) &&
            !isNaN($txtQte.val())) {
            $txtQte.val(parseInt($txtQte.val()) + qteToAdd);
        }
        //e.preventDefault();
    });
}


/*************************/
/* MESSAGE */
function InitMessage() {
    /*jQuery('.errormessage').message();*/
    jQuery('.successmessage').message();
};

/*************************/
/* FICHE PRODUIT */
function InitCompositionProduit() {
    jQuery('#ficheproduit .colDesc .detail.composition ').toggle(function() {
        jQuery('#ficheproduit .colShoot .frame img').fadeOut('fast', function() {
            jQuery('#ficheproduit .colShoot .frame .composition').fadeIn('fast');
        });
    }, function() {
        jQuery('#ficheproduit .colShoot .frame .composition').fadeOut('fast', function() {
            jQuery('#ficheproduit .colShoot .frame img').fadeIn('fast');
        });
    });
};

/*************************/
/* FORMULAIRE */
function InitTypeClient() {
    jQuery('.form .radPart').click(function() {
        jQuery(this).find('input').attr('checked', 'checked');
        divpro = jQuery(this).parent().parent().parent().find('.professionnel');
        divpro.hide('fast');
    });
    jQuery('.form .radPro').click(function() {
        var t = this;
        jQuery(t).find('input').attr('checked', 'checked');
        divpro = jQuery(t).parent().parent().parent().find('.professionnel');
        divpro.show('fast');
    });
};


/*************************/
/* COMPOSITION */
function InitCompositionMenu() {

    jQuery('#produit .menu-composition').click(function(e) {
        jQuery('#popupproduit .btncompose').click();
    });

    jQuery('#popupproduit .btncompose').click(function(e) {
        e.preventDefault();
        jQuery('#jecompose').slideToggle("fast");
        // Fixe le bug ie6 de disparition de #jecompose .btncompose 
        jQuery('#jecompose').css('overflow', 'visible');
    });

    var selected = 0;
    // Sélection du premier onglet incomplet
    if (jQuery('#jecompose .selector .completincomplet .incomplet').length > 0) {
        var premierIncomplet = jQuery('#jecompose .selector .completincomplet .incomplet')[0];
        selected = jQuery('#jecompose .selector .completincomplet span').index(jQuery(premierIncomplet));
    }

    // Sélection de l'onglet
    jQuery('#jecompose .selector:eq(' + selected + ')').addClass('selected');
    jQuery('#jecompose .partie:eq(' + selected + ')').addClass('selected');

    jQuery('#jecompose .selector').click(function(e) {
        e.preventDefault();
        if (!jQuery(this).hasClass('selected')) {
            jQuery('#jecompose .selector').removeClass('selected');
            jQuery(this).addClass('selected');
            jQuery('#jecompose .partie').removeClass('selected');
            var selected = jQuery('#jecompose .selector').index(this);
            jQuery('#jecompose .partie:eq(' + selected + ')').addClass('selected');
        }
    });
};
 

