$(document).ready(function()
{
 $(".email").each(function()
 {
  var e = $(this).attr("href") + "@" + $(this).text();
  $(this).text(e);
  $(this).attr("href", "mailto:" + e);
 }
 );
}
);

function loadform(alias)
{
 $('#'+alias).ajaxForm(
 { 
  dataType:  'json', 
  success:   processJsonForm
 }
 );	
}

function processJsonForm(data)
{
 $("#inputresult").html("<div class=\"error\"><div class=\"errorpad\">"+data.text+"</div></div>");	
 if(data.done==1) 
 {
  $("#submit_data").attr("disabled","disabled");
  // ставим задержку, чтобы пользователь мог прочитать надпись
  setTimeout(function(){window.location = data.link;}, 8000);
 }
 else
 {
  $("#submit_data").attr("disabled","");	
 }
 $("#formajaxloader").empty();
}

function loadindicator()
{
 $("#formajaxloader").html("<div style=\"position:absolute;top:5px;left:50%;width:16px;height:16px\"><img src=\"/i/ajax/orange.gif\" width=\"16\" height=\"16\" alt=\"\" /></div>");
}

/* считаем клики по баннерам */
function CountClick(id,link)
{
 $.getJSON(
 '/ajax',
 {
  id: id,
  link: link,
  count_clicks: true
 },
 function(obj)
 {
  //window.location = obj.result;
  window.open(obj.result);
 }
 );	
}

/* shop functions */

function intobasket(id)
{
 $("#buybutton"+id).attr("src","/i/buy_dis.gif");	
 $.getJSON(
 '/basket',
 {
  id: id,
  into_basket: true
 },
 intobasketaccept
 );	
}

function intobasketaccept(obj)
{
 $("#basketinf").html(obj.basket);
 $("#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(';');	
   // item[0] - это номер строчки
   // item[1] - это дельта (разница между новым количеством и стрым) 
   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");
 // Открываем кнопочку для дальнейших действий
}

/* imgs manipulation */
function showdopimg(id,i,type)
{
 $.getJSON(
 '/catalogue',
 {
  block: i,	
  dopimg: id,
  mainimg: $("#mainimgid").get(0).value,
  maintype: $("#mainimgtype").get(0).value,
  type: type,
  show_img: true
 },
 showimgaccept
 );
}

function showimgaccept(obj)
{
 $("#mainimgtype").attr("value",obj.mainimgtype);
 $("#mainimgid").attr("value",obj.mainimgid);
 $("#bigger").attr("href",obj.link);
 $("#mainimg").html(obj.mainimg); 
 $("#dopimg"+obj.block).html(obj.dopimg);  
}

function send_to_friend_form(param)
{
 var top = $("#letter").offset().top;
 var left = $("#letter").offset().left;
 top = top + 25;
 left = left + 4;
 $("#delivery_form").css(
  {
   top: top+"px",
   left: left+"px"	
  }
 )	;
 $("#delivery_form").show();
 $("#delivery_form").html("<div style=\"position:absolute;top:38px;left:84px;width:16px;height:16px\"><img src=\"/i/ajax/gray.gif\" width=\"16\" height=\"16\" alt=\"\" /></div>");
 $.getJSON(
 '/ajax',
 {
  draw_friend_form: true
 },
 function(obj)
 {
  $("#delivery_form").html(obj.html);
 }
 );	
}

function send_to_friend()
{
 var email = $("#d_email").get(0).value;
 var name = $("#d_name").get(0).value;
 $("#delivery_form").html("<div style=\"position:absolute;top:38px;left:84px;width:16px;height:16px\"><img src=\"/i/ajax/gray.gif\" width=\"16\" height=\"16\" alt=\"\" /></div>");
 $.getJSON(
 '/ajax',
 {
  name: name,	
  email: email,
  send_to_friend: true
 },
 function(obj)
 {
  $("#delivery_form").html(obj.html);
 }
 );		
}

function catalogue_vote(param,id)
{
 $.getJSON(
 '/ajax',
 {
  param: param,
  id: id,
  submit_vote: true
 },
 getvote
 );
}

function getvote(obj)
{
 if (obj.done==1)
 {	
  $("#raiting"+obj.id).html('<b>' + obj.rating + '</b>' + obj.text);
 }
 else
 {
  alert(obj.text);	
 }
}

function showimg(id)
{
 $.getJSON(
 '/catalogue',
 {
  id: id,
  show_img: true
 },
 function(obj)
 {
  var height = $("#page").height();	
  var width = $("#page").width();	
  $("#bigimgfon").css(
  {
   width: width+"px",
   height:height+"px"
  }
  )	
  $("#bigimgfon").show();
  $("#bigimg").show();
  $("#bigimg").html(obj.html);
 }
 );	
}