// JavaScript Document

$(function(){
	// View Button animation
	/*
	$('#homepageViewNewShowroomArea, #homepageViewPreOwnedShowroomArea').hover(function(){
		$('.viewButton img', this).animate({ width: '56px'}, 500);
	}, function(){ 
		$('.viewButton img', this).animate({ width: '0px'}, 200);
	});
	*/
	
	// Boat thumbnails
	$('ul.thumbnails li img').click(function(){
		var image = $(this).attr('id');
		$('#main_image').attr('src', '/img/boats/big/'+image);
	});
	
	// Boat add manufacturer change
	if($('#BoatManufacturerId').length > 0){
		function changeCategory($originalId){
				var id = $('#BoatManufacturerId option:selected').val();
				var originalId = $originalId;
				if(id){
					$('#BoatCategoryId').parent().html('<label for="BoatCategoryId">Category</label><div id="BoatCategoryId">Please wait...</div>');
					$.post('/categories/getCategories', { manufacturer_id: id, category_id: originalId}, function(data){
						$('#BoatCategoryId').parent().html(data);																												
					});
				}
		}
		changeCategory($('#BoatOriginalCategoryId').val());
		$('#BoatManufacturerId').change(function(){
			changeCategory(999999);
		});
	}
	
	$('#enquiryLink').click(function(){
			var t = this.title || this.name || null;
			var a = '#TB_inline?height=372&width=485&inlineId=enquiryForm&modal=true'
			var g = this.rel || false;
			tb_show(t,a,g);												 
	});
	
	$('#quickQuote').click(function(){
			$('#quickQuoteForm').slideDown();
			$('#quickQuoteForm input:first').focus();								 
	});
	
	$('.closeModal').click(function(){
		tb_remove();
	});
	
	// Quick quote
	
	$('#quickQuoteForm select').change(function(){
		updateQuote();
	});
	$('#quickQuoteForm .checkbox input').click(function(){
		updateQuote();
	});
});

function updateQuote(){
	var basePrice = $('#basePrice').val();	
	var optionPrices = getQuotePrices();
	var total = (basePrice*1) + (optionPrices*1);
	$('#optionsTotal').html('&#163;'+addCommas(optionPrices.toFixed(2)));
	$('#quoteTotal').html('&#163;'+addCommas(total.toFixed(2)));
}

function getQuotePrices(){
	var total = 0;
	$checked = $('#quickQuoteForm input:checked, #quickQuoteForm select option:selected');
	$.each($checked, function(){
		total += $(this).val()*1;
	});
	return total;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

