// define the dirty globals
var refreshTimer = null;
var items = [];
var timer = null;
var updateTimer = null;
var updateInterval = 30000;
var limit = 3;
var since = 0;

$(function() {

	var addItem = function(data, flip) {
		var icon = null;
		var item = null;
		var controls = null;
		var replyUrl = false;

		switch (data.type) {
			case 'tweet':
				icon = '/images/icon_sm-twitter.png';
				replyUrl = 'http://twitter.com/intent/tweet?in_reply_to=' + data.id;
				break;
			case 'facebook':
				icon = '/images/icon_sm-facebook.png';
				replyUrl = false;
				break;
		}

		item = $('<div />', {
			'class' : 'social-media-feed-item'
		}).css({
			'background-image' : "url('" + icon + "')",
			'display' : 'none'
		});

		if (data.type == 'tweet') {

			item.append($('<span />', {
				'class' : 'feed-item-from',
				html : '<strong><a href="http://twitter.com/' + data.from + '">' + data.from + '<a/></strong> '
			}));

		} else {

			item.append($('<span />', {
				'class' : 'feed-item-from',
				html : '<strong>' + data.from + '</strong> '
			}));

		}

		item.append($('<span />', {
			'class' : 'feed-item-content',
			html : data.html + '&nbsp;'
		}));

		controls = $('<div />', {
			'class' : 'social-media-feed-item-controls'
		});

		controls.append($('<abbr />', {
			'class' : 'feed-item-sent timeago',
			'title' : data.sent_format1,
			html : data.sent_format2
		}));

		if (replyUrl) {

			controls.append($('<span />', {
				'html' : '&nbsp;&middot;&nbsp;'
			}));

			controls.append($('<a />', {
				'class' : 'feed-item-reply',
				'href' : replyUrl,
				'html' : 'reply',
				'rel' : 'external',
				'target' : '_blank'
			}));

		}

		item.append(controls);

		items.push(item);

		if (data.sent > since)
			since = data.sent;

		if (timer == null) {
			timer = setInterval(function() {
				addItemToPage(flip);
			}, 500);
		}

	};

	var addItemToPage = function(flip) {

		if (items.length > 0) {

			// stop updating
			clearInterval(updateTimer);

			if (flip) {
				var item = items.pop();
				$('#social-media-feed h3 div.scrollBar div#scrollableContent').append(item);
			} else {
				var item = items.shift();
				$('#social-media-feed div.scrollBar div#scrollableContent').append(item);
			}

			if ($('#social-media-feed div.scrollBar div#scrollableContent > div').length > limit) {
				$('#social-media-feed div.scrollBar div#scrollableContent > div:last').remove();
			}

			// initiate timeago script
			$("abbr.timeago").timeago();

			item.fadeIn('fast');

		} else {

			clearInterval(timer);
			timer = null;

			// set up an interval for updating
			updateTimer = setInterval(function() {
				getUpdate();
			}, updateInterval);

		}

		$('.scroll-pane').jScrollPane();
	};

	var getUpdate = function() {
		$.ajax({
			url : '/helpers/updateSocialFeed.php',
			data : {
				'limit' : 0,
				'since' : since
			},
			success : function(data) {
				for (i in data) {
					addItem(data[i], true);
				}
			},
			dataType : 'json'
		});
	};

	// get the initial data, when everything else is loaded
	$(window).load(function() {
		
		$.ajax({
			url : '/helpers/updateSocialFeed.php',
			data : {
				'limit' : limit,
				'since' : false
			},
			success : function(data) {
				
				$('#social-media-feed div.scrollBar div.progressBar').remove();
				$('#social-media-feed div.scrollBar div#scrollableContent > div').remove();

				for (i in data) {
					addItem(data[i], false);
				}
			},
			dataType : 'json'
		});
	});

});
