function show_now_in_site()
{
	if($('.now_in_site').length) 
	{
		$.post("/cgi-bin/dsp_ajax.pl", { cl: "chat", event: "show_now_in_site", rnd: Math.random(), utf8: 1 }, 
		function(data) {
			$('.now_in_site').html(data);
		});	
	}
}

// --------------------------------
var chat_was_updated = 0; // последнее время запроса на обновление
var chat_new_updated = 0; // последнее время обновления чата

function is_sound_off()
{
	var off = $.cookie("chat_sound_off");
	if (off == null)
	{
		off = 0;
	}
	return off;
}

function set_switch_on()
{
	$('#sound_swicher').attr('off', '0');
	$('#sound_swicher img').attr('src', '/images/sound.png');
}

function set_switch_off()
{
	$('#sound_swicher').attr('off', '1');
	$('#sound_swicher img').attr('src', '/images/sound_mute.png');
}

function show_sound_swicher()
{
	if (is_sound_off() == 1)
	{
		set_switch_off();
	}
	else
	{
		set_switch_on();
	}
}

function _switch_sound()
{
	if (is_sound_off() == 1)
	{
		set_switch_on();
		$.cookie("chat_sound_off", "0", { expires: 30 });
	}
	else
	{
		set_switch_off();
		$.cookie("chat_sound_off", "1", { expires: 30 });
	}
}

function show_chat()
{
	if($('.right_chat').length) 
	{
		$('.right_chat').html('<tr><td align="center"><img src="/images/loading1.gif"></td></tr>');
		_show_chat_log("full");
		setInterval('_show_chat_log("last")', 10000);	
	}
}

function _show_chat_log(mode)
{
	if (mode == 'last')
	{
		_set_chat_new_updated();
		if (chat_new_updated > chat_was_updated) // если время последнего запроса на обновление меньше, чем время обновления чата - значит были изменения
		{
			$.post("/cgi-bin/dsp_ajax.pl", { cl:"chat", event:"get_chat", after:chat_was_updated, mode:mode, rnd:Math.random(), utf8:1 }, 
				function(data) 
				{
					$(".right_chat").prepend(data);
					// if (is_sound_off() == 0)
					// {
						// $(".right_chat_exclaim").html("<embed src=\"/upload/sounds/miss8.wav\" height=\"1px\" width=\"1px\"></embed>");
					// }
					chat_was_updated = chat_new_updated; // дата последнего запроса на обновление
				}
			);
		}
	}
	else
	{
		_set_chat_new_updated();
		$.post("/cgi-bin/dsp_ajax.pl", { cl:"chat", event:"get_chat", mode:mode, before:chat_new_updated, rnd:Math.random(), utf8:1 }, 
			function(data)
			{
				$('.right_chat').html('');
				$(".right_chat").prepend(data);
				chat_was_updated = chat_new_updated; // дата последнего успешного запроса на обновление
			}
		);
	}
}

function _set_chat_new_updated()
{
	$.ajax({
   type: "POST",
   url: "/cgi-bin/dsp_ajax.pl",
   data: "cl=chat&event=get_chat_updated&rnd=Math.random()&utf8=1",
	 async: false,
   success: function(data)
	 {
		 chat_new_updated = data;
   }
 });
}

$(document).ready(function() 
{
	// show_now_in_site();
	get_chat_head();
	show_chat();
	// show_sound_swicher();
	$('.add_chat_message').click(function ok()
	{
		add_chat_message();
		return false;
	});	
	// $('.switch_sound').click(function switch_sound()
	// {
		// _switch_sound();
		// return false;
	// });	
});


//------------------------
//чтобы работала отправка по enter, нужно в поле ввода добавить  onkeypress="add_message_enter(event);"
function add_message_enter(event)
{		
	if (event.keyCode == 13) 
	{
		add_chat_message();
	}
}

function add_chat_message()
{
	var message = $('#chat_message').attr("value");
	$('#chat_message').attr("value", '');
	$.post("/cgi-bin/dsp_ajax.pl", { cl: "chat", event: "add_chat_message", value: message, rnd: Math.random(), utf8: 1 }, 
		function(data) 
		{
			_show_chat_log("last");
		}
	);
	return false;
}

function get_chat_head()
{
	$.ajax({
   type: "POST",
   url: "/cgi-bin/dsp_ajax.pl",
   data: "cl=chat&event=get_chat_head&rnd=Math.random()&utf8=1",
	 async: false,
   success: function(data)
	 {
		  if (data == 'not_authorized' )
		  {
				$('.chat_login').show();
		  }
		  else
		  {
				$('.chat_input_text').show();
		  }
   }
	});
	get_is_user_admin();
}

function get_is_user_admin()
{
	$.ajax({
		type: "POST",
		url: "/cgi-bin/dsp_ajax.pl",
		data: "cl=chat&event=print_is_user_admin&rnd=Math.random()&utf8=1",
		async: false,
		success: function(data)
		{
		// alert(data);
			if (data == 1 )
			{
				$('.delete_all_messages').show();
				// $('.delete_message').show();
			}
		}
	});	
}

function delete_message(msg_id)
{
	$.post("/cgi-bin/dsp_ajax.pl", { cl: "chat", event: "delete_message", msg_id: msg_id, rnd: Math.random(), utf8: 1 });
}

function delete_all_messages()
{
	$.post("/cgi-bin/dsp_ajax.pl", { cl: "chat", event: "delete_all_messages", rnd: Math.random(), utf8: 1 });
}
