

function setSubNavs() {
	$('#mainnav ul li')
		.each(
			function() {
				if($('ul', this).length) {
					
					var top = $(this).offset().top;
					var left = $(this).offset().left

					var submenu = $('ul', this);
					
					// Get max width of all LIs
					submenu.css({height:1, overflow:'auto'}).show();
					var max_width = submenu.width();
					$('li', submenu).each(function(){ max_width = Math.max($(this).width(), max_width); });
					
					submenu.css({height:'auto', overflow:''}).hide();
										
					//Sets position
					submenu.css({'top':top+32, 'left': left});
					//Sets UL height
					submenu.height($('li', submenu).length * 31);

					
					if(max_width < $(this).width()) {
						submenu.width($(this).width() + 2);
						$('li', submenu).width($(this).width());
					} else {
						submenu.width(max_width + 2);
						$('li', submenu).width(max_width);
					}
					
										
					$(this)
						.hoverIntent({
							over: function(){
								$('ul', this).slideDown(Math.min(Math.max(2.5, Math.sqrt($('ul li', this).length)), 6) * 100);
							}, 
							out: function() {
								$('ul', this).slideUp(Math.min(Math.max(2.5, Math.sqrt($('ul li', this).length)), 6) * 75);
							},
							sensitivity: 200,
							interval: 50,
							timeout: 250
						});
				}
			}
		);
}

function getCssHeight(el) {
	var height = el.height();
	height += parseInt(el.css("padding-top"));
	height += parseInt(el.css("padding-bottom"));
	return height; 
}

function getCssWidth(el) {
	var width = el.width();
	width += parseInt(el.css("padding-left"));
	width += parseInt(el.css("padding-right"));
	return width; 
}

function setAyhNav() {
	
	var parent_li= $("#topnav li:contains('about your holiday')");

	var top = parent_li.offset().top;
	var left = parent_li.offset().left

	var submenu = $('ul', parent_li);

	// Get max width of all LIs
	submenu.css({height:1, overflow:'hidden', position:'absolute', top:'-999px', left:'-999px', zIndex:'200'}).show();
	var max_width = submenu.width();
	var li_height = getCssHeight(submenu.find("li"));
	$('li', submenu).each(function(){ max_width = Math.max(getCssWidth($(this)), max_width); });
	submenu.css({height:'auto', overflow:''}).hide();

	//Sets position
	submenu.css({'top':top + getCssHeight(parent_li), 'left': left  + parseInt(parent_li.css("padding-left")) - parseInt(parent_li.find("li").css("padding-left")) });
	//Sets UL height
	submenu.height($('li', submenu).length * li_height);
	
	//Set the UL width and the LI widths
	if(max_width < parent_li.width()) {
		submenu.width(parent_li.width() + 2);
		$('li', submenu).width(parent_li.width());
	} else {
		submenu.width(max_width + 2);
		$('li', submenu).width(max_width - parseInt($('li', submenu).css("padding-left")) - parseInt($('li', submenu).css("padding-right")));
	}
	
						
	parent_li
		.hoverIntent({
			over: function(){
				submenu.slideDown(Math.min(Math.max(2.5, Math.sqrt($('li', submenu).length)), 6) * 100);
			}, 
			out: function() {
				submenu.slideUp(Math.min(Math.max(2.5, Math.sqrt($('li', submenu).length)), 6) * 75);
			},
			sensitivity: 200,
			interval: 50,
			timeout: 250
		});

		/*** FOOTER NAV ****/
		var footer_li = $("#footer li:contains('your holiday')");
		var footer_li_a = $('a', footer_li);
		var new_ayh = $(".ayh").clone(true).appendTo(footer_li);
		var top_val = footer_li_a.offset().top - 1;
		var left_val = footer_li_a.offset().left;
		var height_val = new_ayh.show().height();

		new_ayh
			 .hide()
			 .find("li")
			.css({
				background: '#fff',
				padding: '2px 5px'
			})
			.end()
			.css({
				overflow: 'hidden',
				height: 0,
				top: top_val,
				left: left_val - parseInt($("li", new_ayh).css("padding-left")),
				border: '0px',
				padding: '0'
			});

		footer_li
				.hoverIntent({
					over: function(){
						new_ayh
							.show()
							.animate({
									top: top_val - height_val,
									height: height_val
								},
								Math.min(Math.max(2.5, Math.sqrt($('li', new_ayh).length)), 6) * 100);
					}, 
					out: function() {
						new_ayh
							.animate({
									top: top_val,
									height: 0
								},
								Math.min(Math.max(2.5, Math.sqrt($('li', new_ayh).length)), 6) * 75,
								function() {
									$(this).hide();
								}
								);
					},
					sensitivity: 200,
					interval: 50,
					timeout: 250
				});
}


function prepareSlideshow() {
	var x = $("#prop_images #thumbs").html();
	$("#prop_images #thumbs").empty();
	$('<div id="prev" class="nav">Prev</div>').appendTo($("#prop_images #thumbs"));
	$('<div id="jcarousel">' + x + '</div>').css({height:'311px',overflow:'hidden'}).appendTo($("#prop_images #thumbs"));
	$('<div id="next" class="nav">Next</div>').appendTo($("#prop_images #thumbs"));
	
	$("#prop_images #thumbs #jcarousel").jCarouselLite({
        btnNext: "#next",
        btnPrev: "#prev",
				vertical: true,
				visible: 2.725,
				circular: false
    });
	
	$("#prop_images #thumbs a")
		.click(
			function(e) {
				e.preventDefault();
				
				$("#main_image img")
					.css({opacity: .6})
					.load(function() { $(this).stop().animate({opacity: 1}, 'fast'); })
					.attr('src', $(this).attr('href'));
			}
		);
}


function setSearchResort(id) {
	$("#resort_id")
		.parent("li")
			.load("backend/be_html.asp?action=getResortSelect&region_id=" + $("#region_id").val(),
				function() {
					if(typeof(id) != "undefined") // First Load
						$("#resort_id").val(id);
					else { // Update
						$("#resort_id")
							.animate({backgroundColor: "#ffff66"}, 75)
							.animate({backgroundColor: "#fff"}, 900);
					}
				}
			);
}


function updateSearchResort() {
	setSearchResort();
}

function cl(txt) { console.log(txt); }