//
//	DESTINATION LEAVENWORTH SEARCH WIDGET
//	G.Suntop 2008
//
//	Updated: 9.28.08
//

$(document).ready(function() {
if($('div#search_widget').length) {
	
	//
	//	VARIABLES
	//
	
	var ajaxRunning = 0; // Boolean to track if AJAX request is running
	var queryProperty = $.jqURL.get('property_type');
	
	if(queryProperty) queryProperty = queryProperty.toLowerCase();
		
	//
	//	FUNCTIONS:
	//
	
	// Run AJAX and return results in the "search_results" DIV:
	function searchAJAX() {
		if(!ajaxRunning) {
			ajaxRunning = 1;
			$('img#ajax-progress').css({visibility:"visible"});
			
			var options = { 
				target:     '#search_results', 
				success: function() {
					$('img#ajax-progress').css({visibility:"hidden"});
					ajaxRunning = 0;
				}
			}; 
		
			$('form#property_search').ajaxSubmit(options);
		}
		
		return false;
	}
	
	function disableInput(input) {
			input.attr("disabled","disabled").removeAttr("checked").parent().addClass("disabled");
		}

	function toggleSizeOptions(propertyType) {		

		// Enable all options:
		$('div#checklist-size ul li input').removeAttr("disabled").attr("checked","checked");
		$('div#checklist-size ul li').removeClass("disabled");
		
		$('div#checklist-location ul li input').removeAttr("disabled").attr("checked","checked");
		$('div#checklist-location ul li').removeClass("disabled");
		
		
				
		// Disable options not applicable to current property type:
		switch(propertyType) {
			
			case "lodge":	
			disableInput($('input#sleeps_2_to_4'));
			disableInput($('input#sleeps_5_to_8'));
			break;
			
			case "home":
			disableInput($('input#sleeps_13_to_16'));
			disableInput($('input#sleeps_17_up'));
			break;
			
			case "cabin":
			disableInput($('input#sleeps_13_to_16'));
			disableInput($('input#sleeps_17_up'));
			break;			
			
			case "condo":
			disableInput($('input#sleeps_9_to_12'));
			disableInput($('input#sleeps_13_to_16'));
			disableInput($('input#sleeps_17_up'));
			
			disableInput($('input#location_plains'));
			disableInput($('input#location_wenatchee'));
			break;
			
			default:
			disableInput($('input#sleeps_2_to_4'));
			disableInput($('input#sleeps_5_to_8'));
		}
	}
	

	//
	//	PAGE SETUP:
	//
	
	// Switch off background with Update Results button
	$('div#search_widget-bot').addClass("search_widget-bot-nobtn");
	
	// Remove hidden update button
	$('div#search_widget-bot').html("");
	
	// Make info buttons visible
	$('ul#search_widget-navbar li a.info_hotspot').css("visibility","visible");
	
	// Hide all "more_info" popups
	$('div.more_info a.close-more_info').click(function() {
		$('div.more_info').fadeOut();
		$('ul#search_widget-navbar li a.info_hotspot').removeClass('info_hotspot-active');
	});
	
	// Remove all Hrefs from search widget hotspots (hrefs only useful for non-JS)
	$('a.hotspot').removeAttr('href');
	
	// Change property_search's action to only return search results
	$('form#property_search').attr("action","/destinations/search-results/");
	
	// Hide Ajax progress image
	$('img#ajax-progress').css({visibility:"hidden"});
	
	// Switch options related to property type in query string:
	toggleSizeOptions(queryProperty);
	
	
	//
	//	EVENT HANDLERS:
	//
	
	
	// Fade in pop up corresponding to destination type (referenced from a#info_hotspot ID)
	$('ul#search_widget-navbar li a.info_hotspot').click(function() {
		
		$('ul#search_widget-navbar li a.info_hotspot').removeClass('info_hotspot-active');
		$(this).toggleClass('info_hotspot-active');
		
		var destinationType = $(this).attr("id");
		
		// Position based off the related info_hotspot anchor
		$('div#more_info-'+destinationType).css('left', $(this).offset().left-262);
		$('div#more_info-'+destinationType).css('top', $(this).offset().top+16);
		
		$('div.more_info').fadeOut();	// Fade out other "more_info" popups
		
		$('div#more_info-'+destinationType).fadeIn();	// Fade in pop up
	});
	
	// Switch on destination type selector on click
	$('ul#search_widget-navbar li a.hotspot').click(function() {
		// Don't let the user select a destination box if an ajax search is already running:
		if(!ajaxRunning) {		
			// Turn off all navbars
			$('ul#search_widget-navbar li a.hotspot').removeClass('active');	
	
			// Turn on clicked navbar
			$(this).addClass('active');
			
			// Change form's property_type value and hide inappropriate size options
			switch($(this).parent().attr('id')) {
				case "swn-lodges":
				document.forms['property_search'].property_type.value = "Lodge";
				toggleSizeOptions("lodge");
				break;
				
				case "swn-homes":
				document.forms['property_search'].property_type.value = "Home";
				toggleSizeOptions("home");
				break;
				
				case "swn-cabins":
				document.forms['property_search'].property_type.value = "Cabin";
				toggleSizeOptions("cabin");
				break;
				
				case "swn-condos":
				document.forms['property_search'].property_type.value = "Condo";
				toggleSizeOptions("condo");
				break;
			}
			
			// Run AJAX:
			searchAJAX();
		}
	});
	
	// Run AJAX on checkbox click (if it isn't already running a request)
	$('input[type="checkbox"]').click(function() {
		if(!ajaxRunning) searchAJAX();
	});
	
	
	// Run AJAX on click of "Update Results" button:
	$('form#property_search').submit(function() {
		if(!ajaxRunning) searchAJAX();
		return false;
	});
	
}});
