
var uksnow={map:null,icon:null,latest:0,tweets:[],fade:1000,slide:500,now:0,iconset:'',ttl:3600,max:1000};$(document).ready(function(){$.ajaxSetup({'timeout':15000,'error':function(XMLHttpRequest,textStatus,errorThrown){setStatus('Update failed',3);}});gmapsLoad();$(document).oneTime('1s',getTweets).everyTime('60s',getTweets);uksnow.iconset=$('select[name=iconset] option:selected').val();updateKey();$('select[name=iconset]').change(changeIcons);});function gmapsLoad(){if(GBrowserIsCompatible()){uksnow.map=new GMap2(document.getElementById('map'));uksnow.map.setMapType(G_SATELLITE_MAP);uksnow.map.setCenter(new GLatLng(54.8,-3),5);uksnow.map.addControl(new GLargeMapControl3D());uksnow.map.addControl(new GMapTypeControl());uksnow.map.addMapType(G_PHYSICAL_MAP);uksnow.map.enableScrollWheelZoom();uksnow.map.enableContinuousZoom();uksnow.icon=new GIcon();uksnow.icon.iconAnchor=new GPoint(16,16);uksnow.icon.iconSize=new GSize(32,32);uksnow.icon.infoWindowAnchor=new GPoint(16,16);uksnow.icon.shadow='';uksnow.icon.transparent='';}}
function changeIcons(){uksnow.iconset=$('select[name=iconset] option:selected').val();$.each(uksnow.tweets,function(i,tweet){if(tweet.home_marker)tweet.home_marker.setImage(iconUrl(tweet));});updateKey();}
function updateKey(){$('#key img').each(function(){$(this).attr('src','./images/'+$(this).attr('title')+uksnow.iconset+'.png');});}
function getTweets(){setStatus('Updating tweets...');$.getJSON('./app/json.php?since='+uksnow.latest+'&'+random(),function(data){if(data.tweets){data.tweets=data.tweets.sort(sortTweets);$.each(data.tweets,function(i,tweet){findPhotos(tweet);plotTweet(tweet);logTweet(tweet);uksnow.tweets.push(tweet);});setStatus(data.tweets.length+' new tweet'+((data.tweets.length==1)?'':'s'),10);}
else{setStatus('No new tweets found',3);}
uksnow.latest=data.latest;uksnow.now=data.now;removeOldTweets();});}
function removeOldTweets(){while(uksnow.tweets.length>uksnow.max){tweet=uksnow.tweets.shift();removeTweet(tweet);}
$.each(uksnow.tweets,function(i,tweet){if(tweet.created_at_time<(uksnow.now-uksnow.ttl)){removeTweet(tweet);}});}
function removeTweet(tweet){if(tweet.home_marker)uksnow.map.removeOverlay(tweet.home_marker);$('#tweets li[rel='+tweet.status_id+']').slideUp().remove();uksnow.tweets=jQuery.grep(uksnow.tweets,function(value){return value!=tweet;});}
function findPhotos(tweet){if(tweet.text==null)return;var tweetphoto=tweet.text.match('http://(www\.)?tweetphoto.com/[A-Za-z0-9]{5,8}');if(tweetphoto){tweet.picLinkUrl=tweetphoto[0];tweet.picThumbUrl='http://tweetphotoapi.com/api/tpapi.svc/imagefromurl?size=thumbnail&url='+tweetphoto[0];}
var twitpic=tweet.text.match('http://(www\.)?twitpic.com/([A-Za-z0-9]{5,6})');if(twitpic){tweet.picLinkUrl=twitpic[0];tweet.picThumbUrl='http://twitpic.com/show/thumb/'+twitpic[2];}
var yfrog=tweet.text.match('http://(www\.)?yfrog.(com|us)/[A-Za-z0-9]{6,8}');if(yfrog){tweet.picLinkUrl=yfrog[0];tweet.picThumbUrl=yfrog[0]+'.th.jpg';}}
function plotTweet(tweet){if(tweet.home_located>0){tweet.home_location=new GLatLng(tweet.home_latitude,tweet.home_longitude);uksnow.icon.image=iconUrl(tweet);tweet.home_marker=new GMarker(tweet.home_location,{icon:uksnow.icon});GEvent.addListener(tweet.home_marker,'click',function(){var html='<div class="clear popup"><div class="image"><a href="http://twitter.com/'+tweet.from_user+'" target="_blank"><img class="profilepic" src="'+tweet.profile_image_url+'" /></a></div><div class="content"><h3><a href="http://twitter.com/'+tweet.from_user+'" target="_blank">'+tweet.from_user+'</a></h3><p>'+linkify(tweet.text)+'</p>';if(tweet.picThumbUrl)html+='<div class="photo"><a href="'+tweet.picLinkUrl+'" target="_blank"><img alt="*" src="'+tweet.picThumbUrl+'" /></a></div>';html+='<small>Tweeted at '+tweet.created_at+'</small></div></div>';tweet.home_marker.openInfoWindowHtml(html);});uksnow.map.addOverlay(tweet.home_marker);}}
function iconUrl(tweet){return'./images/'+rateIcon(tweet.rate)+uksnow.iconset+'.png?refresh';}
function rateIcon(rate){if(rate>6)return'4';if(rate>4)return'3';if(rate>2)return'2';if(rate>0)return'1';return'0';}
function logTweet(tweet){var html='<div class="image"><a href="http://twitter.com/'+tweet.from_user+'" target="_blank"><img alt="'+tweet.from_user+'" src="'+tweet.profile_image_url+'" /></a></div><div class="content">';if(tweet.home_marker)html+='<span class="show">&nbsp;</span>';html+='<h3><a href="http://twitter.com/'+tweet.from_user+'" target="_blank">'+tweet.from_user+'</a></h3><p>'+linkify(tweet.text)+'</p>';if(tweet.picThumbUrl)html+='<div class="photo"><a href="'+tweet.picLinkUrl+'" target="_blank"><img alt="*" src="'+tweet.picThumbUrl+'" /></a></div>';html+='<small>Tweeted at '+tweet.created_at+'</small></div>';$("#tweets").prepend('<li class="clear" rel="'+tweet.status_id+'">'+html+'</li>').children('li:first').find('.show').click(function(){GEvent.trigger(tweet.home_marker,'click');});if(uksnow.latest>0)$('#tweets li:first').hide().slideDown(uksnow.slide);}
function setStatus(text,hold){if(text)$('#status').html(text);if($('#status:hidden').length>0)$('#status').fadeIn(uksnow.fade);if(hold>0)$('#status').oneTime(hold+'s',function(){hideStatus();});}
function hideStatus(){$('#status').fadeOut(uksnow.fade,function(){$('#status').html('');});}
function linkify(text){if(text==null)return;var urls=text.match(/http\S+/ig);if(urls){for(u=0;u<urls.length;u++){var url=urls[u];var t=(url.length>30)?url.substr(0,27)+'...':url;var a='<a href="'+url+'" target="_blank">'+t+'</a>';text=text.replace(url,a);}}
return text;}
function sortTweets(x,y){return x.status_id-y.status_id;}
function random(){return new Date().getTime();}