﻿Type.registerNamespace("Res.Commerce.Web.Composants.FicheProduit.V01");
Res.Commerce.Web.Composants.FicheProduit.V01.InfoProduitPrincipale = function (element) {
    Res.Commerce.Web.Composants.FicheProduit.V01.InfoProduitPrincipale.initializeBase(this, [element]);

    this._IsScrollable = null;
    this._AncreScroll = null;
    this._CurrencySymbol = null;
    this._UrlRetour = null;
    this._CodeDocument = null;
    this._IsPrixSansHtml = null;
    this._CurrencySeparator = null;
};

Res.Commerce.Web.Composants.FicheProduit.V01.InfoProduitPrincipale.prototype = {
    initialize: function () {
        Res.Commerce.Web.Composants.FicheProduit.V01.InfoProduitPrincipale.callBaseMethod(this, "initialize");

        if (this._IsScrollable) {
            $(this.FormatId('.smooth-scroll')).live('click', Function.createDelegate(this, this.clickScrollable));
        }

        var boutonRetour = $(this.FormatId('.memo_fp_retour'));
        if (boutonRetour != null) {
            boutonRetour.live('click', Function.createDelegate(this, this.clickRetour));
        }
        this.movePicto();

        // début méca promo
        // si le src n'est pas renseigné, on masque l'img
        if ($(this.FormatId(".picto_meca_promo")).attr("src")) {
            if ($(this.FormatId(".picto_meca_promo")).attr("src") == "") {
                $(this.FormatId(".picto_meca_promo")).css("display", "none");
            }
            else {
                $(this.FormatId(".picto_meca_promo")).css("display", "");
            }
        }
        else {
            $(this.FormatId(".picto_meca_promo")).css("display", "none");
        }
        // fin méca promo
    },
    FormatId: function (selector) {
        return '#' + this._element.id + ' ' + selector;
    },
    dispose: function () {
        $(this.FormatId('.smooth-scroll')).die('click');
        var boutonRetour = $(this.FormatId('.memo_fp_retour'));
        if (boutonRetour != null) {
            boutonRetour.die('click');
        }
        Res.Commerce.Web.Composants.FicheProduit.V01.InfoProduitPrincipale.callBaseMethod(this, "dispose");
    },

    saveClientState: function () {
        return null;
    },
    movePicto: function () {
        var source = $('.memo_fp_stickers img[alt=versandkosten]:first');
        var clone = source.clone();
        clone.attr('id', 'moved-picto');
        clone.appendTo($('#' + registeredComponents.SelecteurProduit));
        source.remove();
    },
    clickRetour: function (ev) {
        // Si on proviens précédément du site
        if (document.referrer != null && document.referrer != '') {
            var currentDomain = window.location.protocol + '//' + window.location.host;
            if (document.referrer.substr(0, currentDomain.length) === currentDomain) {
                ev.preventDefault();
                history.back();
            }
        }
        // Sinon le liens du bouton est utilisé
    },
    clickScrollable: function (ev) {

        if (this._IsScrollable) {
            ev.preventDefault();
            $.scrollTo($(this.FormatId('.smooth-scroll')).attr('href'), 500, 'easeIn');
        }
    },
    // début meca promo
    // permet d'afficher/masquer le picto de mécanique promo
    DisplayPictoMecaPromo: function (produitTaille) {
        if (produitTaille == null) {
            $(this.FormatId(".picto_meca_promo")).css("display", "none");
        }
        else if ((produitTaille.NumeroMecaPromo >= 0) && (produitTaille.NumeroSelection >= 0)) {
            $(this.FormatId(".picto_meca_promo")).css("display", "");
            $(this.FormatId(".picto_meca_promo")).attr("src", produitTaille.UrlPictoMecaPromo);
        }
        else {
            $(this.FormatId(".picto_meca_promo")).css("display", "none");
        }
    },
    // fin meca promo
    SetPrix: function (produitTaille) {
        if (produitTaille == null)
            return;

        var split = produitTaille.Prix.split('.');
        if (split.length == 1)
            split = produitTaille.Prix.split(',');
        if (split.length == 1)
            split[1] = "00";
        if (split[1].length == 1)
            split[1] += "0";

        var prix = this.GetFormatPrix(split);

        $(this.FormatId('.memo_fp_prix_final')).html(prix);

        split = produitTaille.PrixBarre.split('.');
        if (split.length == 1)
            split = produitTaille.PrixBarre.split(',');
        if (split.length == 1)
            split[1] = "00";
        if (split[1].length == 1)
            split[1] += "0";

        var prixBarre = this.GetFormatPrix(split);
        var composantPrixBarre = $(this.FormatId('.memo_fp_prix_barre'));
        if (prix == prixBarre) {
            composantPrixBarre.css({ display: 'none' });
        }
        else {
            composantPrixBarre.css({ display: 'inline' });
            $(this.FormatId('.memo_fp_prix_barre del')).html(prixBarre);
        }

        if (produitTaille.TauxDemarque != null) {
            $(this.FormatId(".remise")).attr("src", produitTaille.TauxDemarque);
            $(this.FormatId(".remise")).show();
        }
        else
            $(this.FormatId(".remise")).hide();

        // On met à jour le tooltip sur le bouton retour
        $(this.FormatId('.memo_fp_retour')).attr('title', this._CodeDocument + ' - ' + produitTaille.ReferencePresentation);

    },
    GetFormatPrix: function (split) {
        var prix;
        if (this._IsPrixSansHtml) {
            if (this._CurrencySeparator != '.') {
                prix = split[0] + this._CurrencySeparator + split[1] + ' ' + this._CurrencySymbol + '';
            }
            else
                prix = this._CurrencySymbol + String.fromCharCode(160) + split[0] + this._CurrencySeparator + split[1] + '';
        }
        else {
            prix = split[0] + '<sup>' + this._CurrencySymbol + split[1] + '';
        }
        return prix;
    },
    get_CurrencySymbol: function () {
        return this._CurrencySymbol;
    },

    set_CurrencySymbol: function (value) {
        this._CurrencySymbol = value;
    },

    get_IsScrollable: function () {
        return this._IsScrollable;
    },

    set_IsScrollable: function (value) {
        this._IsScrollable = value;
    },

    get_AncreScroll: function () {
        return this._AncreScroll;
    },

    set_AncreScroll: function (value) {
        this._AncreScroll = value;
    },

    get_CodeDocument: function () {
        return this._CodeDocument;
    },

    set_CodeDocument: function (value) {
        this._CodeDocument = value;
    },

    get_UrlRetour: function () {
        return this._UrlRetour;
    },
    set_UrlRetour: function (value) {
        this._UrlRetour = value;
    },
    get_IsPrixSansHtml: function () {
        return this._IsPrixSansHtml;
    },
    set_IsPrixSansHtml: function (value) {
        this._IsPrixSansHtml = value;
    },
    get_CurrencySeparator: function () {
        return this._CurrencySeparator;
    },
    set_CurrencySeparator: function (value) {
        this._CurrencySeparator = value;
    }
};
Res.Commerce.Web.Composants.FicheProduit.V01.InfoProduitPrincipale.registerClass("Res.Commerce.Web.Composants.FicheProduit.V01.InfoProduitPrincipale", Res.Commerce.Web.Composants.ScriptControlBase);
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded(); 

