update_user_status = function(id)
{
	jQuery.get("admin.php?page=live-chat/admin/admin.php&live_chat_user=" + id);
	setTimeout('update_user_status(\'' + id + '\')', 5000);
}

get_admin_room_content = function(room_id, last_msg_id)
{
	jQuery('#room_content_' + room_id).load('admin-ajax.php', { action: "LC_get_admin_room_content", room_id: room_id, plast_msg_id: last_msg_id });
//	setTimeout('get_admin_room_content(\'' + room_id + '\')', 5000);
}

room_send_message = function(room_id)
{
	jQuery.post('admin-ajax.php', { action: "LC_get_admin_add_room_content", room_id: room_id, room_content: jQuery('#room_text_send_' + room_id).attr("value") });
	jQuery('#room_text_send_' + room_id).attr("value", "");
//	get_admin_room_content(room_id, 0);
}

var pre_page = 1;
get_log_page = function(page)
{
	jQuery('#room_avalible').load('admin-ajax.php', { action: "LC_get_log_page", page: page });
	jQuery('#paging_chat_' + pre_page).html('<a href="javascript:void(0)" class="page-numbers" onclick="get_log_page(' + pre_page + ');">' + pre_page + '</a>');
	jQuery('#paging_chat_' + page).html('<span class="page-numbers current">' + page + '</span>');

	pre_page = page;
}

live_chat_show_log = function(id)
{
	if(jQuery('#room_content_' + id).css('display') == 'none')
	{
		jQuery('#room_content_' + id).show();
	}
	else
	{
		jQuery('#room_content_' + id).hide();
	}
}

admin_add_room = function(without)
{
	jQuery.post('admin-ajax.php',
				{
					action: "LC_admin_add_room",
					without: without
				},
				function (data, textStatus) {
					if(textStatus == 'success')
					{
						jQuery('#room_avalible').append(data);
					}
				}
				);
}

client_room_send_message = function(session_id)
{
	jQuery('#live-chat-window-input input.live-chat-button').hide();
	jQuery('#live-chat-window-input img.live-chat-spiner').show();

	jQuery.post(root_url + '/wp-content/plugins/live-chat/do-ajax.php', { action: "LC_client_room_send_message", session_id: session_id, room_content: jQuery('#client_room_text_send').attr("value") });
	jQuery('#client_room_text_send').attr("value", "");
//	client_update_chat_room(session_id);
	scroll_to_message('live-chat-window-content', '.message_last');
}

scroll_to_message = function(id, to)
{
/*	var destinationSelector = to;
	jQuery('#' + id).each(
		function()
		{
			this.scrollTo(destinationSelector);
		}
	);*/
	return false;
}

show_client_chat = function()
{
	if(jQuery('#live-chat-window').css('display') == 'none')
	{
		jQuery('#live-chat-window').show();
		jQuery('#live-chat-avalible').attr('style', 'background-image:url(' + root_url + '/wp-content/plugins/live-chat/images/live_chat_online_open.gif);');
		jQuery('#live-chat-avalible-open').attr('style', 'background-image:url(' + root_url + '/wp-content/plugins/live-chat/images/live_chat_online_open.gif);');
	}
	else
	{
		jQuery('#live-chat-window').hide();
		jQuery('#live-chat-avalible').attr('style', 'background-image:url(' + root_url + '/wp-content/plugins/live-chat/images/live_chat_online.gif);');
		jQuery('#live-chat-avalible-open').attr('style', 'background-image:url(' + root_url + '/wp-content/plugins/live-chat/images/live_chat_online.gif);');

		if(jQuery('#reload_chat_page').attr('value') == 1)
		{
			client_room_close();
		}
	}
}

client_room_close = function()
{
	jQuery.post(root_url + '/wp-content/plugins/live-chat/do-ajax.php', { action: "LC_client_room_close" },
				function (data, textStatus) {
					if(textStatus == 'success')
					{
						window.location = window.location;
					}
				}
				);
}

isValidEmail = function(str)
{
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

client_create_chat_room = function()
{
	if(jQuery('#live_chat_name').attr('value') == "" || jQuery('#live_chat_name').attr('value') == "Name")
	{
		jQuery('#live_chat_name').attr('value', '');
		jQuery('#live_chat_name').focus();
		jQuery('#live_chat_name').css('border-color', '#cc0000');
		jQuery('#live-chat-error').text('* Enter Name');

		return false;
	}

		if(jQuery('#live_chat_email').attr('value') == "" || jQuery('#live_chat_email').attr('value') == "Email")
	{
		jQuery('#live_chat_email').attr('value', '');
		jQuery('#live_chat_email').focus();
		jQuery('#live_chat_email').css('border-color', '#cc0000');
		jQuery('#live-chat-error').text('* Enter Email');

		return false;
	}

	if(!isValidEmail(jQuery('#live_chat_email').attr('value')))
	{
		jQuery('#live_chat_email').focus();
		jQuery('#live_chat_email').css('border-color', '#cc0000');
		jQuery('#live-chat-error').text('* Enter valid Email');

		return false;
	}

	if(jQuery('#live_chat_text').val() == "" || jQuery('#live_chat_text').val() == "Question")
	{
		jQuery('#live_chat_text').val('');
		jQuery('#live_chat_text').focus();
		jQuery('#live_chat_text').css('border-color', '#cc0000');
		jQuery('#live-chat-error').text('* Enter Question');

		return false;
	}

	jQuery('#live-chat-window-form input.live-chat-button').hide();
	jQuery('#live-chat-window-form img.live-chat-spiner').show();

	var result = jQuery.post(root_url + '/wp-content/plugins/live-chat/do-ajax.php',
							{
								action: "LC_client_create_chat_room",
								user_name: jQuery('#live_chat_name').attr('value'),
								user_email: jQuery('#live_chat_email').attr('value'),
								user_text: jQuery('#live_chat_text').attr('value')
							},
							function (data, textStatus) {
								if(textStatus == 'success')
								{
									jQuery('#live-chat-window-form').submit();
/*									client_update_chat_room(data);
									jQuery('#live-chat-window-input').show();*/
								}
								else
								{
									alert("Error: chat room not create!");
								}
							}
							);
	return false;
}

var jscroll_panel_update = true;
client_update_chat_room = function(session_id)
{
	jQuery('#live-chat-window-content').load(root_url + '/wp-content/plugins/live-chat/do-ajax.php',
									{
										action: "LC_client_update_chat_room",
										session_id: session_id
									}
									,
									function (data, textStatus) {

										jQuery('#live-chat-window-input img.live-chat-spiner').hide();
										jQuery('#live-chat-window-input input.live-chat-button').show();

										if(textStatus == 'success')
										{
											if(jscroll_panel_update == true)
											{
												scroll_to_message('live-chat-window-content', '.message_last');
												jscroll_panel_update = false;
											}
										}
									}
									);
}

LC_negativeImageObjects = new Image(100, 54);
LC_negativeImageObjects.src = root_url + '/wp-content/plugins/live-chat/images/live_chat_online_open.gif';