var map,icon;var latest='';var tweets=new Array();var addthis_brand="";var addthis_share={templates:{twitter:'{{url}}'}};$(document).ready(function(){$.ajaxSetup({'timeout':15000});gmapsLoad();$('#map').oneTime('1ms',getTweets).everyTime('30s',getTweets);$('#map').oneTime('2ms',getTopTen).everyTime('60s',getTopTen);$('a[rel=add]').click(showAdd);$('a[rel=close]').click(closeAdd);$('#submit').click(addLocation);});function gmapsLoad(){if(GBrowserIsCompatible()){map=new GMap2(document.getElementById("map"));map.setMapType(G_NORMAL_MAP);map.setCenter(new GLatLng(30,0),2);map.addControl(new GLargeMapControl3D());map.addControl(new GMapTypeControl());map.enableScrollWheelZoom();map.enableContinuousZoom();var mapTypes=G_DEFAULT_MAP_TYPES;for(var i=0;i<mapTypes.length;i++){mapTypes[i].getMaximumResolution=function(latlng){return 14;};mapTypes[i].getMinimumResolution=function(latlng){return 2;};}
icon=new GIcon(G_DEFAULT_ICON);}}
function sortTweets(x,y){return y.status_id-x.status_id;}
function getTweets(){$.getJSON('/app/json.php?method=tweets&since='+latest,function(data){if(data.tweets){data.tweets=data.tweets.sort(sortTweets);$.each(data.tweets,function(i,tweet){var home_location,home_marker;var popUpHtml='<div class="popup"><a href="http://twitter.com/'+tweet.from_user+'" target="_blank"><img class="profilepic" height="48" src="'+tweet.profile_image_url+'" width="48" /></a><div><h3><a href="http://twitter.com/'+tweet.from_user+'" target="_blank">'+tweet.from_user+'</a></h3>'+tweet.text+'</div></div>';if(tweet.home_located>0){home_location=new GLatLng(tweet.home_latitude,tweet.home_longitude);home_marker=new GMarker(home_location,{icon:icon});}
if(home_marker){GEvent.addListener(home_marker,"click",function(){home_marker.openInfoWindowHtml(popUpHtml);});map.addOverlay(home_marker);tweets.push(tweet);}});}
updateLatest();latest=data.statusId;});}
function updateLatest(){tweets=tweets.sort(sortTweets);$.each(tweets,function(i,tweet){if(i<5){$('#tweets li:eq('+i+')').html('<a href="http://twitter.com/'+tweet.from_user+'" target="_blank"><img class="profile-pic" src="'+tweet.profile_image_url+'" alt="'+tweet.from_user+'" /></a><strong><a href="http://twitter.com/'+tweet.from_user+'" target="_blank">'+tweet.from_user+'</a></strong><br/>'+tweet.text);}});}
function getTopTen(){$.getJSON('/app/json.php?method=topten',function(data){if(data.topten){$.each(data.topten,function(i,place){var item='<a href="#" title="Show '+place.name+' on the map">'+place.name+' ('+place.n+')</a>';$('#topten li:eq('+i+')').html(item).click(function(){map.setCenter(new GLatLng(place.latitude,place.longitude),10);});});}});}
function showAdd(){$('#add').fadeIn();}
function closeAdd(){$('#add').fadeOut();}
function addLocation(){var name=$('#name').val();if(name.length==0){alert('Please enter your name.');return false;}
var location=$('#location').val();if(location.length==0){alert('Please enter your location.');return false;}
$('#add table').slideUp(function(){$('#add #message').text('Adding location...').slideDown(function(){$.post('/app/json.php?method=add',{name:name,location:location},function(data){if(data.success===true){$('#add #message').text('You were successfully added. Thanks!');$(this).oneTime(2000,closeAdd);}
else{$('#add #message').text('Sorry, your location could not be determined.');$('#add table').slideDown();}},'json');});});}