/* jQuery executable  -
======================================================================*/
$(document).ready(function() {
	var CurrentClass
	$('.subcat ul li:even').addClass('subalt')

	$('#cat-nav').accordion({
		active: false,
		header: '.head',
		autoheight: false, //added by oscar
		alwaysOpen: false //added by oscar
	})

	//CurrentClass = $('body').attr("id")
	//alert(CurrentClass);

	$("#cat-nav li[@class=" + $('body').attr("id") + "] ul").toggle();
	//$("#cat-nav li[@class=" + $('body').attr("id") + "] ul").css({ overflow: "hidden", display: "block" });

	//$('a[@rel$="external"]').click(function(){this.target = "_blank";})
	
	
  /* Product-Listing
  ======================================================================*/
  // When the page loads all price displays will be set to the
  // value inside the drop down menu
	$('select[name="iPrice"]').each(update_price)
	
	// When a new price option is selected the relevant price display
	// will be updated
	$('select[name="iPrice"]').change(update_price)
	
	  /* Wine-Listing
  ======================================================================*/
  // When the page loads a subtotal will be set or hidden depending on
  // the value of the input box
  $('ul.listings input[name=iQuantity]').each(update_sub_total)
  $('body.wine-listing input[name=iQuantity]').each(update_sub_total)
  
  // When someone changes the value of of the input box subtotal is updated
  $('ul.listings input[name=iQuantity]').keyup(update_sub_total)
  $('body.wine-listing input[name=iQuantity]').keyup(update_sub_total)
  
	  /* Ajax Add to Cart buttons
  ======================================================================*/
  $('input.buy_now').click(function() {
    // When a 'buy now' link is clicked the parent div (which should
    // contain the information for the product being purchased) will
    // appear to be moved to the shopping cart summary area on the page.
    parent_div = $(this).parents('div')[0]
    $(parent_div).TransferTo({
      to: 'cart-container',
      duration: 450,
      className: 'transfer'
    })
    
    // Find the details of this product
    product_name      = $(this).parents('form').find('*[name=sName]').val()
    product_price     = $(this).parents('form').find('*[name=iPrice]').val()
    product_quantity  = $(this).parents('form').find('*[name=iQuantity]').val()
    
    // Find any special attributes for this product
    options = {
      iWeight : $(this).parents('form').find('*[name=iWeight]').val(),
      bWine   : $(this).parents('form').find('*[name=bWine]').val()
    }
    
    // Add the item to current shopping cart
    add_to_cart(product_name, product_price, product_quantity, options)
    
    // Supress the default click behavior
    return false
  })

})

// This will find the price stored inside the select box
// passed to it, format the price and display it inside
// the relevant area on the page.
update_price = function () {
  // Find the selected price and format it
  var selected_price = $(this).val()
  var formatted_price = "$" + currency_formatted(selected_price)
  
  // Display the formatted price on the screen
  $(this).parents('form').find('span.product-price').html(formatted_price)
  
  // Update the hidden field used for the name shown in the shopping cart
  var base_name = $(this).parents('form').find('input.sBaseName').val()
  var suffix = $(this).find('option:selected').html()
  $(this).parents('form').find('input[name=sName]').val(base_name + " (" + suffix + ")")
}

// This will update the subtotal area with the correct price
// multiplied by the quantity. If there is no sub-total to
// display then the area will be hidden.
update_sub_total = function () {
  // Grab quantity and format price
  var quantity = $(this).val();
  var price = $(this).parents('form').find('input.price').val()
  var formatted_sub_total = "$" + currency_formatted(quantity * price)
  
  // Output the formated price to the sub total area
  var sub_total_display = $(this).parents('form').find('span.sub-total-value')
  sub_total_display.html(formatted_sub_total)
  
  // If they don't want any wine then hide the sub total
  if(quantity == 0 || quantity == null) {
    $(this).parents('form').find('.sub-total').fadeOut()
  } else {
    $(this).parents('form').find('.sub-total').fadeIn()
  }
}

  /* Ajax Cart functions
======================================================================*/
function add_to_cart (name, price, quantity, options) {
$.getJSON("cart-ajax.asp",
	merge_objects({
	  "sCartAction": "add",
	  "sName": name,
	  "iPrice": price,
	  "iQuantity": quantity
	}, options),
	function(data){
	  // Update the cart summary on the page with new number of
	  // products in the cart and the total price
	  $('span#numberOfProducts').html(data["numberOfProducts"])
	  $('span#totalCostOfProducts').html(
	    "$" + currency_formatted(data["totalCostOfProducts"])
	  )
	});
}