/* jQuery Shopping Cart 
 *
 * Published under GNU License 
 * 
 * 2010/07/01 - by delete ( olivier@smartmarseille.com )
 * 2010/07/14 - by delete ( olivier@smartmarseille.com ) - Added Attributes support     
 */
$(document).ready(function() {

// Add to cart for product_info page
//
$('#pi-add-to-cart').click(function()
{
// Partial Source code from :  http://webresourcesdepot.com/wp-content/uploads/file/jbasket/fly-to-basket/
    var productX = $('#cart-image-1').offset().left;
    var productY = $('#cart-image-1').offset().top;
 
    var basketX = $('#boxcart-content').offset().left;
    var basketY = $('#boxcart-content').offset().top;

    var gotoX = basketX - productX ;
    var gotoY = basketY - productY  ;

    var newImageWidth = $('#cart-image-1').width() / 3;
    var newImageHeight = $('#cart-image-1').height() / 3;
     
    $('#wrapper').html('');
    $('#wrapper').css(
        {
          'position':'absolute',
          'top': productY,
          'left': productX
        });
    
    $('#cart-image-1').clone()
        .prependTo('#wrapper')
        .css({'position' : 'absolute', 'border' : '1px dashed black'})
        .animate({opacity: 0.6})
        .animate({opacity: 0.0, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200,
        function() {
          products_id = parseInt( $('input[name="products_id"]').val() ) ;
          qty = parseInt($('#pq-' + products_id).html()) ;

          if ( qty )  qty = qty + 1 ; 
          else qty = 1 ;
     
          // Look for attributes
          //
          products_attributes = '' ; 
          $('form[name=cart_quantity]').find('select option:selected').each(function() {
               products_attributes += '{' + $(this).parent().attr('name').replace(/[^0-9]/g, '') + '}' + $(this).val() ; 
          });  
          if ( products_attributes != '' ) products_id = products_id + products_attributes;
               
          // Delete button in shopping cart infoboxe
          // 
          new_button = $('#boxcart-button-remove').clone() ;
          new_button.find('a').attr('id', 'bcr' + $('input[name="products_id"]').val()) ;
          new_button.find('a').attr('rel', products_id) ;

          // Products details in shopping cart infoboxe
          //           
          product_name = '<tr id="pc-' + products_id + '"><td align="right" valign="top" class="infoBoxContents"><span class="newItemInCart">';
          product_name += '<span id="pq-' + products_id +  '">' + qty +'</span>&nbsp;x&nbsp;</span></td><td valign="top" class="infoBoxContents"><span class="newItemInCart">'  ; 
          product_name += $('#pi-product-info').html() ;
          product_name += '</span>' ;          
          product_name += new_button.html()  ;
          product_name += '<input type="hidden" name="products_id[]" value="' + products_id + '" />'  ;        
          product_name += '<input type="checkbox" name="cart_delete[]" value="' + products_id + '" style="display:none;" />'  ;
          product_name += '</td></tr>' ;
          

          // Updating infobox content 
          
          $.ajax({
              type: 'POST',
              url: encodeURI($('form[name=cart_quantity]').attr('action')) + '&show_total=1&ajax=1',
              data: $('form[name=cart_quantity]').serialize(),
              success: function(data) {
                  $('#boxcart-total').html(data);                                
                  //Hide_Load();
                  }
              });


          // Remove product from infobox list
          // 
          $('tr[id=pc-' + products_id + ']').remove() ; ;
          
          // Product count 
          //
          count = $('tr[id^="pc-"]').size() ;

          if ( count == 0 ) $('#boxcart-content').html('') ;  
          
          // Append product to the list
          //
          $('#boxcart-content').append( product_name ) ;
          
          $('#' + new_button.find('a').attr('id') ).click(function()
          {
            return($(this).boxcart_remove());
          }); 
          


        });

return(false) ;

});

// Update cart infobox
//
function update_cart()
{
   //Display_Load();
   $.ajax({
        type: 'GET',
        url:  encodeURI($(location).attr('href')) + '?action=show_total&ajax=1',
        data: $('form').serialize(),
        success: function(data) {
            $('#headercart').html(data);
            //Hide_Load();
            }
        });
   return(false);

}

// Remove from cart infobox 
//
$.fn.boxcart_remove = function() { 

   products_id = $(this).attr('rel') ;

   if ( ! confirm( $('#boxcart-text-remove').html() + ' ?' ) ) return false ;
                      
   $('input[value="' + products_id + '"][name=cart_delete[]]').attr('checked', true) ;

  // Refresh entire page if current page is shopping_cart.php !
  //
  url = $(location).attr('href').split("/");
  filename = url[url.length - 1];
  filename = filename.split('\?')[0] ;           
  
  if ( filename == 'shopping_cart.php' ) 
  {
    $('form[name=boxcart_quantity]').submit(); 
  }

    // Updating cart total
    //
    $.ajax({
        type: 'POST',
        url: encodeURI($('form[name=boxcart_quantity]').attr('action')) + '&show_total=1&ajax=1',
        data: $('form[name=boxcart_quantity]').serialize(),
        success: function(data) {
            $('#boxcart-total').html(data);  
            //Hide_Load();
            }
        });
        
  $('tr[id=pc-' + products_id + ']').remove() ;

  // Product count 
  //
  count = $('tr[id^="pc-"]').size() ;

  if ( count == 0 ) $('#boxcart-content').html('<tr><td>' + $('#boxcart-text-empty').html() + '</td></tr>') ;  

  return(false);        
}
$('.boxcart-remove').click(function()
{
  return($(this).boxcart_remove());
});

// Remove from cart
//
$.fn.cart_remove = function() 
{
    products_id = $(this).attr('rel') ;

    $('tr[id=pc-' + products_id + ']').remove() ; ;
      
    $('input[value="' + products_id + '"][name=cart_delete[]]').attr('checked', true) ;
    
    $.ajax({
        type: 'POST',
        url: encodeURI($('form[name=cart_quantity]').attr('action')) + '&ajax=1',
        data: $('form[name=cart_quantity]').serialize(),     
        success: function(data) {
          $("#content-body").html(data);
          //Hide_Load();
          update_cart();          
          $.ajax({
              type: 'POST',
              url: encodeURI($('form[name=cart_quantity]').attr('action')) + '&show_total=1&ajax=1', 
              data: $('form').serialize(),
              success: function(data) {
                  $('#boxcart-total').html(data);
                  //Hide_Load();
                  }
              });
        },
        dataType: 'html'
      });
    return(false);
}
$('.cart-remove').click(function()
{
  return($(this).cart_remove());
});


// Plus or Minus function
//
// unbind para evitar el doble click que se produce en kasana... żż??

$('.update-qty').unbind("click").click(function()
{
  products_id = $(this).attr('rel') ;

  val = parseInt( $('input[id="pl' + products_id + '"][name=cart_quantity[]]').val() ) ;
  
  action = $(this).attr('class').split(' ').slice(-1) ;
     
  if ( action  == 'plus' ) 
  {
    val = val + 1 ;
  }
  else if ( action == 'moins' )
  {
    if ( val <= 1 ) return(false) ;
    val = val - 1 ;
  } 
  else
  {
    return(false) ;
  }
  
  //Display_Load();

  $('input[id="pl' + products_id + '"][name=cart_quantity[]]').val(val) ;

    // osCommerce default shopping cart infoboxe product line :
    //
    product_name = '<tr id="pc-' + products_id + '"><td align="right" valign="top" class="infoBoxContents"><span class="newItemInCart">';
    product_name += '<span id="pq-' + products_id + '">' + val + '</span>&nbsp;x&nbsp;</span></td><td valign="top" class="infoBoxContents"><span class="newItemInCart">'  ; 
    product_name += $('span[id=pn-' + products_id + ']').html() ;
    product_name += '</span>' ;
    product_name += $('#boxcart-button-remove').html() ;
    product_name += '<input type="hidden" name="products_id[]" value="' + products_id + '" />'  ;        
    product_name += '<input type="checkbox" name="cart_delete[]" value="' + products_id + '" style="display:none;" />'  ;
    
    // Look for attributes
    //
    products_attributes = '' ; 
    $('form[name=cart_quantity]').find('select option:selected').each(function() {
      products_attributes += '{' + $(this).parent().attr('name').replace(/[^0-9]/g, '') + '}' + $(this).val() ; 
    });  
    if ( products_attributes != '' ) products_id = products_id + products_attributes;

    product_name += '</td></tr>' ;
  
    // Updating infobox content 
    //
    
    
    // Remove product from infobox list
    // 
    $('tr[id=pc-' + products_id + ']').remove() ; ;
    
    // Append product to the list
    //
    $('#boxcart-content').append( product_name ) ;

  $.ajax({
      type: 'POST',
      url: encodeURI($('form[name=cart_quantity]').attr('action')) + '&ajax=1',
      data: $('form[name=cart_quantity]').serialize(),
      async:false,
      success: function(data) {
        $("#content-body").html(data);
        //Hide_Load();
        //update_cart();
      },
      dataType: 'html'
    });
    // Updating cart total
    //
    $.ajax({
        type: 'POST',
        url: encodeURI($('form[name=cart_quantity]').attr('action')) + '&show_total=1&ajax=1', 
        data: $('form').serialize(),
        success: function(data) {
            $('#boxcart-total').html(data);
            //Hide_Load();
            }
        });
  return(false);
});

$('.update-qty').css('visibility', 'visible'); 
$('.cart-remove').css('visibility', 'visible');
$('.boxcart-remove').css('visibility', 'visible');
$('input[name=cart_delete[]]').css('display', 'none');

});
/* EO jQuery Shopping Cart */


