﻿Type.registerNamespace("Res.Commerce.Web.Composants.FicheProduit.V01");
Res.Commerce.Web.Composants.FicheProduit.V01.Partage = function (element) {
    Res.Commerce.Web.Composants.FicheProduit.V01.Partage.initializeBase(this, [element]);

    this._lnkEnvoiAmi = null;
    this._lnkBazaarVoice = null;
    this._IdNoteAvis = null;
    this._IdImgNote = null;
    this._IdLinkOverlay = null;
    this._IdMiniNote = null;
    this._IdMiniImgNote = null;
    this._IdMiniComposantPartage = null;
    this._IdMiniLinkOverlay = null;
    this.HandlerBzvUrl = null;
    this.UrlBazaarVoiceEmpty;
    this.isEmpty = null;

};
Res.Commerce.Web.Composants.FicheProduit.V01.Partage.prototype = {
    initialize: function () {
        Res.Commerce.Web.Composants.FicheProduit.V01.Partage.callBaseMethod(this, "initialize");

        this._lnkEnvoiAmi = $(this.FormatId("#" + this._lnkEnvoiAmi));
        this._lnkEnvoiAmi.live("click", Function.createDelegate(this, this.EnvoiAmiClick));
        this._lnkBazaarVoice = $(this.FormatId(".memo_fp_avis_general"));
        this.linkJsAddThis();

        $(this.FormatId(".memo_fp_avis_general")).click(Function.createDelegate(this, this.OnClickNote));

        this.call = Function.createDelegate(this, this.pushTemplateMini);
    },
    FormatId: function (selector) {
        return '#' + this._element.id + ' ' + selector;
    },
    SetBVOverlay: function () {
        //Overlay(this._IdLinkOverlay, false);
    },
    dispose: function () {
        Res.Commerce.Web.Composants.FicheProduit.V01.Partage.callBaseMethod(this, "dispose");
        this._lnkEnvoiAmi.die('click');
        this._lnkBazaarVoice.die('click');
    },
    saveClientState: function () {
        return null;
    },
    ClickNote: function (func) {
        $('#' + this._element.id).live('ClickNoteEvent', func);
    },

    OnClickNote: function (evtArgs) {

        // Le comportement du composant change si il n'y a pas de commentaire
        if (this.isEmpty == true) {
            return;
        }

        // on fait comme cela à cause d'un bug qui empêche
        // de ré-ouvrir la popup une deuxième fois
        var overlayData = $("#" + this._IdLinkOverlay).data("overlay");

        if (overlayData != null) {
            overlayData.load();
        }
        else {
            Overlay(this._IdLinkOverlay, false, false, null, null, true);
        }

        var event = jQuery.Event("ClickNoteEvent");
        $(this.FormatId(".memo_fp_avis_general")).trigger(event);
    },

    EnvoiAmiClick: function (ev) {
        $("#" + this._element.id).trigger('Partage_EnvoiAmi');
    },

    PartageBazaarVoice: function (ev) {
        $("#" + this._element.id).trigger('Partage_BazaarVoice');
    },

    linkJsAddThis: function (ev) {
        $.getScript("http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4c5832574319714c");
    },

    SetDataBzV: function (url, note) {
        $(this.FormatId('#' + this._IdNoteAvis)).append(' ' + note);
        $(this.FormatId('#' + this._IdImgNote)).attr('src', url);
    },

    SetDataBzVEmpty: function () {
        this.isEmpty = true;
        if (this.get_LinkDepotAvisEnable()) {
            var url = this.HandlerBzvUrl + "?url=" + this.UrlBazaarVoiceEmpty;
            $(this.FormatId('.coloris_color')).click();
            var delegateRetrieveData = Function.createDelegate(this, this.RetrieveData);
            $.get(url, function (data) { delegateRetrieveData($(this), data) });
        }
    },

    RetrieveData: function (currentElement, data) {
        var html = $(data).find(".BVRRRatingSummaryNoReviews");
        $(this.FormatId('.memo_fp_avis_general')).html(html.html());
        $(this.FormatId('.memo_fp_avis_general')).css('display', 'block');
    },

    LoadAndParseReviewsMini: function (url, IdMiniNote, IdMiniImgNote, IdMiniComposantPartage, IdMiniLinkOverlay) {
        this._IdMiniLinkOverlay = IdMiniLinkOverlay;
        this._IdMiniNote = IdMiniNote;
        this._IdMiniImgNote = IdMiniImgNote;
        this._IdMiniComposantPartage = IdMiniComposantPartage;
        $.get(url, this.call);
    },

    pushTemplateMini: function (data) {
        var control = this;
        var html_reviews = $(data).find(".BVRRReviewDisplayStyle3");
        var reviews = [];
        if (html_reviews.length != 0) {
            this.set_infosEnteteMiniFiche(data);
        }
        else {
            $('#' + this._IdMiniComposantPartage).css('display', 'none');
        }
    },

    SetupOverlay: function () {
        if (this._reviews != undefined && this._reviews != null) {
            var overlaySelect = "#memo_fp_overlay_avis .contentWrap";
            $(overlaySelect).append(tmpl("review_template", { reviews: this._reviews }));
            this.set_datatables(overlaySelect);
            $("#memo_fp_overlay_avis th").attr('style', '');
        }
    },

    set_infosEnteteMiniFiche: function (_sdata) {
        var SrcPicAvis = $(_sdata).find('#BVRRSReviewsSummaryRatingImageID img').attr('src').toString().replace('Large', '');
        var altPicAvis = $(_sdata).find('.BVRRNumber').html();

        $('#' + this._IdMiniLinkOverlay).css('display', 'block');

        $('#' + this._IdMiniNote).html($('#' + this._IdMiniNote).html() + ' ' + altPicAvis);
        $(_sdata).find('.BVRRBuyAgainContainer').appendTo('#' + this._IdMiniComposantPartage);
        $('#' + this._IdMiniImgNote).attr('src', SrcPicAvis);

    },

    get_LinkDepotAvisEnable: function () {
        return this._LinkDepotAvisEnable;
    },
    set_LinkDepotAvisEnable: function (value) {
        this._LinkDepotAvisEnable = value;
    },
    get_IdNoteAvis: function () {
        return this._IdNoteAvis;
    },
    set_IdNoteAvis: function (value) {
        this._IdNoteAvis = value;
    },
    get_IdImgNote: function () {
        return this._IdImgNote;
    },
    set_IdImgNote: function (value) {
        this._IdImgNote = value;
    },
    get_IdLinkOverlay: function () {
        return this._IdLinkOverlay;
    },
    set_IdLinkOverlay: function (value) {
        this._IdLinkOverlay = value;
    },
    get_HandlerBzvUrl: function () {
        return this.HandlerBzvUrl;
    },
    set_HandlerBzvUrl: function (value) {
        this.HandlerBzvUrl = value;
    },
    get_UrlBazaarVoiceEmpty: function () {
        return this.UrlBazaarVoiceEmpty;
    },
    set_UrlBazaarVoiceEmpty: function (value) {
        this.UrlBazaarVoiceEmpty = value;
    }

};
Res.Commerce.Web.Composants.FicheProduit.V01.Partage.registerClass("Res.Commerce.Web.Composants.FicheProduit.V01.Partage", Res.Commerce.Web.Composants.ScriptControlBase);
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded(); 
