$(function() {
	// Show Nav on Homepage
	if($('body').hasClass('home')) {
		var q = window.location.search.substring(1);
		if(q.length>0) {
			q=q.split('=');
			var section = q[1].toString();
			$('ul#nav-primary').children('li#primary-'+section).addClass('current').children('ul').show();			
		} else {
			$('ul#nav-primary').children('li#primary-vacations').addClass('current').children('ul').show();
		}
	}	

	if ($('ul#nav-primary li.current').length == 0) $('li#primary-vacations').addClass('current').children('ul').show();

	// Hover Nav
	$('body.home #nav-primary li').hover(
		function() {
			if ($('#booking-console').hasClass('inactive')) {
				$(this).parent().children('ul').hide();
				$(this).children('ul').show();
				if (!$(this).hasClass('current')) {
					$(this).addClass('hover');
					$(this).siblings().removeClass('hover').addClass('unhover');
					$(this).siblings().children('ul').hide();
				} else {
					$(this).siblings().removeClass('hover');
				}
			}
		},function() {
			if ($('#booking-console').hasClass('inactive')) {
				if( !$(this).hasClass('current') )
					$(this).removeClass('hover');
				if (!$(this).hasClass('current')) {
					$(this).children('ul').hide();
					$(this).siblings().removeClass('unhover');
					$(this).siblings().children('ul').show();
				}
			}
		}
	);
	
	$('body:not(.home) #nav-primary li:not(.current)').addClass('not-current');
	
	if($('body').hasClass('vacations')) {
		$('#primary-meetings ul').hide();
	}
	
	if($('#subnav #ter-nav').length === 0) $('#subnav').remove();

		
	var target_slide, target_text, leftMarginIn;

	// Home Push Rotation
	$('#slider').anythingSlider( {
		buildStartStop	: false,
		delay			: 6000,
		pauseOnHover	: true,
		autoPlayLocked	: true,
		autoPlay		: false
	});
	
	// Gorge Experience Rotation
	$('#experience-slider').anythingSlider( {
		buildStartStop	: false,
		delay			: 6000,
		buildNavigation	: false,
		buildStartStop  : false,
		pauseOnHover	: true,
		autoPlayLocked	: true,
		autoPlay		: true,
		resumeDelay     : 4000
	});
	
	
	$('#ter-nav li.current ul').closest('li').addClass('parent');
	
	var openFnMap=function(hash){	
		hash.w.show();
		$('#map-modal').css({left:($(window).width()-$('#map-modal').width())/2,top:$(window).scrollTop()+10});
	};
	var fixFnMap=function(){
		$('#condos #condo-list-12').insertBefore("#condos #condo-list-1");
	};
	var hideFnMap=function(hash){
		hash.w.hide()
		hash.o.remove();
	};
	
	$('#map-modal').jqm({ajax:'/includes/map-new.php', trigger: 'a#map-link', onShow: openFnMap, onLoad: fixFnMap, onHide: hideFnMap, target:'div#map-content' });

	$.fn.initializeConsole = function(options) {
		var defaults = {
			calText: 'Click to select a date',
			dateFormat: 'mm/dd/yyyy'
		},
		opts = $.extend(defaults, options),
		focusIsSupported = (function(){
		    var ud = 't' + +new Date(),
		    anchor = $('<a id="' + ud + '" href="#"/>').css({top:'-999px',position:'absolute'}).appendTo('body'),
		    style = $('<style>#'+ud+'{font-size:10px;}#'+ud+':focus{font-size:1px !important;}</style>').appendTo('head'),
		    supported = anchor.focus().css('fontSize') !== '10px';
		    anchor.add(style).remove();
		return supported;

		})();

		return this.each(function() {
			var form = $(this);

			if ( !focusIsSupported ) {
				$(".textfield")
					.focus(function() { $(this).css({backgroundColor: "#fdfcfa"}); })
					.blur(function() { $(this).css({backgroundColor: "#ffffff"}); });
				$('.required', form).each(function() {
					$(this).prev().addClass('ie-icon-required');
				}); 
			}
			
			$(".date-picker", form).each(function() {
				$(this).datepicker({ 
					beforeShow: setDatePicker,
					buttonImage: "/images/icon-cal.gif", 
					buttonImageOnly: true ,
					buttonText: opts.calText,
					duration: "fast",
					gotoCurrent: true,
					hideIfNoPrevNext: true,
					numberOfMonths: 2,
					showAnim: "blind",
					showOn: "both",
					disabled: true
				});
				
				if($(this).val()==opts.dateFormat) {
					$(this).val('');
				}
				else {
					if($(this).hasClass('date-end')&&$(this).hasClass('update-blocks'))
						getRange(this);
				}
				
			});

		});
	};
	
	var validArrival = 0;
	
	// Booking Console animation - only 'Reserve a Room' heading can close to avoid accidental closing when entering date
	$('#booking-console').each(function() {
		$('#booking-console h3').toggle(function() {
			$('#ui-datepicker-div').css('opacity','0');
			// check to see if arrival date filled in
			validArrival = $('#arrival').val().length;
			//console.log(validArrival);
			$('#check-avail-wrapper').animate({ width:'755', left:'0'},'slow', function() {
				$('#ui-datepicker-div').animate({ opacity:'1'},'fast'); // lame, temp until get this working correctly w/o breaking non-console datepickers
				$('#nav').css('z-index','50'); // z-index changes required to keep section nav from bleeding through when console open
				$('#booking-console').removeClass('inactive').removeClass('ie7-hack').addClass('active');
				$('.ui-datepicker-trigger-inactive').hide();
				$('.ui-datepicker-trigger').show();
				$('#hide-closed').css('display','block');
				$('#reservations-console').initializeConsole();
				if (validArrival < 6) {
					$('#reservations-console .date-picker').datepicker({ disabled: false });
					$('#arrival').focus();
				}
			});
		}, function() {
			$('#reservations-console .date-picker').datepicker({ disabled: true });
			$('#booking-console').removeClass('active').css('overflow','hidden');
			$('#check-avail-wrapper').animate({left:'839'},'1000', function() {
			$('#ui-datepicker-div').css('opacity','0');
			$('#hide-closed').css('display','none');
			$('.ui-datepicker-trigger').hide();
			$('.ui-datepicker-trigger-inactive').show();
			$('#nav').css('z-index','200');
			$('#booking-console').removeClass('active').addClass('inactive');
			});
		});
	});

	// Allow clicking on any area of closed console to open
	$('#booking-console.inactive #expand-wrapper div').click(function() {
		$('#ui-datepicker-div').css('opacity','0');
		validArrival = $('#arrival').val().length;
		$('#check-avail-wrapper').animate({ width:'755', left:'0'},'slow', function() {
			$('#ui-datepicker-div').animate({ opacity:'1'},'fast'); // lame, temp until get this working correctly w/o breaking non-console datepickers
			$('#nav').css('z-index','50'); // z-index changes required to keep section nav from bleeding through when console open
			$('#booking-console').removeClass('inactive').removeClass('ie7-hack').addClass('active');
			$('.ui-datepicker-trigger-inactive').hide();
			$('.ui-datepicker-trigger').show();
			$('#hide-closed').css('display','block');
			$('#reservations-console').initializeConsole();
			if (validArrival < 6) {
				$('#reservations-console .date-picker').datepicker({ disabled: false });
				$('#arrival').focus();
			}
		});
	});

	// Booking console - Check availability promo code click button
	$("#reservations-console").each(function(){
		var x = $(this);
		$("a#promo-click", x).click(function(){
			$("#dropdown-fields",x).show();
			return false;
		});
		$("#extra-fields-close").click(function() {
			$("#dropdown-fields",x).hide();
			return false;
		});
	});
	$("#promo-done-btn").click(function(){
		$("#dropdown-fields").hide();
		if ($('#group-code').val() || $('#promo-code').val() || $('#iata-number').val()) {
			$("#promo-click").addClass("selected-promo-click");
		} else {
			$("#promo-click").removeClass("selected-promo-click");
		}
		return false;
	});
	
	// Variable content min-height
	$('#sidebar').each(function() {
		var sidebarHeight = $(this).height();
	
		if(sidebarHeight > 0)
		{
		var minHeight = sidebarHeight -62; // account for top:-62px  of #sidebar
		var pullquoteHeight = $('#pull-quote').height(); 	 
		if (pullquoteHeight !== null) minHeight -= pullquoteHeight; 	 
		$('#content').css('min-height', minHeight); 	
		
		}
	});
	
	
	
	$("form#golf-pass input#submit").click(function() {
		if($("form#golf-pass input#name").val() == 'Name') {
			alert('A valid Name is required.');
			return false;
		}
	});
	
		
	// Clear field on user click
	$('#signup-push #email').each(function() {
		var defaultVal = $('#email').val();
		var bgColor;
		if ($(this).closest('body').hasClass('home')) {
			bgColor = '#f6f1e6';
		} else {
			bgColor = '#ffffff';
		}
		$('#signup-push #email').click(function() {
			if ($(this).val() == defaultVal)
				$(this).val('').css('background-color',bgColor); // add css function needed for IE7
		}); 
	});

	$('.blog-comment').hide();
	$('.post-comments a').click(function(e) {
	    e.preventDefault();
	    $(this).parent().parent().children('.blog-comment').show();
	    $(this).parent().addClass('active');
	});

	$('.blog #archive-months').change(function() {
		$(this).closest('form').submit();
	});

	$('.blog #archive-tags').change(function() {
		$(this).closest('form').submit();
	});

	if($('form#golf-pass input#name').val()=='') {
		$('form#golf-pass input#name').val('Name');
	}
	
	if($('form#golf-pass input#phone').val()=='') {
		$('form#golf-pass input#phone').val('Phone (xxx-xxx-xxxx)');
	}
	
	
	$('form#golf-pass input').focus(function() { $(this).val(''); });


var urlParams = {};
(function () {
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
})();

	
	$('#sidebar-archive-wrapper a').each(function() {
		var thisLink = $(this).attr('href');
		var thisMonthPos = thisLink.indexOf('month');
		var month = thisLink.substring(thisMonthPos+6,thisMonthPos+8);
		var thisYearPos = thisLink.indexOf('year');
		var year = thisLink.substring(thisYearPos+5,thisYearPos+9);
		if ( (month == urlParams['month']) && (year == urlParams['year'])) {
			$(this).addClass('current');
		}
	});

	$('.social #social-buzz li.sb-border-color:gt(1)').hide();
	$('.social #twitter-wrapper .twtr-widget .twtr-tweets div:eq(1)').addClass('first');
	
	$('.event-links').filter(':last').css({backgroundImage:'none'});
	
	$('a.meeting-map-btn').click(function(event) {
		event.preventDefault();
		$('#meeting-map-modal').jqm({closeClass:'jqmClose'}).jqmShow();
	});
	
	// homepage masthead
	$('#masthead-images').cycle({
		fx: 'fade',
		speed: 600,
		timeout: 7000,
		after:   onAfter 
	});
	
	function onAfter() {
		var x = $(this).attr('class');
		switch(x) {
			case 'slide-1':
				leftMarginIn = -940;
				break;
			case 'slide-2':
				leftMarginIn = -630;
				break;
			case 'slide-3':
				leftMarginIn = -890;
				break;
			case 'slide-4':
				leftMarginIn = -940;
				break;
			case 'slide-5':
				leftMarginIn = -540;
				break;
		}
		var y = $('#js-masthead').find('#masthead-text li.'+x);
		$(y).addClass('active');
		target_text = $(y).find('.slider-text');
		target_text.css({ marginLeft: 1000, display: 'block' });

		target_text.animate({
			marginLeft: leftMarginIn
		}, 2500, 'easeInOutExpo', function() {
			target_text.delay(3500).fadeOut('slow');
			$(y).removeClass('active');
		});
		
		$(y).removeClass('active');
	}
	
	var d = new Date();
	if (d.getMonth() == 11 && d.getDate() >= 7 && d.getDate() <= 18) {
	if ( $.fn.countDown ) {
	$('#countdown_dashboard').countDown({
		 targetDate: {
			'day':      18,
			'month':    12,
			'year':     2011,
			'hour':     23,
			'min':      59,
			'sec':      0
			},
		omitWeeks: true
		}); 
	 }
	}
	
});

