      var x;
      var y;
      var geo;
      var geocoder;
      var searchPics = false;
			var currentTweets;
			var map;
			var centerMarker;
			var markers = [];
      $(document).ready(function(){
          geocoder = new google.maps.Geocoder();
						var mapOpts = {
														mapTypeId:google.maps.MapTypeId.ROADMAP,
														draggable:false,
														scrollwheel:false,
														disableDefaultUI:true,
														disableDoubleClickZoom:true
													};
						map = new google.maps.Map(document.getElementById("map_container"),mapOpts);
						setCloudmadeTile();
						map.setCenter(new google.maps.LatLng(0,0));
						map.setZoom(2);
          if (navigator && navigator.geolocation){
              geo = navigator.geolocation;
          }
          else if (google.gears){
              geo = google.gears.factory.create('beta.geolocation');
          }
          if (geo != undefined){
              $("#locating_div").show();
              $("#welcome_div").hide();
              geo.getCurrentPosition(geolocation_tweets);
          }
				

      });
			$("#ac_me").awesomebar("http://ws.geonames.org/searchJSON",{extraParams:{
				featureClass:"P",
				style:"full",
				maxRows: 10
			}, dataKeys:["name","adminName1","countryName"],
			formatItem:formatABResults, onItemSelect:selectABResult,width:"354px"});
			
			function formatABResults(row){
				var str = "";
				if (row[1] == "" || row[0] == row[1]){
					str = row[0]+", "+row[2];
				} else {
					str = row.join(", ");
				}
				return str;
			}
		
		function setCloudmadeTile(){
			var tileOpts = {
					getTileUrl: function(coord,zoom,ownerDocument){
						var subdoms = ["a","b","c"];
		      	var sdidx = ((coord.x + coord.y) % 3);
						var tile_x = coord.x;
						var tile_y = coord.y;
						var earth_width = Math.pow(2,zoom);
						if (tile_x < 0){
							tile_x += Math.pow(2,zoom);
						} else if (tile_x >= Math.pow(2,zoom)){
							tile_x -= Math.pow(2,zoom);
						}
						return "http://"+subdoms[sdidx]+".tile.cloudmade.com/24cd86df6b154afb807eb1baf2ba378c/12188/256/"+zoom+"/"+tile_x+"/"+tile_y+".png";
					},
					tileSize: new google.maps.Size(256,256),
					isPng: true,
					maxZoom: 19,
					name: "Cloudmade"
			};
			var cloudmadeMapType = new google.maps.ImageMapType(tileOpts);
			map.mapTypes.set('Cloudmade',cloudmadeMapType);
			map.setMapTypeId('Cloudmade');
		}

			
			
			function searchLocation(){
				$("#tweetlist").hide();
				$("#welcome_div").hide();
        $(".loc_info").show();
        $("#locating_div").show();
        var text = $("#ac_me").val();
        geocoder.geocode({'address':text},function(results,status){
            if (status==google.maps.GeocoderStatus.OK) {
                place = results[0];
                $("#address_info").text(place.formatted_address);
                y = place.geometry.location.lat();
                x = place.geometry.location.lng();
                searchTweets(y,x);
            }
        });
			}
			function selectABResult(){
				searchLocation();
			}
      String.prototype.unescapeHtml = function () {
          var temp = document.createElement("div");
          temp.innerHTML = this;
          var result = temp.childNodes[0].nodeValue;
          temp.removeChild(temp.firstChild)
          return result;
      }
      function locate_tweets(item) {
          y = item.y;
          x = item.x;
          $("#refresh").show();
          searchTweets(y,x);
      }
      function geolocation_tweets(pos){
          y = pos.coords.latitude;
          x = pos.coords.longitude;
          var latlng = new google.maps.LatLng(y,x);
          geocoder.geocode({'latLng':latlng},
          function(results, status){
              if (status == google.maps.GeocoderStatus.OK) {
                  place = (results.length > 1)? results[1] : results[0];
                  $("#address_info").text(place.formatted_address);
                  $("#refresh").show();
                  $("#locating_div").hide();
                  searchTweets(y,x);
              }
          });
      }
      function refreshTweets(){
          if (x != undefined && y != undefined){
              searchTweets(y,x);
          }
      }
      function searchTweets(lat,lng){
					
          $("#tweetlist").hide();
          $("#locating_div").hide();
          $("#loading_div").show();
          $("#tweetlist").html("");

						map.setCenter(new google.maps.LatLng(lat,lng));
						map.setZoom(13);
						$(markers).each(function(){
							this.setMap(null);
						});
						markers = [];
						var main_marker = new google.maps.Marker({position:new google.maps.LatLng(lat,lng),map:map});
						markers.push(main_marker)

          var geostr = lat+","+lng+",10km";
          var twitterSearchUrl = "http://search.twitter.com/search.json?geocode="+geostr+"&callback=?&rpp=50";
          $.getJSON(twitterSearchUrl,
          function(data){
						
              if (data == ""){
                  $("#tweetlist").html("<li><h2>Fail Whale</h2></li>");
              }
              if (data.results.length == 0){
                  $("<li></li>").html("<h3>Sorry there doesn't appear to be any recent tweets near you</h3>").appendTo($("#tweetlist"));
              }
              else{
                  currentTweets = new Array();
									$.each(data.results, function(){
										  var newTweet = new Tweet(this);
											currentTweets.push(newTweet);
											if (newTweet.geo){
												var tweetMarker = new google.maps.Marker({position: newTweet.latlng, map:map})
												markers.push(tweetMarker);
											}
                      $("<li></li>").html(newTweet.parseHTML()).appendTo($("#tweetlist"));
                  });
              }
              $("span.pdate").prettyDate();
              $("#loading_div").hide();
              $(".loc_info").show();
              $("#tweetlist").show();


          });
      }

      $("#ac_me").focus(function(){
          if ($(this).hasClass("init")){
              $(this).removeClass("init");
              $(this).val("");
          }
      });
      $("#loc_search").submit(function(){
          if (!$("#ac_me").hasClass("init")){
              searchLocation();
          }
          return false;
      });
