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

/**
 * 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',$('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() );
		$('#retail-price').html('$'+market_price.toFixed(2));
		
		var our_price = parseFloat( $('variation>our_price',response).text() );
		$('#sale-price').html('$'+our_price.toFixed(2));
		
		$('#is_available').attr('value',$('variation>is_available',response).text());

		set_availability();

		/*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));*/
	});
}

//Checks if the currently selected variation is available and disables the qty and add to cart buttons if not available
function set_availability()
{
	if($('#is_available').val() == 1)
	{
		$('#qty, #add_to_cart').unset('disabled');
		$('#add_to_cart').attr('src','/img/addtocart_orange.png');
		$('#availability').css('color','#000000');
	}
	else
	{
		$('#qty, #add_to_cart').attr('disabled', 'disabled');
		$('#add_to_cart').attr('src','/img/addtocart_orange_off.png');
		$('#availability').css('color','#D0161D');
	}
}


function validate_email_friend(frm){

	var message = "Please correct the following errors\n\n";
	var valid = true;

	if(frm.to.value == '')
	{
		valid = false;
		message += "Please enter a valid email adderess\n";
	}

	if(frm.sendername.value == '')
	{
		valid = false;
		message += "Please enter your name\n";
	}
	
	if(!valid)
	alert(message);
	
	return valid;
}

//called when some clicks on swatch image
function swapthumb(a_el)
{
	var src = $(a_el).attr('href');

    $('#main_image img').attr( 'src', src );       
    $('#main_image a').attr( 'href', src ); 
	
	return false;
}

