
var disable_chat_log_scroll = false;
getData = function()
{
	var url = "transaction/ajaxGetMessages/" + selected_offer_id + ".html";
	var options = {url : url, success: parseView };
	$.ajax( options );
}

//$(document).ajaxComplete(function(e, x) {
//    alert(x.getResponseHeader("Content-Type"));
//});


parseView = function( html )
{
	//alert( html );
	var scrolling = false;
	if( objDiv.scrollTop == objDiv.scrollHeight - objDiv.clientHeight ) scrolling = true;
//	var a = "";

	
//	$("message", html ).each(function()
//	{
//		pattern = $("#chat_pattern").html();
//		pattern = pattern.replace( "__body__", $( "content", this ).text() );
//		pattern = pattern.replace( "__time__", $( "time", this ).text() );
//		pattern = pattern.replace( "__date__", $( "date", this ).text() );
//		pattern = pattern.replace( "__login__", $( "login", this ).text() );
//		pattern = pattern.replace( "__user_type__", "chat_" + $( "type", this ).text() );
//		a = a + pattern;
//	});


	$("#chat_log").html( html );
	if( scrolling ) objDiv.scrollTop = objDiv.scrollHeight;

}



$(document).ready(function()
{
	if( objDiv = document.getElementById("chat_log") )
	{
		objDiv.scrollTop = objDiv.scrollHeight;
		setInterval( getData, 2500 );
	}

	$("form#send_message").submit(function()
	{
		$(".message_body").css( "height", "14px" );
		$(this).ajaxSubmit( { success: function() { getData(); $(".message_body" ).attr( "value", "" ); } });
		return false;
	});

	//alert( objDiv.clientHeight );
	//$("#chat_log").scroll(function() { disable_chat_log_scroll = true });



//	$(".message_body").keydown( function( e )
//	{
//		regexp = /[^\s]{25}$/;
//		console.log( $(this).val() );
//		if( $(this).val().match( regexp ) )
//		{
//			//$(this).val( $(this).val().substr( 0, $(this).val().length - 1) );
//		}
//	});


	$(".message_body").keyup( function( e )
	{
		//console.log( this.scrollHeight - parseInt($(this).css("height")) );
		if( parseInt( $(this).css( "height" ) ) + 10 < this.scrollHeight )
			$(this).css( "height", this.scrollHeight );

		code = e.keyCode;
		if( code == 13 )
		{
			$(this).parent("form").submit();
		}
	})



});
