function show_form(hash) {
	jQuery('#inntopia-sidebar-widget .tabs a').removeClass('current');
	jQuery('#inntopia-sidebar-widget .tabs a[href=' + hash + ']').addClass('current');
	jQuery('#inntopia-sidebar-widget form').hide();
	jQuery('#inntopia-form-' + hash.replace('#','')).show();
}
function prefix(string,length,character) {
	string = String(string);
	while (string.length < length) {
		string = character + string;
	}
	return string;
}
function splitDate(formattedDate) {
	var dateArray = formattedDate.split('/');
	return [dateArray[2],Number(dateArray[0]) - 1,dateArray[1]];
}
function dateToObj(formattedDate) {
	var a = splitDate(formattedDate);
	return new Date(a[0],a[1],a[2]);
}
function dateToUTC(formattedDate) {
	var a = splitDate(formattedDate);
	return Date.UTC(a[0],a[1],a[2]);
}
function addDays(formattedDate,days) {//Format is MM/DD/YYYY
	var date = dateToObj(formattedDate);
	date.setDate(date.getDate() + days);
	return prefix(date.getMonth() + 1,2,'0') + '/' + prefix(date.getDate(),2,'0') + '/' + String(date.getFullYear());
}
function calcNights() {
	var a = jQuery('#arrivaldate-lodging').val();
	var d = jQuery('#departuredate-lodging').val();
	if ((a != '') && (d != '')) {
		jQuery('#nights-lodging').val((dateToUTC(d) - dateToUTC(a)) / 86400000);
	}
}
jQuery(function() {
	var hash = window.location.hash;
	show_form(hash?hash:'#lodging');
	jQuery('.tabs a').click(function() {
		show_form(jQuery(this).attr('href'));
	});
	jQuery('.arrivaldate').datepicker({minDate:0});
	jQuery('.departuredate').datepicker({minDate:1});
	jQuery('.arrivaldate').change(function() {
		jQuery('#' + jQuery(this).attr('id').replace('arrival','departure')).datepicker('option','minDate',addDays(jQuery(this).val(),1));
	});
	jQuery('.departuredate').change(function() {
		jQuery('#' + jQuery(this).attr('id').replace('departure','arrival')).datepicker('option','maxDate',addDays(jQuery(this).val(),-1));
	});
	jQuery('#arrivaldate-lodging, #departuredate-lodging').change(calcNights);
	jQuery('.calendar').click(function() {
		jQuery(this).siblings('.datepicker').focus();
	});
	jQuery('.daterange').click(function() {
		var $fieldset = jQuery('#departuredate-' + jQuery(this).attr('id').split('-')[1]).parent();
		(jQuery(this).val() == '1')?$fieldset.show():$fieldset.hide();
	});
	jQuery('.datepicker').val('');
	jQuery('.daterange[checked=checked]').click();
	jQuery('#arrivaldate').datepicker({
		minDate:0,
		onSelect:function(dateText,inst) {jQuery('#departuredate').datepicker('option','minDate',addDays(dateText,1));}
	});
	jQuery('#departuredate').datepicker({
		minDate:1,
		onSelect:function(dateText,inst) {jQuery('#arrivaldate').datepicker('option','maxDate',addDays(dateText,-1));}
	});
	jQuery('#inntopia-widget-arrival-calendar').click(function() {
		jQuery('#arrivaldate').focus();
	});
	jQuery('#inntopia-widget-departure-calendar').click(function() {
		jQuery('#departuredate').focus();
	});
});
