jQuery(function() {
	$(".menu ul").each(function(){
		if($(this).children("li").length == 0) $(this).hide();
	});
});

var rc_total_count = 0;

function recalc_offer_price(){
	rc_total_count = 0;

	$('.product-variants input').each(function(){
		var val = $(this).val();
		if (isNaN(val) || val < 0){ 
			val = 0; $(this).val(val);
    	} 
		rc_total_count = rc_total_count + $(this).attr("price")*val;
	});
	$("#create_order").css("opacity",rc_total_count > 0 ? 1 : 0.5).attr("disabled",rc_total_count > 0 ? "" : "disabled");
	$("#price-field").html( InSales.formatMoney(rc_total_count, cv_currency_format));
}

function cartTriggers(){
	/* РЈРґР°Р»РµРЅРёРµ */
	$(".cart-table .del a").each(function(i) {
	    if ( $(this).attr('processed') ) { return; }
		$(this).attr('processed',true);
		
		$(this).bind("click dblclick", function(e){
		    if ( $(this).attr('in_process') ) { return; }
			$(this).attr('in_process',true);
			
		    e.preventDefault();
			var link = $(this);
			var item_row = link.parents("tr:first");
			var fields =  new Object;
			fields['_method'] = 'delete';
			id = link.attr("id").replace("delete_","");
			var path   = '/cart_items/'+id+'.json';
			show_preloader(); // РџРѕРєР°Р·С‹РІР°РµРј РїСЂРµР»РѕР°РґРµСЂ
			$.ajax({
			    url:      path,
			    type:     'post',
			    data:     fields,
			    dataType: 'json',
			    success:  function(response) {
					if( !$(".cart-table td").length ){
						$("#cartform").hide();
						$(".cart-empty").show();
					}
					hide_preloader(); // РЈР±РёСЂР°РµРј РїСЂРµР»РѕР°РґРµСЂ
					item_row.slideUp("normal", function(){ item_row.remove(); });
					$('.total_amount').html(InSales.formatMoney(response.total_price, cv_currency_format));
			    },
			    error:  function(response) { 
			    	if ( $(this).attr('in_process') ) { return; }
			    	// alert("РїСЂРѕРёР·РѕС€Р»Р° РѕС€РёР±РєР°");
		    	}
			});
			return false;
		});
	});

    // РџРµСЂРµСЃС‡РёС‚Р°С‚СЊ
    $(".cart-table .quantity input").each(function() {
        if ( $(this).attr('processed') ) { return; }
		$(this).attr('processed',true);
		$(this).bind("change keyup", function() {
		    val = $(this).val();
		    if (isNaN(val) || val <= 0){ 
	    		val = 1;
	    		$(this).val(val);
	    	}
	    	$(this).parents("tr:first").find(".total-price strong").html( InSales.formatMoney(val*$(this).attr('price'), cv_currency_format) )
	    	recalculate_order(); 
		});
    });
    
    
    function recalculate_order() {
	  var fields =  new Object;
	  fields = $('#cartform').serialize();
	  //var path   = $('#cartform').attr('action')+'.json';
      var path = '/cart_items/update_all.json'
	  
	  show_preloader(); // РџРѕРєР°Р·С‹РІР°РµРј РїСЂРµР»РѕР°РґРµСЂ
	  $.ajax({
	      url:      path,
	      type:     'post',
	      data:     fields,
	      dataType: 'json',
	      success:  function(response) {
			  hide_preloader(); // РЈР±РёСЂР°РµРј РїСЂРµР»РѕР°РґРµСЂ		
			  //$(".js_cart_update").hide();
			  $('.total_amount').html( InSales.formatMoney(response.total_price, cv_currency_format) );
	      },
	      error:  function(response) {  alert("РїСЂРѕРёР·РѕС€Р»Р° РѕС€РёР±РєР°"); hide_preloader(); }
	  });
    
    }

}