function chatScroll()
{
	var scrollDiv = $('chat-container');
	var currentHeight = 0;
	
	if (scrollDiv.scrollHeight > 0)
	{
		currentHeight = scrollDiv.scrollHeight;
	}
	else if (scrollDiv.offsetHeight > 0)
	{
		currentHeight = scrollDiv.offsetHeight;
	}

	scrollDiv.scrollTop = currentHeight;
	
	scrollDiv = null;
}


Event.observe(window, 'load', function() 
{
	if ($('chat-link'))
	{
		if ($('chat_submit'))
		{
			$('chat_submit').observe('submit', function(event)
			{
				if ($F('chat'))
				{
					$('chat').disable();
					$('send').disable();
					$('send').value = 'Sending...';
					
					new Ajax.Request($('chat_submit').action, 
					{
						method: 'post',
						parameters: 'chat='+encodeURIComponent($F('chat')),
						onSuccess: function(transport) 
						{
							var returnMsg = transport.responseText;
							
							if (returnMsg === 'ok')
							{
								$('chat').value = '';
								
								$('send').value = 'Send';
								
								$('chat').enable();
								$('send').enable();
							}
							
							if (returnMsg === 'throttled')
							{
								$('chat').value = 'To much chat sent... please wait 20 seconds.';
								
								$('send').value = 'Please wait...';
													
								new PeriodicalExecuter(function(pe) 
								{
									$('chat').value = '';
									
									$('send').value = 'Send';
									
									$('chat').enable();
									$('send').enable();
								
									$('chat').focus();
									
									pe.stop();
								}, 20);
							}
							
							update(3);
							$('chat').focus();
						}
					});
				}
				
				Event.stop(event);
			});
		}
		
		// Attach this here too
		$('chat-link').observe('click', function(event)
		{
			if ($('chat-holder').visible())
			{
				// Hide the chat pane, and stop all updates.
				updateLoop.stop();
			}
			else
			{
				// Show the chat pane, and begin updates.
				update(3);
			}
			
			Effect.toggle('chat-holder', 'blind', {duration:.5});	
			
			Event.stop(event);
		});
		
	}
});

var updateLoop;
		
function update(time)
{
	if (updateLoop) { updateLoop.stop(); }
	
	time = time * 1.35;
	
	if (time > 10)
	{
		time = 10;
	}
	
	var chatTemplate = new Template('<div id="#{id}" class="chat-item"><div class="chat-user"><a href="http://mpora.com/#{username}/" target="_blank" class="hover_underline">#{username}</a> (#{date})</div><div class="chat-text">#{text}</div></div>');
	
	var lastId = 0;
	if ($('chat-container').childElements().last())
	{
		var lastId =  $('chat-container').childElements().last().id;
	}
	
	// Get the chat.
	new Ajax.Request('/ajax/get_chat/'+filmID+'/'+lastId+'/'+Math.floor(Math.random()*681984)+'/', 
	{
		method: 'get',
		onSuccess: function(transport) 
		{
			json = transport.responseText.evalJSON();
			
			if (json.length > 0)
			{
				time = 3;

				json.each(function(j)
				{
					$('chat-container').insert(chatTemplate.evaluate(j));
					chatScroll();
				});
			}
			
			updateLoop = new PeriodicalExecuter(function(pe) 
			{
				update(time);
				pe.stop();
				
			}, time);
		}
	});	
}

/*Event.observe(window, 'load', function() 
{
	update(3);
});*/



