jQuery(document).ready(function(){ 

(function($) {
    $.fn.inputChange = function (callback)
    {
     return this.bind(
     {
      mousedown: callback,
      keyup: callback,
      blur: callback
     });
    };
})(jQuery);

getsum();

$('.pricefield').inputChange(function(){getsum();});

$('.purchase').live('click', function() {
    id = $(this).attr('id');
    addProduct({id:id,type:1,variant:1,count:1});
    return false;
});

$('.refuse').live('click', function() {
    id = $(this).attr('id');
    addProduct({id:id,type:1,variant:1,count:0});
    $(this).parents('tr:first').remove();
    if($('div#order table tr').length <=2) {window.location = 'http://'+window.location.host;}
    getsum();
    return false;
});

$('.toggle').toggle( function() {
    $('#pass').hide();
    $('#LoginForm_recover').val(1);
    return false;
}, function() {
    $('#pass').show();
    $('#LoginForm_recover').val(0);
    return false;
});

$('.addProductList').live('click', function() {
    //title = $(this).prev().children('option:selected').html();
    id = $('#showcase_product').val();
    $.ajax({
        type: "POST",
        url: 'http://'+window.location.host+'/admin/addproductlist/'+id,
        data: {id:id},
        cache: false,
        success: function(data)
        {
            $('.ui-sortable').append(data);
        }
    });    
    
});

$('.del_li').live('click', function() {
    $(this).parent('li').remove();
});

$('.addInputFile').live('click', function() {
    $(this).prev().after('<input type="file" id="ProductPhoto_title" name="ProductPhoto[][title]"><br/>');
    return false;
})

});

function getsum() {
    sum=0;
    $('.pricefield').each(function() {
        sum+=parseFloat($(this).val())*parseFloat($(this).parent('td').next().html());
        });
    $('.order_sum span').html(sum+' руб.');
}

function addProduct(arr) {
    $.ajax({
        type: "POST",
        url: 'http://'+window.location.host+'/catalogue/addProduct',
        dataType: 'json',
        data: arr,
        cache: false,
        success: function(data)
        {
            $('#cart').html(data.message);
            if(data.error) {
                alert('Для того чтобы оформить заказ вам необходимо авторизоваться или зарегистрироваться');
            }
        }
    });
}
