var default_ch = 0;
function blocks_height () {
    var height = $("#leftblock").height();
    if (default_ch == 0) {
        default_ch = $("#content").height();
    };
    if ($("#content").innerHeight() < height) {
        $("#content").height(height);
    } else if ($("#content").innerHeight() > height) {
        $("#content").height(height > default_ch ? height : default_ch);
    };
};
function to_wish (id) {
    var id = parseInt(id || 0);
    if (id > 0) {
        $.get(
            SITE_URL + 'ajax/wish/' + id + '/',
            function(data) {
                alert(data.result==true
                    ? 'Мы постараемся оповестить магазин о Вашем желании сотрудничать с ним!'
                    : 'Сигнал уже был принят ранее :)'
                );
            },
            'json'
        );
    };
};
$(document).ready(function() {
    blocks_height();
    if($(this).val()==$(this).attr('title')) {
        $(this).addClass('gray');
    };
    $('a[href="#"]').addClass("dotted");
    $('#shopsList,#statList').show();
    blocks_height();
    $('#shopsList,#statList').animate({opacity: '0.2'}, 500);
    $('#shopsList,#statList').mouseover(function () {
        $('#shopsList,#statList').stop().animate({opacity: '0.9'}, 500);
    });
    $('#shopsList,#statList').mouseout(function () {
        $('#shopsList,#statList').stop().animate({opacity: '0.2'}, 500);
    });
    setInterval(function () {
        var itog = orders.getItog();
        $("#ordersItems").html(itog.items);
        $("#ordersSumm").html(itog.summ);
    },300);
    setInterval(function(){blocks_height();},200);
});
$(".cusel").each(function () {
    var w = parseInt(jQuery(this).width()),
        scrollPanel = jQuery(this).find(".cusel-scroll-pane");
    if(w>=scrollPanel.width()) {
        $(this).find(".jScrollPaneContainer").width(w);
        scrollPanel.width(w);
    };
});
var params = {
    changedEl: ".lineForm select",
    visRows: 10,
    scrollArrows: true
}
//cuSel(params);
var cookie = {
    set: function (name, value, expiredays, path, domain, secure) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        var expires = ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
        document.cookie =
            name + "=" + escape(value) +
            ((expires) ? "; expires=" + expires : "") +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : "");
    },
    get: function (name) {
        var cookie = " " + document.cookie;
        var search = " " + name + "=";
        var setStr = null;
        var offset = 0;
        var end = 0;
        if (cookie.length > 0) {
            offset = cookie.indexOf(search);
            if (offset != -1) {
                offset += search.length;
                end = cookie.indexOf(";", offset)
                if (end == -1) {
                    end = cookie.length;
                };
                setStr = unescape(cookie.substring(offset, end));
            };
        };
        return(setStr);
    },
    del: function (name, path, domain) {
        if (this.get(name)) {
            document.cookie =
                name + "=" +
                ";expires=Thu, 01-Jan-1970 00:00:01 GMT" +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "");
        };
    },
    getList: function () {
        var list = { };
        if (document.cookie && document.cookie != '') {
            var split = document.cookie.split(';');
            for (var i = 0; i < split.length; i++) {
                var name_value = split[i].split("=");
                name_value[0] = name_value[0].replace(/^ /, '');
                list[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
            };
        };
        return list;
    }
};
var orders = {
    add: function (id, price, qty) {
        var id = parseInt(id || 0),
        price = parseFloat(price || 0),
        qty = parseInt(qty || 1);
        if (id == 0) {
            return false;
        };
        qty = parseInt((cookie.get("item_"+id) || "0:0").split(":",2)) + qty;
        cookie.set("item_"+id, qty+":"+price, 10, "/");
    },
    getItog: function () {
        var list = cookie.getList();
        var items = 0;
        var summ = 0;
        for (var i in list) {
            if (i.substr(0,5) == "item_") {
                var v = list[i].split(":",2);
                items = items + parseInt(v[0] || 0);
                summ = summ + (parseFloat(v[1] || 0)*parseInt(v[0] || 0));
            }
        };
        return {items: items, summ: summ};
    }
};

