jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var _cookieName = "basket";
var _options = { path: '/', expires: 0.5 };

function SetCookieValue(key) {
    if (key != '') {
        if ($.cookie(_cookieName) != null) {
            var cookieList = $.cookie(_cookieName).trim().split(".");
            var flag = 0;
            for (i = 0; i < cookieList.length; i++) {
                var cookie = cookieList[i].split("-");
                if (cookie[0] == key) {
                    cookie[1] = parseInt(cookie[1]) + 1;
                    cookieList[i] = cookie.join("-");
                    flag = 1;
                    break;
                }
            }
            if (flag == 1)
                $.cookie(_cookieName, cookieList.join("."), _options);
            else {
                $.cookie(_cookieName, cookieList.join(".") + "." + key + "-1", _options);
            }
        }
        else {
            $.cookie(_cookieName, key + "-1", _options);
        }
        $.jGrowl("Продукт успешно добавлен в корзину.<br />" +
            "<a title='Оформить заказ' href='http://www.dlcom.by/catalog/basket/'>Оформить заказ</a>");
    }
}
$(document).ready(function () { UpdateBacket(); });

function UpdateBacket() {
    if ($.cookie(_cookieName) != null) {
        $("#hlBacket").attr("class", "active");

        $("#hlBacket b.count").text(GetOrderCount() + " тов.");
    }
    else {
        $("#hlBacket").className = '';
        $("#hlBacket b.count").text("0 тов.");
    }
    return false;
}
function GetOrderCount() {
    var order_count = 0;
    if ($.cookie(_cookieName) != null) {

        var cookieList = $.cookie(_cookieName).trim().split(".");

        for (i = 0; i < cookieList.length; i++) {
            var cookie = cookieList[i].split("-");
            order_count = order_count + parseInt(cookie[1]);
        }
    }
    return order_count;
}
