/* shop functions */

function intobasket(id)
{
 //$("#buybutton"+id).attr("src","/i/buy_dis.gif");	
 $("#shopindicator").html("<img src=\"/i/ajax/black.gif\" width=\"16\" height=\"16\" alt=\"\" />");
 $.getJSON(
 '/basket',
 {
  id: id,
  into_basket: true
 },
 intobasketaccept
 );	
}

function intobasketaccept(obj)
{
 $("#basketinf").html(obj.basket);
 $("#shopindicator").html("");
 $("#inbasket").html("<a href=\"/basket/\">Товар заказан</a>");
 //$("#buybutton"+obj.id).attr("src","/i/buy.gif");
 if (obj.mode=='delete') alert(obj.msg);
}

function remove_from_basket(id)
{
 $.getJSON(
 '/basket',
 {
  id: id,
  from_basket: true
 },
 frombasketaccept
 );	
}

function frombasketaccept(obj)
{
 //alert(obj.redirect);	
 $("#row"+obj.id).hide();	
 $("#summa").html(obj.summa);
 $("#basketinf").html(obj.basketinf);
 if (obj.redirect==1) window.location = "/";
}

function button_disable(name,src)
{
 $("#"+name).attr("src","/i/"+src);
}

function loadbasketform(alias)
{
 $('#'+alias).ajaxForm(
 { 
  dataType:  'json', 
  success:   processbasketform
 }
 );	
}

function processbasketform(obj)
{
 var str = obj.str;
 var cost = 0;
 var oldcount = 0;
 var id = 0;
 var delta = 0;
 var deltasumma = 0;
 var summa = 0;
 var newoldcount = 0
 var item = new Array();
 str = str.split(',');
 for (var i=0; i < str.length; i++)
 {
  if(str[i])
  { 
   item = str[i].split(';');	
   id = parseInt(item[0]);
   delta = parseInt(item[1]);
   oldcount = parseInt($("#oldcount"+id).get(0).value);
   oldcost = parseInt($("#cost"+id).html());
   newoldcount = oldcount + delta;
   newcost = oldcost*newoldcount/oldcount;
   deltasumma = deltasumma + newcost - oldcost;
   $("#oldcount"+id).attr("value",newoldcount);
   $("#cost"+id).html(newcost);
  } 
 }

 if (deltasumma!=0)
 {
  oldsumma = parseInt($("#summa").html());
  newsumma = oldsumma + deltasumma;
  $("#summa").html(newsumma);
 } 
 $("#recount_summa").attr("src","/i/recount.gif");
}