﻿var month = 30 * 24 * 60 * 60 * 1000;
var year = 365 * 24 * 60 * 60 * 1000;
var isShowNotify = false;

if(Array.indexOf = "undefined" || !Array.indexOf)
{
Array.prototype.indexOf = function(obj)
{
for(var i=0; i<this.length; i++)
{
if(this[i]==obj)
{
return i;
}
}
return -1;
}
}
$(function() {
    Basket();
});

function Basket() {
    $("a[id^=addBasket_],span[id^=addBasket_],span[id^=addBasket2_]").unbind("click").click(function() {
        isShowNotify = true;
        var a = $(this);
        var topLine = $("#divTopline");
        var id = a[0].id.split('_')[1];
        a = $("span[id^=addBasket_" + id + "],span[id^=addBasket2_" + id + "],a[id^=addBasket_" + id + "]")
        var count = 0;
        if (a.hasClass("to-cart-disabled")) {
            //Remove from basket
            ShowLoader(a, "to-cart-disabled", "to-cart");
            a.attr("title", "Добавить в корзину");
            a.html(a.html().replace("Отмена", "КУПИТЬ"));
            var arr = GetCookie(BasketCookieName).split(',');
            var val = "";
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] != id) {
                    if (val == "") {
                        val += arr[i];
                    }
                    else {
                        val += ',' + arr[i];
                    }
                }
            }

            if (val == "") {
                SetCookie(BasketCookieName, val, new Date(new Date().getTime() - month).toUTCString());
                topLine.addClass("empty");
                ShowOrderLink(false);
            }
            else {
                SetCookie(BasketCookieName, val, new Date(new Date().getTime() + month).toUTCString());
                count = val.split(",").length;
                topLine.removeClass("empty");
                ShowOrderLink(true);
            }
        }
        else {
            //Add to basket
            ShowLoader(a, "to-cart", "to-cart-disabled");
            a.attr("title", "Удалить из корзины");
            a.html(a.html().replace("КУПИТЬ", "Отмена"));
            var val = GetCookie(BasketCookieName);
            if (val == null)
                val = id;
            else
                val += "," + id;
            SetCookie(BasketCookieName, val, new Date(new Date().getTime() + month).toUTCString());
            count = val.split(",").length;
            topLine.removeClass("empty");
            ShowOrderLink(true);
            pageTracker._trackPageview("/tovar_v_korzine");
        }

        $(".top-basket-count").html(count);
        return false;
    });
}

function ShowLoader(element, beforeClass, afterClass) {    
        var e = $(element);
        e.hide();
        e.after("<span class='to-cart-loading'></span>");
        e.removeClass(beforeClass);
        e.queue(function() {
            setTimeout(function() {
                e.next().remove();
                e.addClass(afterClass);
                e.fadeIn();
                e.dequeue();
                Notify(afterClass);
            }, 300);
        });    
}

function Notify(afterClass) {
    if (isShowNotify) {
        isShowNotify = false;
        if (afterClass.match('dis')) {
            $("#divPopupToCart").fadeIn();
            //$.notifier("Товар успешно добавлен в корзину", "success");
        }
        else {
            $.notifier("Товар успешно удален из корзины", "success");
        }
    }
}

function ShowOrderLink(isShow) {
    if (isShow) {
        $("span[class=hiding order]").html('<a href="/order/">Оформить заказ</a>');
    }
    else {
        $("span[class=hiding order]").html("");
    }
}
