Shadowbox.init({
    modal: true,
    continuous: true,
    displayCounter: false,
    onOpen: function() {
        $('#sb-nav-close').appendTo('#sb-title');
    }
});

function submitTravelPlanner() {
	window.open('','search','width=1000,height=800,menubar=No,toolbars=No,scrollbars=yes,resizable=yes,top=0,left=0');
	jQuery('form.active').submit();
}
function check_cdfs(form) {
	return true;
}
function doSubmit() {
	if (check_cdfs(document.survey)) {
		window.open('','signup','resizable=1,scrollbars=0,width=300,height=150');
		return true;
	}
	else { return false; }
}

jQuery(document).ready(
	function(){
		/* photo gallery */
		jQuery('#page.property #content .more-photos').click(
			function() {
				Shadowbox.setup(jQuery('#page.property #content .photo a'));
				jQuery('#page.property #content .photo a').trigger('click');
				return false;
			}
		);
		/* dates */
		Date.firstDayOfWeek = 0;
		Date.format = 'mm/dd/yyyy';
		jQuery('.date').datePicker().bind(
			'change keyup',
			function() {
				
				var dateType = jQuery(this).attr('name'),
					thisDate = jQuery(this).val();
				
				jQuery('input[name=' + dateType + ']').val(thisDate);
			}
		);
		
		jQuery('.date').dpSetPosition($.dpConst.POS_BOTTOM, $.dpConst.POS_RIGHT);
		jQuery('.date').dpSetOffset(-20, 0);
		
		$('.date[name=date1]').bind(
			'dpClosed',
			function(e, selectedDates) {
				
				var d = selectedDates[0];
				
				if (d) {
					d = new Date(d);
					$(this).parents('li').next().find('.date[name=date2]').dpSetStartDate(d.addDays(1).asString());
				}
			}
		);
		
		$('.date[name=start-date]').bind(
		'dpClosed',
		function(e, selectedDates) {
			
			var d = selectedDates[0];
			
			if (d) {
				d = new Date(d);
				$(this).parents('div').next().find('.date[name=end-date]').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
		
		// Run the following before superfish initializes to properly equalize the Accommodations sub-menu column heights
		var menuEqualizer = jQuery('header #header-content ul#nav li.menu-item-accommodations ul.right li:last'),
			menuEqualizerHeight = menuEqualizer.height(),
			accommodationsLeft = jQuery('header #header-content ul#nav li.menu-item-accommodations ul.left').height(),
			accommodationsRight = jQuery('header #header-content ul#nav li.menu-item-accommodations ul.right').height();
		menuEqualizer.css('height', menuEqualizerHeight + (accommodationsLeft - accommodationsRight));
				
		jQuery('#nav').superfish({
			delay: 800,
			autoArrows: false,
			speed: 'fast'
		})
		.find('> li').has('ul').addClass('menu-parent');
		
		jQuery('#form-selector, #form-lodging').fadeIn().has('ul.col2').addClass('active');
		
		jQuery('#form-selector input[type=radio]').click(function(){
		
				var clicked = jQuery(this),
					clickedId = clicked.attr('id');
			
				if (jQuery('#form-' + clickedId).hasClass('active')) return;
			
				jQuery('form.active').removeClass('active');			
				jQuery('#form-' + clickedId).addClass('active');
							
				jQuery('#form-lodging:visible, #form-flight:visible, #form-lodging-flight:visible').fadeOut('normal', function(){
					jQuery('#form-' + clickedId).fadeIn();
				})
			});
		
		$('form.active').live('keypress', function(event) {
			if (event.keyCode == '13') {
				event.preventDefault();
				submitTravelPlanner();
			}
		});
					
		jQuery('#travel-planner-submit a').live('click', function(){
			submitTravelPlanner();
			return false;
		});
		
		jQuery('a.airport-codes').click(function(){
			window.open(URI_SITE + '/airport-codes.html','airports','width=600,height=725,menubar=No,toolbars=No,scrollbars=Yes,resizable=yes,top=0,left=0');
			return false;
		});
		
		// survey form
		
		var propertyClasses = [
			'the-antlers',
			'canyon-creek',
			'christie-club',
			'cimarron',
			'dulany',
			'eagleridge-lodge',
			'eagleright-townhomes',
			'inn-at-steamboat',
			'kutuk',
			'lacasa',
			'the-lodge-at-steamboat',
			'ptarmigan-house',
			'ski-inn',
			'stonewood',
			'trailhead',
			'timber-run',
			'waterford',
			'waterstone',
			'the-west',
			'yampa-view',
			'chateau-chamonix'
			];
			
		jQuery('#survey-form table:has("tr.first")').addClass('table-header');
		
		jQuery('#location').change(function() {
			
			var value = jQuery(this).val(),
				text = jQuery(this).children('option[value="' + value + '"]').text();
				
			jQuery('input[name="property-name"]').val(text);
							
			if (value === '') {
				jQuery('#survey-form').hide(); return;
			}
			
			jQuery('#survey-form tr').removeClass('alt inactive').show();
						
			jQuery('#survey-form tr').each(function(){
				for (var i = propertyClasses.length - 1; i >= 0; i--){
					if (jQuery(this).hasClass(propertyClasses[i])) jQuery(this).addClass('inactive').hide();
				};
			});
			jQuery('#survey-form tr.' + value).removeClass('inactive').show();
						
			jQuery('#survey-form table').not('.table-header').find('tr:visible:odd').addClass('alt');
			
			$('#survey-form').show();
		});
		
		jQuery('.pager a.compare').live('click', function(){
			jQuery('#results-form').submit();
			return false;
		});
		
		/* ajax for filter/compare properties page */
		var filterXhr = null,
			filterTimer = null,
			gaTriggerTimer;
		jQuery('#search-form :input:not(:submit)').click(function() {
			clearTimeout(filterTimer);
			clearTimeout(gaTriggerTimer);
			
			// show loading icon
			jQuery('<div id="loading-icon"><img src="' + URI_SKIN + '/images/shadowbox/loading.gif" alt="Loading" /></div>').appendTo('#content');
			
			// using a timer so that AJAX requests aren't made on every click. Only after 1000 milliseconds
			filterTimer = setTimeout(function() {
				// Attempt to cancel previous ajax if we're still in the middle of it
				filterXhr = null;
				filterXhr = jQuery.post(URI_CURRENT + '?ajax', jQuery('#search-form').serialize(), function(data) {
					// update properties
					jQuery('#content').html(data);
					jQuery('#property-results .error').remove();
					
					// remove loading icon
					jQuery('#loading-icon').remove();
					
					// Trigger google analytics data after 1000 milliseconds of sitting idle
					gaTriggerTimer = setTimeout(function() {
						var joinedFilterItems = new Array();
						joinedFilterItems = jQuery('#search-form').find('input:checked').map(function() {
							var itemName = this.name.replace('filter_', '')
							_gaq.push(['_trackEvent', 'Lodging Selector (Narrow your search)', 'Individual', itemName]);
							return itemName;
						}).get().join(', ');
						_gaq.push(['_trackEvent', 'Lodging Selector (Narrow your search)', 'Grouped', joinedFilterItems]);
					}, 1000);
				}, 'html');
			}, 1000);
		});
		
		var propertiesCompareCount = 0;
		jQuery('#results-form input[name=compareProperties\[\]]').live('click', function() {
			if (jQuery(this).is(':checked')) {
				propertiesCompareCount++;
			} else {
				propertiesCompareCount--;
			}
			
			if (propertiesCompareCount >= 4) {
				jQuery('#results-form input[name=compareProperties\[\]]:not(:checked)').attr('disabled', 'disabled');
			} else {
				jQuery('#results-form input[name=compareProperties\[\]]:not(:checked)').attr('disabled', '');
			}
		});
		
		/* compare page check price button */
		jQuery('.btn-check').click(function() {
			var planner = jQuery('#compare-planner-container'),
				ez_id = jQuery(this).attr('id').replace('ezrez-', '');
			
			if (planner.is(':visible')) {
				planner.slideUp('fast', function() {
					jQuery('.supplierid', planner).val(ez_id);
				});
			} else {
				jQuery('.supplierid', planner).val(ez_id);
			}
			
			planner.slideDown('fast');
			
			return false;
		});
		
		var searchInput = $('#search input.search-input'),
			defaultSearchText = searchInput.val();

		searchInput.bind('focusin focusout', function(e){
			var eventType = e.type;
			if (eventType == 'focusin') {
				if (searchInput.val() == defaultSearchText) {
					searchInput.val('');
				}
			} else {
				if (searchInput.val() == '') {
					searchInput.val(defaultSearchText);
				}
			}
		});
		
		jQuery('.marketing-box img').unwrap().prependTo(jQuery('.marketing-box'));
		
});
