var hidden = get_cookie('pg_chat');

new Ajax.Request('newchat.php?do=get&skip=true', {
    asynchronous: true,
    method: 'get',
    onComplete: function(response) {
    	if (response.responseText) {
        	$('newchat').innerHTML = response.responseText;
        	scrollChat();
    	}
    }
});

function getChat() {	
    new Ajax.Request('newchat.php?do=get', {
        asynchronous: true,
        method: 'get',
	    onComplete: function(response) {
	    	if (response.responseText) {
	        	$('newchat').innerHTML = response.responseText;
	        	scrollChat();
	    	}
	    }
    });
}

function chatSend() {
	var msg = $('newchatmsg').value;
	$('newchatmsg').value = '';

    new Ajax.Request("newchat.php?do=send&m="+encodeURIComponent(msg), {
        asynchronous: true,
        method: 'get',
        onComplete: function(response) {
            $('newchat').innerHTML = response.responseText;
            scrollChat();
        }
    });
}

function deleteLine(lineid) {
    new Ajax.Request("newchat.php?do=nuke&m="+encodeURIComponent(lineid), {
        asynchronous: true,
        method: 'get',
        onComplete: function(response) {
            $('newchat').innerHTML = response.responseText;
            scrollChat();
        }
    });
}

function scrollChat() {
	//objDiv = $('newchat_inner');
	
	//objDiv.scrollTop = objDiv.scrollHeight;
}

function playerMenu(obj, charid, msgid) {
	playername = document.getElementById('name_'+msgid).textContent;
	
    new Ajax.Request("includes/ajax_control.php?do=playermenu&charid="+charid+"&msgid="+msgid, {
        asynchronous: true,
        method: 'get',
        onComplete: function(response) {
			new Control.Modal('name_'+msgid, {
				contents: "<div class='thead'>" + playername + "</div>" + response.responseText,
				position: 'relative',
				width: 120,
				height: 120,
				offsetLeft: 20
			});
			
			Control.Modal.current.open();
        }
    });
}

function enterPlayerCmd(msgid, cmd) {
	playername = document.getElementById('name_'+msgid).textContent;

	if (typeof playername != undefined) {
		$('newchatmsg').value = '/' + cmd + ' ' + playername + ' ';
		Control.Modal.close();
		$('newchatmsg').focus();
	}
}

function fadeTip() {
	
	var tipfader 	= $('tipfader');
	var tips 		= new Array();
	
	if (typeof tipfader != undefined && tipfader != undefined) {
		tips[0] 	= "Clicking a player's name in chat will give you more options for interacting with that player.";
		tips[1] 	= "You can whisper another player by clicking their name in chat and selecting 'Send Tell'";
		tips[2] 	= "Administrators and moderators have colored names in chat.";
		tips[3] 	= "Acting inappropriately in chat can get you booted from the game or even banned.";
		tips[4] 	= "Do not ask for or provide quest help in chat, as it spoils the game for other players.";
		tips[5] 	= "Make sure you read the rules before chatting with other players.";
		tips[6] 	= "If you encounter a bug or glitch, please report it on the forum.";
		tips[7] 	= "The manual, located at the top of the page, contains information about most parts of the game.";
		tips[8] 	= "Please do not ask to become a staff member. Asking will make sure you never become staff.";
		tips[9] 	= "Play fair and honestly and you may be rewarded by other players or even staff members.";
		tips[10] 	= "Both players must confirm a trade before it will be processed.";
		tips[11]	= "Occasionally, all mail that has been read will be deleted from the database, as well as inactive trades.";
		tips[12]	= "A member of staff will never ask you for your password.";
		tips[13]	= "If you have problems with your character, please contact an administrator. Our current administrators are Covai and Krelian.";
		tips[14]	= "Items with the 'cursed' tag may not be unequipped normally. You must visit a Spirit Shrine to rid yourself of them.";
		tips[15]	= "Inappropriately named characters may be subject to a name change or deletion.";
		tips[16]	= "Learning a more powerful version of a technique will replace the less powerful version(s) you already have.";
		
		if ((tipfader.childNodes).length == 0) {
			var newtip = document.createElement('div');
			
			newtip.setAttribute('class', 'legend');
			
			newtip.appendChild(document.createTextNode(tips[0]));
			
			tipfader.appendChild(newtip);
		} else {
			var thistip = tipfader.firstChild;
			
			Effect.Fade(thistip, {
				afterFinish: function() {
					if (tipfader && tipfader.firstChild) {
						tipfader.removeChild(tipfader.firstChild);
					}
					
					var rand 	= Math.floor(Math.random()*tips.length);
					var newtip 	= $(document.createElement('div'));
					
					newtip.setAttribute('class', 'tiptext');
					newtip.hide();
					
					newtip.appendChild(document.createTextNode(tips[rand]));
					
					tipfader.appendChild(newtip);
					Effect.Appear(newtip);
				}
			});
		}
	}
}

setInterval('fadeTip()', 10000);
setInterval('getChat()', 13000);