// 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
	
	$('#BoatManufacturerId').change(function(){
		var id = $('option:selected', this).val();
		if(id){
			$('#BoatCategoryId').parent().html('<label for="BoatCategoryId">Category</label><div id="BoatCategoryId">Please wait...</div>');
			$.post('/categories/getCategories', { manufacturer_id: id}, function(data){
				$('#BoatCategoryId').parent().html(data);																												
			});
		}
	});
	
	$('#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(){
			var t = this.title || this.name || null;
			var a = '#TB_inline?height=365&width=685&inlineId=quickQuoteForm&modal=true'
			var g = this.rel || false;
			tb_show(t,a,g);												 
	});
	
	$('.closeModal').click(function(){
		tb_remove();
	});
	
	// Quick quote
	
	$('.largeModal .enquiry select').change(function(){
		updateQuote();
	});
	
});

function updateQuote(){
	var basePrice = $('#BoatBasePrice').val();	
	var optionPrices = getQuotePrices();
	var total = (basePrice*1) + (optionPrices*1);
	$('#optionsTotal').html('£'+addCommas(optionPrices.toFixed(2)));
	$('#quoteTotal').html('£'+addCommas(total.toFixed(2)));
}

function getQuotePrices(){
	var total = 0;
	if($('#BoatEngineUpgrades option:selected').val()){
		total += ($('#BoatEngineUpgrades option:selected').val()*1);
	}
	if($('#BoatCanvasOptions option:selected').val()){
		total += ($('#BoatCanvasOptions option:selected').val()*1);
	}
	if($('#BoatSeatingOptions option:selected').val()){
		total += ($('#BoatSeatingOptions option:selected').val()*1);
	}
	if($('#BoatGeneralOptions option:selected').val()){
		total += ($('#BoatGeneralOptions option:selected').val()*1);
	}
	if($('#BoatTrailers option:selected').val()){
		total += ($('#BoatTrailers option:selected').val()*1);
	}
	if($('#BoatDelivery option:selected').val()){
		total += ($('#BoatDelivery option:selected').val()*1);
	}
	if($('#BoatOutboards option:selected').val()){
		total += ($('#BoatOutboards option:selected').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;
}
