/* Author: Lee Boyce */
// Home images 
$(document).ready(function() {
	$('#home-cycle').cycle({ 
	    fx:    'fade', 
	    timeout: 4500, 
	    speed:  800,
	    before: onBefore,
	    after: onAfter
	 });
	function onBefore() { $('#home-caption').html(this.alt); } 
	function onAfter() { $('#home-caption').html(this.alt); }
});

// Twitter Feed
	// When the document is loaded (jQuery function)
		$(document).ready(function() {
			// Call the Twitter API to retrieve the last 10 tweets in JSON format for the pcpro Twitter account
			$.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=monikerprojects&count=25&callback=?", function(tweetdata) {
				// Grab a reference to the ul element which will display the tweets
				var tl = $("#the-feed");
				// For each item returned in tweetdata
				$.each(tweetdata, function(i,tweet) {
					// Append the info in li tags to the ul, converting any links to HTML <a href=.. code and convert the tweeted date
					// to a more readable Twitter format
					tl.append("<div class='feed-item'>" + urlToLink(tweet.text) + "<br><font style='font-style:italic; font-size:10px;'>" + relTime(tweet.created_at) + "</font></div>");
				});
			});	
	});
		// Converts any links in text to their HTML <a href=""> equivalent
		function urlToLink(text) {
		  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
		  return text.replace(exp,"<a href='$1' target='_blank'>$1</a>"); 
		}
		// Takes a time value and converts it to "from now" and then returns a relevant text interpretation of it
		function relTime(time_value) {
			time_value = time_value.replace(/(\+[0-9]{4}\s)/ig,"");
			var parsed_date = Date.parse(time_value);
			var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
			var timeago = parseInt((relative_to.getTime() - parsed_date) / 1000);			
			if (timeago < 60) return 'less than a minute ago';
			else if(timeago < 120) return 'about a minute ago';
			else if(timeago < (45*60)) return (parseInt(timeago / 60)).toString() + ' minutes ago';
			else if(timeago < (90*60)) return 'about an hour ago';
			else if(timeago < (24*60*60)) return 'about ' + (parseInt(timeago / 3600)).toString() + ' hours ago';
			else if(timeago < (48*60*60)) return '1 day ago';
			else return (parseInt(timeago / 86400)).toString() + ' days ago';
		}	
	
// Scroller
$(document).ready(function() {
	$('.scroll-down').click(function() { $('.scroll-pane-container').scrollTo( {top:'+=91px', left:'+=0px'}, 800 ); });
	$('.scroll-up').click(function() { $('.scroll-pane-container').scrollTo( {top:'-=91px', left:'+=0px'}, 800 ); });
	$('.scroll-down-news').click(function() { $('#news-container').scrollTo( {top:'+=130px', left:'+=0px'}, 900 ); });
	$('.scroll-up-news').click(function() { $('#news-container').scrollTo( {top:'-=130px', left:'+=0px'}, 900 ); });
	
	
	$('#prog1').click(function() { $('.programme').removeClass('active'); $(this).addClass('active'); $('.programme-details').fadeOut(0); $('#prog1-d').fadeIn(300); });
	$('#prog2').click(function() { $('.programme').removeClass('active'); $(this).addClass('active');  $('.programme-details').fadeOut(0); $('#prog2-d').fadeIn(300); });
	$('#prog3').click(function() { $('.programme').removeClass('active'); $(this).addClass('active');  $('.programme-details').fadeOut(0); $('#prog3-d').fadeIn(300); });
	$('#prog4').click(function() { $('.programme').removeClass('active'); $(this).addClass('active');  $('.programme-details').fadeOut(0); $('#prog4-d').fadeIn(300); });
	
});
