/**
 * Update our product page when we select a product variation
 * Set up our tab control
 */
$(document).ready(function()
{
	$(".variation_option_definition").each(function(i){
		$("#product_"+this.name).bind("change",load_product_variation);
	});

	//Create jquery tabs from our list (specifications, overview, buying guide, related products)
	$('#tabthis').tabs();

});

function show_in_stock()
{
	$('#add_to_cart').attr('src','/images/pooldawg09_addtocart.png');	
	$('#quantity, #add_to_cart').unset('disabled');
	$('#in-stock-message').show();
	$('#no-stock-message').hide();
}

function show_no_stock()
{
	$('#add_to_cart').attr('src','/images/pooldawg09_outofstock.png');
	$('#quantity, #add_to_cart').unset('disabled');
	$('#in-stock-message').hide();
	$('#no-stock-message').show();
}

/**
 * Updates product variation fields on the page using Ajax with jquery
 */
function load_product_variation()
{
	$('#availability').html('Loading...');
	$('#quantity, #add_to_cart').attr('disabled', 'disabled');
	$('#add_to_cart').attr('src','/images/buttons/add-to-cart-loading.png');
	
	//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',$('variation>id',response).text());
		$('#availability').html($('variation>availability',response).text());
		$('#ship_latency').html($('variation>ship_latency',response).text());
		
		var market_price = parseFloat( $('variation>retail_price',response).text() );
		$('#market_price').html(market_price.toFixed(2));
		
		var our_price = parseFloat( $('variation>our_price',response).text() );
		$('#our_price').html(our_price.toFixed(2));
		
		if($('variation>is_available',response).text() == 1)
			show_in_stock();
		else
			show_no_stock();

		var save_amount = parseFloat( $('variation>retail_price',response).text() ) - parseFloat( $('variation>our_price',response).text() ) ;
		var retail_price = parseFloat( $('variation>retail_price',response).text() );
		var save_percentage = (save_amount / retail_price) * 100;
		
		$('#save_percentage').html(Math.round(save_percentage));
		$('#you_save').html(save_amount.toFixed(2));
	});
}

/**
 * Updates product rating on the page using DHTML
 */
function rate(product_id,rating)
{
		
 	$.get('/product/ajax_set_product_rating/'+product_id+'/'+rating,function(response){
 		$("#current_rating").css("width", $('average_rating',response).text()+'px');
 		$("#selected_rating").css("width", $('selected_rating',response).text()+'px');
 		$('#number_of_votes').html($('quantity',response).text());
 	});

	return false;//stop link firing 
}
