$("input.datepicker").datepicker({ 
	showOn:'both',
	buttonImage: '/images/icons/calendar.gif',
	buttonImageOnly: true
});

$("#reservations-submit").click(function(){
	$(this).closest("form").submit();
	return false;
});

// target=_blank compliancy
$("a.blank").each(function(){
	$(this).attr("target", "_blank");
});

// contact inquiry type
var selectbox = $("#inquirytype").change( checkInquiryType );
function checkInquiryType(){
	var self = (this instanceof jQuery) ? this : selectbox;
	$("tr.hide-newsletter")[ $(self).val() === "Email Signup" ? "hide" : "show" ]();
};

// perform check on pg load
checkInquiryType();

// Dropdown menu
(function($){

	function Dropdown(item){
		this.onItem = false;
		this.onSubNav = false;
		this.isOpen = false;
		this.item = $(item);
		this.subnav = this.item.siblings("ul");
		
		// bind events & start
		this.init();
		
		return this;
	}
	
	Dropdown.prototype.start = function(){
		var self = this;
		
		this.timer = window.setInterval(function(){
			if(!self.onItem && !self.onSubNav && self.isOpen){
				self.stop();
			}
		}, 200);
	}
	
	Dropdown.prototype.stop = function(){
		window.clearInterval(this.timer);
		this.subnav.hide();
		this.isOpen = false;
		this.item.removeClass("active");
	}
	
	Dropdown.prototype.init = function(){
		var self = this;
		
		// bind to the main item
		this.item.bind("mouseenter mouseleave", function(e){
			
			// assume no
			self.onItem = false;
			
			if(e.type === "mouseenter"){
				$(this).addClass("active");
				self.start();
				self.subnav.show();
				self.onItem = true;
				self.isOpen = true;
			}
		});
		
		// bind to subnav
		this.subnav.bind("mouseenter mouseleave", function(e){
			self.onSubNav = (e.type === "mouseenter") ? true : false;
		});
	}
	
	// use this dropdown class on each navigation link that has a sibling subnav
	$("#navigation > li a:has(~ ul)").each(function(){
		return new Dropdown(this);
	});
})(jQuery);
