Shadowbox.init();

var backgroundTimer = null;

function setupBackgrounds(duration) {
	
	if (duration == undefined) {
		duration = 5000;
	}
	
	clearTimeout(backgroundTimer);
	
	backgroundTimer = setTimeout('rotateBackgrounds()', duration);
}

function rotateBackgrounds() {
	
	var element = $('#canvas .fullscreen:last')
	var next = parseInt(parseInt($(element).attr('rel')) + 2);
	
	if (next > 5) {
		next -= 5;
	}
	
	$(element).fadeOut(500,
		function() {
			$(this).remove();
			$('#canvas').prepend($('#canvas .fullscreen').clone().attr('src', Path_skin + '/images/canvases/' + next + '.jpg').attr('rel', next));
		}
	);
	
	setupBackgrounds();
}

function backgroundImageActions() {
	
	jQuery('#canvas').css('height', jQuery(window).height())
	setupBodyHeight();
}

function centerBackgroundImage(target) {	
	
	target = jQuery(target);
	
	var the_window = jQuery(window);
	
	target.css({
		'width': 'auto',
		'height': (the_window.height() - 140) + 'px'
	});

	if (target.attr('width') < the_window.width()) {
		target.css({
			'width': the_window.width(),
			'height': 'auto'
		});
	}
	
	var margin_top =  Math.floor((the_window.height() / 2) - (target.height() / 2)) - 70;
	var margin_left = Math.floor((the_window.width() / 2) - (target.width() / 2));
	
	jQuery(target).css({
		'top': margin_top + 'px',
		'left': margin_left + 'px'
	});
}

function setupBodyHeight() {
	body = jQuery('body');

	body.height('auto');
	
	if (jQuery('#canvas').height() < body.height()) {
		
		body.height('100%');
	}
	else if (jQuery('#canvas').height() != body.height()) {
		
		body.height('auto');
	}
}

function checkDate(date) {
	
	if (new Date(date).compareTo(new Date.today()) == -1) {
		
		alert('The chosen date is in the past. Please choose a new date.');
		return false;
	}
	
	if (new Date(date).compareTo(new Date.today()) == 0) {
		
		alert('The chosen date is today. Please choose a new date.');
		return false;
	}
	
	return true;
}

function checkDates(dateA, dateB) {
	
	if (new Date(dateA).compareTo(new Date(dateB)) == 1) {
		
		alert('Your arrival date is after your departure date. Please choose a different date.');
		return false;
	}
	
	if (new Date(dateA).compareTo(new Date(dateB)) == 0) {
		
		alert('Your departure date is the same as your arrival date. Please choose a different date.');
		return false;
	}
	
	return true;
}

$(document).ready(
	function() {
		
		Cufon.replace('#footer h1, #footer h2, #footer h3, #footer h4, #sub-background h2, #copy h3, #sidebar h4');
		
		backgroundImageActions();
		
		$('#reservations a.submit').click(
			function() {
				
				$('#reservations-form').submit();
				
				return false;
			}
		);
		
		$('#reservations input.calendar').blur(
			function() {
				
				if ($(this).val() != '') {
					if (!checkDate($(this).val())) {
						$(this).val('');
					}
				}
				
				if ($(this).val() != '' && $(this).closest('input.calendar').val() != '') {
					
					if ($(this).attr('rel') == 'ArvDate') {
						var DepDate = $('#DepDate').val()
						var ArvDate = $(this).val();
					}
					else {
						var ArvDate = $('#ArvDate').val();
						var DepDate = $(this).val();
					}
					
					if (checkDates(ArvDate, DepDate)) {
						$('#iNgts').val(new Date(DepDate).getDayOfYear() - new Date(ArvDate).getDayOfYear());
					}
					else {
						$(this).val('');
					}
				}
			}
		);
		
		$('#reservations .choose').datePicker({
			createButton: false,
			startDate: Date.today().addDays(1).toString('dd/MM/yyyy')
		}).bind(
			'click',
			function() {
				
				$(this).dpDisplay();
				this.blur();
				return false;
			}
		).bind(
			'dateSelected',
			function(e, selectedDate, $td) {
				
				var currentDate = selectedDate.toString('MM/dd/yyyy');
				
				if ($(this).attr('rel') == 'ArvDate') {
					var DepDate = $('#DepDate').val()
					var ArvDate = currentDate;
				}
				else {
					var ArvDate = $('#ArvDate').val();
					var DepDate = currentDate;
				}
				
				$('#' + $(this).attr('rel')).val(currentDate);
				
				if (ArvDate != '' && DepDate != '') {
					
					if (checkDates(ArvDate, DepDate)) {
						$('#iNgts').val(new Date(DepDate).getDayOfYear() - new Date(ArvDate).getDayOfYear());
					}
					else {
						$('#' + $(this).attr('rel')).val('');
					}
				}
				
				if (!checkDate(currentDate)) {
					$('#' + $(this).attr('rel')).val('');
				}
			}
		).dpSetPosition($.dpConst.POS_BOTTOM, $.dpConst.POS_LEFT);
		
		$('#reservations .choose.arrive').bind(
			'dpClosed',
			function(e, selectedDates) {
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#reservations .choose.depart').dpSetStartDate(d.addDays(1).asString());
				}
			}
		);
		
		$('#reservations .choose.depart').bind(
			'dpClosed',
			function(e, selectedDates) {
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#reservations .choose.arrive').dpSetEndDate(d.addDays(-1).asString());
				}
			}
		);

		$('#canvas .fullscreen').load(
			function() {
				centerBackgroundImage(jQuery(this));
			}
		);
		
		if ($('#copy-footer').length == 1) {
			
			$('#footer #information').appendTo('#copy-footer');
			$('#footer #share').appendTo('#copy-footer');
		}
		
		if ($('#canvas .fullscreen').length > 1) {
			
			setupBackgrounds();
		}
	}
);

jQuery(window).resize(
	function() {
		
		centerBackgroundImage(jQuery('#canvas .fullscreen'));
		backgroundImageActions();
	}
);