jQuery(document).ready(function(){
	
	// LOCATION FORM
	// clear location form when initially clicked
   jQuery('.location_field').click(function() {
	   if(jQuery(this).val() == 'Enter your City, State or Zipcode' || jQuery(this).val() == 'Ex: 90028 or Hollywood, Ca'){
		   jQuery(this).attr('value', '');
		 }
	});
	
	// location form submit
	jQuery('#location_form').submit(function(event) {
		event.preventDefault();
		var locationRegEx = /.*, ../;
		var zipCodeRegEx = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
		
		if(jQuery('.location_field').val() == 'Ex: 90028 or Hollywood, Ca' || jQuery('.location_field').val() == 'Enter your City, State or Zipcode' || jQuery('#location_field').val() == '') {
			// if its default or empty...
			jQuery('.error_text').html('<strong>Location not found!</strong><br />Enter zip code or location using this format: "City, St"');
		} else if(isNaN(jQuery('.location_field').val()) && locationRegEx.test(jQuery('.location_field').val()) == false ){
			// if its not numeric and if string doesnt match...
			jQuery('.error_text').html('<strong>Location not found!</strong><br />Enter location using this format: "City, St"');
		} else if(!isNaN(jQuery('.location_field').val()) && zipCodeRegEx.test(jQuery('.location_field').val()) == false){
			// if is numeric and number doesnt match...
			jQuery('.error_text').html('<strong>Location not found!</strong><br />Enter a valid Zip Code');
		} else {
			document.forms['location_form'].submit();
		}
	});
	
	// SEARCH FORM
	jQuery('.search_field').click(function() {
	   if(jQuery(this).val() == 'What are you searching for?'){
		   jQuery(this).attr('value','');
		 }
	});
	
	// SEARCH TYPE TABS
	
	jQuery('#search_tabs li a').click(function(event) {
		event.preventDefault();
		
		jQuery('#search_tabs li .selected').removeClass('selected');
		jQuery(this).addClass('selected');
		jQuery('.search_type').val(jQuery(this).find('span').text().toLowerCase());
	});
	
	// SHOW MORE TABS
	var opened_content_height;
	var closed_content_height;
	var slide_speed = 500;
	var extra_spacing = 0;
	
	jQuery('.show_more_content a.closed').live('click', function(event) {
		event.preventDefault();
		// get and store default size
		jQuery(this).prev('.default_size').html(jQuery(this).parent().parent().height());
		// get opened content height
		opened_content_height = jQuery(this).parent().parent().find('.content').height()+ extra_spacing;		
		
		// animate
		 jQuery(this).removeClass('closed').addClass('opened').html('Show Less<span>-</span>').find('span').css('line-height', '12px').end().parent().parent().animate({ height: opened_content_height, paddingBottom:'60px'}, slide_speed, function(){jQuery(this).css('height', 'auto')});	
	});
	
	jQuery('.show_more_content a.opened').live('click', function(event) {
		event.preventDefault();
		// set closed content height to default height
		closed_content_height = jQuery(this).prev().html();
		// animate
		jQuery(this).removeClass('opened').addClass('closed').html('Show More<span>+</span>').find('span').css('line-height', '17px').end().parent().parent().animate({ height: closed_content_height, paddingBottom:'0px'}, slide_speed);
	});

  	// ALL CITIES TABS
	var cities_slide_speed = 300;
	jQuery('.letter_header.opened').live('click', function(event) {
		event.preventDefault();
		jQuery(this).removeClass('opened').addClass('closed').find('.button').html('+').end().next().slideUp(cities_slide_speed);
	}); 

	jQuery('.letter_header.closed').live('click', function(event) {
		event.preventDefault();
		jQuery(this).removeClass('closed').addClass('opened').find('.button').html('-').end().next().slideDown(cities_slide_speed);
	});   
  
  
  
  	// load google maps
	if ($("#map_canvas").length > 0){
		$(window).load(googleMapsInitialize);
	}
	
	// GOOGLE MAPS CODE
	var geocoder;
	var map;
	var bounds;
	var latLngList = Array();
	var markerTrackLatLng = Array();
	var saveMarker = Array();
	var delayMenuPopupTimeoutID = null;
	var countPins = 0;

	function codeAddress(address,count) {
		//console.log('codeAddress count: ' + count);

		geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {

				countPins++;

				map.setZoom(15);

				//  Make an array of the LatLng's of the markers you want to show
				latLngList[count]=(new google.maps.LatLng(results[0].geometry.location.Ba, results[0].geometry.location.Ca));

				// Track the id from yellowbook
				markerTrackLatLng[count]=results[0].geometry.location.Ba + ', ' + results[0].geometry.location.Ca;

				//  Create a new viewpoint bound
				bounds.extend (latLngList[count]);

				//  Fit these bounds to the map
				if(requestedAddress.length == 1 || countPins == 1){
					map.setCenter(results[0].geometry.location);
				} else {
					map.fitBounds (bounds);
				}

				// var origin = markerSpriteOrigin(count);

				var marker = new google.maps.Marker({
					map: map,
					cursor: 'hand',
					position: results[0].geometry.location
				});
				saveMarker[requestedAddressId[count]]=marker;

				if (count < requestedAddress.length-1) {
					count++;
					codeAddress(requestedAddress[count],count);
				}

			} else {
				//console.log("Geocode was not successful for the following reason: " + status);
				markerTrackLatLng[count] = '';
				if (count < requestedAddress.length-1) {
					count++;
					codeAddress(requestedAddress[count],count);
				}
			}
		});
	}

	function googleMapsInitialize() {
		geocoder = new google.maps.Geocoder();
		bounds = new google.maps.LatLngBounds();

		var latlng = new google.maps.LatLng(38.1569, -96.8555);

		var myOptions = {
			zoom: 2,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

		codeAddress(requestedAddress[0],0);
	}
	
	
// end jquery document ready
});

