/**
 * @author owen@jbanetwork.com
 */
(function($) {
	$.fn.notify = function(o) {
		o = jQuery.extend({auto : false, interval : 120000}, o);
		var msg = new Object();
		getUnread = function(){
			$.post('ajaxProcess.php', {module: 'User',method: 'ajaxGetUnreadCount'}, function(data){
				var count = data > 0 ? parseInt(data) : 0;
				$(el).find('.msg').remove();
				switch (count) {
					case 0:
						break;
					case 1:
						$(el).append(' <a class="msg" href="my_home.php?mod=messages" title="view your message"><span>' + count + ' unread message<\/span><\/a> ');
						break;
					default:
						$(el).append(' <a class="msg" href="my_home.php?mod=messages" title="view your messages"><span>' + count + ' unread messages<\/span><\/a> ');
						break;
				}
			});
		};
		getFriendRequests = function(){
			$.post('ajaxProcess.php', {module: 'User', method: 'ajaxGetFriendRequests', count: true}, function(data){
				var count = data > 0 ? parseInt(data) : 0;
				$(el).find('.fRequest').remove();
				switch (count) {
					case 0:
						break;
					case 1:
						$(el).append(' <a class="fRequest" href="my_home.php?mod=notifications" title="view your friend request"><span>' + count + ' new friend request<\/span><\/a> ');
						break;
					default:
						$(el).append(' <a class="fRequest" href="my_home.php?mod=notifications" title="view your friend requests"><span>' + count + ' new friend requests<\/span><\/a> ');
						break;
				}
			});
		};

		return this.each(function(){
			el = $(this);
    		function fetch(){
				getUnread();
				getFriendRequests();
			}
/**
 *  We have an issue with people being redirected to ajaxProcess for this call during login
 *  I've added a delay to the initial call to "fetch" in the hopes that it will prevent that
 */

			if (o.auto) {
				setInterval(fetch, o.interval);
			}
			setTimeout(fetch, 5000);
		});
	}
})(jQuery);

$(document).ready(function(){
	$('#notify').notify({auto: true});
});
