/* reservations */
var reservations = new function() {
	this.setup = function() {
		jQuery("#checkoutDate").datepicker({dateFormat: 'mm/dd/yy'});
		jQuery("#checkinDate").datepicker({
			dateFormat: 'mm/dd/yy', 
			onClose: function(date) {
				var departure = new Date(date);
				departure.setDate(departure.getDate() + 2);
				jQuery("#checkoutDate").datepicker('setDate', departure);
			}
		});
	};
	
	this.submit = function() {
		if (typeof(_gaq) != "undefined") {
			_gaq.push(['_trackEvent', 'Book Now', 'Click', 'Reservations Mask']);
		}
		document.frm1.submit();
		return false;		
	};
	/*
	this.submit = function() {
		if (typeof(pageTracker) != "undefined") {
			pageTracker._trackPageview('/revenue/genaresResMask');
			pageTracker._linkByPost(document.frm1);
		}
		document.frm1.submit();
		return false;		
	};
	*/
};



/* menu */
var menu = new function() {
	this.slide_speed = 250;
	this.timer_delay = 1000;  //time it takes the main menu to close automatically after the mouse moves away
	
	this.menu_id = "main_menu";
	this.q = new Array();
	this.timer = 0;
	this.items = ['item1','item2','item3','item4','item5','item6'];
	
	this.setup = function() {
		for(var i=0; i<this.items.length; i++) {
				var item_id = this.items[i];
				
				jQuery('#' + this.menu_id + " li." + item_id)
					.data('item_id', item_id)
					.hover(
								function() { 
									menu.queue(jQuery(this).data('item_id'), 0); 
								}, 
								function() { 
									menu.queue(jQuery(this).data('item_id'), 1); 
								} 
							);	
		}
	};
	
	this.restart = function() {
		clearInterval(this.timer);
		this.timer = setInterval("menu.tick()", this.timer_delay);
	};
	
	this.queue = function(item_id, bHide) {
		if (bHide == 1) {
			var bFound = false;
			for(var j=0; j<this.q.length; j++) {
				if (this.q[j] == item_id) {
					bFound = true;	
					break;
				}
			}
			if (!bFound) {
				this.q.push(item_id);
			}
			this.restart();
		} else {
			if (jQuery("#" + this.menu_id + " li." + item_id).children('ul').length > 0) {
				jQuery("#" + this.menu_id + " li." + item_id).children('ul').slideDown(this.slide_speed);
				jQuery("#" + this.menu_id + " li." + item_id).addClass('dyn_active');
			
				var new_menu = new Array();
				for(var i=0; i<this.q.length; i++) {
					if (this.q[i] != item_id)
						new_menu.push(this.q[i]);
				}
				this.q = new_menu;
				
				this.close_all(item_id);
			}
		}
	};

	this.close = function(item_id) {
		jQuery("#" + this.menu_id  + " li." + item_id).removeClass('dyn_active');
		jQuery("#" + this.menu_id  + " li." + item_id).children('ul').slideUp(this.slide_speed);	
	};


	this.close_all = function(item_id) {
		for( var i=0; i<this.items.length; i++) {
			var cur_item = this.items[i];
			if (cur_item != item_id)
				this.close(cur_item);
		}
	};

	this.tick = function() {
		for(var i=0; i<this.q.length; i++) {
			this.close(this.q[i]);
		}
		this.q = new Array();
	};
	
};

/* image rotate */
var image_rotate = new function() {
	this.image_delay = 8000;	//time (ms) between each slide change
	this.transition_time = 500; //time (ms) spent animating between slides
	this.base_dir = '/images/slides/';
	this.container_selector = "#main_display";	

	this.slide_css =
		{
			position: "absolute",
			top: 0,
			right: 0,
			display: "none"
		};

	this.slides = ['slide1.jpg', 'slide2.jpg', 'slide3.jpg', 'slide4.jpg', 'slide5.jpg', 'slide6.jpg'];
	this.index = 0; //current index
	this.timer = 0; //main timer	
	
	this.container = null; //jquery object
	this.container_slides = []; //array of jquery slides
	this.preload_arr = [];

	this.setup = function(slides) {
		this.container = jQuery(this.container_selector);

		this.setupHTML();
		this.start();
		this.preload();
	};
	
	this.preload = function() {
		for(var i=0; i<this.slides.length; i++) {
			var img = new Image();
			img.src = this.base_dir + this.slides[i];
			this.preload_arr.push(img);
		}
	};


	this.change = function() {	
		var next_id = this.index + 1;
		if (next_id >= this.slides.length)
			next_id = 0;

		var current_slide = this.container_slides[this.index];
		var next_slide = this.container_slides[next_id];
		
		current_slide.css('z-index',10);
		next_slide.css('z-index',9);
		next_slide.show();
		
		current_slide.fadeOut(this.transition_time);
		
		this.index = next_id;
	};
		
	this.start = function() {
		this.timer = setInterval("image_rotate.tick()", this.image_delay);
	};
	
	this.setupHTML = function() {
		this.container.html("");
		
		for(var i=0; i<this.slides.length; i++) {
			var slide = jQuery('<div />').css(this.slide_css).html('<img src="' + this.base_dir + this.slides[i] + '" />');
			this.container_slides.push(slide);
			this.container.append(slide);
		}		
		this.container_slides[0].show();
	};
	
	this.tick = function() {
		this.change();
	};

};


/* setup secripts */
jQuery(document).ready(function(){
	reservations.setup();
	menu.setup();			
	image_rotate.setup();
	
	var img = new Image();
	img.src = "/images/nav-subnav-bg.png";
	image_rotate.preload_arr.push(img);
});

