//ALL code in the file is copyright 2009-2010 Mark Goddard http://0100.tv/ & TwiPho
//All rights reserved

function relative_time(time_value){var parsed_date=Date.parse(time_value);var relative_to=(arguments.length>1)?arguments[1]:new Date();var delta=parseInt((relative_to.getTime()-parsed_date)/1000);if(delta<60){return'less than a minute ago';}else if(delta<120){return'about a minute ago';}else if(delta<(45*60)){return(parseInt(delta/60)).toString()+' minutes ago';}else if(delta<(90*60)){return'about an hour ago';}else if(delta<(24*60*60)){return'about '+(parseInt(delta/3600)).toString()+' hours ago';}else if(delta<(48*60*60)){return'1 day ago';}else{return(parseInt(delta/86400)).toString()+' days ago';}}
function supports_local_storage(){return('localStorage'in window)&&window['localStorage']!==null;}
function return_thumb(url){if(url.indexOf("yfrog.com")>-1){var echo=url+".th.jpg"
return echo;}
if(url.indexOf("twitpic.com")>-1){var id=url.substr(19);return"http://twitpic.com/show/thumb/"+id;}
if(url.indexOf("instagr.am")>-1){var id=url.substr(18);return"http://instagr.am/"+id+"media/?size=t";}
if(url.indexOf("img.ly")>-1){var id=url.substr(14);return"http://img.ly/show/thumb/"+id;}
if(url.indexOf("twitvid.com")>-1){var id=url.substr(19);return"http://images.twitvid.com/"+id+".jpg";}}
function runQuery(results,query){$.cookie('last-search',query);$("#share a").fadeIn("slow");_gaq.push(['_trackEvent','search','ran']);_gaq.push(['_trackPageview','/search.php']);if(query){_gaq.push(['_trackEvent','search-term',query]);}else{_gaq.push(['_trackEvent','search-term',"public-timeline"]);}
$("#search-icon").show();window.location.hash=$("#search-box").val();$("#search-items").prepend("<li>"+query+"</li>");$("#search-items li:gt(9)").remove();if(supports_local_storage()){var searches=$("#search-items").html();localStorage.setItem("searches",searches);}
window.scrollTo(0,0);var clients=$("#services input:checked");var searchQuery="";$.each(clients,function(i,n){if(i!==clients.length-1){searchQuery=searchQuery+n.value+" OR ";}else{searchQuery=searchQuery+n.value;}});if($("#rt input[@name='option_layout']:checked").val()=="yes"){searchQuery=searchQuery+" -RT";}
$.getJSON("http://search.twitter.com/search.json?q="+query+" + "+searchQuery+"&rpp="+results+"&include_entities=1&callback=?",function(json){$("#results").text("");if(json.results.length==0){$("#results").append("<h2 class='noResults'>There were no results for that search, try again</h2>");}else{if(!query){query="public timeline";}
var today=new Date();var h=today.getHours();var m=today.getMinutes();var s=today.getSeconds();var time=h+":"+m+":"+s;$("#results").prepend("<h1>"+time+": Showing the most recent <strong>"+json.results.length+" results</strong> for <strong>"+query+"</strong></h1>");}
$("#results").fadeIn(2000);$.each(json.results,function(i,val){var src="";var srcLink="";var imageRegEx=/(http:\/\/(www.|)+(twitpic.com|img.ly|yfrog.com|twitvid.com|t.co|instagr.am)\/){1}[a-zA-Z0-9]{1,}/g;if(val.text.search(imageRegEx)!==-1){if($.isEmptyObject(val.entities)){return"skip";}
imgFind=val.entities.urls[0].expanded_url;var tweet="<p class='words'>"+val.text+"</p>";if(imgFind.indexOf("http://twitpic.com/")>-1){tweet=tweet.replace(imgFind,"");imgId=imgFind.substr(19);srcLink=imgFind;src="http://twitpic.com/show/thumb/"+imgId;imgSource="twitpic.com";}
if(imgFind.indexOf("http://instagr.am/")>-1){tweet=tweet.replace(imgFind,"");imgId=imgFind.substr(18);srcLink=imgFind;src="http://instagr.am/"+imgId+"media/?size=t";imgSource="instagr.am";}
if(imgFind.indexOf("http://www.twitpic.com/")>-1){tweet=tweet.replace(imgFind,"");imgId=imgFind.substr(23);srcLink=imgFind;src="http://twitpic.com/show/thumb/"+imgId;imgSource="twitpic.com";}
if(imgFind.indexOf("http://img.ly/")>-1){tweet=tweet.replace(imgFind,"");imgId=imgFind.substr(14);srcLink=imgFind;src="http://img.ly/show/thumb/"+imgId;var imgSource="img.ly";}
if(imgFind.indexOf("http://yfrog.com/")>-1||imgFind.indexOf("http://www.yfrog.com/")>-1){tweet=tweet.replace(imgFind,"");srcLink=imgFind;src=imgFind+".th.jpg";var imgSource="yfrog.com";}
if(imgFind.indexOf("http://twitvid.com/")>-1){tweet=tweet.replace(imgFind,"");imgId=imgFind.substr(19);srcLink=imgFind;src="http://images.twitvid.com/"+imgId+".jpg";imgSource="twitvid.com <img src='images/video.png' class='video' alt='video'/>";}
var patt1=/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/g;array=tweet.match(patt1);if(array){$.each(array,function(i,val){tweet=tweet.replace(val,"<a href='"+val+"' target='_blank' rel='nofollow'>"+val+"</a>");});}
var tweet=tweet.replace(/(^|\s)@(\w+)/g,"$1@<a href='http://twitter.com/$2' target='_blank' rel='nofollow'>$2</a>");var user='<div class="details"></div>';var tweetOptions='<p class="options"><br />';tweetOptions=tweetOptions+'    <a href="http://twitter.com/'+val.from_user+'/status/'+val.id_str+'"><img src="images/original.png" alt="Original"/></a>';tweetOptions=tweetOptions+'    <a href="'+srcLink+'"><img src="images/full.png" alt="View full"/></a>';tweetOptions=tweetOptions+'    <a class="retweet" href="/retweet.php?status='+val.id_str+'"><img src="images/retweet.png" alt="Retweet"/></a>';tweetOptions=tweetOptions+'</p>';var imageInput='<div class="imageTweet"><a href="'+srcLink+'"><img src="'+src+'" alt="Related to '+query+'" /></a></div>';}
if(val.text.search(imageRegEx)!==-1){$("#results").append("<div class='entry-wrap'>"+imageInput+"<div class='entry'><div class='inner'><div class='profile'><div class='profileOptions'><img src='"+val.profile_image_url+"' alt='"+val.from_user+"' width='48' height='48' class='avatar' /></div><h2>"+val.from_user+"</h2></div> <div class='tweet'>"+tweet+"<p class='time'>"+relative_time(val.created_at)+" from "+html_entity_decode(val.source)+"</p></div>"+user+tweetOptions+"</div></div></div>");}});$("#search-icon").hide();$("#results").append("<div class='clear'></div>");var config={interval:300,over:inEntry,timeout:0,out:outEntry};$(".entry-wrap").hoverIntent(config)
function inEntry(){$(".entry",this).css({display:"block",zIndex:"3"});$(".imageTweet",this).css({zIndex:"4"});var pos=$(this).position();var width=$(this).outerWidth(true);var rightPos=(pos.left+width);var innerWidth=(window.innerWidth-300);if(rightPos>innerWidth){$(".entry",this).addClass("flip");}}
function outEntry(){$(this).removeClass("flip");$(".entry",this).css({display:"none"});$(".imageTweet",this).css({zIndex:"1"});}});}
var results=100;$(function(){$("#share a").hide();$("#share a").click(function(){$.iGrowl(1,"Sharing...");$.post("promo.php",{term:$("#search-box").val()},function(data){if(data=="SUCCESS"){$.iGrowl(2,"Thanks!");}else if(data=="LOGIN"){$.iGrowl(3,"Please login to twitter first");}else{$.iGrowl(3,"Already shared?");}});});$("#buysell").click(function(){_gaq.push(['_trackEvent','advert','clicked']);});$("#save").click(function(){_gaq.push(['_trackEvent','save','interaction']);});$("#services").click(function(){_gaq.push(['_trackEvent','services','interaction']);});$(".login-follow").click(function(){_gaq.push(['_trackEvent','login','follow']);});$(".just-login").click(function(){_gaq.push(['_trackEvent','login','no-follow']);});$('#tweet').keyup(function(){var val=$('#tweet').val();if(val.length>=122){var newval=val.substr(0,122);$('#tweet').val(newval)}
$("#count").html(val.length+18);$("#show-tweet").html("Example: "+val+" http://twipho.net");});$(".like").click(function(){$("#like").dialog({modal:true,width:400,height:350,buttons:{"Tweet!":function(){$.post('promo.php',{tweet:$("#tweet").val()},function(data){if(data=="SUCCESS"){alert("Thanks for tweeting");$("#like").dialog('close');}else{alert("There was a problem with Twitter, Epic Fail, Whale. Make sure you're logged in still.");$("#like").dialog('close');}});}}});});$(".retweet").live("click",function(){window.scrollTo(0,0);$.iGrowl(1,"Retweeting...");href=$(this).attr("href");$.get(href,function(data){if(data=="SUCCESS"){$.iGrowl(2,"All done!");}else if(data=="LOGIN"){$.iGrowl(3,"Please login to twitter first");}else{$.iGrowl(3,"We can't retweet");}});return false;});$("#search-icon").hide();if($("#search-items").length==0){$("#search-list").append("<ul id='search-items'></ul><a id='clear'>Clear Searches</a>");$("#clear").buttonset();if(supports_local_storage()){var searches=localStorage.getItem("searches");}
$("#search-items").html(searches);}
$("#clear").click(function(){if(supports_local_storage()){localStorage.clear();}
$("#search-items").html("");});if(!supports_local_storage()){$("#save").addClass("disabled");}else{$(".old-browser").hide();}
$("#number-results").slider({change:function(event,ui){results=ui.value;}});if($.cookie('sidebar')=="hidden"){$("#setting,#more-options").hide();}
$("#sidebar h1").click(function(){$("#setting,#more-options").toggle("slow",function(){if($("#more-options").is(":visible")==true){$.cookie('sidebar','shown');}
else{$.cookie('sidebar','hidden');}});});$("#autoload, #services, #rt, #save, #welcome-button, #login").buttonset();$("input[name='autoload']").change(function(){if($(this).val()=="off"){clearInterval(autorefresh);}else{var autorefresh=setInterval('runQuery(results, $("#search-box").val(), true)',parseInt($(this).val()));}});if($("input[name='autoload']").val()=="on"){}
$("#search-box").focus();var t;$("#search-button").click(function(){runQuery(results,$("#search-box").val());});$("#search-items li").click(function(){$("#search-box").val($(this).html())
runQuery(results,$(this).html());});$("#public-yes").click(function(){runQuery(results,"");});$("#public-no").click(function(){$("#search-box").focus();$(this).after("<h3 id='rude'>How rude. Maybe you should start your search using the box above then? Start typing!</h3>");});$("#search-box").keyup(function(event){clearTimeout(t);t=setTimeout("runQuery(results, '"+$("#search-box").val()+"'); window.location.hash = $('#search-box').val();",1000);if(event.keyCode==13){clearTimeout(t);}});$("#search-box").keypress(function(e){if(e.keyCode==13||e.keyCode==10){clearTimeout(t);runQuery(results,$("#search-box").val())
return false;}});if(window.location.hash){$("#search-box").val(decodeURIComponent(window.location.hash.substr(1).replace(/\+/g,'%20')));runQuery(results,window.location.hash.substr(1));}
else{if($.cookie('last-search')){$("#search-box").val($.cookie('last-search'));}}
$("#number-results").slider({value:100,min:10,max:100,step:10,slide:function(event,ui){$("#amount p").html('Results: '+ui.value);}});$("#amount p").html('Results: '+$("#number-results").slider("value"));$("#locate").click(function(){$.get('ip.php',function(data){ip=data;$.getJSON("http://api.twitter.com/1/geo/search.json?ip="+ip+"&callback=?",function(json){$.each(json.result.places,function(index,value){id=value.id;iplocation=value.name;country=value.country;});$("#geo-results").html("<p>We think you're in "+iplocation+", "+country+" is this correct?</p>");$("#geo-results").dialog({resizable:false,height:200,width:500,modal:true,buttons:{'Yes!':function(){runQuery(results,"place:"+id);$("#search-box").val("place:"+id);window.location.hash=$("#search-box").val();$(this).dialog('close');},'No, don\'t be stupid':function(){var locate=confirm("Would you like to enter your location yourself?");if(locate){$(this).dialog('close');$("#geo-user").dialog({resizable:false,height:300,width:500,modal:true,buttons:{'Locate!':function(){$.getJSON("http://api.twitter.com/1/geo/search.json?query="+escape($("#user-geo").val())+"&callback=?",function(json){$.each(json.result.places,function(index,value){id=value.id;iplocation=value.name;country=value.country;});var userlocate=confirm("Twitter says you're at '"+iplocation+", "+country+"' is that close enough?");if(userlocate){runQuery(results,"place:"+id);$("#search-box").val("place:"+id);window.location.hash=$("#search-box").val();$("#geo-user").dialog('close');}});},'Don\'t worry about it':function(){var locate=confirm("Would you like to enter your location yourself?");$(this).dialog('close');}}});}else{$(this).dialog('close');}}}});});});});});
