$(document).ready(function() {
	///////////////////////////
	// Filter Hotel Results
	///////////////////////////
	 var timeoutLoadingScreen;
	 readUserFilterOptions();
	 
	 // Make filter selection or de-selection	
	 if ($.browser.msie) {
		$('#refineSearch input').click(function () {
			startFiltering();
		});
		$('#refineSearch select').change(function () {
			startFiltering();
		});
	} else { 				 
		$('#refineSearch input, #refineSearch select').change(function() {
			//check/uncheck all destinations for regions menu
			if($(this).attr('id') == 'dfsCheckall'){
				if($(this).is(':checked')) {
					$('input[id^="dfs_"]').attr('checked', 'checked');
				} else {
					$('input[id^="dfs_"]').removeAttr('checked');
					$('input[id^="dfs_"]:first').attr('checked', 'checked');
				}
			}
			
			startFiltering();
		 });
	}
	
	destCheckBoxes = $('input[id^="dfs_"]').length;
	destCheckBoxes_Checked = $('input[id^="dfs_"]:checked').length;
	if(destCheckBoxes == destCheckBoxes_Checked) {
		$('#dfsCheckall').attr('checked', 'checked');
	}
	
	//clear filters
	$('.clearFiltersBtn').live('click', function() {
		$('#refineSearch input[type="checkbox"]').each(function() {
			this.checked = false;
		});
		$('#refineSearch input.dest_cb').each(function() {
			this.checked = true;
		});
		$('#refineSearch input.reg_cb').each(function() {
			this.checked = true;
		});
		$('#refineSearch input[type="radio"]').each(function() {
			this.checked = false;
		});
		$('#refineSearch select').each(function() {
			this.selectedIndex = 0;
		});
		
		startFiltering();
	});

	 
	 // Filter Hotel Functions
	 function startFiltering() {
		saveUserFilterOptions();
		showLoadingScreen();				 	
		timeoutLoadingScreen = window.setTimeout(getHotelListings, 300);
		return;
	 }
	 function getHotelListings() {

			
			$.getJSON("/ajax/ajx_hotelRefineSearch.php", 
					$('#refineSearch').serialize(), 
					function(json){
						$('#hotelSearchResults').html(json.html);
						$('#filteredCount').text(json.shownHotels);
						$('#totalHotelCount').text(json.totalHotelCount);
						
						$('.iFTTBtn').iFrameToolTip({
							hPos: 'left',
							hOffset: -10,
							vPos: 'top',
							vOffset: -6					
						 });
						
						removeLoadingScreen();
						
						//build hotel listing qtips
				 		buildHotelListingQTips();
						
    				},
					'html');

		//document.location.search = 'foo=false';
		return false;
	 }
	 function showLoadingScreen() {
		$('.hotelListing').show().addClass('transparentBG');
		$('#loadingOverlay').show();
		$('.familyAmenities').addClass('hide');
		return; 
	 }
	 function removeLoadingScreen() {
		$('.hotelListing').removeClass('transparentBG');
		$('#loadingOverlay').hide();
		window.clearTimeout(timeoutLoadingScreen);
		return;
	 }

	 function updateHotelCount() {
		hotelsShown = $('.hotelListing:not(:hidden)').length;
		hotelsHidden = $('.hotelListing:hidden').length;
		
		if(hotelsHidden <= 0) { $('#clearFilterHTML').hide(); }
		 else { $('#clearFilterHTML').show(); }
		 
		if(hotelsShown <= 0) { $('#noHotelResults').show(); }
		 else { $('#noHotelResults').hide(); }
		
		$('#filteredCount').text(hotelsShown);
		return;
	 }





	 
	 function saveUserFilterOptions() {
		 cookieName = 'fvcHtlFiltOpt';
		 cookieValue = '';
		 cookieDays = 0;
		 
		 
		 //add pathname as first item in the array
		 cookieValue = cookieValue + window.location.pathname + window.location.search +'|';
		 
		 //start adding the filter options
		 $(".ages_cb:checked").each(function(){
				cookieValue = cookieValue + $(this).val() + ":";
		 });
		 cookieValue = cookieValue + '|';
		 $(".interests_cb:checked").each(function(){
				cookieValue = cookieValue + $(this).val() + ":"
		 });
		 cookieValue = cookieValue + '|';
		 $(".dest_cb:checked").each(function(){
				cookieValue = cookieValue + $(this).val() + ":"
		 });
		 cookieValue = cookieValue + '|';
		 $(".reg_cb:checked").each(function(){
				cookieValue = cookieValue + $(this).val() + ":"
		 });
		 /*cookieValue = cookieValue + '|';
		 $(".amenities_cb:checked").each(function(){
				cookieValue = cookieValue + $(this).val() + ":";
		 });*/
		 cookieValue = cookieValue + '|';
		 cookieValue = cookieValue + $('[name="hrs_memberReviewRating"]:checked').val();
		 /*cookieValue = cookieValue + '|';
		 cookieValue = cookieValue + $('[name="fvcHotelRating"]:checked').val();*/
		 cookieValue = cookieValue + '|';
		 cookieValue = cookieValue + $('[name="hotelsOrderBy"]').val();
		 
		 
		 createCookie(cookieName, cookieValue, cookieDays);
		 return;
	 }
	 function readUserFilterOptions() {	
		cookie = readCookie('fvcHtlFiltOpt');
		if(!cookie) { return false; }

		filtOptions = cookie.split('|');
		if(filtOptions[0] == window.location.pathname + window.location.search){
			
			$(".reg_cb").removeAttr('checked');
			$(".dest_cb").removeAttr('checked');
		
			for(var i=0;i < filtOptions.length;i++) {
				
				selectedOptions = filtOptions[i].split(':');
				for(var n=0;n < selectedOptions.length;n++) {
					if(selectedOptions[n] != '' && selectedOptions[n] != 'undefined') {
						$('#refineSearch input[value="'+selectedOptions[n]+'"]').attr('checked', true);
						$('#refineSearch option[value="'+selectedOptions[n]+'"]').attr('selected', 'selected');
					}
				}
				
			}
			startFiltering();
		} else {
			//landed on new destination page
			//reset options from previous destination page
			eraseCookie('fvcHtlFiltOpt');
			return;
		}
		
		
		return;
	 }
});	 

