$(document).ready(function(){

    // Table style
    $(".content .table tr:nth-child(odd)").addClass("odd");
    $("#basket_items table tr:nth-child(odd)").addClass("odd");

    // Disable search
    $('.search-box form').submit(function(){
        if ($('.string',this).val()=='')
        {
            alert('Введите поисковый запрос');
            return false;
        }
    });
    

    // OL style
    $('li',$('.content ol').not('.nostyle')).wrapInner('<span>');

    // Hover
    //    $().hover(function(){
    //        $(this).toggleClass('hover');
    //    });

    // Placeholder
    //    $('input:text,textareaa').placeholder();

    // выбор заказа
    $('#catalog_select form input, #catalog_select form select').bind('change keyup', function(e){
        
        //alert(e.which);
        
        if (
            $(this).is('input[name=order_num]') 
            && 
            (
                (e.type=='keyup' && e.which!=8 && e.which!=0 && e.which!=46 && (e.which<48 || e.which>57)) 
                || 
                e.type=='change'
                )
                )
            return false;
    
        getJson('/json/order_recount/', $(this).parents('form').serialize(), $('#catalog_select .load'));
    });
    
    $('#catalog_select input[name=order_num]').bind('change keyup', function(e){
        recountOrder();
    });
    

    
    $("#catalog_select input[name=order_num]").keypress(function(e){
        if (e.which!=8 && e.which!=0 && e.which!=46 && (e.which<48 || e.which>57))
            return false;  
    });

    $('#catalog_select form').submit(function(){
        getJson('/json/order_send/', $(this).serialize(), $('#catalog_select .load'));
        
        return false;
    });



    // Корзина
    $('#basket_items .count input').keyup(function(e){
        if (e.which!=8 && e.which!=0 && e.which!=46 && (e.which<48 || e.which>57))
            return false; 
        
        getJson('/json/basket_recount/', {
            id: $(this).parents('tr').attr('id'),
            count: $(this).val()
        });        
    });
    
    $('#basket_items .count input').keypress(function(e){
        if (e.which!=8 && e.which!=0 && e.which!=46 && (e.which<48 || e.which>57))
            return false;        
    });
    
    $('#basket_items .count a').click(function(){
        val = $('input',$(this).parents('.count')).val();
        if ($(this).hasClass('min'))
        {
            if (val>1) val = parseFloat(val)-1;
        }
        if ($(this).hasClass('max'))
        {
            val = parseFloat(val)+1;
        }
        $('input',$(this).parents('.count')).val(val);
        
 
        getJson('/json/basket_recount/', {
            id: $(this).parents('tr').attr('id'),
            count: val
        });
        
        
        return false;
    });
    
    $('#basket_items .delete').click(function(){
        getJson('/json/basket_delete/', {
            id: $(this).parents('tr').attr('id')
        });
        return false;
    });

    $('#basket_form form').submit(function(){
        getJson('/json/basket_send/', $(this).serialize(), $('#basket_form .load'));
        return false;
    });
    
    $('#feedback_form form').submit(function(){
        getJson('/json/feedback_send/', $(this).serialize(), $('#feedback_form .load'));
        return false;
    });

});


function recountOrder()
{
    count = parseFloat($('#count_field').text());
    inp = $('#catalog_select input[name=order_num]');
    if (parseFloat(inp.val()) > count)
    {
        inp.val(count);
    }        
}
