(function($) {
	$(document).ready(function() {
		var localSearch = new google.search.LocalSearch(),
			geocoder = new GClientGeocoder(),
			$latitude = $('#latitude'),
			$longitude = $('#longitude'),
			$location = $('#location'),
			location, isZipCode, doSubmit = false,
			locations;
			
		function checkForZipCode(value) {
			var zipCodeRegEx = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
			return zipCodeRegEx.test(value);
			//return /^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$/i.test(value);
		}

		function submitResults(latitude, longitude) {
			doSubmit = true;
			
			$('#latitude').val(latitude);
			$('#longitude').val(longitude);
			
			$('#frmDL').submit();
		}
		
		function localSearchResultsHandler() {
			$location.removeClass('active');
			
            if (localSearch.results[0]) {
				submitResults(localSearch.results[0].lat, localSearch.results[0].lng);
            } else {
                alert($location.val() + ' not found');
            }
		}
		
		function geocoderResultsHandler(response) {
			$location.removeClass('active');
			var html = '';
			
			if (!(response.Placemark && response.Placemark.length > 0)) {
				alert($location.val() + ' not found');
				return;
			}
			
			if (response.Placemark.length == 1) {
				submitResults(
					response.Placemark[0].Point.coordinates[1],
					response.Placemark[0].Point.coordinates[0]
				);
				
				return;
			}

			locations = [];
			var cl = '';
			
			for (var i = 0; i < response.Placemark.length; ++i) {
				cl = (i % 2 != 0 ? 'even' : 'odd');
				
				locations[i] = {
					address: response.Placemark[i].address,
					lat: response.Placemark[i].Point.coordinates[1],
					lng: response.Placemark[i].Point.coordinates[0]
				};
				
				html += '<li class="' + cl + '"><a href="#" id="location-' + i + '">' + response.Placemark[i].address + '</a></li>';
			}
			
			$('#multiple-results ul').html(html);
			$('#multiple-results:hidden').slideDown(1000);
		}
	
		localSearch.setSearchCompleteCallback(null, localSearchResultsHandler);
		
		// Bind search button
		$('#dlSearchButton').click(function() {
			location = $location.val().replace(/^\s+|\s+$/g, "");

			if (location == '') {
				alert('Please enter a zip code.');
				return false;
			}

			if($("#dl-services-list input:checked").length == 0){
				alert("Please choose at least one industry.");
				return false;
			}

			$location.addClass('active');

			isZipCode = checkForZipCode(location);
			
			// Perform local search if location is a zip code for better accuracy
			if (isZipCode) {
				localSearch.execute(location);
			} else {
				geocoder.getLocations(location, geocoderResultsHandler);
			}
			
			return false;
		});
		
		// Bind multiple results
		/*$('#multiple-results ul a').live('click', function() {
			var index = this.id.substring(9);
			
			$location.val(locations[index].address);
			submitResults(locations[index].lat, locations[index].lng);
			
			return false;
		});*/

		$('#frmDL').submit(function() {
			if (!doSubmit) {
				$('#dlSearchButton').click();
				return false;
			}
			
			return true;
		});
		
		// Enable form search button
		$('#dlSearchButton').removeAttr('disabled');		
	});
})(jQuery);

