// SETTINGS (times are in milliseconds)
	// Viewfinder
	var pauseTime = 10000;
	var fadeTime = 1000;
	// Widget & Overlays
	var homeOpenTime = 8000;
	var slideCount = '2';
	var background = $.cookie('background');
	var viewInterval;
	
var switchInt;
var widgetClicked = false;
var needToConfirm = false;
jQuery(document).ready( function($) {
	
/* PRINT BUTTON */
	if ($('body').is('.print')) {
		$('.print-controls').prepend('<p class="submit printer"><a class="printer" href="#" title="Send to printer.">Print</a></p>');
		$('.print-controls a.printer').click(function() {
			window.print();
			return false;
		});
		window.print();
	}
	
// COLORBOX
	//if ($('body').is('.stay') || $('body').is('.search') || $('body').is('.special')) {
	if ($('body').is('.home')) {
	
	} else {
		$(".gallery").each(function(index, obj){
			$(obj).find("a").colorbox({rel:$(obj).attr("id"), maxWidth:"100%", maxHeight:"100%"});
		});
	}
	
// SEARCH
	var inputTextDefault = $("#searchform input#s").attr("value"); //"input here";
	var inputBox = $("#searchform input#s");
	inputBox.attr("value", inputTextDefault);
	inputBox.focus(inputFocus);
	inputBox.blur(inputBlur);
	function inputFocus(){ 
		$(this).addClass("active"); 
		if($(this).attr("value") == inputTextDefault) $(this).attr("value", "");
	};
	function inputBlur(){ 
		$(this).removeClass("active"); 
		if($(this).attr("value") == "") $(this).attr("value", inputTextDefault); 
	};
	
// CONTACT FORMS, first and last name functions
	var firstBox = $("#first_name");
	var lastBox = $("#last_name");
	var firstName = firstBox.attr("value");
	var lastName = lastBox.attr("value");
	
	//firstBox.attr("value", firstName);
	//lastBox.attr("value", lastName);
	firstBox.focus(function() {
		boxFocus($(this), firstName);
	});
	firstBox.blur(function() {
		boxBlur($(this), firstName);
	});
	lastBox.focus(function() {
		boxFocus($(this), lastName);
	});
	lastBox.blur(function() {
		boxBlur($(this), lastName);
	});
	function boxFocus(object, defaultText){
		object.addClass("active");
		if(object.attr("value") == defaultText) object.attr("value", "");
	};
	function boxBlur(object, defaultText){
		object.removeClass("active");
		if(object.attr("value") == "") object.attr("value", defaultText);
	};
	
// LOGIN / SIGNUP FOCUS
	var filename = get_filename();
	//document.write(filename);
	if (filename == 'upload.html') {
		if (get('do') == 'login') {
			document.login.email.focus();
		} else if (get('do') == 'signup') {
			document.signup.email.focus();
		}
		if (get('type') == 'video') {
			$('div.upload .photo ul').hide();
			$('div.upload .video ul').show();
			$('div.upload .photo h4').removeClass('selected');
			$('div.upload .video h4').addClass('selected');
		}
	}
	function get_filename() {
		//var pathname = document.location.href;
		var filename = document.location.href;//pathname.substr(pathname.lastIndexOf("/")+1,pathname.length);
		var end = (!filename.indexOf("?")) ? filename.length : filename.indexOf("?");
		return filename.substring(filename.lastIndexOf("/")+1, end);
	}
	function get(varname) {
		var url = window.location.href;
		var url_parts = url.split("?");
		
		if (url_parts.length == 0)
			return "";
		
		var query_string = url_parts[1];
		var query_vars = query_string.split("&");
		
		var value = "";
		for (i=0; i<query_vars.length; i++) {
			var parts = query_vars[i].split("=");
			if (parts[0] == varname) {
				value = parts[1];
				break;
			}
		}
		
		value = unescape(value);
		value.replace(/\+/g," ");
		return value;
	}

// TRIP PLANNER
    var myDate = new Date();
	
	var myDateMil = Date.UTC(myDate.getFullYear(),myDate.getMonth(),myDate.getDate());
	var tomorrowMil = myDateMil + 86400000;
	var threeDayMil = tomorrowMil + 259200000;
	
    var tomorrowDate = new Date(tomorrowMil);
    var threeDayDate = new Date(threeDayMil);
	
	var tomorrow = tomorrowDate.getMonth()+1 + '/' + tomorrowDate.getDate() + '/' + tomorrowDate.getFullYear();
    var day3 = threeDayDate.getMonth()+1 + '/' + threeDayDate.getDate() + '/' + threeDayDate.getFullYear();
	
    /*var month = myDate.getMonth() + 1;
	var day = myDate.getDate() + 1;
    var tomorrow = month + '/' + day + '/' + myDate.getFullYear();
	var day = myDate.getDate() + 4;
    var day3 = month + '/' + day + '/' + myDate.getFullYear();*/
	
	$("#trip_arrive").datepicker({
		minDate:1,
		onSelect:function(theDate) {
			$("#trip_depart").datepicker('option','minDate',new Date(theDate))
		}
	}).val(tomorrow);
	$("#trip_depart").datepicker({
		minDate:2,
		onSelect:function(theDate) {
			$("#trip_arrive").datepicker('option','maxDate',new Date(theDate))
		}
	}).val(day3);
	
	$('#trip_guests').selectbox();//{debug: true}
	
	$("ul.select ul").addClass("jshide");
	$('ul.select li').hoverIntent(function() {
		$(this).addClass('hover');
		$(this).children("ul").slideDown('fast');
	}, function() {
		$(this).children("ul").slideUp('fast', function(){ $(this).parent().removeClass('hover'); });
	});
		
// UPLOAD PHOTO/VIDEO SELECTOR
	$('div.upload h4 a').click(function(){
		//$(this).toggle();
		$('div.upload ul ul').hide();
		$(this).parent().next().show();
		$('div.upload h4').removeClass('selected');
		$(this).parent().addClass('selected');
		$(this).blur();
		return false;
	});
	$("input[type=file]").change(function(){
		$("img.aniup").show();
	});
	
    if ($('body').is('.upload')) {
		window.onbeforeunload = askConfirm;
	}
	function askConfirm() {
		if (needToConfirm) return "Navigating away from this page will cancel the upload process.";
	};
	/** /
	$("input[type=file]").filestyle({ 
		image: "../../images/form/btn-choosefile.png",
		imageheight : 27,
		imagewidth : 111,
		width : 250
	});
	/*$("div.upload form p.submit input").click(function(){
		//$(this).parent("form").find("img.aniup").show();
		$("img.aniup").show();
	});*/
	
// HOMEPAGE
	if ($('body').is('.home')) {
	
	// VIEWMASTER SETUP
		
		// Make Slideshow functionality active if js is enabled
		$('.bg-slides').addClass('viewmaster-slides');
		$('.viewmaster-slides').each(function(i){
			$(this).children('li:first').addClass('active');
		});
		
		$('#page').attr('rel', $('.viewmaster li.view02 a').attr('rel'));
		
		if (background) {
			$('#page').css({'background-image' : 'url(uploads/HomePhoto/'+ background +')' });
			for (var i=1; i<=5; i++) {
				if ($('.viewmaster li.view0'+i).attr('rel') == background) {
					$('#page').attr('rel', $('.viewmaster li.view0'+i+' a').attr('rel'));
				}
			}
			setSlideCount();
			$('.viewmaster li[rel="'+background+'"]').addClass('selected').siblings().removeClass('selected');
		}
		
		$('.viewmaster li').click(function(){
			$('#page').attr('rel', $('a', this).attr('rel')).css({'background-image' : 'url(uploads/HomePhoto/'+ $(this).attr('rel') +')' });
			
			$.cookie('background', $(this).attr('rel'));
			background = $.cookie('background');
			setSlideCount();
			clearInterval(viewInterval);
			viewInterval = setInterval('switchViewmasterSlide()', pauseTime);
			
			$(this).addClass('selected').blur();
			$(this).siblings().removeClass('selected');
			console.log(slideCount);
			return false;
		});
		
		viewInterval = setInterval('switchViewmasterSlide()', pauseTime);
		
		
		/* part of an old system that made whole background clickable, this was abandoned when every saw how confusing it was * /
		$('#page').mousemove(function(kmouse) {
			if ($(this).attr('rel')) {
				//$('.tooltip').css({left:kmouse.pageX+15, top:kmouse.pageY+15});
				$(this).css("cursor","pointer");
				//$("body").append('<div class="tooltip"><h3>Click to find out more!</h3> <p>('+$(this).attr('rel')+")</p></div>");
				//$('.tooltip').css({'opacity':'0.8', 'display':'none'}).fadeIn(400);
			} else {
				$(this).css("cursor","auto");
			}
		}).click(function(){
			if ($(this).attr('rel')) {
				window.location = $(this).attr('rel');
			}
		});
		/**/
		
	// WIDGET
		/*$('div#widget .nav li > a').removeClass('active');
		$('div#widget .nav li.specials a').addClass('active');
		$('div#widget .widget-content li#widget-videos').hide();
		$('div#widget .widget-content li#widget-events').hide();
		$('div#widget .widget-content li#widget-specials').show();*/
		
		// Widget & overlay animation removal for IE7 which has unreconcilable problems with transparent pngs animated (no fades or slides)
		if (jQuery.browser.version.substr(0,1)=="7" || jQuery.browser.version.substr(0,1)=="6") {
			switchInt = setInterval("ieGetStarted()", 1000);
			
			$('ul.viewmaster').hover(function(event) {
					$('p.find-view').show();
				}, function(event) {
					$('p.find-view').hide();
				});
			$('ul.primary').hover(function(event) {
					$('p.start-vacation').show();
				}, function(event) {
					$('p.start-vacation').hide();
				});
			$('body.home div#footer').hover(function(event) { // .trip-planner
					$('p.trip-planner').show();
				}, function(event) {
					$('p.trip-planner').hide();
				});
		} else {
			// Open widget and set it up.
			$('div#widget')
				.animate({opacity: 1.0}, 1000)
				.data('rightClosed', $('div#widget').css('right'))
				.animate({right:"0px"}, 500)
				.animate({opacity: 1.0}, homeOpenTime, function(){
					if (!widgetClicked) {
						$(this).animate({right:$('div#widget').data('rightClosed')}, 500, 'linear', function(){  //
							if (!widgetClicked) $('div#widget .nav li > a').addClass('active');
							// setup click to hide widget
						});
					}
				});
			$(document).bind('click', function(e){
				var $clicked = $(e.target);
				if (!($clicked.parents().is('div#widget'))) { //!($clicked.is('div#widget .nav li a')) && !($clicked.is('div#widget .video object')) && 
					$('div#widget').animate({right:$('div#widget').data('rightClosed')}, 500 );
					$('div#widget .nav li > a').addClass('active');
				}
			});
			$('div#widget .nav li a').click(function(){
				$('div#widget').animate({right:"0px"}, 500 );
				$('div#widget .nav li > a').removeClass('active');
				$(this).addClass('active');
				
				$('div#widget .widget-content li').hide().filter(this.hash).show();
				widgetClicked = true;
				$(this).blur();
				return false;
			});
		
	// OVERLAYS
			// Show overlay instructions and then hide them, and have them show up on rollover.
			$('p.overlay')
				.animate({opacity: 1.0}, 1000)
				.fadeIn('slow')
				.animate({opacity: 1.0}, homeOpenTime)
				.fadeOut('slow');
			$('ul.viewmaster').hover(function(event) {
					$('p.find-view').stop(true,true).fadeIn('fast');
				}, function(event) {
					$('p.find-view').stop(true,true).fadeOut('fast');
				});
			$('ul.primary').hover(function(event) {
					$('p.start-vacation').stop(true,true).fadeIn('fast');
				}, function(event) {
					$('p.start-vacation').stop(true,true).fadeOut('fast');
				});
			$('body.home div#footer').hover(function(event) { // .trip-planner
					$('p.trip-planner').stop(true,true).fadeIn('fast');
				}, function(event) {
					$('p.trip-planner').stop(true,true).fadeOut('fast');
				});
		}
	
	}
});

