jQuery(document).ready(function($){ 
	// booking widget toggle
	$("#bookWidgetToggleBtn.openBtn").live("click",
		function(){
		    $(this).removeClass("openBtn").addClass("closeBtn");
			$("#widgetFormWrap").show();
		}
	);
	$("#bookWidgetToggleBtn.closeBtn").live("click",
		function(){
		    $(this).removeClass("closeBtn").addClass("openBtn");
			$("#widgetFormWrap").hide();
		}
	);
	 
	// booking widget calendars
	$("#bw_checkin").datepick({
		minDate: new Date(),
		onClose: function(theDate){  
			// set departure date to 7 days after arrival date
			var departDate = theDate[0];
			$.datepick.add(departDate, 7, 'd');
			$("#bw_checkout").datepick('setDate', departDate)
		}	
	});
	$("#bw_checkout").datepick({minDate: new Date()});
	// click calendars activate calendar
    $("#widgetFormWrap img.cal").click(function(){
		$(this).siblings("input.date").focus();
	});   
	
	// booking path interstitual from top nav
	// or any other plain link
	$(".bookingPath").click( 
		function(e){ 
			// pass href along for the redirect
			var redirUrl = $(this).attr("href");
			esh_booking_redirect(redirUrl); 
			// prevent the link from working, so they can't just click thru without the turnstile
			return false;
		}	
	);
	
	// close the modal with the "go now" button in it is clicked
	$("#bookRedirectLink").click(function(){
		$.modal.close();
	});
	
	// booking widget button rollover
	eshVars.bookNowBtnOver = $('<img />').attr('src', eshVars.themeDir + '/images/btn-book-now-160x36-over.png'); 
	eshVars.bookNowBtn = $('<img />').attr('src', eshVars.themeDir + '/images/btn-book-now-160x36.png');  
	$("#bookFormBtn").hover(
		function(){
			$(this).attr('src', eshVars.bookNowBtnOver.attr('src'));  
		},
		function(){
			$(this).attr('src', eshVars.bookNowBtn.attr('src'));
		}	
	);
	
	
	if ($("#photoFooter").length > 0){
   
		// flickr photos in the footer  
		// all the photos default to faded
		// and go full strength when the mouse enters the area
		$("#photoFooter").mouseenter(function(){
			$(this).removeClass("fadePhotos");
		}); 
		$("#photoFooter").mouseleave(function(){
			$(this).addClass("fadePhotos"); 
			$(".photoOver").hide();
		});
	    // image overlays when the mouse is over a photo  
		// flickr ones
		$("#flickrWrap a.photo img").live("mouseover", 
			function(){
				var offset = $(this).offset();
				var link = $(this).attr("id"); 
				$("#flickrOver").show();
				$("#flickrOver a").attr("href", link); 
				$("#flickrOver").offset({ top:offset.top-1, left:offset.left-1});  
			}); 
		// desktop image
		$("#desktopWrap a.photo img").mouseover(
			function(){ 
				var offset = $(this).offset();
				var link = $(this).parents("a").attr("href");
				$("#desktopOver").show(); 
				$("#desktopOver a").attr("href", link);
				$("#desktopOver").offset({ top:offset.top-1, left:offset.left-1});  

			});	
		// the over image takes the mouse event when placed over the photo
		$(".photoOver").mouseout( 
			function(){
				$(this).hide();
		});
	
		// video play buttons
		$(".screenshot").hover(
			function(){
				$(this).find(".overlay").addClass("overlayOn");
			},
			function(){
				$(this).find(".overlayOn").removeClass("overlayOn");
			}	
		);  
	  
	    // web cam
		// coming soon, update once web cam is available
		$("#btnLiveCam, .webCam").click(function(){
			alert("Trust us, it's beautiful here (web cam coming soon).");
		});
	
	
		// load photos from flickr.
		// keep this at the bottom of doc ready, its at the bottom of the page anyways
		$.getJSON("http://api.flickr.com/services/feeds/groups_pool.gne?jsoncallback=?",
		  {
		    id: "1603133@N24",
		    format: "json"
		  },
		  esh_flickr_photos
		  );
	  } // end if #photoFooter           
	  
	
}); // end doc ready    
/* **************************************** */

// process form info into url params & pass to the modal  
function bookingFormSubmit(){
	$ = jQuery; 
	var submitUrl = $("#bookingWidgetForm").attr("action");
	var params = "";
	// build redirect url from form params  
	$("#bookingWidgetForm input").each(
		function(){ 
			
			var val = $(this).val();
			var key = $(this).attr('id').split("bw_");
			key = key[1]; 
			// get the values for the plain numeric values
			// adults, children, rooms
			if (val && key && (!isNaN(val))){
				params += "&" + key + "=" + val;
			}  
			// get the dates & split in day month year
			if ($(this).hasClass("date") && val){
				//console.log(key + " : " + val); 
				params += "&" + key + "=" + val;
			} 
			// for the deals/promotions hidden value, which is gleaned from the deal's url
			if(key == "promoPass"){
				params += "&" + val;
			}	   
		}	
	);	

	esh_booking_redirect(submitUrl + params);
}	

// this shouldn't be called, but just in case...
function esh_booking_modal(redirUrl){ 
 	esh_booking_redirect(redirUrl); 
}	

// called when booking redirect modal opens 
// updates "go now" link
// launches redirect countdown 
// sets tracking variable
function esh_booking_redirect(redirUrl){ 
	var turnstileUrl = "/esh_turnstile.php?redirUrl=" + encodeURIComponent(redirUrl); 
	window.location = turnstileUrl;
} 	

// takes a css value string ("100px") and returns the numeric value (100)
function esh_get_num_from_px(str) {
	var tmp = str.split("px");
	tmp = parseInt(tmp[0]);
	return(tmp);
}

function esh_flickr_photos(data){
	$ = jQuery;
	$.each(data.items, function(i,item){ 
		
		var smImg = item.media.m.replace("_m.jpg", "_s.jpg") 
		// id being used to reassing the link to the mouseover image
		var photoImg = '<span><img src="' +smImg+ '" height="56" width="56" id="'+ item.link +'"></span>'; 
			
	  	var photoBlock = $("<a>")
			.attr("href", item.link)
			.attr("target", "_blank")
			.addClass("photo")
			.html(photoImg);
		   
	  $("#flickrWrap").append(photoBlock);

      if ( i == 11 ) return false;
    });
}   
