/* DEFINE NAMESPACE */

if (!CHANGO) var CHANGO = {};


/* IMAGE ROTATOR */

CHANGO.ROTATOR = function() {
	
	var config = {};
	var cid;
	var rotate_interval;
	var paused;
	
	return {
		
		init: function(o) {
			
			config = o;
			cid = config.containerId;
			
			var numItems = $(cid+" .rotator-items > div").size();
			var me = this;
			
			if (numItems > 1) {
			
				//Set window width
				$(cid+" .rotator-items").width($(cid+" .rotator-window").width() * numItems);
			
				//On image hover
				$(cid+" .rotator-items > div").hover(
					function () {
						paused = true;
						clearTimeout(rotate_interval);
					},
					function () {
						paused = false;
						me.rotateStart();
					}
				);
				
				$(cid+" .rotator-pages > a").hover(
					function () {
						paused = true;
						clearTimeout(rotate_interval);
					},
					function () {
						paused = false;
						me.rotateStart();
					}
				);
				
				//On page click
				var me = this;
				$(cid+" .rotator-pages > a").click(function(e) {
					clearTimeout(rotate_interval);
					paused = true;
					me.rotate($(this));
					e.preventDefault();
				});
				
				// Activate first page link & start rotation
				$(cid+" .rotator-pages > a:first").addClass("active");
				this.rotateStart();
				
			} else {
				
				$(cid+" .rotator-pages").css("display","none");
				
			}
			
		},
		
		//Paging and Slider Function
		rotate: function(active_page_btn) {
			var imageId = active_page_btn.attr("rel") - 1;
			var image_reelPosition = imageId * ($(cid+" .rotator-window").width());
			var me = this;

			$(cid+" .rotator-pages a.active").removeClass('active');
			active_page_btn.addClass('active');
			clearTimeout(rotate_interval);
			$(cid+" .rotator-items").animate({ left: -image_reelPosition}, 200, function() {
				if (!paused) rotate_interval = setTimeout(function() {me.triggerRotate();}, config.delay*1000);
			});
		},
		
		//Rotation and Timer
		rotateStart: function() {
			var me = this;
			rotate_interval = setTimeout(function() {me.triggerRotate();}, config.delay*1000);
		},
		
		triggerRotate: function() {
			var active_page_btn = $(cid+' .rotator-pages a.active').next();
			if (active_page_btn.length == 0) {
				active_page_btn = $(cid+' .rotator-pages a:first');
			}
			this.rotate(active_page_btn);
		}
	}
}


/* TWITTER */

CHANGO.TWEETS = function() {

	var config = {};

	return {
		
		init: function(cfg) {
			config = cfg;
			var me = this;
			$.ajax({
				url: config.src,
				dataType: "json",
				success: function(o) { me.twitterDataHandler(o) }
			});
		},
		
		twitterDataHandler: function(o) {
			o = o.results;
			var tweetData = [];
			var totalTweets = o.length;
			if (config.numTweets > totalTweets) config.numTweets = totalTweets;
			for (var i=0; i<config.numTweets; i++) {
				var tweet = o[i];
				tweetData.push({text:tweet.text, age:this.parseAge(tweet.created_at)});
			}
			this.output(tweetData);
		},
		
		parseAge: function(datetime_string) {
			var datetime_tweet = new Date(datetime_string);
			var datetime_now = new Date(new Date().toUTCString());
			var time_elapsed = datetime_now.getTime() - datetime_tweet.getTime();
			
			var d = Math.floor(time_elapsed / (1000 * 60 * 60 * 24));
			var h = Math.floor(time_elapsed / (1000 * 60 * 60));
			var m = Math.floor(time_elapsed / (1000 * 60));
			
			var age_text = '';
			
			if (m < 2) {
				age_text = "just now";
			} else if (m < 50) {
				age_text = m + " minutes ago";
			} else if (m < 100) {
				age_text = "about an hour ago";
			} else if (h < 20) {
				age_text = h + " hours ago";
			} else if (h < 36 ) {
				age_text = "about a day ago";
			} else if (h >= 36 || d > 1) {
				age_text = d + " days ago";
			}
			
			return age_text;
		},
		
		output: function(tweetData) {
			var tweetHtml = "<ul>";
			for (var i=0; i<tweetData.length; i++) {
				var tweet = tweetData[i];
				tweetHtml+= "<li>" + tweet.text + " <span>" + tweet.age + "</span></li>";
			}
			tweetHtml+= "</ul>";
			// activate links
			var linkRegex = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
			tweetHtml = tweetHtml.replace(linkRegex, ' <a target="_blank" href="$1">$1</a> ');
			var userRegex = /[\@]+([A-Za-z0-9-_]+)/gi;
			tweetHtml = tweetHtml.replace(userRegex, ' <a target="_blank" href="http://twitter.com/$1">@$1</a> ');
			var hashRegex = /(?:^| )[\#]+([A-Za-z0-9-_]+)/gi;
			tweetHtml = tweetHtml.replace(hashRegex, ' <a target="_blank" href="http://twitter.com/#!/search?q=%23$1">#$1</a> ');
			// output to page
			$(config.containerId).html(tweetHtml);
		}
	}
}

