$("document").ready(function(){

	// Set highest z-index for the mini cart
	$('#mini_cart_box').css( "z-index", "1000" );
	
	/*Mini Shoping Cart*/
	$("#mini_cart").mouseover(function(){
		$(this).addClass("mini_cart_active");
		$("#mini_cart_box").show();
	}).mouseout(function(){
		$(this).removeClass("mini_cart_active");
		$("#mini_cart_box").hide();
	});

	function highlightDay(date){

			//Arrival
			if(this.id == 'arrival'){

				var arrival_date = loadArrivalTimestamp();
				var departure_date = loadDepartureTimestamp();
				var today = parseInt(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));

				if(arrival_date == today){
					return [true, ''];
				}else if(departure_date > 0 && today <= departure_date && today > arrival_date){
					return [true, 'date_range'];
				}else{
					return [true, ''];
				}

			}

			//Departure
			if(this.id == 'departure'){

				var arrival_date = loadArrivalTimestamp();
				var departure_date = loadDepartureTimestamp();
				var today = parseInt(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));

				if(arrival_date == today){
					return [false, 'date_arrival'];
				}else if(arrival_date >= today){
					return [false, ''];
				}else if(departure_date > 0 && today <= departure_date){
					return [true, 'date_range'];
				}else{
					return [true, ''];
				}

			}

			return [true, ''];

	}

	function loadArrivalTimestamp(){

			var arrival = $("#arrival").val();
			var a = arrival.split("-");
			var arrival_date = Date.UTC(a[2], a[0]-1, a[1]);

			return parseInt(arrival_date);

	}

	function loadDepartureTimestamp(){

			var departure = $("#departure").val();
			var d = departure.split("-");
			var departure_date = Date.UTC(d[2], d[0]-1, d[1]);

			return parseInt(departure_date);

	}

	/*Date Picker*/
	window.addDatepicker = function(target) {
		$(target).datepicker({
			showOn: 'button',
			buttonImage: '/images/calendar.png',
			buttonImageOnly: true,
			numberOfMonths: 3,
			minDate: '+1D',
			maxDate: '+6M +1Y',
			dateFormat: 'mm-dd-yy',
			inline: true,
			gotoCurrent: true,
			onSelect: function(selectDate){

				if(this.id == 'arrival'){

					var arrival = loadArrivalTimestamp();
					var departure = loadDepartureTimestamp();

					//Departure before arrival or departure is blank
					if(arrival >= departure || isNaN(departure)){

						var new_departure = new Date(arrival+86400000*5);
						var month = new_departure.getMonth()+1;
						if(month < 10){
							month = "0"+month;
						}
						var day = new_departure.getDate();
						if(day < 10){
							day = "0"+day;
						}
						var year = new_departure.getFullYear();

						var new_departure_string = month+"-"+day+"-"+year;
						$("#departure").val(new_departure_string);

					}
	
				}

			},
			beforeShowDay: highlightDay
		});
	}
	addDatepicker('.datepicker');

	/*Login*/
	$("#login_link").fancybox({
		'width'           : 500,
		'height'					: 315,
		'transitionIn'    : 'none',
		'transitionOut'   : 'none',
		'type'            : 'iframe',
		'padding'         : 0,
		'scrolling'       : 'no',
		'centerOnScroll' 	: true
	});

	/*Create Account*/
	$("#create_account_link").fancybox({
		'width'           : 500,
		'height'          : 590,
		'transitionIn'    : 'none',
		'transitionOut'   : 'none',
		'type'            : 'iframe',
		'padding'         : 0,
		'scrolling'       : 'no',
		'centerOnScroll' 	: true
	});

	/*Checkout Create Account*/
	$("#cart_create_account_link").fancybox({
		'width'           : 500,
		'height'          : 590,
		'transitionIn'    : 'none',
		'transitionOut'   : 'none',
		'type'            : 'iframe',
		'padding'         : 0,
		'scrolling'       : 'no',
		'centerOnScroll' 	: true
	});

	/*Lodging Results More Details*/
	$(".lodging_details").fancybox({
		'width'           : 575,
		'height'          : 590,
		'transitionIn'    : 'none',
		'transitionOut'   : 'none',
		'type'            : 'iframe',
		'padding'         : 0,
		'scrolling'       : 'yes',
		'centerOnScroll' 	: true
	});

	/*Lodging Results More Details*/
	$(".no_price_note").fancybox({
		'width'           : 300,
		'height'          : 250,
		'transitionIn'    : 'none',
		'transitionOut'   : 'none',
		'type'            : 'iframe',
		'padding'         : 0,
		'scrolling'       : 'no',
		'centerOnScroll' 	: true
	});

	/*Lodging Results More Details*/
	$(".lodging_search_lightbox").fancybox({
		'width'           : 650,
		'height'          : 250,
		'transitionIn'    : 'none',
		'transitionOut'   : 'none',
		'type'            : 'iframe',
		'padding'         : 0,
		'scrolling'       : 'no',
		'centerOnScroll' 	: true
	});

});


/*Validation Function*/
jQuery.fn.addTextFieldError = function(){
	this.css("border", "1px solid #FF0000");
};

jQuery.fn.removeTextFieldError = function(){
	this.css("border", "1px solid #CCCCCC");
};

jQuery.fn.addLabelError = function(){
	this.css("color", "#FF0000");
};

jQuery.fn.removeLabelError = function(){
	this.css("color", "#000000");
};


