var inter;
var inter2;

function animateNumbers(e,num){
  var cost=$(e).html();
  cost=cost.replace(" ","");
  cost=cost.replace(" ","");
  cost=parseInt(cost);
  lastcost=num;
  var diff=cost-lastcost;
  
  cost=cost-(diff/10);
  cost=parseInt(cost);
  
  if((diff<10&&diff>0)||(diff>-10&&diff<0)||diff==0){
    cost=lastcost;
    clearInterval(inter);
  }
  
  $(e).html(number_format(cost,0,'',' '));
}

function animateCost(num){
  var cost=$('#cartcost').html();
  cost=cost.replace(" ","");
  cost=cost.replace(" ","");
  cost=parseInt(cost);
  lastcost=num;
  var diff=cost-lastcost;
  
  cost=cost-(diff/10);
  cost=parseInt(cost);
  
  if((diff<10&&diff>0)||(diff>-10&&diff<0)||diff==0){
    cost=lastcost;
    clearInterval(inter2);
  }
  
  $('#cartcost').html(number_format(cost,0,'',' '));
}

function closeCart(){
  var cols=$("#cart_block .item-tr");
  if(cols.length==0){
    $(".cart_block2").animate({opacity: 'hide'});
  }
}

function number_format( number, decimals, dec_point, thousands_sep ) {	// Format a number with grouped thousands

	var i, j, kw, kd, km;

	if( isNaN(decimals = Math.abs(decimals)) ){
		decimals = 2;
	}
	if( dec_point == undefined ){
		dec_point = ",";
	}
	if( thousands_sep == undefined ){
		thousands_sep = ".";
	}

	i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

	if( (j = i.length) > 3 ){
		j = j % 3;
	} else{
		j = 0;
	}

	km = (j ? i.substr(0, j) + thousands_sep : "");
	kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
	kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");

	return km + kw + kd;
}



$(function(){
  var body_height = $(window).height();
  var content_height = $(".container").height();

  if (content_height < body_height ) {
    $(".container").css("minHeight", body_height+"px");
    if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)){
      $(".container").css("height", body_height+"px"); 
    }
  }
  
  
  $('.left_menu table a').click(function() {
    if($(this).attr('href')!="#") return true;
    var n =  $(this).attr("class");
    if (n == "act") { 
      $(this).removeClass("act"); 
      $(this).parent("td").parent("tr").parent("tbody").parent("table").next("ul").animate({height: 'hide'  }, 300);        
      
      return false
    } else {
      $(".left_menu  table  a").removeClass("act");
      $(this).addClass("act");
      $(".left_menu ul").animate({height: 'hide'  }, 300);
      $(this).parent("td").parent("tr").parent("tbody").parent("table").next("ul").animate({height: 'show'  }, 300);
      return false
    }
  });

  $(".cart_link").click(function(){
    var cols=$("#cart_block .item-tr");
    var rel=$(this).attr('rel');
    if(cols.length>0&&rel!='none'){
      $(".cart_block2").animate({opacity: 'toggle'});
    }
  });
  $(".search_input").click(function(){
      ish_val = $(this).val();
      if (ish_val=="поиск"){
        $(this).val("");
      }else{}
  });
  $(".search_input").blur(function() {
    var temp_val = $(this).val();
    if(temp_val == ""){  
      $(this).val(""+ish_val);
    }
  });
  $(".addtocart").click(function(){
    var item=$(this).attr('rel');
    var col=$(".itemcol").val();
    $("#cartmodal").modal({opacity:20});
    $.post("/ajax/cart.php",{'act':'add','item':item,'col':col},function(data){
      $(".c_text").html(data);
      $.post("/ajax/cart.php",{'act':'block'},function(data){
        $("#cart_block .table2 tbody").html(data);
      });
      $.post("/ajax/cart.php",{'act':'col'},function(data){
        clearInterval(inter2);
        inter2=setInterval('animateCost('+data+')',25);
      });
    });
    return false;
  });
  
  $("#cart_block input").keydown(function(e){
    var col=$(this).val();
    if(parseInt(col)!=col||col==""){
      return false;
    }
    
    if(e.keyCode==38){
      if(col<999){
        col++;
        $(this).val(col);
      }
      return false;
    }
    if(e.keyCode==40){
      if(col>1){
        col--;
        $(this).val(col);
      }
      return false;
    }
    
  });
  
  var col=$(".itemcol").val();
  col=parseInt(col);
  $(".itemcol").val(col);
  
  $("#cart_block input").each(function(){
    var col=$(this).val();
    col=parseInt(col);
    $(this).val(col);
  });

  
  $("#cart_block .delitem").click(function(){
    if(confirm('Вы действительно хотите удалить этот товар из корзины?')){
      var id=$(this).attr('rel');
      $("#itemtr"+id).animate({'opacity':'toggle'}).remove();
      $.post("/ajax/cart.php",{'act':'del','item':id},function(data){
        clearInterval(inter2);
        inter2=setInterval('animateCost('+data+')',25);
      })
      closeCart();
      $.post("/ajax/cart.php",{'act':'refresh'},function(data){
        $(".c_text").html(data);
      });
    }
    return false;
    
  });
  
  $("#cart_block input").keyup(function(){
    var cost=$(this).attr('cost');
    var id=$(this).attr('rel');
    var col=$(this).val();
    var element=".cost"+id;
    if(col!=""){
      col=parseInt(col);
      if(col<0) col=1;
      $(this).val(col);
      
      var newcost=col*cost;
      
      $.post("/ajax/cart.php",{'act':'col','col':col,'item':id},function(data){
        clearInterval(inter2);
        inter2=setInterval('animateCost('+data+')',25);
      });
      
      $.post("/ajax/cart.php",{'act':'refresh'},function(data){
        $(".c_text").html(data);
      });
      
      clearInterval(inter);
      inter=setInterval('animateNumbers("'+element+'",'+newcost+')',25);
    }
  });
  $("#cart_block2 input").keyup(function(){
    var cost=$(this).attr('cost');
    var id=$(this).attr('rel');
    var col=$(this).val();
    var element=".cost"+id;
    if(col!=""){
      col=parseInt(col);
      if(col<0) col=1;
      $(this).val(col);
      
      var newcost=col*cost;
      
      $.post("/ajax/cart.php",{'act':'col','col':col,'item':id},function(data){
        clearInterval(inter2);
        inter2=setInterval('animateCost('+data+')',25);
      
        $.post("/ajax/cart.php",{'act':'refresh'},function(data){
          $(".c_text").html(data);
        });
        
      });
      
      clearInterval(inter);
      inter=setInterval('animateNumbers("'+element+'",'+newcost+')',25);
    }
  });
  $("#cart_block2 input").keydown(function(e){
    var col=$(this).val();
    if((e.keyCode>47&&e.keyCode<58)||e.keyCode==8||e.keyCode==46||(e.keyCode>36&&e.keyCode<41)){
      if(e.keyCode==38){
        if(col<999){
          col++;
          $(this).val(col);
        }
        return false;
      }
      if(e.keyCode==40){
        if(col>1){
          col--;
          $(this).val(col);
        }
        return false;
      }
    }else{
      return false;
    }
  });
  
  $("#cart_block2 .delitem").click(function(){
    if(confirm('Вы действительно хотите удалить этот товар из корзины?')){
      var id=$(this).attr('rel');
      $("#itemtr"+id).animate({'opacity':'toggle'});
      $.post("/ajax/cart.php",{'act':'del','item':id},function(data){
        clearInterval(inter2);
        inter2=setInterval('animateCost('+data+')',25);
      })
    }
  });
});



