//
//========================================
var product_in_cart = false;
var product_id = -1;
var cart_is_busy = false;


//
//========================================
function load_cart (){
  cart_action('/shop/cart/ajax');
}


//
//========================================
function cart_action(ajurl){

  if (!cart_is_busy){
    cart_is_busy = true;
    jQuery("#cart").slideUp('fast', function(){ jQuery.getJSON(ajurl, {}, cart_parser, 'xml'); });
  }

  return false;

}

//
//========================================
function cart_parser(data){

  product_in_cart = false;
  var s = "", si = "";

  //
  if (data.length>1){

    var total = 0;
    var qtotal = 0;

    s = "<div id='cartlist'><h3>Содержимое корзины</h3><p class='total'>" + data[0] + "</p>"; 

    for (var i=1; i<data.length; i++){

      //
      si += "<li class='" + ((product_id==Number(data[i].id))?"this":"other") + "'>" + (i) + ". ";
      si += "<a href='" + data[i].url + "'>" + data[i].name + "</a>";
      si += "<em>" + data[i].price + "р. &times; " +  data[i].quantity + " = " + Number(data[i].price) * Number(data[i].quantity) + "р. ";

      si += "<a href='#' class='button green' onclick='return add_to_cart(" + data[i].id + ");' title='увеличить количество'>+</a>\n";
      si += "<a href='#' class='button' onclick='return sub_to_cart(" + data[i].id + ");' title='уменьшить количество'>&minus;</a>\n";

      si += "</em></li>";

      if (Number(data[i].id)==product_id) product_in_cart=true;

      jQuery('#prodboxadd' + data[i].id).hide();

    }

    if (1 || product_id>=0) s += "<ul>" + si + "</ul>";

    s += "</div>\n<a href='/shop/cart/' class='bigbutton yellow'>оформить&nbsp;заказ<em class='r'>&rarr;</em></a>";

  }else{

    s = "<div id='cartlist'><h3>Содержимое корзины</h3><p class='total'>Корзина пока пуста!</p></div>";

  }

  if (product_in_cart){
    s = "<p>этот товар уже в корзине!</p>" + s;
  }else{
    if (product_id>=0){
      if (product_deliverable) s = "<a href='#' onclick='return add_to_cart();' class='bigbutton green'>добавить в корзину<em>+</em></a>" + s;
      else s = "<p class='sorry'>Извините, товар временно недоступен для заказа!</p>" + s;
    }
  }

  jQuery("#cart").html(s);

  cart_is_busy = false;

  jQuery("#cart").slideDown('slow');

}

//
//========================================
function add_to_cart(pid){

  if (!pid) pid = Number(product_id);

  cart_action('/shop/cart/add/' + pid);
  return false;

}

//
//========================================
function sub_to_cart(pid){

  if (!pid) pid = Number(product_id);

  jQuery('#prodboxadd' + pid).show();
  cart_action('/shop/cart/sub/' + pid);
  return false;

}

//
//========================================
function remove_from_cart(pid){

  jQuery('#prodboxadd' + pid).show('slow');
  cart_action('/shop/cart/remove/' + Number(pid));
  return false;

}

//
//========================================
function submit_cart_form(){

  document.getElementById("cartproducts").submit();
  return false;

}

//
//========================================
function submit_feedback_form(){

  //
  var get = location.search;
  try { if (get.match(new RegExp('^.order.[0-9]+$'))) get = get.replace('?order=', ''); }
  catch(e) {}

  //
  document.getElementById("fbhp").value = 1;
  
  //
  document.getElementById("feedback").submit();
  
  return false;

}

//
//========================================
function submit_cart_form_by_field(f,e){

  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;
  else return true;

  if (keycode == 13){
    f.form.submit();
    return false;
  }

  return true;

}

//
//========================================
function init_cartform(){

  jQuery('#update_cart').css('opacity', '0.3');

  if (cart_total<cart_minimal){
    jQuery('#place_order').css('opacity', '0.3');
    jQuery('#cartform_message').html("<em>Внимание!</em><br/>Минимальная сумма заказа &mdash; " + cart_minimal + " рублей!<br/> Сумма вашего заказа пока только " + cart_total + " рублей.");
  }else{
    jQuery('#cartform_message').hide();
  }


  return false;

}

//
//========================================
function goto_order_page(){

  if (cart_total<cart_minimal){
    alert("Внимание!\n\nМинимальная сумма заказа -- " + cart_minimal + " рублей!\n\nСумма вашего заказа -- " + cart_total + " рублей.\n");
    return false;
  }else{
    return true;
  }

}

//
//========================================
function enable_update_cart() {

  jQuery('#update_cart').css('opacity', '1');
  jQuery('#cartform_message')
    .html("Если Вы изменили количество или отметили товар для удаления &mdash; нажмите &laquo;сохранить изменения&raquo;.")
    .fadeIn();

}

//
//========================================
function init_category(){

  load_cart();
  return false;

}

//
//========================================
function close_pixa() {
  try { jQuery('#pixapop').remove();  }
  catch(e) {}
}

//
//========================================
function show_pixa(url, w, h, label){

  //
  var ww = w;
  var hh = h;
  var winW = jQuery(window).width();
  var winH = jQuery(window).height();

  if ((w/1.5+40)>winW || (h/1.5+40)>winH) { ww=w/2; hh=h/2;}
  else if (w>winW-40 || h>winH-40) { ww=w/1.5; hh=h/1.5;} 

  //alert (winW + " " + winH + "\n" + w + " " + h + "\n" + ww + " " + hh);

  close_pixa();

  jQuery('body').append("<div id='pixapop'><a href='javascript:close_pixa();'>закрыть [&times;]</a><img src=\'" + url + "\' width='" + ww + "' height='" + hh +"'/><span>" + label +"</span></div>");

  var offy;
  var IE = document.all?true:false;
  if (IE && !window.opera) {
    offy = document.documentElement.scrollTop;
  }else{
    offy = window.pageYOffset;
  }

  //
  jQuery('#pixapop')
    .css('top', (offy + (winH - hh - 50)/2) + 'px')
    .css('right', ((winW - ww)/2) + 'px')
    .css('width', ww + 'px')
    .click(close_pixa)
    .fadeIn("slow");

}

//
//========================================

