$(window).load(function(){

	// navigation
	var topNavConfig = {    
		interval: 100,
		sensitivity: 4,
		over: openNav,
		timeout: 100,
		out: closeNav
	};
	$('li.topNav').hoverIntent(topNavConfig);
	$('li.lodgingNav').hoverIntent(topNavConfig);
	
	// left nav
	$('#leftNav .navPlus').click(function(){
		$(this).parent().toggleClass("open");
	});
	
	// search button
	var searchConfig = {    
		interval: 100,
		sensitivity: 4,
		over: openSearch,
		timeout: 100,
		out: closeSearch
	};	
	$('#divSearch').hoverIntent(searchConfig);
		
	
	// landing page flyout
	if ($('#landingPageFlyout').size()) {
		$('#landingPageFlyoutToggle').click(function(){
			if ( $('#landingPageFlyout').hasClass("open") ) { closeFlyout();	}
			else { openFlyout(); }
		});
		if($('#toggleSnow').hasClass("on")) {
			$('#divWeather').hide();
			$('#divSnow').show();
			}
			else {
			$('#divSnow').hide();
			$('#divWeather').show();
			}
		$('#toggleWeather').click(function(){
			if ( ! $('#landingPageFlyout').hasClass("open") ) {	openFlyout(); }
			$('#divSnow').hide();
			$('#divWeather').show();
			$('#toggleWeather').addClass("on");
			$('#toggleSnow').removeClass("on");
		});
		$('#toggleSnow').click(function(){
			if ( ! $('#landingPageFlyout').hasClass("open") ) {	openFlyout(); }
			$('#divWeather').hide();
			$('#divSnow').show();
			$('#toggleWeather').removeClass("on");
			$('#toggleSnow').addClass("on");
		});
		openFlyout();
		setTimeout('closeFlyout()', 2000);

	}

	// interior page flyout
	if ($('#interiorPageFlyout').size()) {
		$('#landingPageFlyoutToggle').click(function(){
			if ( $('#interiorPageFlyout').hasClass("open") ) { closeFlyout();	}
			else { openFlyout(); }
		});
		
		if($('#toggleSnow').hasClass("on")) {
			$('#divWeather').hide();
			$('#divSpecials').hide();
			$('#divSnow').show();
			}
			else if($('#toggleWeather').hasClass("on")){
			$('#divSnow').hide();
			$('#divSpecials').hide();
			$('#divWeather').show();
			}
			else{
			$('#divSnow').hide();
			$('#divWeather').hide();
			$('#divSpecials').show();
			
			}
		
		$('#toggleWeather').click(function(){
			if ( ! $('#interiorPageFlyout').hasClass("open") ) {	openFlyout(); }
			$('#divSnow').hide();
			$('#divWeather').show();
			$('#divSpecials').hide();
			$('#toggleWeather').addClass("on");
			$('#toggleSnow').removeClass("on");
			$('#toggleSpecials').removeClass("on");
		});
		$('#toggleSnow').click(function(){
			if ( ! $('#interiorPageFlyout').hasClass("open") ) {	openFlyout(); }
			$('#divWeather').hide();
			$('#divSnow').show();
			$('#divSpecials').hide();
			$('#toggleWeather').removeClass("on");
			$('#toggleSnow').addClass("on");
			$('#toggleSpecials').removeClass("on");
		});
		$('#toggleSpecials').click(function(){
			if ( ! $('#landingPageFlyout').hasClass("open") ) {	openFlyout(); }
			$('#divWeather').hide();
			$('#divSnow').hide();
			$('#divSpecials').show();
			$('#toggleSnow').removeClass("on");
			$('#toggleWeather').removeClass("on");
			$('#toggleSpecials').addClass("on");
		});			
	}	

	// landing Page Highlights
	if ($('#landingPageHighlights').size()) {
		$('#landingPageHighlightsNext').click(function(){
			var $current = ($('#highlightImages li.active'));
			var $next = $current.next();
			if (!$next.length) $next = $('#landingPageHighlights li:first');
			
			$next.fadeIn('slow');		
			$next.addClass('active');
			$current.hide();
			$current.removeClass('active')

			$('#highlightsCount .showing').html( $('#landingPageHighlights li').index($next)+1 );
			
			var sHero = $next.find('.backgroundImg').html();
			$('#divBG').hide();
			$('#divBG').css('background-image','url(' + sHero + ')');
			$('#divBG').fadeIn();
			
		});	
		
		$('#landingPageHighlightsPrev').click(function(){
			var $current = ($('#highlightImages li.active'));
			var $next = $current.prev();
			if (!$next.length) $next = $('#landingPageHighlights li:last');
			
			$next.fadeIn('slow');		
			$next.addClass('active');
			$current.hide();
			$current.removeClass('active')
			
			$('#highlightsCount .showing').html( $('#landingPageHighlights li').index($next)+1 );

			var sHero = $next.find('.backgroundImg').html();
			$('#divBG').css('background-image','url(' + sHero + ')');
			
		});		

		if ( $('#landingPageHighlights li').size() > 1 ) {
			$('#highlightsCount .total').html( $('#landingPageHighlights li').size() );
			$('#landingPageHighlightsNav').show();
		} else {
			$('#landingPageHighlightsNav').hide();
		}	
		
	}

	// landing Page Specials
	if ($('#landingPageSpecials').size()) {
		$('#landingPageSpecialsNext').click(function(){
			var $current = ($('#landingPageSpecials li.active'));
			var $next = $current.next();
			if (!$next.length) $next = $('#landingPageSpecials li:first');
			
			$next.fadeIn('slow');		
			$next.addClass('active');
			$current.hide();
			$current.removeClass('active')

			$('#landingPageSpecialsCount .showing').html( $('#landingPageSpecials li').index($next)+1 );
			
		});	
		
		$('#landingPageSpecialsPrev').click(function(){
			var $current = ($('#landingPageSpecials li.active'));
			var $next = $current.prev();
			if (!$next.length) $next = $('#landingPageSpecials li:last');
			
			$next.fadeIn('slow');		
			$next.addClass('active');
			$current.hide();
			$current.removeClass('active')
			
			$('#landingPageSpecialsCount .showing').html( $('#landingPageSpecials li').index($next)+1 );
			
		});		
		
		if ( $('#landingPageSpecials li').size() > 1 ) {
			$('#landingPageSpecialsCount .total').html( $('#landingPageSpecials li').size() );
			$('#landingPageSpecialsNext').click();
			$('#landingPageSpecialsNav').show();
		} else {
			$('#landingPageSpecialsNext').click();
			$('#landingPageSpecialsNav').hide();
		}
	}	

	// interior Page Specials
	if ($('#interiorPageSpecials').size()) {
		$('#interiorPageSpecialsNext').click(function(){
			var $current = ($('#interiorPageSpecials li.active'));
			var $next = $current.next();
			if (!$next.length) $next = $('#interiorPageSpecials li:first');
			
			$next.fadeIn('slow');		
			$next.addClass('active');
			$current.hide();
			$current.removeClass('active')

			$('#interiorPageSpecialsCount .showing').html( $('#interiorPageSpecials li').index($next)+1 );
			
		});	
		
		$('#interiorPageSpecialsPrev').click(function(){
			var $current = ($('#interiorPageSpecials li.active'));
			var $next = $current.prev();
			if (!$next.length) $next = $('#interiorPageSpecials li:last');
			
			$next.fadeIn('slow');		
			$next.addClass('active');
			$current.hide();
			$current.removeClass('active')
			
			$('#interiorPageSpecialsCount .showing').html( $('#interiorPageSpecials li').index($next)+1 );
			
		});		
		
		if ( $('#interiorPageSpecials li').size() > 1 ) {
			
			$('#interiorPageSpecialsCount .total').html( $('#interiorPageSpecials li').size() );
			$('#interiorPageSpecialsNext').click();
			$('#interiorPageSpecialsNav').show();
		} else {
			$('#interiorPageSpecialsNext').click();
			$('#interiorPageSpecialsNav').hide();
			
		}
	}		
	
	// Gallery
	// photos
	$('.togglePhotos').click(function(){ showPhotos(); });	
	$('.toggleVideo').click(function(){ showVideos(); });
	$('.toggleWebCams').click(function(){ showWebCams(); });	

	$("#divPhotosNext").click(function(){
		var $current = ($("#divPhotosImages li.active"));
		var $next = $current.next();
		if (!$next.length) $next = $("#divPhotosImages li:first");

		if ( !($next.hasClass("loaded")) ) {
			$next.find('img').attr('src', sGalleryURL + "303&photoID=" + $next.find('img').attr('id'));
			$next.addClass('loaded');
		}		
		$next.fadeIn("slow");		
		$next.addClass("active");
		$current.hide();
		$current.removeClass("active")

		// pre-load next one also for smoother operation
		var $nextnext = $next.next();
		if (!$nextnext.length) $nextnext = $("#divPhotosImages li:first");
		if ( !($nextnext.hasClass("loaded")) ) {
			$nextnext.find('img').attr('src', sGalleryURL + "303&photoID=" + $nextnext.find('img').attr('id'));
			$nextnext.addClass('loaded');
		}		
		
	});	
	
	$("#divPhotosPrev").click(function(){
		var $current = ($("#divPhotosImages li.active"));
		var $next = $current.prev();
		if (!$next.length) $next = $("#divPhotosImages li:last");
		
		if ( !($next.hasClass("loaded")) ) {
			$next.find('img').attr('src', sGalleryURL + "303&photoID=" + $next.find('img').attr('id'));
			$next.addClass('loaded');
		}	
		$next.fadeIn("slow");		
		$next.addClass("active");
		$current.hide();
		$current.removeClass("active")

		// pre-load next prev one also for smoother operation
		var $nextnext = $next.prev();
		if (!$nextnext.length) $nextnext = $("#divPhotosImages li:last");
		if ( !($nextnext.hasClass("loaded")) ) {
			$nextnext.find('img').attr('src', sGalleryURL + "303&photoID=" + $nextnext.find('img').attr('id'));
			$nextnext.addClass('loaded');
		}		
	});		
	$("#divPhotosNext").click();

	// webcams
	$("#divCamsNext").click(function(){
		var $current = ($("#divCamsImages li.active"));
		var $next = $current.next();
		if (!$next.length) $next = $("#divCamsImages li:first");

		if ( !($next.hasClass("loaded")) ) {
			$next.find('img').attr('src', sGalleryURL + "303&latestCam=" + $next.find('img').attr('id'));
			$next.addClass('loaded');
		}		
		$next.fadeIn("slow");		
		$next.addClass("active");
		$current.hide();
		$current.removeClass("active")

		// pre-load next one also for smoother operation
		var $nextnext = $next.next();
		if (!$nextnext.length) $nextnext = $("#divCamsImages li:first");
		if ( !($nextnext.hasClass("loaded")) ) {
			$nextnext.find('img').attr('src', sGalleryURL + "303&latestCam=" + $nextnext.find('img').attr('id'));
			$nextnext.addClass('loaded');
		}		
		
	});	
	
	$("#divCamsPrev").click(function(){
		var $current = ($("#divCamsImages li.active"));
		var $next = $current.prev();
		if (!$next.length) $next = $("#divCamsImages li:last");
		
		if ( !($next.hasClass("loaded")) ) {
			$next.find('img').attr('src', sGalleryURL + "303&latestCam=" + $next.find('img').attr('id'));
			$next.addClass('loaded');
		}	
		$next.fadeIn("slow");		
		$next.addClass("active");
		$current.hide();
		$current.removeClass("active")

		// pre-load next prev one also for smoother operation
		var $nextnext = $next.prev();
		if (!$nextnext.length) $nextnext = $("#divCamsImages li:last");
		if ( !($nextnext.hasClass("loaded")) ) {
			$nextnext.find('img').attr('src', sGalleryURL + "303&latestCam=" + $nextnext.find('img').attr('id'));
			$nextnext.addClass('loaded');
		}		
	});		
	$("#divCamsNext").click();				
	
	$(".galleryImageFancybox").fancybox({
		type : 'image',
		overlayOpacity: .5,
		cyclic : 'true'
	});
	// to do - grab width: height from xml
	$(".aMediaGalleryPopup").fancybox({
		'type' : 'iframe',
		overlayOpacity: .5,
		'width' : 820,
		'height' : 500
	});
	
	
	
	// interior page gallery flyout
	if ($('#interiorPageGallery').size()) {
		$('#interiorPageGalleryToggle').click(function(){
			if ( $('#interiorPageGallery').hasClass("open") ) { closeGalleryFlyout();	}
			else { openGalleryFlyout(); }
		});
		$('#interiorPageGallery .togglePhotos').click(function(){
			if ( ! $('#interiorPageGallery').hasClass("open") ) {	openGalleryFlyout(); }
			showPhotos();
		});		
		$('#interiorPageGallery .toggleVideo').click(function(){
			if ( ! $('#interiorPageGallery').hasClass("open") ) {	openGalleryFlyout(); }
			showVideos();
		});
		$('#interiorPageGallery .toggleWebCams').click(function(){
			if ( ! $('#interiorPageGallery').hasClass("open") ) {	openGalleryFlyout(); }
			showWebCams();
		});		
	
	}		
});


