google.load("jquery","1.3.2");google.load("jqueryui","1");google.load("maps","2");var MAP,map,geocoder,window_template,news_current_place,legend_template,items_per_site=50;var sites={flickr:{name:"Flickr",url:"flickr.com",api:"http://api.flickr.com/services/rest/?jsoncallback=?",icon:"http://l.yimg.com/g/images/dot1_p.png",options:{method:"flickr.photos.search",per_page:items_per_site,min_upload_date:"1100000000",sort:"interestingness-desc",api_key:"41f94fc147b4d8327ea77fa7a4f1b842",format:"json",extras:"geo,url_s,owner_name,path_alias"}},youtube:{name:"YouTube",url:"youtube.com",api:"http://gdata.youtube.com/feeds/api/videos?callback=?",icon:"http://www.youtube.com/favicon.ico",options:{"max-results":items_per_site,alt:"json-in-script"}},picasa:{name:"Picasa",url:"photos.google.com",api:"http://picasaweb.google.com/data/feed/api/all?callback=?",icon:"picasa.png",options:{kind:"photo",access:"public",thumbsize:"160c","max-results":items_per_site,alt:"json-in-script"}},panoramio:{name:"Panoramio",url:"panoramio.com",api:"http://panoramio.com/map/get_panoramas.php?callback=?",icon:"http://www.panoramio.com/favicon.ico",options:{order:"popularity",set:"public",size:"small",from:"0",to:items_per_site}},wikipedia:{name:"Wikipedia",url:"wikipedia.org",api:"http://ws.geonames.org/wikipediaBoundingBoxJSON?callback=?",icon:"wikipedia.png",options:{maxRows:items_per_site}},wikicommons:{name:"Wikimedia Commons",url:"commons.wikimedia.org",api:"http://pipes.yahoo.com/pipes/pipe.run?_callback=?",icon:"wikicommons.png",options:{_id:"841c3705af747918661b40a29f41b3d6",_render:"json",max:items_per_site}},pipes:{api:"http://pipes.yahoo.com/pipes/pipe.run?_callback=?"},brightkite:{name:"BrightKite",url:"brightkite.com",icon:"http://brightkite.com/favicon.ico"},google_news:{name:"Google News",url:"news.google.com",api:"http://ajax.googleapis.com/ajax/services/search/news?callback=?",icon:"http://www.gstatic.com/news/img/favicon.ico"},twitter:{name:"Twitter",url:"twitter.com",api:"http://search.twitter.com/search.json?callback=?",icon:"http://twitter.com/favicon.ico",options:{rpp:items_per_site,q:"*"}},upcoming:{name:"Upcoming",url:"upcoming.yahoo.com",api:"http://upcoming.yahooapis.com/services/rest/?callback=?",icon:"http://upcoming.yahoo.com/favicon.ico",options:{method:"event.search",api_key:"50bc243b76",format:"json",per_page:items_per_site,min_date:yyyy_mm_dd(),sort:"distance-asc"}}};function yyyy_mm_dd(){var b=new Date();var c=b.getMonth()+1;var a=b.getDate();if(c<10){c="0"+c}if(a<10){a="0"+a}return[b.getFullYear(),c,a].join("-")}var api,api_key="031ec0b669f41864a351c3d03de829a0",xd_receiver="http://"+window.location.host+"/xd_receiver.htm",made_fb_call=false,fb_friends=[];function friend_to_location(b,c,a){if(b.current_location&&b.current_location[c]==a){$("#fb_friends").append('<div class="facebook"><a href="http://facebook.com/profile.php?id='+b.uid+'" target="_blank">'+b.name+"</a></div>")}}function friends_from_state(a){$("#fb_friends").prepend("<p>You have "+$("#fb_friends .facebook").length+" friends from the state of "+a+" on Facebook.</p>")}function fb_show_friends(b,a){$("#fb_friends").empty();if(b>7&&a.CountryName=="USA"){var c=us_states[a.AdministrativeArea.AdministrativeAreaName];if(!made_fb_call){FB_RequireFeatures(["Connect"],function(){FB.Facebook.init(api_key,xd_receiver);api=FB.Facebook.apiClient;FB.Connect.get_status().waitUntilReady(function(d){if(d==FB.ConnectState.connected){api.friends_get(new Array(),function(e){api.fql_query("SELECT uid, name, current_location FROM user WHERE uid IN ("+e.join(",")+")",function(f,g){if(!g){made_fb_call=true;$.each(f,function(h,j){friend_to_location(j,"state",c);fb_friends.push(j)});friends_from_state(c)}})})}else{$("#fb_friends").html('<div><img src="http://wiki.developers.facebook.com/images/e/ed/Connect_dark_large_short.gif" class="pointer"/></div>').click(function(){FB.Connect.requireSession(function(){load_markers()})})}})})}else{$.each(fb_friends,function(d,e){friend_to_location(e,"state",c)});friends_from_state(c)}}else{$("#fb_friends").html("You might have friends here, but I have yet to have the time to sit down and fine tune a good way to support this at the moment. My apologies.")}}function get_news(geo){if(news_current_place!=geo){$.get(sites.google_news.api,{rsz:"large",v:"1.0",geo:geo},function(result){news_current_place=geo;$("#news").empty();$("#news").append("<p>News for "+geo+' brought to you by <a href="http://'+sites.google_news.url+'" class="legend" style="background:url('+sites.google_news.icon+') no-repeat">Google News</a></p>');$.each(result.responseData.results,function(i,news){var news_html=window_template;with(news){var news_item={author:publisher,title:title,content:content,link:unescapedUrl,author_link:unescapedUrl}}$.each(news_item,function(i){var re=new RegExp("{"+i+"}","g");news_html=news_html.replace(re,this)});$("#news").append(news_html)})},"json")}}function geo_resolve_location(){geocoder.getLocations(map.getCenter(),function(result){var zoom_level=map.getZoom();var place=result.Placemark[0].AddressDetails.Country;var geo;if(zoom_level>13){geo=place.AdministrativeArea.Locality.PostalCode.PostalCodeNumber}else{if(zoom_level>10){with(place.AdministrativeArea){geo=[Locality.LocalityName,AdministrativeAreaName].join(", ")}}else{if(zoom_level>7){with(place){geo=[AdministrativeArea.AdministrativeAreaName,CountryName].join(", ")}}else{geo=place.CountryName}}}fb_show_friends(zoom_level,place);get_news(geo)})}function get_tweets(coordinates,radius){$.get(sites.twitter.api,$.extend(sites.twitter.options,{geocode:[coordinates,radius+"km"].join(",")}),function(result){$("#tweets").empty();if(result.error){$("#tweets").html("<img src='http://img190.imageshack.us/img190/1512/whalev.png'/>");return}with(sites.twitter){$("#tweets").append('<p>From <a href="http://'+url+'" class="legend" style="background:url('+icon+') no-repeat">'+name+"</a></p>")}$.each(result.results,function(i,tweet){var tweet_html=window_template;with(tweet){var tweet_item={author:from_user,title:"",content:text,link:"http://twitter.com/"+from_user+"/"+id,author_link:"http://twitter.com/"+from_user}}$.each(tweet_item,function(i){var re=new RegExp("{"+i+"}","g");tweet_html=tweet_html.replace(re,this)});$("#tweets").append(tweet_html)})},"json")}function get_brightkite(coords,radius){$.get(sites.pipes.api,{_id:"c176254533ebac5254c13228670f6ea4",_render:"json",coords:coords},function(result){$.get(sites.pipes.api,{_id:"fbcbd48e7c1c722b51f5d15ef5e60c80",_render:"json",hours_ago:2,object_name:result.value.items[0].content,radius:((radius>100)?100:radius)*1000},function(re){$.each(re.value.items[0].photo,function(i,marker){with(marker){add_item({lat:place.latitude.content,lng:place.longitude.content,link:"http://brightkite.com/objects/"+id,title:"Photo",image:photo.replace(/\.(\w{3})$/,"-feed.$1"),width:400,height:300,description:body,author:creator.login,author_link:"http://brightkite.com/people/"+creator.login,icon:sites.brightkite.icon})}});$.each(re.value.items[0].checkin,function(i,marker){with(marker){add_item({lat:place.latitude.content,lng:place.longitude.content,link:"http://brightkite.com/objects/"+id,title:"Check in",description:[creator.login,"checked in at",place.display_location,created_at_as_words,"ago"].join(" "),author:creator.login,author_link:"http://brightkite.com/people/"+creator.login,icon:sites.brightkite.icon})}});$.each(re.value.items[0].note,function(i,marker){with(marker){add_item({lat:place.latitude.content,lng:place.longitude.content,link:"http://brightkite.com/objects/"+id,title:"Note",description:[body,"~ posted",created_at_as_words,"ago"].join(" "),author:creator.login,author_link:"http://brightkite.com/people/"+creator.login,icon:sites.brightkite.icon})}});var item_count=0;$.each(re.value.items[0],function(){item_count+=this.length});add_legend(sites.brightkite,item_count)},"json")},"json")}function latlng(b,a){return new MAP.LatLng(b,a)}function make_icon(a){var b=new MAP.Icon();b.image=a;b.iconSize=new GSize(16,16);b.iconAnchor=new GPoint(9,9);b.infoWindowAnchor=new GPoint(9,0);return b}function add_item(c,b){b=b||"";var a=new MAP.Marker(latlng(c.lat,c.lng),make_icon(c.icon));MAP.Event.addListener(a,"click",function(){var d=window_template;c.content="";if(c.image&&c.image.length>0){c.content+='<a href="'+c.link+'" target="_blank"><img src="'+c.image+'" style="width:'+c.width+"px; height:"+c.height+'px"/></a>'}if(c.description){c.content+="<div>"+c.description+"</div>"}$.each(c,function(e){var f=new RegExp("{"+e+"}","g");d=d.replace(f,this)});$("#current_window").html(d);d=d.replace("{window_width}",$("#current_window").width()).replace("{window_height}",$("#current_window").height());a.openInfoWindowHtml(d)});map.addOverlay(a)}function add_legend(c,a){var b=a||0;var d=legend_template;$.each(c,function(e){var f=new RegExp("{"+e+"}","g");d=d.replace(f,this)});$("#legend").append(d.replace("{item_count}",b))}function load_markers(){var bounds=map.getBounds();var SW=bounds.getSouthWest();var NE=bounds.getNorthEast();var left=SW.lng();var right=NE.lng();var bottom=SW.lat();var top=NE.lat();var LATLNG=[map.getCenter().lat(),map.getCenter().lng()].join(",");var bbox=[left,bottom,right,top].join(",");var radius=Math.floor(map.getCenter().distanceFrom(latlng(top,map.getCenter().lng()))/1000);if(radius>1000){radius=1000}$("#legend").empty();map.clearOverlays();$.get(sites.flickr.api,$.extend(sites.flickr.options,{bbox:bbox}),function(result){$.each(result.photos.photo,function(i,marker){with(marker){add_item({lat:latitude,lng:longitude,link:"http://flickr.com/photo.gne?id="+id,title:title,image:url_s,width:width_s,height:height_s,author:ownername,author_link:"http://flickr.com/"+pathalias,icon:sites.flickr.icon})}});add_legend(sites.flickr,result.photos.photo.length)},"json");$.get(sites.youtube.api,$.extend(sites.youtube.options,{location:LATLNG,"location-radius":radius+"km"}),function(result){var item_count=0;$.each(result.feed.entry,function(i,marker){item_count++;with(marker){var youtube_content={link:media$group.media$player[0].url,title:media$group.media$title.$t,image:media$group.media$thumbnail[0].url,width:120,height:90,author:author[0].name.$t,author_link:"http://youtube.com/"+author[0].name.$t,icon:sites.youtube.icon}}if(marker.georss$where){var coords=marker.georss$where.gml$Point.gml$pos.$t.split(" ");with(marker){add_item($.extend(youtube_content,{lat:coords[0],lng:coords[1]}))}}else{geocoder.getLatLng(marker.yt$location.$t,function(coords){add_item($.extend(youtube_content,{lat:coords.lat(),lng:coords.lng()}))})}});add_legend(sites.youtube,item_count)},"json");$.get(sites.picasa.api,$.extend(sites.picasa.options,{bbox:bbox}),function(result){var item_count=0;$.each(result.feed.entry,function(i,marker){if(marker.georss$where){item_count++;var coords=marker.georss$where.gml$Point.gml$pos.$t.split(" ");with(marker){add_item({lat:coords[0],lng:coords[1],link:link[1].href,title:media$group.media$title.$t,image:media$group.media$thumbnail[0].url,width:160,height:160,author:author[0].name.$t,author_link:author[0].uri.$t,icon:sites.picasa.icon})}}});add_legend(sites.picasa,item_count)},"json");$.get(sites.panoramio.api,$.extend(sites.panoramio.options,{minx:left,miny:bottom,maxx:right,maxy:top}),function(result){$.each(result.photos,function(i,marker){with(marker){add_item({lat:latitude,lng:longitude,link:photo_url,title:photo_title,image:photo_file_url,width:width,height:height,author:owner_name,author_link:owner_url,icon:sites.panoramio.icon})}});add_legend(sites.panoramio,result.photos.length)},"json");$.get(sites.wikipedia.api,$.extend(sites.wikipedia.options,{north:top,south:bottom,east:right,west:left}),function(result){$.each(result.geonames,function(i,marker){add_item({lat:marker.lat,lng:marker.lng,link:"http://"+marker.wikipediaUrl,title:marker.title,description:marker.summary,image:marker.thumbnailImg,width:100,height:100,author:sites.wikipedia.name,author_link:"http://"+marker.wikipediaUrl+"?action=history",icon:sites.wikipedia.icon})});add_legend(sites.wikipedia,result.geonames.length)},"json");$.get(sites.wikicommons.api,$.extend(sites.wikicommons.options,{bbox:[right,bottom,left,top].join(",")}),function(result){var item_count=0;$.each(result.value.items,function(i,marker){var coords=marker.Point.coordinates.split(",");if(marker.description.length>5){item_count++;add_item({lat:coords[1],lng:coords[0],link:"",title:"",description:marker.description,author:"Wikimedia Commons contributors",author_link:"#",icon:sites.wikicommons.icon})}});add_legend(sites.wikicommons,item_count)},"json");$.get(sites.upcoming.api,$.extend(sites.upcoming.options,{location:LATLNG,radius:radius}),function(result){$.each(result.rsp.event,function(i,marker){with(marker){add_item({lat:latitude,lng:longitude,link:"http://upcoming.yahoo.com/event/"+id,title:[name,venue_name].join(" at "),image:photo_url,width:75,height:75,description:description,author:venue_name,author_link:"http://upcoming.yahoo.com/user/"+user_id,icon:sites.upcoming.icon})}});add_legend(sites.upcoming,result.rsp.event.length)},"json");geo_resolve_location();get_tweets(LATLNG,radius);get_brightkite(LATLNG,radius)}var us_states={AL:"Alabama",AK:"Alaska",AZ:"Arizona",AR:"Arkansas",CA:"California",CO:"Colorado",CT:"Connecticut",DE:"Delaware",DC:"District Of Columbia",FL:"Florida",GA:"Georgia",HI:"Hawaii",ID:"Idaho",IL:"Illinois",IN:"Indiana",IA:"Iowa",KS:"Kansas",KY:"Kentucky",LA:"Louisiana",ME:"Maine",MD:"Maryland",MA:"Massachusetts",MI:"Michigan",MN:"Minnesota",MS:"Mississippi",MO:"Missouri",MT:"Montana",NE:"Nebraska",NV:"Nevada",NH:"New Hampshire",NJ:"New Jersey",NM:"New Mexico",NY:"New York",NC:"North Carolina",ND:"North Dakota",OH:"Ohio",OK:"Oklahoma",OR:"Oregon",PA:"Pennsylvania",RI:"Rhode Island",SC:"South Carolina",SD:"South Dakota",TN:"Tennessee",TX:"Texas",UT:"Utah",VT:"Vermont",VA:"Virginia",WA:"Washington",WV:"West Virginia",WI:"Wisconsin",WY:"Wyoming"};google.setOnLoadCallback(function(){MAP=google.maps;window_template=$("#window_template").val();legend_template=$("#legend_template").val();geocoder=new MAP.ClientGeocoder();$("#search").submit(function(){geocoder.getLatLng($(this).find("input[name='query']").val(),function(a){map.setCenter(a);load_markers()});return false});$("#accordion").accordion({autoHeight:true,fillSpace:true,collapsible:true});$(window).unload(function(){GUnload()});$("#update-map").click(load_markers);map=new MAP.Map2(document.getElementById("map"));map.setCenter((function(){var a=google.loader.ClientLocation;if(a){return latlng(a.latitude,a.longitude)}else{return latlng(38.895111,-77.02)}})(),13);map.setUIToDefault();MAP.Event.addListener(map,"dragend",load_markers);load_markers()});