

$(document).ready
(
		function()
		{
			$.idleTimer(30000);//300000 = 5 minutes
			
			$(document).bind("idle.idleTimer", function(){
				// function you want to fire when the user goes idle
				

				//chat_update_user_status('idle');//too much processing overhead
				$('.chat_status_user_id_'+USER_ID).attr('src','/app_shared/public/media/icons/color_droplet_orange.png');
				
			});
				 
				 
			$(document).bind("active.idleTimer", function(){
				// function you want to fire when the user becomes active again

				
				chat_update_user_status('active');
				$('.chat_status_user_id_'+USER_ID).attr('src','/app_shared/public/media/icons/color_droplet_green.png');
			});			
			
			
			//chat_update_user_status('active');too slow
			
			chat_get_available_users_interval = setInterval('chat_get_available_users()',60000);
			//chat_get_available_users(); too slow
			
			
			chat_general_interval = setInterval('chat_general_event_interval()',10000);
			 

			/*
			interactive events
			*/
			$('.chat_start_message_with_user').live
			(
				"click",
				function()
				{
					var obj_params = eval('('+$(this).attr('rel')+')');
					
					chat_start_message_with_user(obj_params);
				}
			)
		}
);







function chat_general_event_interval()
{
	chat_get_list_of_senders_that_have_sent_an_unviewed_message();
	chat_update_user_status( $.data(document,'idleTimer') );
}

function chat_get_list_of_senders_that_have_sent_an_unviewed_message()
{
	if( USER_ID )
	{
		$.ajax
		(
			{
				type:"POST",
				url:'/filing/chat/get_list_of_senders_that_have_sent_an_unviewed_message',
				data: {recipient_user_id:USER_ID},
				dataType:"json",
				success: function(data)
				{	
					if( data.is_success == true )
					{
						for(i in data.arr_senders)
						{
							chat_get_private_messages( data.arr_senders[i] ,USER_ID);
						}
					}
				},
				error: function(data)
				{
					//common_error_message( 'Error with the data for chat_get_list_of_senders_that_have_sent_an_unviewed_message.' );
					
				}
			}
		);
	}
}

function chat_get_private_messages(sender_user_id,recipient_user_id)
{
	
	if( typeof sender_user_id != 'undefined' )
	{
		$.ajax
		(
			{
				type:"POST",
				url:'/filing/chat/get_formatted_private_messages',
				data: {sender_user_id:sender_user_id,recipient_user_id:recipient_user_id},
				dataType:"json",
				success: function(data)
				{	
					if( data.is_success == true )
					{
						if( $('.chat_private_message_'+sender_user_id+'_to_'+recipient_user_id).length > 0 )
						{
							$('.chat_private_message_'+sender_user_id+'_to_'+recipient_user_id).append( data.str_results );
						}
						else if(  $('.chat_private_message_'+recipient_user_id+'_to_'+sender_user_id).length > 0 )
						{
							$('.chat_private_message_'+recipient_user_id+'_to_'+sender_user_id).append( data.str_results );
						
						}
						else
						{
							
							var obj_params 				= new Object();
							obj_params.user_id 			= recipient_user_id;
							obj_params.username 		= data.username;
							obj_params.is_incoming_message 		= true;
							obj_params.name 			= data.name;
							obj_params.sender_user_id 	= sender_user_id;
							
							chat_start_message_with_user(obj_params);
							
						}	
						
						
						$(".chat_private_message").prop({ scrollTop: $(".chat_private_message").prop("scrollHeight") });
					}
					else
					{
						//alert('no post');
					}
					
					//set_the_ui();
				},
				error: function(data)
				{
					common_error_message( "Error with chat_get_private_messages function " );
				}
			}
		);
	}
	
	//common_error_message('sender_user_id:'+sender_user_id+' , recipient_user_id:'+recipient_user_id+' ');
}