// navigation
function openNav(){  
	$(this).addClass('topNavOpen');  
}  
function closeNav(){  
	$(this).removeClass('topNavOpen');  
}

// search
function openSearch(){  
	$('#divSearchBtn a').addClass('on');
	$('#divSearchBox').show(); 
	$('#gsearchBox').focus();
	
}  
function closeSearch(){  
	$('#divSearchBtn a').removeClass('on');
	$('#divSearchBox').hide();
}
function submitSearch() {
  $("#gsearch").submit();
}

// landingpage Highlights
function loadRandomHighlight() {
	// load a random item
	var iRandom = (Math.floor( Math.random()* ($('#landingPageHighlights li').size()) )) 
	loadHighlight(iRandom);
}

// landingpage Highlights
function loadHighlight(iShowMe) {
	var $next =	$('#highlightImages li:eq(' + iShowMe + ')');
	$next.fadeIn('slow');		
	$next.addClass('active');
	$('#highlightsCount .showing').html( $('#landingPageHighlights li').index($next)+1 );
	var sHero = $next.find('.backgroundImg').html();
	$('#divBG').css('background-image','url(' + sHero + ')');
}


// flyout
function closeFlyout() {
	if ($('#landingPageFlyout').size()) {
		$('#landingPageFlyout').removeClass("open");
		$('#landingPageFlyout').animate({
			top: "457px",
			height: "33px"
		});
	} 
	if ($('#interiorPageFlyout').size()) {
		$('#interiorPageFlyout').removeClass("open");
		$('#interiorPageFlyout').animate({
			top: "413px",
			height: "33px"
		});
	} 	
	$('#landingPageFlyoutToggle').css("background-position","0px -9px");
}
function openFlyout() {
	if ($('#landingPageFlyout').size()) {
		$('#landingPageFlyout').addClass("open");
		$('#landingPageFlyout').animate({
			top: "301px",
			height: "189px"
		});
	}
	if ($('#interiorPageFlyout').size()) {
		$('#interiorPageFlyout').addClass("open");
		$('#interiorPageFlyout').animate({
			top: "227px",
			height: "219px"
		});
	} 	
	$('#landingPageFlyoutToggle').css("background-position","0px 0px");
}


