var tweetUsers = ['tavistockcup','IanJamesPoulter','TigerWoods','TrevorImmelman','JustinRose99','Graeme_mcdowell','Oliver_Wilson','JBHolmesGolf','Arjunatwal','RetiefGoosen555','henrikstenson','StuartAppleby','briandavispga','LeeJanzen','adamscottonline','GaryWoodland','Ross_Fisher','ChrisDiMarco','MaartenLafeber','aussieallenby','Arjunatwal'];
var buildString = "";

$(document).ready(function(){
	
	//clock
	cssClock('hours', 'minutes');
	
	//Twitter widget
	$('#twitter-ticker').slideDown('slow');
	
	for(var i=0;i<tweetUsers.length;i++)
	{
		if(i!=0) buildString+='+OR+';
		buildString+='from:'+tweetUsers[i];
	}
	
	var fileref = document.createElement('script');
	
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", "http://search.twitter.com/search.json?q="+buildString+"&callback=TweetTick&rpp=50");
	
	document.getElementsByTagName("head")[0].appendChild(fileref);
	
});

function TweetTick(ob)
{
	var container=$('#tweet-container');
	container.html('');
	
	$(ob.results).each(function(el){
	
		var str = '	<div class="tweet">\
					<div class="avatar"><a href="http://twitter.com/'+this.from_user+'" target="_blank"><img src="'+this.profile_image_url+'" alt="'+this.from_user+'" /></a></div>\
					<div class="user"><a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a></div>\
					<div class="time">'+relativeTime(this.created_at)+'</div>\
					<div class="txt">'+formatTwitString(this.text)+'</div>\
					</div>';
		
		container.append(str);
	
	});
	
	//container.jScrollPane();
}

function formatTwitString(str)
{
	str=' '+str;
	str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
	str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
	str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	return str;
}

function relativeTime(pastTime)
{	
	var origStamp = Date.parse(pastTime);
	var curDate = new Date();
	var currentStamp = curDate.getTime();
	
	var difference = parseInt((currentStamp - origStamp)/1000);

	if(difference < 0) return false;

	if(difference <= 5)				return "Just now";
	if(difference <= 20)			return "Seconds ago";
	if(difference <= 60)			return "A minute ago";
	if(difference < 3600)			return parseInt(difference/60)+" minutes ago";
	if(difference <= 1.5*3600) 		return "One hour ago";
	if(difference < 23.5*3600)		return Math.round(difference/3600)+" hours ago";
	if(difference < 1.5*24*3600)	return "One day ago";
	
	var dateArr = pastTime.split(' ');
	return dateArr[4].replace(/\:\d+$/,'')+' '+dateArr[2]+' '+dateArr[1]+(dateArr[3]!=curDate.getFullYear()?' '+dateArr[3]:'');
}

/*
$(document).ready(function() {
	cssClock('hours', 'minutes');
	 
	 $('.video a').fancybox({'autoDimensions': false,'width':800,'height':450,'scrolling' : 'no', 'content':'<!-- Begin VideoJS --><div class="video-js-box" id="video"><!-- Using the Video for Everybody Embed Code http://camendesign.com/code/video_for_everybody --><video class="video-js" width="800" height="450" controls preload><source src="http://www.tavistockcup.com/video/2010_Tavistock_Cup_Video.mp4" type="video/mp4; codecs="mp4a.40.2"" /><!-- Flash Fallback. Use any flash video player here. Make sure to keep the vjs-flash-fallback class. --><object class="vjs-flash-fallback" width="800" height="450" type="application/x-shockwave-flash" data="http://www.tavistockcup.com/video/flowplayer-3.2.5.swf"><param name="movie" value="http://www.tavistockcup.com/video/flowplayer-3.2.5.swf" /><param name="allowfullscreen" value="true" /><param name="flashvars" value=\'config={"playlist":["http://www.tavistockcup.com/images/video_placeholder.jpg", {"url": "http://www.tavistockcup.com/video/2010_Tavistock_Cup_Video.mp4","autoPlay":true,"autoBuffering":true}]}\' /><!-- Image Fallback. Typically the same as the poster image. --></object></video><!-- Download links provided for devices that cant play video in the browser. --></div><!-- End VideoJS -->','onComplete': function (){ $("#video").show(); VideoJS.setupAllWhenReady();} });

});
*/

