$(document).ready(function() {
	var first = 0;
	var pause = 4000;
	var firstID = 0;

	var interval = 0;
	
	// initialise the slidemenu
	slideMenu.build('sm',200,10,10,1);
	
	$.ajaxSetup({ cache: false, timeout: 8000 }); // TODO: check whether this is a good idea. 
	$(function() { $(".lavaLamp").lavaLamp({ fx: "backout", speed: 700 })});
	lavaMove($("li.selectedCategory").index()); // moves lava thingy to selected category name, see getHeaderCategories in the categories class.
	updateLiveFeed(); // kick-start the live-feed. 
	
	// mood selection
	$(".moodsel").change(function () {
	  var src = 'images/moods/';
	  $("select option:selected").each(function () {
			src += $(this).val();
			$('#moodimg').attr('src', src); // change the image
			$.get('jsReq.php?mood=' + $(this).text()); // update in DB
		  });
	});

	// moves lava thingy to selected element no. 
	function lavaMove(el) {
		if (!el && el !== 0) 
			return false;
		else if (/^[0-9]+$/.test(el)) 
			el = $($("ul#lavaLamp > li").get(el));
		 el.trigger('mouseenter');
		 el.trigger('click');
		 return true;
	}
	/* grap updated items */
	function updateLiveFeed() {
		$.ajaxSetup({ cache: true }); // getJSON sets the cache to false. so we explicitly set it to true here.
		if(interval > 0)
			clearInterval(interval); // kill the timer do we don't have req's queuing up if server is slow.
		$.getJSON('feed.php?firstID='+firstID, parseInfo); 
		interval = setInterval(updateLiveFeed, pause); // restart the timer after req finsihes.
	}
	
	function parseInfo(data) {
		var i=0;
		if(data.firstID != firstID) { // if newest commentID has changed, update the content
			$('#livefeed').html(data.html);
			firstID = data.firstID;
		}
		var iShow = data.iShow; // number of elements to show
		var iTotal = data.iTotal; // total number of elements
		first = $('ul#livefeed li').eq(0).html(); // store first element
		for (i=0; i < iTotal; i++) {
			$('ul#livefeed li').eq(i).html($('ul#livefeed li').eq(i+1).html());
		}
		$('ul#livefeed li').eq(iTotal-1).html(first); // set last element to first.		
		$('ul#livefeed li').slice(iShow).hide();
		$(".unreadPmCount").html("Inbox (" + data.unreadPmCount + ")");
		$(".followNotifyCount").html("Following posts(" + data.followNotifyCount + ")");
		$(".newMentionsCount").html("Mentions (" + data.newMentionsCount + ")");
		$(".threadUpdateCount").html("Subscribed (" + data.threadUpdateCount + ")");

		var notifyTxt = "*new*";
		if(data.unreadPmCount > 0)
		{
			$(".unreadPmCount").addClass("newly-notified");
			$(".unreadPmCount").append("<span class=\"new-notifier\">" + notifyTxt + "</span>");
		}
		
		if(data.followNotifyCount > 0)
		{
			$(".followNotifyCount").addClass("newly-notified");
			$(".followNotifyCount").append("<span class=\"new-notifier\">" + notifyTxt + "</span>");
		}
		
		if(data.newMentionsCount > 0)
		{
			$(".newMentionsCount").addClass("newly-notified");
			$(".newMentionsCount").append("<span class=\"new-notifier\">" + notifyTxt + "</span>");
		}
		
		if(data.threadUpdateCount > 0)
		{
			$(".threadUpdateCount").addClass("newly-notified");
			$(".threadUpdateCount").append("<span class=\"new-notifier\">" + notifyTxt + "</span>");
		}
	}
});
