/**
 * @package ajax shopping cart
 * @copyright Copyright 2005-2010, Andrew Berezin eCommerce-Service.com
 * @copyright Copyright 2003-2005 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: jscript_ajax_cart.js 2.1 18.08.2010 15:02:33 AndrewBerezin $
 */

$(document).ready(function() {

  var updateHTMLpageBlocks = function(HTMLblocks){
    $.each(HTMLblocks, function(block_id, block_html) {
//      var_dump(block_id);
//      var_dump(block_html);
      $("#" + block_id).html(block_html);
    });
  }

  // buy_now
  $('[href*="action=buy_now"]').click(function() {
    var $this = $(this);
    var href = $this[0]['href'];
    pid = href.match(/products_id=(\d+)/);
    if (pid) {
      pid = pid[1];
      $.ajax({
        url: '/index_ajax.php?ajax_module=cart_action&cart_action=buy_now',
        type: 'get',
        dataType: 'json',
        data: { products_id: pid },
        success: function(data){
          if (data) updateHTMLpageBlocks(data.HTMLblocks);
        }
      });
      return false;
    }
  });

  // multi add to cart
  $("form[name='multiple_products_cart_quantity']").submit(function(){
    $this = $(this);
    $(this).ajaxSubmit({
      url: '/index_ajax.php?ajax_module=cart_action&cart_action=multiple_products_add_product',
      type: 'post',
      dataType: 'json',
      clearForm: true,
      resetForm: true,
      //target: '#output1', // target element(s) to be updated with server response
      //beforeSubmit: showRequest, // pre-submit callback
      success: function(data){
        if (data) updateHTMLpageBlocks(data.HTMLblocks);
      }
    });
    return false;
  });

  // add_product
//  $("form[name='cart_quantity']").submit(function(){
  $("#productGeneral > form[name='cart_quantity']").submit(function(){
    $this = $(this);
    $(this).ajaxSubmit({
      url: '/index_ajax.php?ajax_module=cart_action&cart_action=add_product',
      type: 'post',
      dataType: 'json',
      clearForm: true,
      resetForm: true,
      success: function(data){
//        var_dump(data);
        if (data) updateHTMLpageBlocks(data.HTMLblocks);
      }
    });
    return false;
  });

});
