/**
* Update our product page when we select a product variation
* Set up our tab control
*/
$(document).ready(function()
{
	//$('#product-tabs').tabs();
	$('#quantity').numeric();

	$(".variation_option_definition").each(function(i){
		$("#product_"+this.name).bind("change",load_product_variation);
	});

	/*$("#tabs a").each(function(i){
	(this).bind("click",shap_tab);
	});*/

	show_tab();
	
	//hide_tabs();
});

function show_tab(tab_link){

	$("#tabs a").each(
	function(i){
		
		var href = this.href;
		var divId = href.substring(href.indexOf('#'));
	
		if(!tab_link)
		{
			//hide all but the first tab
			(i==0)?$(divId).show():$(divId).hide();					
		}
		else
		{
			//hide all but the tab_link tab
			(tab_link.href == href)?$(divId).show():$(divId).hide();
		}
	});

	//stop href from firing
	return false;
}

//Disables the qty and add to cart buttons if item not available
function set_availability(is_available)
{
	if(is_available)
	{
		$('#quantity, #add-to-cart').unset('disabled');
		$('#add-to-cart').attr('src','/images/buttons/add-to-cart.png');
	}
	else
	{
		$('#quantity, #add-to-cart').attr('disabled', 'disabled');
		$('#add-to-cart').attr('src','/images/buttons/add-to-cart-off.png');
	}
}

/**
* Updates product variation fields on the page using Ajax with jquery
*/
function load_product_variation()
{
	//Currently selected variation_definition_id => variation_option_value
	var selected_variations = new Object();

	//Name of the select box we just changed
	var changed_select_name = this.name;

	//Get all the current selected variations above and including the changed one
	var above_selected_option = true;
	$(".variation_option_definition").each(function(i){

		if(above_selected_option)
		selected_variations[this.name] = escape(this.value);

		if(this.name == changed_select_name)
		above_selected_option = false;
	});

	selected_variations['product_id'] = $("#product_id").val();

	//Update all of our selection boxes with the new data
	$.post('/product/ajax_get_product_variation.html',selected_variations,function(response)
	{
		$('variation_option',response).each(function(i)
		{
			var variation_option_name = $('name',this).text();

			var select_input = $('#product_'+variation_option_name).get(0);

			//Clear all the select options
			select_input.options.length = 0;

			//Repopulate the options
			$('option',this).each(function(select_index){
				select_input.options[select_index] = new Option($('value',this).text(),$('value',this).text());
			});
			
			select_input.selectedIndex = $('selected_index',this).text();
		});

		//Update product page details
		$('#variation_id').attr('value',$('id',response).text());
		$('#availability').html($('availability',response).text());
		$('#ship_latency').html($('ship_latency',response).text());

		var is_available = parseInt($('is_available',response).text());
		var is_on_sale = parseInt($('is_on_sale',response).text());
		var our_price = $('our_price',response).text();
		var price_without_discount = $('price_without_discount',response).text();
		var discount_percentage = $('discount_percentage',response).text();

		if(is_on_sale)
		{
			$('#price-box').html('List: <strike>$' + $('normal_price',response).text() + '</strike><br />' +
								 '<strong>Sale Price: $' + $('sale_price',response).text() + '</strong>');
		}
		else if(our_price < price_without_discount)
		{
			$('#price-box').html('<small class="discount">Save ' + discount_percentage + '% when you buy today!</small><br />' +
								 'List: <strike>$' + price_without_discount + '</strike><br />' +
								 '<strong>Discount Price: $' + our_price + '</strong>');
		}
		else
			$('#price-box').html('$' + our_price);
		
		set_availability(is_available);
	});
}