// gallery flyout
function closeGalleryFlyout() {
	$('#interiorPageGallery').removeClass("open");
	$('#interiorPageGallery').animate({
		top: "413px",
		height: "33px"
	});
	$('#interiorPageGalleryToggle').css("background-position","0px -9px");
}
function openGalleryFlyout() {
	$('#interiorPageGallery').addClass("open");
	$('#interiorPageGallery').animate({
		top: "109px",
		height: "337px"
	});
	$('#interiorPageGalleryToggle').css("background-position","0px 0px");
}

// gallery
function showPhotos() {
	$('.galleryDiv').removeClass("on").hide();
	$('.galleryToggle').removeClass("on");
	$('#divPhotos').show();
	$('.togglePhotos').addClass("on");
}
function showVideos() {
	$('.galleryDiv').removeClass("on").hide();
	$('.galleryToggle').removeClass("on");
	$('#divVideo').show();
	$('.toggleVideo').addClass("on");
}
function showWebCams() {
	$('.galleryDiv').removeClass("on").hide();
	$('.galleryToggle').removeClass("on");
	$('#divWebCams').show();
	$('.toggleWebCams').addClass("on");
}

// vacation Planner
function setMinDepartureDate(dateText, datepicker) {
	if (!dateText || dateText.length == 0) return;
	var dt = new Date(dateText);
	dt.setDate(dt.getDate() + 1);
	$('#departureDate').datepicker('option', 'minDate', dt);
	// set departure date + 7 of arrival date
	var dtSet = new Date(dateText);
	dtSet.setDate(dtSet.getDate() + 7);
	$('#departureDate').datepicker('setDate', dtSet);	
}
function setMaxArrivalDate(dateText, datepicker) {
	if (!dateText || dateText.length == 0) return;
	var dt = new Date(dateText);
	dt.setDate(dt.getDate() - 1);
	$('#arrivalDate').datepicker('option', 'maxDate', dt);
}
function goBook() {

	var oArrive = document.getElementById('arrivalDate');
	var oDepart = document.getElementById('departureDate');
	var oAdults = document.getElementById('adults');
	var oChildren = document.getElementById('children');
	
	var dtNow = new Date();
	dtNow.setHours(0,0,0,0);
	
	formatDateAfterExit(oArrive);
	var dt = new Date(oArrive.value);
	if (!validateDate(oArrive) || dt < dtNow) {
		alert('Please select a future arrival date.');	
		oArrive.focus();
		return;
	}
	
	formatDateAfterExit(oDepart);
	var dtDep = new Date(oDepart.value);
	if (!validateDate(oDepart) || dtDep < dtNow) {
		alert('Please select a future departure date.');	
		oDepart.focus();
		return;
	}
	
	if (dtDep < dt) {
		alert('Please select a departure date after your arrival date.');	
		oDepart.focus();
		return;
	}
	
//	var sPost = 'http://www.inntopia.travel/search.aspx?salesid=386798' +
//		'&arrivaldate=' + (dt.getMonth()+1) + '/' + dt.getDate() + '/' + dt.getFullYear() +
//		'&departuredate='  + (dtDep.getMonth()+1) + '/' + dtDep.getDate() + '/' + dtDep.getFullYear() +
//		'&adultcount=' + oAdults.value  +
//		'&childcount=' + oChildren.value  +
//		'';
		
//	alert(sPost);

	var newForm = document.createElement('form');
	newForm.setAttribute('action', 'http://www.inntopia.travel/aspnet/09/search.aspx');
	newForm.setAttribute('method', 'post');
	newForm.setAttribute('name', 'frmGoBook2');
	newForm.setAttribute('id', 'frmGoBook2');
	newForm.setAttribute('style', 'display:none;');
	newForm.setAttribute('onSubmit', '__utmLinkPost(this)');
	var newInput = document.createElement('input');
	newInput.setAttribute('type', 'text');
	newInput.setAttribute('name', 'arrivaldate');
	newInput.setAttribute('value', (dt.getMonth()+1) + '/' + dt.getDate() + '/' + dt.getFullYear());
	newForm.appendChild(newInput);
	var newInput2 = document.createElement('input');
	newInput2.setAttribute('type', 'text');
	newInput2.setAttribute('name', 'departuredate');
	newInput2.setAttribute('value', (dtDep.getMonth()+1) + '/' + dtDep.getDate() + '/' + dtDep.getFullYear());
	newForm.appendChild(newInput2);
	var newInput3 = document.createElement('input');
	newInput3.setAttribute('type', 'text');
	newInput3.setAttribute('name', 'adultcount');
	newInput3.setAttribute('value', oAdults.value);
	newForm.appendChild(newInput3);
	var newInput4 = document.createElement('input');
	newInput4.setAttribute('type', 'text');
	newInput4.setAttribute('name', 'childcount');
	newInput4.setAttribute('value', oChildren.value);
	newForm.appendChild(newInput4);
	var newInput5 = document.createElement('input');
	newInput5.setAttribute('type', 'text');
	newInput5.setAttribute('name', 'salesid');
	newInput5.setAttribute('value', '386798');
	newForm.appendChild(newInput5);
	document.body.appendChild(newForm);
	var newSubmitButton = document.createElement('input');
	newSubmitButton.setAttribute('type', 'submit');
	newSubmitButton.setAttribute('name', 'btnSubmit');
	newSubmitButton.setAttribute('value', 'Go');
	newForm.appendChild(newSubmitButton);
	newSubmitButton.click();
}
function openWindow(theURL,winName,features)  {
	window.open(theURL,winName,features);
}
