
$(function() {

    //set common heigths of top products
    $("#top-products li img").load(function() {
        var maxHeight = 0;
        $.each($("#top-products li img"), function() {
            maxHeight = Math.max(maxHeight, $(this).height());
        });
        if (Number(maxHeight)) {
            $.each($("#top-products li img"), function() {
                $(this).parents("div.product-height").height(maxHeight);
            });
        }
    });

    //init jStore
    $.extend(jQuery.jStore.defaults, {
        project: 'product-rating',
        engine: 'flash',
        flash: '/js/jStore/jStore.Flash.html'
    });

    $.jStore.ready(function(engine){
        $.jStore.flashReady(function(){
            engine.ready(function(){
                $.each($("#top-products input"), function() {
                    if ($.jStore.get('prod_' + $(this).val())) {
                        $.cookie('prod_' + $(this).val(), 1, { expires: 365, domain: '.306.xaratt.com', path: '/' });
                        $(this).remove();
                    }
                });
            });
        });
    });
    $.jStore.load();

    $("#top-products div.button input").click(function() {
        var currRating = Number($(this).parent().parent().find(".rating-value").text());
        $(this).parent().parent().find(".rating-value").text(currRating + 1);

        var currWidth = $(this).parent().parent().find("hr").width();
        $(this).parent().parent().find("hr").width(currWidth + 3);

        var productId = $(this).val();
        $.post('/ru/product/rate', { id: productId }, function() {
            $.jStore.store('prod_' + productId, 1);
        });

        $(this).remove();
    });
});

