var chat = ( function ( ) {

	var appendData = ( function ( ) {
		return function ( data, silent ) {
			if ( !data || !data.entries || !data.entries.length ) return false;
			var appended = false;
			for ( var i = 0; i < data.entries.length; i ++ ) {
				appended = chat.addEntry ( data.entries[i], null, silent, !silent ) || appended;
			}
			return appended;
		}
	} )();

	var automate = ( function ( ) {
		var getSuspense = ( function ( ) {
			var suspense = 0;
			var fn = function ( ) {
				return parseInt ( 30 * ( 1 - Math.log ( 10 - Math.min ( 9, suspense ++ ), 10 ) ) );
			}
			fn.reset = function ( ) {
				suspense = 0;
			}
			fn.current = function ( ) {
				return suspense;
			}
			return fn;
		} )();

		var loadData = function ( next ) {
			var request = next + ( next.indexOf("?") == -1 ? "?" : "&" ) + "callback=?";
			$.getJSON ( request, function ( data ) {
				if ( appendData ( data ) ) getSuspense.reset();
				fn ( data.next )
			} );
		}

		var __timer__;
		var __next__;
		
		var fn = function ( next ) {
			if ( next == null ) return false;
			__next__ = next;
			__timer__ = setTimeout ( function ( ) { loadData ( next ); }, ( getSuspense() * 1000 ) + 1 );
		}
		fn.reset = function ( ) {
			getSuspense.reset();
			clearTimeout ( __timer__ );
			fn ( __next__ );
		}
		return fn;
	} )();

	return {

		addEntry : function ( entry, author, silent, scroll ) {
			var id = entry.cid || entry.id;
			if ( $('#e_' + id).length ) return false;

			if ( !author )
				author = { name : entry.author }
			if ( window.pid && ( window.pid == entry.pid ) )
				author.self = true;

			if ( scroll == null )
				scroll = ( $('#middle').height() + $('#middle').scrollTop() >= $('#body').height() - 30 );
			else
				scroll = !!scroll;

			var ts = new Date ( parseInt ( entry.created ) );
			var className = [];
			if ( entry.comment ) className.push ( "comment" );
			else if ( author.self ) className.push ( "self" );
			var author = author.name; //( entry.comment ? '<em>' + author.name + ':</em>' : '<strong>' + author.name + ':</strong>' );
			$('#conversation')
				.append ( '<dt id="a_' + id + '"' + ( className.length ? ' class="' + className.join(" ") + '"' : '' ) + '>' + author + ' <span>at <em>' + ts.toString('%g:%i %a') + '</em></span></dt>' )
				.append ( '<dd id="e_' + id + '"' + ( className.length ? ' class="' + className.join(" ") + '"' : '' ) + '>' + entry.textContent + '</dd>' );
			fixFloatingBars();
			var n = $('#a_' + id + ', #e_' + id);
			//n.find('strong').css('fontWeight','normal');
			//setTimeout ( function ( ) { n.find('strong').css('fontWeight','bold'); }, 100 );
			if ( !silent ) {
				n.effect("pulsate",{times:1},500,function(){
					$(this).css('filter','');
				});
			}

			if ( scroll ) {
				goToBottom();
			} else if ( !silent ) {
				var top = $(n[1]).offset().top;
				var bottom = top + $(n[1]).height();
				var height = window.innerHeight || document.documentElement.clientHeight;
				if ( ( top + 25 > height ) || ( bottom < $(document).scrollTop() ) ) {
					$('#newContent').remove();
					var timeout = setTimeout ( function ( ) {
						$("#newContent").fadeOut();
					}, 5000 );
					var x = $('<div id="newContent">New Message! <strong>&darr;</strong></div>').click ( function ( ) {
						clearTimeout ( timeout );
						$('#newContent').fadeOut();
						goToBottom();
					} );
					$('#bottom').append ( x );
					x.effect("pulsate",{times:1},500);
				}
			}

			return true;
		},

		reset : function ( ) {
			automate.reset();
		},

		init : function ( data ) {
			if ( arguments.callee.called ) return;
			arguments.callee.called = true;
			$(document).ready ( function ( ) {
				appendData ( data, true );
				automate(data.next);
				//goToBottom();
			} );
		}

	}
} )();
