$(document).ready(function(){

	var url = window.location.href.split("/");	

	//form validation
	$("#post_event_f").validate();
	
	$(".poi_images a").lightBox({
		maxHeight: 600,
		maxWidth: 900
    });
	
	//Member details page - load form from the previous page
	//if ($("#previous_form").length > 0 && $(document.referrer +" .search_form") > 0 && $(document.referrer +" #topmenu") > 0)  {
	if (document.referrer != '') {
		if ($("#previous_form").length > 0)  {
			$("#previous_form").load(document.referrer +" .search_form", function(){
				$("#sort_hotels_f #search_btn, #search_meeting_facilities_f  #search_btn").click(function(){
				
					var city = $("#city").val();
					var region = $("#region").val();
					if ($("#type").length > 0) {
						var type = $("#type").val();
					} else {
						var type = "0";
					}
					window.location = document.referrer + "#search=true&city="+ city +"&region="+ region +"&type="+ type;
					return false;
				});
				$(".complete_listing").hide();
			});
			
			//load subnavigation from the previous page
			$("#topmenu-position").load(document.referrer +" #topmenu", function(){
				Cufon.replace('#topmenu-firstlevel li a, #topmenu-firstlevel li span, #topmenu-secondlevel li a, #forecast #temp, #forecast #fiveday_forecast');
				
				var prev_url = document.referrer.split("/");		
		
				if (prev_url[4]!= undefined) {
					var url_part = "/"+prev_url[3]+"/"+prev_url[4];
				} else if (prev_url[3]!= undefined) {
					var url_part = "/"+prev_url[3];
				} else {
					var url_part = "/";
				}
				
				$("ul#nav li a").each(function(){
					if ($(this).attr("href")==url_part) {
						$(this).addClass("on");
					}
				});
				
				//subnav 'on' states-------------------------------------------
				if (prev_url[4] != undefined) {
					var subpage_name = (prev_url[4].split("#"))[0];
					$("#topmenu-firstlevel li a").each(function(){
						var href_part4 = ($(this).attr("href").split("/"))[2]
						if (href_part4==subpage_name) {
							$(this).addClass("on");
						}
					});
				}
				
				//subsubnav 'on' states-------------------------------------------
				if (prev_url[5] != undefined) {
					var subpage_name = (prev_url[5].split("#"))[0];
					$("#topmenu-secondlevel li a").each(function(){
						var href_part5 = ($(this).attr("href").split("/"))[3]
						if (href_part5==subpage_name) {
							$(this).addClass("on");
						}
					});
				}
				
			});
			
			
		}
	}
	
	
	//datepicker
	
	$(".cal input, input.cal").datepicker();

	//Main Nav hover -----------------------------------------------
	
	$("#hover_tooltip").load("/Navigation-Tooltips #left_wide_col .nav_pop", function(){
		$(".Dine ul li a").click(function(){
			if (url[3].split("#")[0] == "Dine") {			
				window.location.hash = ($(this).attr("href")).split("#")[1];
				window.location.reload();
			}
		});
		$(".Events ul li a").click(function(){
			if (url[3].split("#")[0] == "Events") {
				window.location = $(this).attr("href");
				window.location.reload();
			}
		});
	});
	
	var item_top = 0;
	var item_left = 0;
	var tooltip_top = $("#content").offset().top;
	var tooltip_left = $("#content").offset().left;
	var tooltip_height;
	var text;
	var item_id;
	var x = 0;
	var y = 0;
	$("#nav li a").mouseover(function(){
		$(".nav_pop").hide();
		text = $(this).text();
		item_id = text.toLowerCase();
		$("div."+item_id+"").show();
		
		var offset = $(this).offset();
		item_top = offset.top;
		item_left = offset.left;
		item_width = $(this).width();
		tooltip_height = $("div."+item_id+"").height();
	});
	
	$().mousemove(function(e){
		if ($(".nav_pop").is(":visible"))
		{
			x = e.pageX;
			y = e.pageY;
			//hide if move mouse out of area of tooltip and item
			if (y<item_top || y>tooltip_top+tooltip_height || x<tooltip_left || x>tooltip_left+930 || (x<item_left-28 && y<tooltip_top-43) || (x>item_left+item_width+28 && y<tooltip_top-43))
			{
				$(".nav_pop").hide();				
			}
		}
	});
	
	
	//Main Nav 'on' states------------------------------------------
	
	
	if (url[4]!= undefined) {
		var url_part = "/"+url[3]+"/"+url[4];
	} else if (url[3]!= undefined) {
		var url_part = "/"+url[3];
	} else {
		var url_part = "/";
	}
	
	$("ul#nav li a").each(function(){
		if ($(this).attr("href")==url_part) {
			$(this).addClass("on");
		}
	});
	
	//subnav 'on' states-------------------------------------------
	if (url[4] != undefined) {
		var subpage_name = (url[4].split("#"))[0];
		$("#topmenu-firstlevel li a").each(function(){
			var href_part4 = ($(this).attr("href").split("/"))[2];
			if (href_part4==subpage_name) {
				$(this).addClass("on");
			}
		});
	}
	
	//subsubnav 'on' states-------------------------------------------
	if (url[5] != undefined) {
		var subpage_name = (url[5].split("#"))[0];
		$("#topmenu-secondlevel li a").each(function(){
			var href_part5 = ($(this).attr("href").split("/"))[3];
			if (href_part5==subpage_name) {
				$(this).addClass("on");
			}
		});
	}
	
	//tabs on states (static pages)
	if ($("#tabs_nav").length>0) {	
		if (url[5] != undefined) {
			var page_name = (url[5].split("#"))[0];
			$("#tabs_nav li a span").each(function(){
				var tab_name = ($(this).text()).replace(/ /g, '-');
				if (page_name == tab_name) {
					$(this).parent("a").addClass("on");
				}
			});
		} else if (url[4] != undefined) {
			var page_name = (url[4].split("#"))[0];
			$("#tabs_nav li a span").each(function(){
				var tab_name = ($(this).text()).replace(/ /g, '-');
				if (page_name == tab_name) {
					$(this).parent("a").addClass("on");
				}
			});
		}
	}
	
	//reservation widget ------------------------------------------
	
	//home page - expanded
	$('#left_col .book_content:first').show();
	$('#left_col .section_name:first').addClass('on');
	//Stay section - expanded
	if (url[3] == "Stay" || url[3] == "Dine" || url[3] == "Play") {
		$('.book_content:first').show();
		$('.section_name:first').addClass('on');
	}
	
	$(".section_name").click(function(){
		if ($(this).next('.book_content').is(':hidden')) {
			$('.book_content').hide();
			$('.section_name').removeClass('on');
			$(this).addClass('on');
			$(this).next('.book_content').show();
		} else {
			$('.book_content').hide();
			$('.section_name').removeClass('on');
		}
		return false;
		
	});
	
	//DHTML tabs---------------------------------------------------
	
	if ($(".tab_container").length>0) {
		
		var tabs = "<ul id='tabs'>";
		var tab_class;
		var tab_head_text;
		var	page_name = (url_part.split("#"))[0];
		$(".tab_container h2").each(function(){
			tab_class = $(this).attr("class");
			tab_head_text = $(this).text();
			tab_link = tab_head_text.replace(/ /g, "-");
			$(this).remove();
			tabs += "<li><a href='"+page_name+"#"+tab_link+"' class='"+tab_class+"'><span>"+tab_head_text+"</span></a></li>";			
		});
		tabs += "</ul>";
		$("#left_wide_col").prepend(tabs);
		$("div.tab_container").addClass("tab_hidden");
		$("div.tab_container:first").removeClass("tab_hidden");
		
		//billboard image for each tab
		
		$(".tab_container").each(function(){
			$(".attribute-billboard").append($(this).children(".billboard_image").html());
			$(this).children(".billboard_image").remove();
		});
		
		
		//clicking on tabs
		$("#tabs li a").click(function(){
			if (! $(this).hasClass("on") ) {
				tab_class = ($(this).attr("class")).replace(/ /g, "");
				$("#tabs li a").removeClass("on");
				$(this).addClass("on");
				$("div.tab_container").addClass("tab_hidden");
				$("div."+tab_class+"").removeClass("tab_hidden");
				$(".attribute-billboard img").hide();
				$(".attribute-billboard img.img_"+tab_class+"").show();
				
			}
		});
		
		//set url for the first tab - first page loading
		
		if (!(url_part.split("#"))[1]) {
			$("#tabs li a:first").click();
			//set url
			window.location.hash = ($("#tabs li a:first").attr("href")).split("#")[1];
		}
		
		
		//first page loading - redirection from the other page
		if ((url_part.split("#"))[1]) {
			var tab_name_ie = (url_part.split("#"))[1];			
			$("#tabs li a span:contains('"+tab_name_ie+"')").parent("a").click();
		}
		
		//enable linking from tab content link to other tab (with refreshing)
		$(".tab_content a").click(function(){
			window.location.hash = ($(this).attr("href")).split("#")[1];
			window.location.reload();
		});
		
	}
	
	
	// initialize scrollable 
	$("div.scrollable").scrollable({
        size: 1,
        naviItem: "span",
        // items are auto-scrolled in 8 secnod interval 
        interval: 8000, 
        loop: true,  
        speed: 500,
        
        // when seek starts make items little transparent 
        onBeforeSeek: function() { 
            this.getItems().fadeTo(300, 0.2);         
        }, 
        // when seek ends resume items to full transparency 
        onSeek: function() { 
            this.getItems().fadeTo(300, 1); 
        }
        
    });
    
    //post event form
    $("#post_event_f #recurring_event").change(function(){
		if ($(this).is(":checked")) {
			$("#recurrence_pattern").show();
		} else {
			$("#recurrence_pattern").hide();
		}
    });
    
    
    //photo gallery
    $(".gallery").addClass("main_gallery"); // adds new class name to maintain degradability
	
	$("ul.main_gallery").galleria({
		history   : true, // activates the history object for bookmarking, back-button etc.
		clickNext : true, // helper for making the image clickable
		//insert    : '#main_image', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
			
			// fade in the image & caption
			if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				image.css('display','none').fadeIn(1000);
			}
			caption.css('display','none').fadeIn(1000);
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.3);
			
			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');
			
			// add a title for the clickable image
			image.attr('title','Next image >>');
		},
		onThumb : function(thumb) { // thumbnail effects goes here
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.3';
			
			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
			
			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			)
		}		
	});
	
	if ($(".gallery").length > 0) {
		$("p.nav").clone(true).insertAfter(".galleria_container");
		$("p.nav:last").css("display", "none");
	}
	
	
	//show the first gallery image
	$(".galleria_wrapper").html($("ul.galleria li:first").html());
	$(".galleria_wrapper img").css("display", "block");
	$("ul.galleria li:first").addClass("active");
	
	
	//press releases list---------------------------------------------------
	
	var press_month = $("#month").val();
	var press_year = $("#year").val();
	
	if (press_month != 'Month') {
		$(".press_release_list li").each(function(){			
			var press_date = $(this).children("span.date").text();
			if (press_date.substring(0,2) != press_month) {
				$(this).remove();
			}
		});
	}
	if (press_year != 'Year') {
		$(".press_release_list li").each(function(){
			var press_date = $(this).children("span.date").text();
			if (press_date.substring(6,10) != press_year) {				
				$(this).remove();
			}
		});
	}
	
	//press list pagination
	var per_page = 10;
	//$(".press_release_list li:gt("+(per_page-1)+")").hide();
	var li_items = $(".press_release_list li").length;
	var pages;
	if (li_items%per_page != 0) {
		pages = parseInt(li_items/per_page) +1;
	} else {
		pages = parseInt(li_items/per_page);
	}
	var pagination_html = "<ul class='pagination'";
	for (i=1; i<pages; i++) {
		pagination_html += "<li><a class='"+i+"' href='#'>"+i+"</a><span>|</span></li>"
	}
	pagination_html += "<li><a class='"+pages+"' href='#'>"+pages+"</a></li>";
	pagination_html += "</ul>";
	
	if (pages == 1) {
		$("#release_matches").html(li_items+" MATCHES FOUND.");
	} else {
		$(".press_release_results").append(pagination_html);
	}
	
	$(".pagination li a").click(function(){
		var page_to_show = $(this).attr("class");
		var first_item_to_show = (page_to_show-1) * per_page;
		var last_item_to_show = first_item_to_show + per_page -1;
		
		$(".press_release_list li").show();
		$(".press_release_list li:lt("+first_item_to_show+")").hide();
		$(".press_release_list li:gt("+last_item_to_show+")").hide();
		
		$(".pagination li a").css("textDecoration", "underline");
		$(this).css("textDecoration", "none");
		
		if (page_to_show != pages) {
			$("#release_matches").html(li_items+" MATCHES FOUND. DISPLAYING "+(first_item_to_show+1)+" TO "+(last_item_to_show+1)+".");
		} else {
			$("#release_matches").html(li_items+" MATCHES FOUND. DISPLAYING "+(first_item_to_show+1)+" TO "+li_items+".");
		}
		
		return false;
	});
	$(".pagination li:first a").click();
	
	
	//---------------------------------
	
	//drop down links
	$(".search_oa #type").change(function(){		
		if (isNaN($(".search_oa #type option:selected").attr("value"))) {
			window.location = $(".search_oa #type option:selected").attr("value");
		}
	});	
	
	//disable right click on gallery
	if ($(".galleria_container").length > 0) {
		$(document).bind("contextmenu",function(e){
			return false;
		}); 
	}	
	
	//ie subnavigation fix
	if ($("#topmenu-firstlevel li").length == 0) {
		$("#topmenu-firstlevel").css("height", "0");
	}
	
	//info box propagation
	if ($(".ad_box").length == 0 && $("#stay_informed").length > 0) {

		var page_depth = url.length - 3;
	
		if (page_depth == 3) {
	
			$("#ad_box_copy").load("http://" + url[2] + "/" + url[3] + "/" + url[4] + " .ad_box:first", function() {
				if ($(".ad_box").length == 0) {
					$("#ad_box_copy").load("http://" + url[2] + "/" + url[3] + " .ad_box:first", function() {
						if ($(".ad_box").length == 0)	$("#ad_box_copy").load("http://" + url[2] + " .ad_box:first");						
					});
				}
			});
	
		} else if (page_depth == 2) {
			$("#ad_box_copy").load("http://" + url[2] + "/" + url[3] + " .ad_box:first", function() {
				if ($(".ad_box").length == 0) {
					$("#ad_box_copy").load("http://" + url[2] + " .ad_box:first");
				}				
			});
	
		} else if (page_depth == 1) {
			$("#ad_box_copy").load("http://" + url[2] + " .ad_box:first");
		}
	
	}
	/*
	function move_info_box() {
		//move info box below the region map		
		
		if (url[3] == "Coastal" || url[3] == "Valley" || url[3] == "Mountain-Desert" || url[3] == "Test") {			
			var info_box = $("#ad_box_copy").html();
			$("#top_img_area").parent("div").append(info_box);
			$(".ad_box:last").parent("div").remove();					
		}
		
	}
	*/
	
	if ($(".search_press_releases").length > 0 || $("#releases").length > 0) {
		
		if ($("#month").val() != "Month" || $("#year").val() != "Year") {
			$("#search_btn1").hide();
		} else {
			$("#search_btn").hide();
		}
		
		if ($("#month").val() == "Month" && $("#year").val() == "Year" && $("#cse_keyword").val() != "") {
			$(".press_release_results").hide();
		} else if ($("#cse_keyword").val() == "") {
			$("#cse-search-results").hide();
		}
		
		$("#month, #year").change(function(){
			if($("#month").val() != "Month" || $("#year").val() != "Year") {
				$("#search_btn1").hide();
				$("#search_btn").show();
				$("#cse_keyword").val("");
				
			} else {
				$("#search_btn").hide();
				$("#search_btn1").show();
			}
		});
		
		$("#cse_keyword").focus(function(){
			$("#month").val("Month");
			$("#year").val("Year");
		});
		
	
	}
	
	//hidden date on forms
	var current_date = $("#current_date").val();
	$("#current_date").next("input").val(current_date);
	
	//your story form validation
	if ($("#your_story_f").length > 0) {
	
		$("#your_story_f label.rd input").removeAttr("checked");
		
		$("#your_story_f").submit(function(){
			$(".req_lab, .req_lab_p, .req_lab_pr").remove();
			
			var valid = true;
			if ($("#ezcoa-944_month").val() == "0" || $("#ezcoa-945_year").val() == "0") {
				$(".month_and_year").append("<span class=\"req_lab\">Required</span>");
				//alert("Month and Year of your visit are required fields");
				valid = false;
			}
			if ($("#ezcoa-955_state").val() == "0") {
				$("div.state").append("<span class=\"req_lab\">Required</span>");
				valid = false;
			}
			if ($("#your_story_f .stay_info_where label.rd input:checked").length == 0) {
				$("div.stay_info_where").append("<span class=\"req_lab\">Required</span>");
				valid = false;
			}
			if ($("#your_story_f .stay_info_who label.rd input:checked").length == 0) {
				$("div.stay_info_who").append("<span class=\"req_lab\">Required</span>");
				valid = false;
			}
			if ($("#your_story_f .first_name input").val() == "") {
				$("#your_story_f .first_name").append("<p class=\"req_lab_p\">Required</p>");
				valid = false;
			}
			if ($("#your_story_f .last_name input").val() == "") {
				$("#your_story_f .last_name").append("<p class=\"req_lab_p\">Required</p>");
				valid = false;
			}
			if ($("#your_story_f .city input").val() == "") {
				$("#your_story_f .city").append("<p class=\"req_lab_pr\">Required</p>");
				valid = false;
			}
			if ($("#your_story_f .email input").val() == "") {
				$("#your_story_f .email").append("<p class=\"req_lab_p\">Required</p>");
				valid = false;
			}	
			if (valid == false) {
				window.scroll(0,295);
				return false;
			}
		});
		
	}
	
	//valentines day form validation
	if ($("#valentine_f").length > 0) {
	
		$("#valentine_f").submit(function(){
			$(".req_lab, .req_lab_p").remove();
			
			var valid = true;
			if ($("#ezcoa-977_state").val() == "0") {
				$(".state").append("<span class=\"req_lab\">Required</span>");
				valid = false;
			}			
			if ($("#valentine_f .first_name input").val() == "") {
				$("#valentine_f .first_name").append("<p class=\"req_lab_p\">Required</p>");
				valid = false;
			}
			if ($("#valentine_f .last_name input").val() == "") {
				$("#valentine_f .last_name").append("<p class=\"req_lab_p\">Required</p>");
				valid = false;
			}
			if ($("#valentine_f .email input").val() == "") {
				$("#valentine_f .email").append("<p class=\"req_lab_p\">Required</p>");
				valid = false;
			}
			if ($("#valentine_f .address input").val() == "") {
				$("#valentine_f .address").append("<p class=\"req_lab_p\">Required</p>");
				valid = false;
			}
			if ($("#valentine_f .city input").val() == "") {
				$("#valentine_f .city").append("<p class=\"req_lab_p\">Required</p>");
				valid = false;
			}
			if ($("#valentine_f .zip input").val() == "") {
				$("#valentine_f .zip").append("<p class=\"req_lab_p\">Required</p>");
				valid = false;
			}
			if ($("#valentine_f .phone input").val() == "") {
				$("#valentine_f .phone").append("<p class=\"req_lab_p\">Required</p>");
				valid = false;
			}
			if (valid == false) {
				return false;
			}
		});
		
	}
	
	//stay informed - email verification
	$("#si_form").submit(function(){
		var entered_email = $("#si_form input.box:visible").val();
		var at = false;
		var dot = false;
		for (i=0; i < entered_email.length; i++) {			
			if (entered_email.substring(i, i+1) == "@") at = true;
			if (entered_email.substring(i, i+1) == ".") dot = true;
		}		
		if (entered_email.length < 7 ||  at == false || dot == false) {
			alert("Please enter a valid email address.");
			return false;		
		}
	});
	
	
});