function form_chat_private_message_on_complete(data)
{
	$('.chat_private_message_'+data.user_id+'_to_'+data.recipient_user_id+'').append(data.message);
	$(".chat_private_message").prop({ scrollTop: $(".chat_private_message").prop("scrollHeight") });
	$('#'+data.form_id).clearForm();
}

function chat_get_available_users()
{
	$.ajax
	(
		{
			type:"POST",
			url:'/filing/chat/get_available_users',
			data: {role:ROLE,user_id:USER_ID},
			dataType:"json",
			success: function(data)
			{	
				if( data.is_success == true )
				{
					int_chat_users_online =  data.int_users_online;
					$('.int_chat_users_online').html( int_chat_users_online );
					$('.available_users_list').html( data.str_results );

				}
				
				set_the_ui();
			},
			error: function(data)
			{
				//alert('error with the data for chat_get_available_users xxxx ');
			}
		}
	);
}



function chat_update_user_status(status)
{
	
	$.ajax
	(
		{
			type:"POST",
			url:'/filing/chat/update_user_status',
			data: {user_id:USER_ID,status:status},
			dataType:"json",
			success: function(data)
			{	
				//alert( dump(data) );
				
				if( data.is_success == true )
				{
					//$('#available_users_list').html( data.str_results );
				}
				
				//set_the_ui();
			},
			error: function(data)
			{
				//alert('error with data');
			}
		}
	);
	
}



function chat_start_message_with_user(obj_params)
{
	
	var recipient_user_id 	= obj_params.user_id;
	var username 			= obj_params.username;
	var name 				= obj_params.name;
	
	if( typeof obj_params.is_incoming_message == 'undefined')
	{
		obj_params.is_incoming_message = 0;
	}
	
	if( typeof obj_params.sender_user_id != 'undefined')
	{
		var sender_user_id 		= obj_params.sender_user_id;
	}
	else
	{
		var sender_user_id 		= USER_ID;
	}
	
	var str 				= sender_user_id+' recipient:'+recipient_user_id;
	
	var id 					= 'dialog_'+sender_user_id+'_to_'+recipient_user_id;//create_random_element();
	var int_private_messages =  $('.chat_private_message').length ;
	var y_message_position = (int_private_messages)*30;
	var x_message_position = (int_private_messages)*30;
	
	if( recipient_user_id == sender_user_id )
	{
		common_error_message( "Don't talk to yourself :) " );
	}
	else
	{
		$.ajax
		(
			{
				type:"POST",
				url:'/filing/chat/get_composite_private_message',
				data: {sender_user_id:sender_user_id,recipient_user_id:recipient_user_id,is_incoming_message:obj_params.is_incoming_message},
				dataType:"json",
				success: function(data)
				{	
					//alert( dump(data) );
					if( data.is_success == true )
					{
						
						if( $('#'+id).length > 0 )
						{
							$('#'+id).dialog('moveToTop');
							//$('#'+id).html( data.str_results );
						}
						else
						{
						
							$("body").append('<div id="'+id+'" ></div>');
							$('#'+id).html( data.str_results );
							
							obj_params.height 	= 200;
							obj_params.width 	= 350;
		
							$('#'+id).dialog
							(
								{
									autoOpen: false,
									title: 	name+' ('+username+')',
									width: 	obj_params.width,
									height: obj_params.height,
									sticky: true,
									position: [ eval($(window).width()-350-x_message_position),y_message_position],
									close: function(event,ui)
									{
										$(this).dialog('destroy').remove();
									}
								}     
							);	
							
							$('#'+id).parent().css({position:"fixed"}).end().dialog('open');

						}
						setTimeout("chat_move_scroll_down()",500);
					}
					
					set_the_ui();
				},
				error: function(data)
				{
					//alert('error chat_start_message_with_user');
				}
			}
		);			
	}
	
}

function chat_move_scroll_down()
{
	$(".chat_private_message").prop({ scrollTop: $(".chat_private_message").prop("scrollHeight") });
	
}

// pass the string 'destroy' to stop the timer
//$.idleTimer('destroy');