function setSlideCount() {
	slideCount = $('.viewmaster li[rel="'+background+'"]').attr('class').replace('view0','');
}
function manageSlideCount() {
	if (slideCount > 3) {
		slideCount = 0;
	} else {
		slideCount++;
	}
}
function switchViewmasterSlide() {
	manageSlideCount();
	$.cookie('background', $('.viewmaster .view0'+slideCount).attr('rel'));
	background = $.cookie('background');
	$('#page').css({'background-image' : 'url(uploads/HomePhoto/'+ background +')' });
	$('.viewmaster li[rel="'+background+'"]').addClass('selected').siblings().removeClass('selected');
}
		
function textFocus(){
	$(this).addClass("active");
	if($(this).attr("value") == $(this).attr("title") || !$(this).attr("title")){
		$(this).attr("title", $(this).val());
		$(this).attr("value", "");
	}
};
function textBlur(){
	$(this).removeClass("active");
	if($(this).attr("value") == "") $(this).attr("value", $(this).attr("title"));
};
function slideSwitch($nextSlide, $nextThumbItem){
	jQuery('.viewmaster-slides').each(function(i){
		if ($(this).children().size() > 1) {
			var $active = $(this).children('li.active');
			if ($nextSlide)
				var $next = $nextSlide;
			else
				var $next = ($active.next().length) ? $active.next() : $(this).children('li:first');
			
			var $activeThumb = $('.viewmaster li.active');
			if ($nextThumbItem)
				var $nextThumb = $nextThumbItem;
			else
				var $nextThumb = ($activeThumb.next().length) ? $activeThumb.next() : $('.thumbs li:first');
		   
			$active.addClass('last-active');
			$next.css({opacity: 0.0})
				.addClass('active')
				.animate({opacity: 1.0}, fadeTime, function() {
					$active.removeClass('active last-active');
					$nextThumb.addClass('active');
					$activeThumb.removeClass('active');
				});
			$nextThumb.addClass('active');//.children().animate({opacity: 0.0}, 1000);
			$activeThumb.removeClass('active');//.children().animate({opacity: 0.45}, 1000);
		}
	});
};

