var iPagerMessageFromUserId = 0;

var iPagerMessagesLastMessageId = 0;
var bPagerMessagesInForm = false;
var bFrontendPageFirst = true;

function PagerMessagesCheckAwaitingMessages( data ) {
	if (bPagerMessagesInForm) return;
	if (!data.length) return;
	$("#pagermsg").data( "datas" , data );
	var msg = data[0];
	if (iPagerMessagesLastMessageId==msg.pager_message_id) return;
	bPagerMessagesInForm = true;
	// modal form
	iPagerMessagesLastMessageId = msg.pager_message_id;
	$("#pagermsg_poster_user").html( msg.user_name );
	$("#pagermsg_message").html( msg.message );
	$("#pagermsg").dialog( "open" );
}

function ClearPagerMessageForm() {
	$("#pager_target_user").val("");
	iPagermessageFromUserId = 0;
	$("#pager_message").val("");
}

function InitPagerMessageForm() {
	$("#pager_target_user").autocomplete( {
		source: function(request,response) {
			$.ajax({
				type: "POST",
				cache: false,
				data: {
					inajax : 1,
					ajaxevent : "target_user_autocomplete",
					name_filter: request.term
				},
				success: function( data ) {
					response($.map(data,function(item) {
						var img = (item.is_online) ? "/images/online.png" : "/images/offline.png";
						return {
							label: "<img src=\""+img+"\" /> " + item.user_name,
							value: item.user_name,
							uid: item.user_id
						}
					}))
				}
			});
		},
		minLength: 1,
		select: function(event,ui) {
			iPagerMessageFromUserId = (ui.item) ? ui.item.uid : 0;
		}
	});

	// init pager form
	var btns = new Object();
	btns[aJSLangDefs.pager_Cancel]=function() {
		$(this).dialog( "close" );
	}
	btns[aJSLangDefs.pager_Send]=function() {
		if (iPagerMessageFromUserId) {
			var postdata = new Object();
			postdata.inajax = 1;
			postdata.ajaxevent = "send_pager_message";
			postdata.target_user_id = iPagerMessageFromUserId;
			postdata.message = $("#pager_message").val();
			$.ajax(
				{
					type: "POST",
					cache: false,
					data: postdata,
					success: function( retval ) {
						$("#messagepager").dialog( "close" );
						ClearPagerMessageForm();
					}
				}
			);
		} else {
			// nem valid form
			alert( aJSLangDefs.pager_No_target_user_selected );
		}
	}

	$("#messagepager").dialog(
		{
			autoOpen : false,
			width: 400,
			height: 300,
			modal: true,
			buttons: btns,
			close: function() {
			}
		}
	);

	var btns = new Object();
	btns[aJSLangDefs.pager_Acknowledge_message]=function() {
		var postdata = new Object();
		postdata.inajax = 1;
		postdata.ajaxevent = "ack_pager_message";
		var data = $("#pagermsg").data( "datas" );
		var msg = data[0];
		postdata.pager_message_id = msg.pager_message_id;
		$.ajax(
			{
				type: "POST",
				cache: false,
				data: postdata,
				success: function( retval ) {
					$("#pagermsg").dialog( "close" );
				}
			}
		);
	}

	$("#pagermsg").dialog(
		{
			autoOpen : false,
			width: 400,
			height: 300,
			modal: true,
			buttons: btns,
			close: function() {
				bPagerMessagesInForm = false;
			}
		}
	);
}

function InitFrontendPage( id , datas ) {
	var o = $("#"+id );
	o.data( "datas" , datas );
	// InitPagerMessageForm();
	// FrontendPageGlobalAjax( id );
}

function FrontendPageGlobalAjax( id ) {
	var o = $("#"+id );
	var d = o.data( "datas" );

	var postdata = new Object();
	postdata.inajax = 1;
	postdata.ajaxevent = "refresh_infopanel";
	if (bFrontendPageFirst) {
		postdata.first = true;
		bFrontendPageFirst = false;
	}

	if (typeof ChatPanelInitPost!="undefined")
		postdata = ChatPanelInitPost( postdata );
	
	$.ajax(
		{
			type: "POST",
			cache: false,
			data: postdata,
			success: function(allinfo) {
				if (typeof HandleGlobalInfo!="undefined")
					HandleGlobalInfo( allinfo.globalinfo );
				if (typeof ChatPanelPostCallback!="undefined")
					ChatPanelPostCallback( allinfo.chat_messages );
				if (typeof ChatroomUserPanelPostCallback!="undefined" &&
					typeof allinfo.chatroom_users!="undefined")
					ChatroomUserPanelPostCallback( allinfo.chatroom_users );
				// check for pager messages
				if (typeof PagerMessagesCheckAwaitingMessages!="undefined" &&
					typeof allinfo.pager_messages != "undefined")
					PagerMessagesCheckAwaitingMessages( allinfo.pager_messages );
				if (typeof allinfo.stop_callback=="undefined") {
					var o = $("#"+id );
					var d = o.data( "datas" );
					if (d.refresh_time>0) setTimeout( "FrontendPageGlobalAjax('"+id+"');" , d.refresh_time );
				}
			}
		}
	);
}

function CheckValidEmail( s ) {
	var atpos = s.indexOf( "@");
	var dotpos = s.lastIndexOf( "." );
	if (atpos == -1) return false;
	if (dotpos == -1) return false;
	if (atpos>dotpos) return false;
	if (s.indexOf( "," )>=0 || s.indexOf( " " )>=0 || s.indexOf( ";" )>=0) return false;
	return true;
}

function PagerMessageForm() {
	$("#messagepager").dialog("open");
}
