var booking = {
	number_of_adults : 0,
	number_of_children : 0,
	
	adult_cost : 0,
	child_cost : 0,

	base_cost : 0,
	options_cost: 0,
	total : 0,

	init : function() {
		this.adult_cost = parseFloat($('#cost-adult').html());
		this.child_cost = parseFloat($('#cost-child').html());

		$('#tour-options').click(this.update_options);
		$('#adults, #children').change(this.update_participants);
		
		this.update_participants();

		if (document.body.id === 'page-book-rotorua-day-sights') {
			this.fix_options = function() {
				var skyline_skyrides_gondola = document.forms[0]['options[skyline-skyrides-gondola]'];
				var polynesian_spa = document.forms[0]['options[polynesian-spa]'];

				polynesian_spa.disabled = !skyline_skyrides_gondola.checked;
				if (polynesian_spa.disabled) polynesian_spa.checked = false;
			};

			this.fix_options();
		}

		this.calculate();
	},

	update_options : function(e) {
		if (typeof(booking.fix_options) === 'function') booking.fix_options();
		if (e.target.nodeName.toLowerCase() == 'input') booking.calculate(); 
	},

	update_participants : function(e) {
		booking.number_of_adults = parseInt($('#adults').val());
		if (isNaN(booking.number_of_adults)) booking.number_of_adults = 0;
		booking.number_of_children = parseInt($('#children').val());
		if (isNaN(booking.number_of_children)) booking.number_of_children = 0;

		// validate
		//	must be at least once parent with the children
		if (booking.number_of_adults == 0 && booking.number_of_children > 0) {
			alert('At least one parent must accompany any children on our tours');
			$('#adults').val('1');
		}

		booking.calculate();
	},
	
	calculate : function() {
		this.calculate_options_cost();
		this.total = (this.number_of_adults * this.adult_total_cost) + (this.number_of_children * this.child_total_cost);
		$('#cost-total').html(formatCurrency(this.total));
	},

	calculate_options_cost : function() {
		this.adult_total_cost = this.adult_cost;
		this.child_total_cost = this.child_cost;

		$("#tour-options input.checkbox[@checked]:not([disabled])").each(function() {
			var attributes = this.id.split('_');
			
			booking.adult_total_cost += parseInt(attributes[1]);
			booking.child_total_cost += parseInt(attributes[2]);
		});

		$('#cost-adult').html(this.adult_total_cost);
		$('#cost-child').html(this.child_total_cost);
	}
};

function formatCurrency(amount) {
	amount = amount.toString();

	// Add trailing zeros
	var decimalPlace = amount.indexOf('.'), trailingZeros = amount.substring(decimalPlace+1);

	if (decimalPlace !== -1 && trailingZeros.length !== 0) {
		if (trailingZeros.length == 1) {
			amount = amount.substring(0, decimalPlace);
			trailingZeros = '.'+trailingZeros+'0';
		} else trailingZeros = '';
	} else {
		trailingZeros = '.00';
	}

	// Add $ sign
	return '$' + amount + trailingZeros;
}

$(document).ready(function() {
	booking.init();

	$('#route').change(function(e) {
		var route_id = e.target.options[e.target.selectedIndex].value;
		location.href = location.protocol+'//'+location.hostname + (location.port && location.port != 80 ? ':'+location.port : '') + location.pathname + '?route='+route_id;
	});
	
	$('#booking-form').submit(function(e) {
		var terms = $('#terms')[0];

		if (terms && !terms.checked) {
			alert('You must accept the terms and conditions before continuing');
			return false;
		}
	})
	
	$('#anchor1').click(function(e) {
		e.preventDefault();
	    e.stopPropagation();
		calendar.select(document.forms[0].date1,'anchor1','dd/MM/yyyy');
	});
});

var calendar = new CalendarPopup("calanderdisplay");
calendar.showNavigationDropdowns();

//Quick link menu code (quick_link)
function jump_menu(targ,selObj,restore){
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}

//JK Popup Window Script (version 3.0)- By JavaScript Kit (http://www.javascriptkit.com)
//Visit JavaScriptKit.com for free JavaScripts
//This notice must stay intact for legal use
function openpopup(popurl){
	var winpops=window.open(popurl,"","width=500px,height=500px,status,scrollbars,resizable")
}