// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}


// Adapted from TwitSteam (http://iamnoahcooper.com/TwitStream/)
function getTwitterTime(created_at)
{
		var tTime=new Date(Date.parse(created_at));
		var cTime=new Date();
		var sinceMin=Math.round((cTime-tTime)/60000);
		if(sinceMin==0){
			var sinceSec=Math.round((cTime-tTime)/1000);
			if(sinceSec<10)
				var since='less than 10 seconds ago';
			else if(sinceSec<20)
				var since='less than 20 seconds ago';
			else
				var since='half a minute ago';
		}
		else if(sinceMin==1){
			var sinceSec=Math.round((cTime-tTime)/1000);
			if(sinceSec==30)
				var since='half a minute ago';
			else if(sinceSec<60)
				var since='less than a minute ago';
			else
				var since='1 minute ago';
		}
		else if(sinceMin<45)
			var since=sinceMin+' minutes ago';
		else if(sinceMin>44&&sinceMin<60)
			var since='about 1 hour ago';
		else if(sinceMin<1440){
			var sinceHr=Math.round(sinceMin/60);
			if(sinceHr==1)
				var since='about 1 hour ago';
			else
				var since='about '+sinceHr+' hours ago';
		}
		else if(sinceMin>1439&&sinceMin<2880)
			var since='1 day ago';
		else{
			var sinceDay=Math.round(sinceMin/1440);
			var since=sinceDay+' days ago';
		}

	return since;
}

function addLinks(text)
{
	
	return text.replace(/http:\/\/([A-Za-z0-9.\/]+)((\s)|($))/g, "<a href=\"http://$1\" target=\"_blank\">http://$1</a> ");
}

function addLinksToTweet (tweet)
{
	var replyWithPeriodReplacement = "7df7ehr";
	
	// replace hyperlinks
	tweet = tweet.replace(/http:\/\/([A-Za-z0-9.\/]+)((\s)|($))/g, "<a href=\"http://$1\" target=\"_blank\">http://$1</a> ");
	
	// replace replies (first ones ending with a period
	tweet = tweet.replace(/@([A-Za-z0-9_]+)(\.)/g, "<a href=\"http://www.twitter.com/$1\" target=\"_blank\">@$1</a>.");
	tweet = tweet.replace(/@([A-Za-z0-9_]+)(,)/g, "<a href=\"http://www.twitter.com/$1\" target=\"_blank\">@$1</a>,");
	tweet = tweet.replace(/@([A-Za-z0-9_]+)(\!)/g, "<a href=\"http://www.twitter.com/$1\" target=\"_blank\">@$1</a>!");
	tweet = tweet.replace(/@([A-Za-z0-9_]+)(\?)/g, "<a href=\"http://www.twitter.com/$1\" target=\"_blank\">@$1</a>?");
	tweet = tweet.replace(/@([A-Za-z0-9_]+)(\')/g, "<a href=\"http://www.twitter.com/$1\" target=\"_blank\">@$1</a>'");
	tweet = tweet.replace(/@([A-Za-z0-9_]+)((\s)|($))/g, "<a href=\"http://www.twitter.com/$1\" target=\"_blank\">@$1</a> ");
	tweet = tweet.replace(/#(\w+)((\s)|($))/g, "<a href=\"http://twitter.com/#!/search?q=#$1\" target=\"_blank\">#$1</a> ");
	
	return tweet;
		
}

  var _gaq = _gaq || [];
	  _gaq.push(['_setAccount', 'UA-8460445-25']);
	  _gaq.push(['_trackPageview']);
	
	  (function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	  })();



