/**
 * 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)
	$('#tabs-container').tabs();
	
	//Swap images when clicked
	$('#alt-images img').bind('click',function(){
		$('#primary-image').attr('src',arr_alt_image_thumbnail_src[this.id]);
		$('#primary-image').parent('a').attr('href',$(this).parent().get(0).href);
		
		clicked_mini_thumbnail_anchor = $(this).parent('a').attr('href');
		$(this).parent('a').attr('href',current_fullsize_image);
		current_fullsize_image = clicked_mini_thumbnail_anchor;
		
		//Switch the mini thumbnail images over
		clicked_mini_thumbnail_src = this.src;
		this.src = current_image_mini_thumbnail_src;
		current_image_mini_thumbnail_src = clicked_mini_thumbnail_src;
		
		clicked_mini_thumbnail_id = this.id;
		this.id = current_image_mini_thumbnail_id;
		current_image_mini_thumbnail_id = clicked_mini_thumbnail_id;
		

		
		return false;
	});
	
	//Check we have atleast 1 quantity before allowing a form submit
	$("#add-to-cart-form").bind('submit',function(){
		var has_quantity = false;
		
		$('.quantity').each(function(i){
			if(this.value != "")
				has_quantity = true;
		});
		
		if(!has_quantity)
			alert('Please enter atleast 1 quantity before adding to cart');
			
		return has_quantity;
	});
});

/**
 * 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() );
		$('#market_price').html(market_price.toFixed(2));
		
		var our_price = parseFloat( $('variation/our_price',response).text() );
		$('#product-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));
	});
}

/**
 * Updates product rating on the page
 */
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');
 		$('#current-average-rating').show();
 		
 		$('.rate-text').html('Thank you for rating this product');
 	});
}

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 valid email adderess for you friend \n";
	}
	
	if(frm.sender.value == '')
	{
		valid = false;
		message += "Please enter valid email adderess\n";
	}

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