﻿Pandora2.ns("Pandora2.consumer");

Pandora2.consumer.dictionary = {
    viewwishlist: 'View wishlist',
    viewmyjewellery: 'View My Jewellery',
    itemadded: 'Item added',
    error: "An error has acquired"
};

Pandora2.consumer.navigation = {
    login: "/my-pandora",
    mywishlist: "/my-pandora/my-designs/my-wishlist",
    myjewellery: "/my-pandora/my-designs/my-jewellery"
};


Pandora2.consumer.ProductFinder = function ()
{
    this.initmenuview();
    this.initproductview();
};

Pandora2.consumer.ProductFinder.prototype = {

    proxy: new Pandora2.ServiceProxy("/Services/Consumer/Member/MemberAjaxService.svc/"),

    initmenuview: function ()
    {
        $("a.nav-item").click(function (event) { event.preventDefault(); });
        $(".item-list > li:has(.drop-box)").click(function ()
        {
            $(".item-list > li").not(this).removeClass("nav-item-drop");
            $(this).toggleClass("nav-item-drop");
        });
    },

    initproductview: function ()
    {
        $(".list-area > li:not(.christmasProduct)").hover($.proxy(this.hoverin, this), $.proxy(this.hoverout, this));
    },

    hoverin: function (event)
    {
        var li = $(event.currentTarget);
        if (Utils.IsEcomCountry())
        {
            var overlay = li.children(".overlay-ecommerce");
            var addToBasketLink = overlay.find("a:eq(0)").not('.addToValentinesWishes').not('.soldOut').not('.valentinesAddToBag').not('.notSoldOnline');
            if (li.hasClass('available-for-ecommerce'))
            {
                addToBasketLink.bind('click.products', $.proxy(this, "addToBasket"));
            } else
            {
                addToBasketLink.hide();
            }
            overlay.fadeTo("slow", 0.8);
        } else
        {
            var overlay = li.children(".overlay");
            overlay.find("a:eq(0)").not("a.added,.addToChristmasWishes,.addToValentinesWishes,.findNearestStore,.customizeBundleItems,.viewBundleItem,.addBundleToBag").bind('click.products', $.proxy(this.addToJewellery, this));
            overlay.find("a:eq(1)").not("a.added,.addToChristmasWishes,.addToValentinesWishes,.findNearestStore,.customizeBundleItems,.viewBundleItem,.addBundleToBag").bind('click.products', $.proxy(this.addToWishlist, this));
            overlay.fadeTo("slow", 0.6);

            var overlayWishlist = li.children(".bundle-list .overlay-ecommerce");
            overlayWishlist.find("a:eq(0)").not("a.added,.addToChristmasWishes,.addToValentinesWishes,.findNearestStore,.customizeBundleItems,.viewBundleItem,.addBundleToBag").bind('click.products', $.proxy(this.addToJewellery, this));
            overlayWishlist.find("a:eq(1)").not("a.added,.addToChristmasWishes,.addToValentinesWishes,.findNearestStore,.customizeBundleItems,.viewBundleItem,.addBundleToBag").bind('click.products', $.proxy(this.addToWishlist, this));
        }
    },

    hoverout: function (event)
    {
        if (Utils.IsEcomCountry())
        {
            $(event.currentTarget).children(".overlay-ecommerce").hide().find("a").not("a.added,.addToChristmasWishes,.addToValentinesWishes,.findNearestStore,.customizeBundleItems,.viewBundleItem").unbind('click.products');
        } else
        {
            $(event.currentTarget).children(".overlay").hide().find("a").not("a.added,.addToChristmasWishes,.addToValentinesWishes,.findNearestStore,.customizeBundleItems,.viewBundleItem").unbind('click.products');
        }
    },

    itemadded: function (domelement, type)
    {
        var href;
        var text;

        switch (type)
        {
            case 1:
                href = Pandora2.consumer.navigation.myjewellery;
                text = Pandora2.consumer.dictionary.viewmyjewellery
                break;
            case 2:
                href = Pandora2.consumer.navigation.mywishlist;
                text = Pandora2.consumer.dictionary.viewwishlist
                break;
        }

        var element = $(domelement).unbind().attr('href', href).addClass('added').click(function (event) { event.preventDefault(); });
        element.text(htmlDecode(Pandora2.consumer.dictionary.itemadded));
        setTimeout(function () { element.text(htmlDecode(text)).unbind(); }, 4000);
    },

    additem: function (domelement, result, type)
    {

        //type 1  is my Jewelry.

        if (result.Type == 0 || result.Type == 1)
        {
            if (type == 1)
                $(domelement).parents('li.product').attr('injewelry', true)
            if (type == 2)
                $(domelement).parents('li.product').attr('inwishlist', true)
            this.itemadded(domelement, type);
        }
        else if (result.Type == 2)
        {
            if (type == 1)
                $(domelement).parents('li.product').attr('injewelry', false)
            if (type == 2)
                $(domelement).parents('li.product').attr('inwishlist', false)

            document.location.href = Pandora2.consumer.navigation.login;
        }
        else
        {
            alert(Pandora2.consumer.dictionary.error);
        }
    },

    adderror: function ()
    {
        alert(Pandora2.consumer.dictionary.error);
    },

    getLiId: function (domelement)
    {
        // return $(domelement).parent().parent().parent().parent().attr("id").slice(1);
        var container = $(domelement).closest('li[id]');
        var productID = container.attr('firstProductNumber') ? container.attr('firstProductNumber') : container.attr('id');
        var result = productID.substr(1);
        return result;
    },

    addToJewellery: function (event)
    {
        event.preventDefault();

        var self = this;
        var id = self.getLiId(event.currentTarget);

        var action = function ()
        {
            self.proxy.invoke("AddProductToJewellery", { productid: id }, function (result)
            {
                self.additem(event.currentTarget, result, 1);
            }, self.adderror, true);
        };

        if (User.Authenticated)
        {
            action();
        }
        else
        {
            LoginOverlay.Show(action);
        }
    },

    addToWishlist: function (event)
    {
        event.preventDefault();

        var self = this;
        var id = self.getLiId(event.currentTarget);

        var action = function ()
        {
            self.proxy.invoke("AddProductToWishlist", { productid: id }, function (result)
            {
                self.additem(event.currentTarget, result, 2);
            }, self.adderror, true);
        }

        if (User.Authenticated)
        {
            action();
        }
        else
        {
            LoginOverlay.Show(action);
        }
    },

    addToBasket: function (event)
    {
        event.preventDefault();

        var id = this.getLiId(event.currentTarget);
        selectProductSize(id);
    }
};
$(function ()
{
    new Pandora2.consumer.ProductFinder();
});