function ieOverlayOff() {
	clearInterval(switchInt);
	jQuery('p.find-view').hide();
	jQuery('p.start-vacation').hide();
	jQuery('p.trip-planner').hide();
	
	// setup click to hide widget
	$(document).bind('click', function(e){
		var $clicked = $(e.target);
		if (!($clicked.is('div#widget .nav li a'))) {
			$('div#widget').css({right:$('div#widget').data('rightClosed')});
			//$('div#widget').animate({right:-387}, 500 );
			$('div#widget .nav li > a').addClass('active');
		}
	});
	$('div#widget').css({right:$('div#widget').data('rightClosed')});
	$('div#widget .nav li > a').addClass('active');
};
function ieGetStarted() {
	clearInterval(switchInt);
	switchInt = setInterval( "ieOverlayOff()", homeOpenTime );
	// Open widget and set it up.
	$('div#widget')
		.data('rightClosed', $('div#widget').css('right'))
		.css("right","0px");
		//.css({'right' : '0px', 'opacity' : '1.0'});
	$('div#widget .nav li a').click(function(){
		$('div#widget').css("right","0px");
		$('div#widget .nav li > a').removeClass('active');
		$(this).addClass('active');
		
		$('div#widget .widget-content li').hide().filter(this.hash).show();
		
		$(this).blur();
		return false;
	});
}
