$(document).ready(function()
{

 $('.buy').live("click",function(){
 	var el = this;
  	var params = $(el).attr('id').toString();
  	params = params.split("_");
  	var id = params[1];
  	$("#indicator"+id).html("<img src=\"/i/ajax/orange.gif\" width=\"16\" height=\"16\" alt=\"\" />");
 	$.getJSON(
 		'/basket',
 	{
  		id: id,
  		count: $("#count"+id).get(0).value,
  		into_basket: true
 	},
 	function (obj) {
 		//$("#summa").html(obj.summa);
 		$("#count").html(obj.count);
 		$("#indicator"+id).html("");
 		
 		if (obj.mode=='add') {
 			//$("#intobasket_"+id).hide("slow");
    		$("#inbasket"+id).show("slow");
 		} else if (obj.mode=='delete') {
 			alert(obj.msg);
 		} else if (obj.mode=='plus') {
 			alert(obj.msg);
 			$("#count"+id).attr("value","1");
 		}
 	}); 	
 });

});

function remove_from_basket(id)
{
 var delete_note = 'Удалить товар из корзины?';
 if (confirm(delete_note) == false) return false;
 else {
 	$.getJSON(
 	'/basket',
 	{
  	id: id,
  	from_basket: true
 	},
 	frombasketaccept
 	);
 }		
}

function frombasketaccept(obj)
{
 //alert(obj.redirect);	
 $("#count").html(obj.count);
 $("#basket-count").html(obj.count);
 $("#good").html(obj.word);
 $("#summa").html(obj.summa);
 
 $("#row"+obj.id).remove();	
 
 $("#basketsumma").html(obj.summa);
 $("#basketsumma2").html(obj.summa);
 $("#basketinf").html(obj.basketinf);
 $("#ordersumma").html(obj.sumdelivery);
 if (obj.redirect==1) window.location = "/";
}

function loadbasketform(alias)
{
 $('#'+alias).ajaxForm(
 { 
  dataType:  'json', 
  beforeSubmit: function(){
  		$("#recount_animate").html("<img src=\"/i/ajax/orange.gif\" width=\"16\" height=\"16\" alt=\"\" />");
  },
  success:   processbasketform
 }
 );	
}

function processbasketform(obj)
{	
 var str = obj.str;
 var id = 0;
 var summa = 0;
 var item = new Array();
 str = str.split('&');
 for (var i=0; i < str.length; i++)
 {
  if(str[i])
  { 
   item = str[i].split(';');	
   id = item[0];
   summa = item[1];
   $("#cost"+id).html(summa);
  } 
 }
 $("#basketsumma").html(obj.summa);
 $("#basketsumma2").html(obj.summa);
 $("#summa").html(obj.summa);
 $("#delivery").html(obj.delivery);
 $("#ordersumma").html(obj.sumdelivery);
 $("#recount_animate").empty();
}
