function PmMessagingCl () {	  	
	  	this.pm_last_msg_id = false;
		this.pm_window_opend = false;
		this.pm_debug = true;  
	}
	PmMessagingObj = new PmMessagingCl();
var isShift = false;
/* debug funcs */
function DebugCl()
{
	
	this.updLastmsgid = function ()
	{
		if ( $('#pm_debug_last_msg_id').html()!= PmMessagingObj.pm_last_msg_id ) $('#pm_debug_last_msg_id').html(PmMessagingObj.pm_last_msg_id);
		if ( $('#pm_debug_msg_input').html()!= $('#pm_message').val() ) $('#pm_debug_msg_input').html($('#pm_message').val());
		if ( isShift ) $('#pm_debug_shifton').html('on'); else $('#pm_debug_shifton').html('off'); ;
		//alert('ok');
	}
}
DebugObj = new DebugCl();



$(document).ready(function() {
 if ( PmMessagingObj.pm_debug == true )
 {
 	//wenn debug
 	$('#pm_debug').css( 'display' , 'block');
 	//update debug_pm_last_msg_id
 	var updatepm_last_msg_idDEBUG = setInterval( DebugObj.updLastmsgid , 1000);
 	
 } 
 else 
 {	
 	$('#pm_debug').css( 'display' , 'none');
 }
  
} );


/* datenbank nach neuen messages nachfragen */
 var updatePmConversationRefreshId = setInterval( updatePmConversation, 5000);

// clearInterval(updatePmConversationRefreshId);

function updatePmConversation()
{
	// whenn window nicht geoefnet oder gibts keine unterhaltung  = kein update
	if (!PmMessagingObj.pm_window_opend  || $('#pm_to_user_id').length == 0 ) return;
	
	jQuery.ajax({
		// data: 'user_id='+user_id,
		url: site_url+controller.pm+'/update_pm_conversation',
		data: 'to_user_id='+$('#pm_to_user_id').val()+'&pm_last_msg_id='+PmMessagingObj.pm_last_msg_id,
		type: "POST",
		timeout: 5000,
		error: function(){ console.log("25:Fehler"); },
		success: function(ret){
				var responseJSON = $.evalJSON(ret);	
				//$(document.body).append('<div id="messaging_window" style="margin-top:'+topx+'px">'+ret+'</div>');
				// spaeter umschreiben , dass nur neue messages geladen wird
				if (responseJSON.last_msgs!='') 
				{
					$('#pm_content_message_output').append(responseJSON.last_msgs); 
					$( '#pm_content_message_output').attr({ scrollTop: $('#pm_content_message_output').attr("scrollHeight")  });
					PmMessagingObj.pm_last_msg_id = responseJSON.pm_last_msg_id;
				}
				
				//messaging_window_open = true;
			}
	});	
}
function messaging_window( to_user_id )
{
	topx = parseInt(((document.all)?document.body.scrollTop:window.pageYOffset))+20;
	if ($('#messaging_window').length!=0) 
	{
		$('#messaging_window').css('margin-top', topx+20+'px');
		$('#messaging_window').css('display','block');
		
		return;	
	} 
	jQuery.ajax({
		// data: 'user_id='+user_id,
		url: site_url+controller.pm+'/show_pm_window',
		data: 'to_user_id='+to_user_id,
		type: "POST",
		timeout: 5000,
		error: function(){ console.log("25:Fehler"); },
		success: function(ret){
				var responseJSON = $.evalJSON(ret);	
				$(document.body).append('<div id="messaging_window" style="margin-top:'+topx+'px">'+responseJSON.pm_content_message_output+'</div>'); 
				$( '#pm_content_message_output').attr({ scrollTop: $('#pm_content_message_output').attr("scrollHeight")  });
				PmMessagingObj.pm_window_opend = true;
				PmMessagingObj.pm_last_msg_id = responseJSON.pm_last_msg_id;
				
				isShift = false; 
				$('#pm_message').keyup ( function(e){ if(e.which == 16 ) isShift=false;  } );
				
				$('#pm_message').keydown (function(e) {
					//alert(e.which);					
					if (e.which == 16) isShift=true;
					if (e.which ==13 && isShift == true)
				  	{
				  		//$('#pm_message').val($('#pm_message').val()+'\n');				  		
				  		//alert('mit shift');
				  	}
					else  
  					if (e.which ==13)
				  	{
				  		if ($('#pm_message').val()!='')
						{  	
							send_message_to( $('#pm_to_user_id').val() );
	  			 
						}	
						setTimeout( function () {$('#pm_message').val('')}, 100)			  		
				  	}
				  	
  				}); 
				//alert(PmMessagingObj.pm_last_msg_id);
			}
	});	
	
}
function send_message_to(to_user_id)
{
	jQuery.ajax({
		// data: 'user_id='+user_id,
		url: site_url+controller.pm+'/send_message_to',
		data: 'to_user_id='+to_user_id+'&msg_text='+$('#pm_message').val(),
		type: "POST",
		timeout: 5000,
		error: function(){ console.log("25:Fehler"); },
		success: function(ret){	
				var responseJSON = $.evalJSON(ret);				
				PmMessagingObj.pm_last_msg_id = responseJSON.pm_last_msg_id;
				$($('#pm_content_message_output')).append(responseJSON.pm_quark);
				$( '#pm_content_message_output').attr({ scrollTop: $('#pm_content_message_output').attr("scrollHeight")  });
							
			}
	});	
}
