﻿/*!
* twitter.recent.js
* ===========================================
* Copyright (c) 2011 Bauer Consumer Media Ltd
* -------------------------------------------
*/

var recentTweets = {
	cons: {
		twitterUrl: 'http://twitter.com/'
	},
	settings: null,

	init: function (settings) {
		recentTweets.settings = settings;
		recentTweets.loadData();
	},

	displayTweets: function (tweets) {
		var i, s = '';

		for (i = 0; i < tweets.results.length; i++) {
			var j, k, t = tweets.results[i], link;

			k = 0;
			while (k > -1 && (j = t.text.indexOf('http://', k)) > -1) {
				k = t.text.indexOf(' ', j);
				var url = t.text.substring(j, k > -1 ? k : undefined);
				if (url !== 'http://') {
					link = '<a href="' + url + '" target="_blank" class="link">' + url + '</a>';
					t.text = t.text.replace(url, link);
					if (k > -1) {
						k += (link.length - url.length);
					}
				}
			}

			if (recentTweets.settings.linkOtherProfiles) {
				k = 0;
				while (k > -1 && (j = t.text.indexOf('@', k)) > -1) {
					k = t.text.indexOf(' ', j);
					var user = t.text.substring(j, k > -1 ? k : undefined);
					if (user !== '@') {
						link = '<a href="' + recentTweets.cons.twitterUrl + user + '" target="_blank">' + user + '</a>';
						t.text = t.text.replace(user, link);
						if (k > -1) {
							k += (link.length - user.length);
						}
					}
				}
			}

			var time = '';
			var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
			var now = new Date();
			t.date = new Date(t.created_at);
			var diff = (now.getTime() - t.date.getTime()) / 1000 / 60; // minutes

			if (diff < 60) {
				j = parseInt(diff, 10);
				time = j + ' minute' + (j > 1 ? 's' : '') + ' ago';
			} else if ((diff / 60) < 24) {
				j = parseInt(diff / 60, 10);
				time = j + ' hour' + (j > 1 ? 's' : '') + ' ago';
			} else {
				time = t.date.getDate() + ' ' + months[t.date.getMonth()];
			}

			s += '<div class="tweet">';
			s += '	<div class="profile">';
			s += '		<a href="' + recentTweets.cons.twitterUrl + t.from_user + '" target="_blank">';
			s += '			<img src="' + t.profile_image_url + '" width="48" height="48" alt="@' + t.from_user + '" /></a>';
			s += '	</div>';
			s += '	<div class="post">';
			s += '		<a href="' + recentTweets.cons.twitterUrl + t.from_user + '" target="_blank" class="user">@' + t.from_user + '</a><br />';
			s += '		<div class="text">' + t.text + '</div>';
			s += '		<div class="timestamp">' + time + '</div>';
			s += '	</div>';
			s += '	<div class="clear"><!----></div>';
			s += '</div>';
		}

		var pnl = $('#recentTweets');
		pnl.find('.error').hide();
		pnl.find('.loading').hide();
		pnl.find('.feed').html(s).show();

		pnl.find('.tweet').hover(function () {
			$(this).addClass('tweet-hover');
		}, function () {
			$(this).removeClass('tweet-hover');
		}).click(function (e) {
			if (e.target.tagName === 'A') { return; }
			if ($(this).find('a.link').length > 0) {
				window.open($(this).find('a.link').attr('href'));
			} else {
				window.open($(this).find('a.user').attr('href'));
			}
		});
	},

	loadData: function () {
		$.ajax({
			type: 'GET',
			url: recentTweets.settings.queryUrl,
			timeout: 10000,
			dataType: 'jsonp',
			success: function (data) {
				recentTweets.displayTweets(data);
				if (recentTweets.settings.onLoad && typeof recentTweets.settings.onLoad === 'function') {
					recentTweets.settings.onLoad();
				}
			},
			error: function () {
				var pnl = $('#recentTweets');
				if (pnl.find('.loading').is(':visible')) {
					pnl.find('.loading').hide();
					pnl.find('.error').show();
				}
			}
		});

		var ms = (recentTweets.settings.refreshMins * 60 * 1000);
		window.setTimeout(recentTweets.loadData, ms);
	}
};
