jQuery(document).ready(function($) {
	// keyword search default text...
	$('#s').each(function() {
		var default_value = this.value;
	    $(this).focus(function() {
	        if(this.value == default_value) {
	            this.value = '';
	        }
	    });
	    $(this).blur(function() {
	        if(this.value == '') {
	            this.value = default_value;
	        }
	    });
	});
	
	// location search ui functionality
	var myTimer=false;
	$('#ddsearch_activities, #search_activities').mouseenter(function(){		
		$("#search_activities").show();
		clearTimeout(myTimer);
    }).mouseleave(function(){		
		myTimer = setTimeout(function(){
			$("#search_activities").hide();
		},500);		
    });
	
	$('#search_activities').mouseleave(function(){		
		myTimer = setTimeout(function(){
			$("#search_activities").hide();
		},500);		
    });
	
	$(".activities").click(function() {
		disableEmptyActivities();
		doActivityCheck();
		// Do ajax search currently checked activities to then disable any activites that would return 0 results if checked. 
		
	});
	
	// ON PAGE LOAD
	doActivityCheck();
	
	//initialize();


	function disableEmptyActivities(){
		var activities = $("input:checkbox:checked.activities").map(function () {
			return this.value;
		}).get();
		//alert(activities);
		$.ajax({
		   type: "POST",
		   url: "/wp-content/themes/hcodg/ajax/search.php",
		   //url: "http://caseydev.com/wp-content/themes/hcodg/ajax/search.php",
		   data: {
			   activities : activities.toString(),
			   zipcode_id : $('select#dropdown_city').val(),
			   radius : $('select#distance').val()
		   },
		   success: function(msg){
			   //alert(msg);
			   var enabledarr = msg.split(",");
				//alert(enabledarr.length);
				if(enabledarr.length > 1){
					//alert('wack yo');
					$('input:checkbox:.activities').each(function(index) {
						if($.inArray(this.value, enabledarr)>-1){						
							$(this).removeAttr('disabled');
							$(this).parent().css("color", "#5F5849");
							$(this).parent().css("text-decoration", "none");
						} else {
							$(this).attr('disabled', true);
							$(this).parent().css("color", "#c0c0c0");
							$(this).parent().css("text-decoration", "line-through");
						}
					});
				}else{
				//alert('word up');
					$('input:checkbox:.activities').each(function(index) {					
						$(this).removeAttr('disabled');
						$(this).parent().css("color", "#5F5849");
						$(this).parent().css("text-decoration", "none");
					});	
				}
				   //alert( "Data Saved: " + msg );
		   },
		   error : function(XMLHttpRequest, textStatus, errorThrown) {
			   //alert("XMLHttpRequest.status=" + XMLHttpRequest.status);
		   }
		 });
	}

	function doActivityCheck(){
		var allVals = [];
		var allText = [];
		$("#search_activities").find(":checked").each(function() {
			allVals.push($(this).val());
			allText.push($(this).parent().text());
			//alert($(this).parent().text());
		});
		if($.isEmptyObject(allVals)) {
			$('#ddsearch_activities option:selected').val('-1');
			$('#ddsearch_activities option:selected').text('any activity');
		}else{
			$('#ddsearch_activities option:selected').val(allVals);
			$('#ddsearch_activities option:selected').text(allText.join(', '));
		}	
	}

});
// Note that using Google Gears requires loading the Javascript
// at http://code.google.com/apis/gears/gears_init.js
// So put these in the <head>
// <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
// <script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script>
// ---

//var initialLocation;
//var siberia = new google.maps.LatLng(60, 105);
//var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
//var browserSupportFlag =  new Boolean();
//var map;
//var infowindow = new google.maps.InfoWindow();
//  
//function initialize() {
//  var myOptions = {
//    zoom: 6,
//    mapTypeId: google.maps.MapTypeId.ROADMAP
//  };
//  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//  
//  // Try W3C Geolocation method (Preferred)
//  if(navigator.geolocation) {
//    browserSupportFlag = true;
//    navigator.geolocation.getCurrentPosition(function(position) {
//      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
//      contentString = "Location found using W3C standard";
//      map.setCenter(initialLocation);
//      infowindow.setContent(contentString);
//      infowindow.setPosition(initialLocation);
//      infowindow.open(map);
//    }, function() {
//      handleNoGeolocation(browserSupportFlag);
//    });
//  } else if (google.gears) {
//    // Try Google Gears Geolocation
//    browserSupportFlag = true;
//    var geo = google.gears.factory.create('beta.geolocation');
//    geo.getCurrentPosition(function(position) {
//      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
//      contentString = "Location found using Google Gears";
//      map.setCenter(initialLocation);
//      infowindow.setContent(contentString);
//      infowindow.setPosition(initialLocation);
//      infowindow.open(map);
//    }, function() {
//      handleNoGeolocation(browserSupportFlag);
//    });
//  } else {
//    // Browser doesn't support Geolocation
//    browserSupportFlag = false;
//    handleNoGeolocation(browserSupportFlag);
//  }
//}
// 
//function handleNoGeolocation(errorFlag) {
//  if (errorFlag == true) {
//    initialLocation = newyork;
//    contentString = "Error: The Geolocation service failed.";
//  } else {
//    initialLocation = siberia;
//    contentString = "Error: Your browser doesn't support geolocation. Are you in Siberia?";
//  }
//  map.setCenter(initialLocation);
//  infowindow.setContent(contentString);
//  infowindow.setPosition(initialLocation);
//  infowindow.open(map);
//}
