/**
 *  
 * LOTUS CREATIONS AJAX SHOPPING BASKET
 * @author Chris Li
 * @version 0.1
 * @note used in conjunction with JQuery 1.3.2
 * 
 * Shopping basket that interacts with LOTUS CMS to display and generate basket
 * 
 * How to setup:
 * 1.  HTML SETUP - note: this basket could be redesigned if need be
 *  <div id="cart_container" style="">
        <!-- BEGIN ShoppingCartBlock -->                
        <div id="cart-info">                    
            <div class="content">
                <!-- BEGIN CartBlock -->
                    You currently have <strong id="basket-total-items">{basket_total_items}</strong> items in your basket totaling <strong id="basket-total">{basket_total}</strong>
                <!-- END CartBlock -->
            </div>                     
        </div>
        <div id="open-basket">
            <div class="content">
                Open Basket</div>
        </div>
        <div id="checkout">
            <div class="content">
                <a href="{checkout_url}">Checkout</a></div>
        </div>    
        <div class="clear"></div>
        <!-- END ShoppingCartBlock -->
    </div>
    <div id="cart_content_container" style="display:block;">
        <div id="cart_content" style="display:none;"></div>
    </div>
 *
 * 2. Set variables for basket:
 * //init variables for lotus-ajax-basket.js
  var basket_image_loading = "http://192.168.0.100/pariscues-v2/templates/images/loading.gif";
  var get_basket_info_form_action = "http://192.168.0.100/pariscues-v2/index.php?module=ProductsApp&action=ajax_get_basket_info&nav_id=";
  var delete_basket_item_form_action = "http://192.168.0.100/pariscues-v2/index.php?module=ProductsApp&action=ajax_delete_basket_item&nav_id=";
  var ajax_update_quantity_form_action = "http://192.168.0.100/pariscues-v2/index.php?module=ProductsApp&action=ajax_update_quantity&nav_id=";
  var get_basket_totals_form_action = "http://192.168.0.100/pariscues-v2/index.php?module=ProductsApp&action=ajax_get_basket_totals";
  
 *
 * 3. Setup call to basket init
 * $(document).ready(function(){
      //init. functions1
    	toggleBasket();
   });
   
 */
function toggleBasket() {
  $(document).ready(function(){
    $('#open-basket').click(function() {
      if ($('#cart_content').css('display') == 'none') {
        $('#open-basket .content').text('Close Basket');
        $(this).addClass('open');
        getBasketInfo();
      } else {
        $('#open-basket .content').text('Open Basket');
        $(this).removeClass('open');
      }      
      $('#cart_content').slideToggle('fast');
    });
  });
}

//var basket_image_loading = "images/loading.gif";
//var get_basket_info_form_action = "{get_basket_info_form_action}";
function getBasketInfo() {
  $(document).ready(function() {
    //get basket info
    $.ajax({
      type: "GET",
      url: get_basket_info_form_action,
      dataType: "html",
      beforeSend: function(request) {
        //this modifies the basket bar in view_products.html
        $('#cart_content').html('<div align="center"><img src=' + basket_image_loading + ' /></div>');
      },
      error: function(request, textStatus, errorThrown) {            
        $('#cart_content').html('Sorry, we were not able to retrieve the cart information.  Please try again.');
      },
      success: function(data, textStatus) {
        $('#cart_content').html(data);
      }
    });
  });
}

//var delete_basket_item_form_action = "{delete_basket_item_form_action}";
function deleteItemFromCart(cart_id) {
  if ( confirm('Are you sure you want to delete this item?') ) {
    $(document).ready(function(){
      $.ajax({
        type: "GET",
        url: delete_basket_item_form_action,
        data: "cart_id=" + cart_id,
        dataType: "html",
        beforeSend: function(request){
          //this modifies the basket bar in view_products.html
          $('#cart_content').html('<div align="center"><img src=' + basket_image_loading + ' /></div>');
        },
        error: function(request, textStatus, errorThrown){
          alert('There was an error deleting the item.');
          getBasketInfo();
        },
        success: function(data, textStatus){
          $('#cart_content').html(data);
          getBasketBarData();
        }
      });
    });
  }
}

//var ajax_update_quantity_form_action = "{ajax_update_quantity_form_action}";
//var basket_image_loading = "images/loading.gif";
function updateItemQuantity(cart_id, quantity) {
  $(document).ready(function(){
    $.ajax({
      type: "GET",
      url: ajax_update_quantity_form_action,
      data: "cart_id=" + cart_id + "&q=" + quantity,
      dataType: "html",
      beforeSend: function(request){
        //this modifies the basket bar in view_products.html
        $('#cart_content').html('<div align="center"><img src=' + basket_image_loading + ' /></div>');
      },
      error: function(request, textStatus, errorThrown){
        alert('There was an error updating the quantity.');
        getBasketInfo();
      },
      success: function(data, textStatus){
        $('#cart_content').html(data);
        getBasketBarData();
      }
    });
  });
}

