/*---------------------------------------------*/
/* Rudekat Records Store (Island Vibe Tickets) */
/*---------------------------------------------*/

jQuery(document).ready(function($) {
  
  /************************************/
  /* Establish Island Vibe Ticket IDs */
  /************************************/
  
  /* $('body').data('island_vibe_tickets', new Array(227, 331, 332, 229)); */
  
   $('body').data('island_vibe_tickets', new Array(379, 378, 333, 342, 335, 334, 337, 336, 339, 338, 341, 340)); 
  
  /**********************************************/
  /* End of Establishing Island Vibe Ticket IDs */
  /**********************************************/
  
  $('body').data('open_island_vibe_popup', function() {
    
    // define a function which will handle
    // both success and failure of our post
    var complete = function(data, status) { 
      if (!(data instanceof Object)) data = new Object();
      
      // for any type of error use lightbox error
      // function which has been embedded in the body
      if ((!data.success) || (status != 'success'))
      {
        var error = data.error || 'System/Network error; Please try again.';
        $('body').data('plb__show_error')(error);
      }
      
      // otherwise proceed
      // to open lightbox
      else
      {
        // create a jquery 
        // object from the
        // raw html data
        var lb = $(data.html);
        
        // enable clicking on the checkboxes to work
        lb.find('img.checkbox').click(function(e) {
          e.preventDefault();
          if ($(this).parents('tr.ticket.disabled').size()) return;
          $(this).toggleClass('checked');
        });
        
        // enable the add to cart and checkout links to function by first
        // adding the selected items to the cart then showing added lightbox
        // or proceeding directly to the shopping cart page to begin checkout
        lb.find('div.shopping-controls div.links .add').click(function(e) {
          e.preventDefault();
          var link = $(this);
          
          // define a function which will handle
          // both success and failure of our post
          var complete = function(data, status) { 
            if (!(data instanceof Object)) data = new Object();
            
            // no matter what our response update the page totals
            $('body').data('refresh_total_quantity_of_items')();
                      
            // for any type of error use lightbox error
            // function which has been embedded in the body
            if ((!data.success) || (status != 'success'))
            {
              var error = data.error || 'System/Network error; Please try again.';
              $('body').data('plb__show_error')(error);
            }
            
            // we may just redirect to the cart
            else if (link.hasClass('checkout'))
            {
              // peform a redirect to the shopping cart
              window.location.href = 'shopping_cart.php';
            }
            
            // otherwise proceed
            // to open lightbox
            else
            {
              // create a jquery 
              // object from the
              // raw html data
              var lb = $(data.html);
              
              // before opening the lightbox add functionality
              // to the cancel link which makes proper request
              lb.find('.cancel-link').click(function(e) {
                e.preventDefault();
                var td = $(this).parents('td.cancel');
                var just_added_ids = td.find('span.just-added-cart-item-ids').text();
                var just_added_quantities = td.find('span.just-added-cart-item-quantities').text();
                var aja = td.find('div.ajax-activity');
                
                // show the 
                // indicator
                aja.show();
                
                // define a function which will handle
                // both success and failure of our post
                var complete = function(data, status) { 
                  if (!(data instanceof Object)) data = new Object();
                  
                  // for a cancel action we provide no feedback other
                  // than updating the total items in cart on the page
                  $('body').data('refresh_total_quantity_of_items')();
                  plb__lightbox.close();
                };
                
                // compose and execute ajax request
                $.ajax({ url: 'shopping_cart.php',
                         type: 'POST',
                         cache: false,               
                         data: { 'rkat_store': 'ajax_undo_cart_addition',
                                 'just_added_cart_item_ids': just_added_ids,
                                 'just_added_cart_item_quantities': just_added_quantities
                               },
                         dataType: 'json',
                         success: complete,
                         error: complete });
              });                            
            
              // after adding proper behaviors
              // display the lightbox content
              plb__lightbox.open(lb);
            }
          };
          
          // extract the selected products
          // on the album jukebox lightbox
          var product_ids = new Array();
          var quantities = new Array();
          var ivp = $(this).parents('.rkat-lightbox-island-vibe-popup');
          ivp.find('img.checkbox.checked').each(function(i, el) {
            var el = $(el);
            product_ids[product_ids.length] = el.parent().find('.rkat-product-id').text();
            quantities[quantities.length] = 1;
          });
          
          // do not do anything without
          // at least one product to add
          // go to cart page on checkout
          if (!product_ids.length) 
            if (link.hasClass('checkout')) { window.location.href = 'shopping_cart.php'; return; }
            else return;
          
          // show the indicator within a lightbox
          $('body').data('plb__show_activity')();
          
          // compose and execute ajax request
          $.ajax({ url: 'shopping_cart.php',
                   type: 'POST',
                   cache: false,               
                   data: { 'rkat_store': 'ajax_add_product',
                           'mode': 'multiple',
                           'product_ids': product_ids.join('|'),
                           'quantities': quantities.join('|')
                         },
                   dataType: 'json',
                   success: complete,
                   error: complete });
        });
        
        // after adding proper behaviors 
        // display the lightbox content
        plb__lightbox.open(lb);
      }
    };
    
    // show the indicator within a lightbox
    $('body').data('plb__show_activity')();
    
    // compose and execute ajax request
    $.ajax({ url: 'shopping_cart.php',
             type: 'POST',
             cache: false,               
             data: { 'rkat_store': 'ajax_open_island_vibe_popup',
                     'product_ids': $('body').data('island_vibe_tickets').join('|')
                   },
             dataType: 'json',
             success: complete,
             error: complete });
  });
});


