(function ($) {
    var calculate = 'calculate',
    calcPublic,
    items_count,
    items_price,
    settings,
    defaults = {
        containers: '.cell_gird',
        input_count: '.quantity_inp',
        input_price: '.price_inp',
        input_checkbox: '.quantity_inp ~ input:checkbox',
        items_count_top: '#itogo_c1',
        items_price_top: '#itogo_r1',
        items_count_bottom: '#itogo_c2',
        items_price_bottom: '#itogo_r2',
        class_selected: 'grid_selected',
        use_checkbox: false
    };
    
    function count() {
        var tmp_cnt;
        items_count = 0;
        items_price = 0;
        $(settings.containers).each(function(){
            if($(this).find(settings.input_count).length > 0) {
                tmp_cnt = (!$(this).find(settings.input_count).val())?0:parseInt($(this).find(settings.input_count).val());

                items_count += tmp_cnt;
                items_price += tmp_cnt * parseInt($(this).find(settings.input_price).val());
                if(settings.use_checkbox == true) {
                    if(tmp_cnt > 0) {
                        $(this).find(settings.input_checkbox).attr('checked',true);
                    } else {
                        $(this).find(settings.input_checkbox).attr('checked',false);
                    }
                }
                highlight(this, tmp_cnt);
            }
        });
    }
    
    // If flag > 0 then we need to highlite element
    function highlight(element, flag) {
        var el = $(element);
        if(flag > 0) {
            el.addClass(settings.class_selected);
        } else {
            if(el.hasClass(settings.class_selected)){
                el.removeClass(settings.class_selected);
            }
        }
    }
    
    function drawPriceCount() {
        if((settings.items_count_top != '' && $(settings.items_count_top).length > 0) && (settings.items_price_top != '' && $(settings.items_price_top).length > 0)) {
            $(settings.items_count_top).text(items_count);
            $(settings.items_price_top).text(items_price);
        }
        
        if((settings.items_count_bottom != '' && $(settings.items_count_bottom).length > 0) && (settings.items_price_bottom != '' && $(settings.items_price_bottom).length > 0)) {
            $(settings.items_count_bottom).text(items_count);
            $(settings.items_price_bottom).text(items_price);
        }
    }
    
    calcPublic = $.fn.calculate = function (options) {
        element = this;
        $(element).data(calculate, $.extend({}, defaults, options));
        settings = $(element).data(calculate);
        
        count();
        drawPriceCount();
        
        $(settings.input_count).keyup(function() {
            count();
            drawPriceCount();
        });
        
        if(settings.use_checkbox === true) {
            $(settings.input_checkbox).click(function () {
                if($(this).attr('checked') === true) {
                    $(this).prev(settings.input_count).val(1);
                } else {
                    $(this).prev(settings.input_count).val(0);
                }
                count();
                drawPriceCount();
            });
        }
    };
    
})(jQuery);

        
function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

function checkLength(o,n,min,max) {
    
    if ( o.val().length > max || o.val().length < min ) {
        o.addClass('ui-state-error');
        updateTips("Поле &laquo;" + n + "&raquo; может содержать от "+min+" до "+max+" знаков");
        return false;
    } else {
        return true;
    }

}

function checkRegexp(o,regexp,n) {

    if ( !( regexp.test( o.val() ) ) ) {
        o.addClass('ui-state-error');
        updateTips(n);
        return false;
    } else {
        return true;
    }

}

function updateTips(t) {
    $("#validateTips").html('<span class="required">' + t + '</span>').fadeIn("slow");
}

$('document').ready(function() {

    var liveInternet = "http://counter.yadro.ru/hit?t44.3;r"
        +escape(document.referrer)+((typeof(screen)=="undefined")?"":";s"
        +screen.width+"*"+screen.height+"*"
        +(screen.colorDepth?screen.colorDepth:screen.pixelDepth))
        +";u"+escape(document.URL)+";"+Math.random();
     
    $('#live_internet').attr('src', liveInternet);
    
    $('#rambler_1').attr('src', 'http://counter.rambler.ru/top100.cnt?943361');
    $('#rambler_2').attr('src', 'http://top100-images.rambler.ru/top100/banner-88x31-rambler-gray2.gif');
  
    
    if($("a[rel='smallpic']").length > 0) {
        $("a[rel='smallpic']").colorbox({photo: true, width:"60%"}); //{width:"60%", height:"60%"});
    }
        
    if($("a[rel='mainpic']").length > 0) {
        $("a[rel='mainpic']").colorbox({photo: true, width:"60%"}); //{width:"60%", height:"60%"});
    }

    $('#srcicon_id').click(function () {
        $('#searchform').submit()
    });
    
    if($('#select_fab').length > 0) {
        $('#select_fab').change(function () {
            window.open($("#select_fab option:selected").val(),'_top')
        })
    }

    if($('#new_grid').length > 0){
        $('#new_grid').calculate();
    } else if($('#tbody_price').length > 0) {
        $('#tbody_price').calculate({
            containers: '#tbody_price tr', 
            items_count_top: '',
            items_price_top: '',
            items_count_bottom: '#itogo_c',
            items_price_bottom: '#itogo_r',
            use_checkbox: true
        });
    }
    
    if($(".info_on").length > 0) {
        $(".info_on").hover(
            function (e) {
                var info = $('#info');
                if(info!='') {
                    if($(this).attr('alt')!='') {
                        info.html($(this).attr('alt'));
                        $(this).attr('alt','');
                    }
                    info.css('left',e.clientX+(-120)+'px');
                    info.css('top',e.pageY+30+'px');
                }
            }, 
            function (e) {
                var info = $('#info');
                if(info!='') {
                    $(this).attr('alt',info.html());
                    info.html('');
                    info.css('left','0px');
                    info.css('top','-1000px');
                }
            }
        );
    }       

    $("#nojs_buttons").hide();

    if($("#cart_table").length > 0) {
        $("#cart_table .cart_del").click(function () {
            var row = $(this).parent().parent();
            row.css("background-color", "#CC3300");
            row.animate({opacity: 0.2}, 300, "linear", function() {

                if(row.next("tr").children("td.comment").length > 0) { 
                    row.next("tr").remove();
                }
                row.remove();
                cart_count();
                $.post('/cart.php',{
                        "ajax": "1",
                        "do": "1",
                        "action": "del_prod_from_cart",
                        "IDPROD": row.find("input.idp:hidden").val()
                    });
            });
            return false;
        });
        
        $("#cart_table .list_quant input").keyup(function () {
            var val = parseInt($(this).val());
            
            if(val < 1) {
                val = 1;
                $(this).val(val);
            }
            
            $("#frm_action").val("cart_update");
            var myform = $("#cart_form").serialize();
            myform += "&ajax=1";
            cart_count();
            $.post('/cart.php',myform);
            
            $("#frm_action").val("check_cart");
        });
    }
    
    function cart_count() {
        var total = 0;
        
        $("#cart_table tr").each(function(){
            var local_price = $(this).children(".list_price").text(),
                tmp_price;
            if(local_price) {
                tmp_price = parseInt(local_price) * parseInt($(this).find(".list_quant input").val());
                $(this).find(".list_price_total").text(tmp_price);
                total += tmp_price;
            }
        })
        $("#cart_total").text("Итого: " + total + " р.");
    }
})