//var get_basket_totals_form_action = "{get_basket_totals_form_action}";
function getBasketBarData() {
  $(document).ready(function(){
    $.ajax({
      type: "GET",
      cache: false,
      url: get_basket_totals_form_action,
      dataType: "json",
      beforeSend: function(request) {
        //this modifies the basket bar in view_products.html
        $('#cart-info .content').html('Updating basket...');
      },
      error: function(request, textStatus, errorThrown) {
        $('#cart-info .content').html('Sorry. we\'ve experienced some technical difficulties, <a href="javascript:location.href = location.href;">please try and refresh the page here.</a>');
        tb_remove();
      },
      success: function(data, textStatus) {
        if (data.basket_total_items == '') {
          $('#cart-info .content').html('Basket is currently empty.');
          $('#cart_content').hide();
        } else {
          generateBasketBar(data);
          //toggleBasket(); //this activates the "close basket/open basket" button
        }
      }
    });
  });
}

/* 
 * This is used for the product_details.html
 */
//var loading_image = "images/loadingAnimation.gif";
//var ajax_add_to_cart_form_action = "{ajax_add_to_cart_form_action}";
//var back_url = "{back_url}";
function addToBasket(){
  $(document).ready(function(){
    $('#addtobasket').click(function(){
      $(this).submit();
    });
    
    $('#addtobasket').submit(function(){
      _content_string = 'cart_cue_length=' + $('#cue_length').val();
      _content_string += '&cart_cue_tip_size=' + $('#tip_size').val();
      _content_string += '&cart_cue_butt_size=' + $('#butt_size').val();
      _content_string += '&cart_cue_weight=' + $('#cue_weight').val();
      
      //adds the selected product and options to the cart
      $.ajax({
        type: "POST",
        url: ajax_add_to_cart_form_action,
        data: _content_string,
        dataType: "json",
        beforeSend: function(request){
          $('#options-container').html('<div id="image_loader">Adding to basket...<br /><img src="' + loading_image + '" border="0" /></div>'); //create loading bar
          _window_height = $('#TB_window').height(); //get height of parent window
          $('#image_loader').css("margin-top", (_window_height / 3) + 'px'); //sets position for loading bar
        },
        error: function(request, textStatus, errorThrown){
          $('#image_loader').html('Sorry. we\'ve experienced some technical difficulties, please try again.<br />You can call us at <b>07968 065 525</b> OR email at <b>sales@pariscues.com</b>.<br /><img src="images/button_close.gif" onclick="tb_remove();" alt="Cancel" border="0">');
        },
        success: function(data, textStatus){
          location.href = back_url;
          //alert("data: " + data.saveStatus + "\ntextStatus: " + textStatus);
          //tb_remove();
        }
      });
    });
  });
}

function generateBasketBar(data) {
  $('#cart-info .content').html('You currently have <strong id="basket-total-items">' +
    data.basket_total_items +
    '</strong> items in your basket totaling <strong id="basket-total">' +
    data.currency_symbol +
    data.basket_total +
    '</strong>'
  );  
}

/* 
 * This is used for the checkout_options.html pop-up page
 */
//var ajax_add_to_cart_form_action = "{ajax_add_to_cart_form_action}";
//var checkout_loading_image = "images/loadingAnimation.gif";
//var get_basket_totals_form_action = "{get_basket_totals_form_action}";
function checkoutOptions() {
  $(document).ready(function () {
    $('#cancel').click( function() {
      tb_remove();
    });
    
    $('#addtobasket').click( function() {
      $(this).submit();
    });
    
    $('#addtobasket').submit(function() {
      _content_string = 'cart_cue_length=' + $('#cue_length').val();
      _content_string += '&cart_cue_tip_size=' + $('#tip_size').val();
      _content_string += '&cart_cue_butt_size=' + $('#butt_size').val();
      _content_string += '&cart_cue_weight=' + $('#cue_weight').val();
      
      $.ajax({
        type: "POST",
        url: ajax_add_to_cart_form_action,
        data: _content_string,
        dataType: "json",
        beforeSend: function(request) {
          $('#options-container').html('<div id="image_loader">Adding to basket...<br /><img src="' + checkout_loading_image + '" border="0" /></div>'); //create loading bar
          _window_height = $('#TB_window').height(); //get height of parent window
          $('#image_loader').css( "margin-top", (_window_height/3) + 'px' ); //sets position for loading bar
        },
        error: function(request, textStatus, errorThrown) {
          $('#image_loader').html('Sorry. we\'ve experienced some technical difficulties, please try again.<br />You can call us at <b>07968 065 525</b> OR email at <b>sales@pariscues.com</b>.<br /><img src="images/button_close.gif" onclick="tb_remove();" alt="Cancel" border="0">');
        },
        success: function(data, textStatus) {
          //alert("data: " + data.saveStatus + "\ntextStatus: " + textStatus);
          $.ajax({
            type: "GET",
            url: get_basket_totals_form_action,
            dataType: "json",
            beforeSend: function(request) {
              //this modifies the basket bar in view_products.html
              $('#cart-info .content').html('Updating basket...');
            },
            error: function(request, textStatus, errorThrown) {
              $('#cart-info .content').html('Sorry. we\'ve experienced some technical difficulties, <a href="javascript:location.href = location.href;">please try and refresh the page here.</a>');
              tb_remove();
            },
            success: function(data, textStatus) {
              generateBasketBar(data);         
              //toggleBasket(); //this activates the "close basket/open basket" button
              getBasketInfo(); // refresh basket information
              tb_remove();    // thickbox remove window function
            }
          });
        }
      });
    });
  });
}
//END BASKET FUNCTIONS ******************************************************