function logout() {
	//$('#user-menu').load('ajax/logout.php');
	viewcheck();
	$.ajax({
			url:'ajax/logout.php', 
			success: function(){
				$('#chat-online').load('chat/chat-online.php');
				$('#user-menu').hide();
				$('#menu').css("display","inline");
		    }		
		});
}

function addsmiles(text)
{
	text=text.replace(":)", "<img src=\"images/smiles/1.gif\">");
	text=text.replace(":-)", "<img src=\"images/smiles/1.gif\">");
	text=text.replace(":(", "<img src=\"images/smiles/2.gif\">");
	text=text.replace(":-(", "<img src=\"images/smiles/2.gif\">");
	text=text.replace(";)", "<img src=\"images/smiles/3.gif\">");
	text=text.replace(";-)", "<img src=\"images/smiles/3.gif\">");
	text=text.replace(":P", "<img src=\"images/smiles/4.gif\">");
	text=text.replace("8)", "<img src=\"images/smiles/5.gif\">");
	text=text.replace("8-)", "<img src=\"images/smiles/5.gif\">");
	text=text.replace(":D", "<img src=\"images/smiles/6.gif\">");
	text=text.replace(":-[", "<img src=\"images/smiles/7.gif\">");
	text=text.replace("=-0", "<img src=\"images/smiles/8.gif\">");
	text=text.replace(":-*", "<img src=\"images/smiles/9.gif\">");
	text=text.replace(":`(", "<img src=\"images/smiles/10.gif\">");
	text=text.replace("*BEER*", "<img src=\"images/smiles/11.gif\">");
	text=text.replace("*LOL*", "<img src=\"images/smiles/12.gif\">");
	text=text.replace("*YAHOO*", "<img src=\"images/smiles/13.gif\">");
   return text;
}


function addmessage()
{	
	viewcheck();
	if (settimer==true) {clearTimeout(timid); settimer=false;}
	var str = $('#chat-input').val();
	$('#chat-input').val('');
	if (str!='')
	{
		
		//$('#empty').load('chat/chat-add.php', {msg: str, prvmsg: prvmsg});
		$.ajax({
			url:'chat/chat-add.php', 
			data:({msg: str, prvmsg: prvmsg}), 
			type:"post",
			success: function(msg){
				if (msg == '') {
					//remprv();
					var user_in_chat = $('#username').text();
					//strnew = str.replace("/(http|ftp)(s)?:(\\/\\/)((\\w|\\.)+)(\\/)?(\\S+)?/i", "<a href=\"$1\" target=\"_blank\">$1</a>");
					strnew = str.replace("/(https?|ftp)://\S+[^\s.,>)\];'\&quot;!?]/", "fm");
					var tmp = $('#chat-messages').html() + "<b class=\"dsct-nick\">" + user_in_chat + ":</b><span class=\"dsct-message\">" + addsmiles(htmlspecialchars(strnew)) + "</span><br>";
					
					$('#chat-messages').html(tmp);
					
					$('#chat-input').val('');
					$('#chat-scroll').scrollTop($('#chat-messages').height());
					$('#chat-input').focus();
					
					refresh();
				}
				else {
					jAlert( msg );
				}
		    }		
		});
	}
	return false;
}

function refresh()
{	
	if (settimer==true) {clearTimeout(timid); settimer=false;}
	if($('#tmp').html()!='')
	{
		var tmp=$('#tmp').html();
		tmp = $('#chat-messages').html() + tmp;
		$('#chat-messages').html(tmp);
		$('#tmp').html('');
		$('#chat-scroll').scrollTop($('#chat-messages').height());
		settimer=true;
		timid=setTimeout("refresh()", 3000);
		return true;
	}
	onlval++;
	if (onlval>=5)
	{
		onlval=0;
		refreshonline();
		return true;
	}
	$('#tmp').load('chat/chat-refresh.php');
		settimer=true;
		timid=setTimeout("refresh()", 3000);
		return true;
}

function refreshonline()
{
	if (settimer==true) {clearTimeout(timid); settimer=false;}
	$('#chat-online').load('chat/chat-online.php');
	settimer=true;
	timid=setTimeout("refresh()", 3000);
	return true;
}

function viewcheck()
{
	if (setviewtimer==true) {clearTimeout(timviewid); setviewtimer=false;}
	setviewtimer=true;
	timviewid=setTimeout("viewcheckaction()", 3600000);
	return true;
}

function viewcheckaction()
{
	$('#live').hide();
	jConfirm('Continue?', 'Confirmation Dialog', function(r) {
		if (r) {
			$('#live').show();
			viewcheck();
		}
		else {
			$('#nolive').show();
			setviewtimer=false;
		}
	});
	return true;
}

function liveon()
{
	$('#nolive').hide();
	$('#live').show();
	viewcheck();
	return true;
}

function smile(elm)
{
	/*if($('#smilebox').css("display")=='none')
	{
		$('#smilebox').css("top",$(elm).offset().top-430);
		$('#smilebox').css("left",$(elm).offset().left-300);
		$('#smilebox').show();
	}
	else
		$('#smilebox').hide();*/
	$('#smilebox').toggle();
		
	return false;
}

function emoticon(text)
{
	var txtarea = document.getElementById('chat-input');
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos)
	{
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	}
	else
	{
		txtarea.value  += text;
		txtarea.focus();
	}
	$('#smilebox').hide();
}

function remprv()
{
	prvmsg=0;
	$('#prvdiv').html("");
	$('#prvdiv').hide();
	$('#chat-input').focus();
}

function htmlspecialchars(text)
{
   var chars = Array("<", ">", '"', "'");
   var replacements = Array("&lt;", "&gt;", "&quot;", "'");
   for (var i=0; i<chars.length; i++)
   {
       var re = new RegExp(chars[i], "gi");
       if(re.test(text))
       {
           text = text.replace(re, replacements[i]);
       }
   }
	return text;
}

