function ircSetNick() {
	window.nick = window.htirc.nick;
}
function userJoined(user, chan) {
	var message;
	var frame = window.htirc.frames.get(chan);
	if(insEqual(user, this.nick.get())) {
		message = '--> You are now talking on ' + chan;
		frame.activate();
	} else {
		message= '-->' + user + ' has joined ' + chan;
	}
	frame.append(message);
	frame.nl.add(user);
}
function userLeft(user, chan, kicker, pmsg) {
	var currentTime = new Date()
	var message;
	if(!window.htirc.frames.exists(chan)) { return; }
	var frame = window.htirc.frames.get(chan);
	if(insEqual(user, this.nick.get())) {
		if(kicker) {
			message = '<-- You have been kicked out of ' + chan + ' by ' + kicker;
		} else {
			message = '<-- You have left ' + chan;
		}
		frame.nl.clear();
	} else {
		if(kicker) {
			message = '<-- ' + user + ' has been kicked out of ' + chan + ' by ' + kicker;
		} else {
			message = '<--' + user + ' has left ' + chan;
		}
		frame.nl.del(user);
	}
	if(pmsg) {
		message += ' (' + pmsg + ')';
	}
	frame.append(message);
}

function ircCommand(text) {
	var command;
	if(command = /^\/(.*?)(?: (.*))?$/.exec(text)) {
		if(command = /^\/(.*?)(?: (.*))?$/.exec(text)) {
			if(command[1] == 'me' || command[1] == 'action') {
				//XXX FIXME
				msg = (command[2]?command[2]:"");
				appendChatLine('* ' + this.nick.get() + ' ' + msg, window.htirc.frames.current.id);
				serverSendText('PRIVMSG ' + window.htirc.frames.current.id + ' :\001ACTION ' + msg + '\001');
			} else if(command[1] == 'eval') {
				eval(command[2]);
			} else if (command[1]=='j') {
				serverSendText('CS JOIN  ' + command[2]);
			} else if (command[1]=='join') {
				serverSendText('CS JOIN  ' + command[2]);
			} else if (command[1] == 'query' && command[2] != null) {
				openQuery(command[2]);
			} else if (command[1]=='topic') {
				var command2=command[2];
				command3=command2;
				command2=command2.split(" ");
				command20=command2[0];
				com=command3.replace(command20,"");
				serverSendText("TOPIC " + command2[0] + " :" + com);

			} else if (command[1] == 'msg') {
				var command2 = command[2];
				command2 = command2.split(" ");
				if (!(command2[0]).match('#')) {
					openQuery(command2[0]);
				}
				ircPrivMsg(command2[0], command2[1]);
			} else if (command[1] == "notify" || command[1] == "watch") {
				if (command[2]) {
					var nl = getCookie ('notify');
					if (!nl)
						nl = new Array();
					else
						nl = nl.split (',');
					nl.push (command[2]);
					serverSendText ('WATCH +' + command[2]);
					setcookie ('notify', nl);
				} else
					serverSendText ('WATCH');
			} else {
				serverSendText(text.substring(1));
			}
		}
	} 
	else {
		if (window.htirc.frames.current.id != '-server-') {
			ircPrivMsg(window.htirc.frames.current.id, text);
		} else {
			serverSendText(text);
		}
	}
}
function ircPrivMsg(dst, text) {
	var span = document.createElement('span');
	var style = document.createAttribute('style');
	var p = document.createElement('p');
	appendChatLine('<' + this.nick.get() + '> ' + text, dst);
	serverSendText('PRIVMSG ' + dst + ' :' + text);
}
function openQuery(target) {
	var f = window.htirc.frames.get(target);
	f.activate();
	f.append('--> Starting private conversation with ' + target);
	serverSendText("whois " + target);
}
function maskGetNick(mask) {
	return mask.split('!')[0];
}
function insEqual(a, b) {
	return(a.toLowerCase() == b.toLowerCase());
}
window.onunload = function () {
	serverSendText('QUIT :');
}

