/* Terminal Keyboard Library
 * Copyright 2005, Steve Blinch
 *
 * Part of the BBS Web Terminal
 * http://bbs.blitzaffe.com
 *
 * Not licensed for redistribution
 */

function Keyboard(terminal,sendframeid,sendfieldid) {
	this.ctrlPressed = false;
	this.altPressed = false;
	this.shiftPressed = false;
	this.keycr = false;
	this.keyesc = false;
	this.keystring = "";
	this.keyinfo = "";
	this.sendindex = Math.round(Math.random()*100000000);
	this.sendtimer = null;
	this.skip_one_focus = false;

	this.terminal = terminal;
	
	this.sendframe = document.getElementById(sendframeid);
//	this.sendfield = document.sendform.send;
	this.sendfield = document.getElementById(sendfieldid);
	
	return this;
}

Keyboard.prototype.set_timer = function(active) {
	
	if (active) {
		if (this.sendtimer) return;
		this.sendtimer = setTimeout('term.kb.send_text()',500);
	} else {
		if (!this.sendtimer) return;
		clearTimeout(this.sendtimer);
		this.sendtimer = null;
	}
}

Keyboard.prototype.key_press = function(e) {
	var key = 0;
	if (!e) {
		e = window.event;
		key = e.keyCode;
	} else {
		key = e['which'];
	}
	
	// try not to interfere with browser hotkeys
	if (this.ctrlPressed || this.altPressed) return true;

	switch(key) {
		// ignore keys which shouldn't be trapped
		case 0:
		case 16:
		case 17:
		case 18:
			break;
		case 13:
			// trap ENTER
			this.set_timer(false);
			this.keycr = true;
			this.send_text();
			
			if (this.terminal.debug) this.terminal.status.write('status_info1','[' + this.keystring + ']['+this.keyinfo+']',11);
						
			return false;
		case 27:
			// trap ESCAPE
			this.set_timer(false);
			this.keyesc = true;
			this.send_text();
			
			if (this.terminal.debug) this.terminal.status.write('status_info1','[' + this.keystring + ']['+this.keyinfo+']',11);
						
			return false;			
		default:
			// trap character keys
			this.set_timer(true);
			this.keystring += String.fromCharCode(key);
			
			if (this.terminal.debug) this.terminal.status.write('status_info1','[' + this.keystring + ']['+this.keyinfo+']',11);

			return false;
			
	}
	return true;	
}

Keyboard.prototype.key_change = function(e) {
	if (!e) e = window.event;
	

	isdown = (e.type=='keydown');
	this.keyinfo = e.keyCode+'|'+(isdown?'dn':'up');

	//if (!this.terminal) return;	alert('woo');
	this.terminal = term;
	
	if (this.terminal.debug) this.terminal.status.write('status_info1','[' + this.keystring + ']['+this.keyinfo+']',11);
	
	switch(e.keyCode) {
		case 8:
			if (IE4 && !isdown) {
				this.keyinfo = "spc";
				if (this.terminal.debug) this.terminal.status.write('status_info1','[' + this.keystring + ']['+this.keyinfo+']',11);
				e['which'] = e.keyCode;
				this.key_press(e);
				return false;
			}
		case 13:
			break;
		case 16:
			this.shiftPressed = e.shiftKey;
			break;
		case 17:
			this.ctrlPressed = e.ctrlKey;
			break;
		case 18:
			this.altPressed = e.altKey;
			break;
		case 27:
			if (IE4 && !isdown) {
				this.keyinfo = "spc";
				if (this.terminal.debug) this.terminal.status.write('status_info1','[' + this.keystring + ']['+this.keyinfo+']',11);
				e['which'] = e.keyCode;
				this.key_press(e);
				return false;
			}
			return false;
	}
	return true;
	
}

Keyboard.prototype.send_text = function() {
	// am I on glue, or does escape() really screw up on strings containing plus characters?
	this.keystring = escape(this.keystring.replace(/\+/g,"\x00"));
	this.keystring = this.keystring.replace(/%00/g,"%2B");
	
	this.sendindex++;
	
	url = "proxysend.php?bbs=" + this.terminal.connectedto + "&cr=" + (this.keycr ? 1 : 0) + '&esc=' + (this.keyesc ? 1 : 0) + '&txt=' + this.keystring + '&token=' + this.terminal.token + '&ie=' + this.sendindex;
	if (this.terminal.connected) this.sendframe.src = url;
	
	this.keystring = "";
	this.keycr = false;
	this.keyesc = false;
	
	this.set_timer(false);	
}

Keyboard.prototype.send_focus = function(only_if_connected) {
	if (this.skip_one_focus) {
		this.skip_one_focus = false;
		return;
	}
	if (only_if_connected && !this.terminal.connected) return;
	this.sendfield.focus();
}

Keyboard.prototype.no_send_focus = function() {
	this.skip_one_focus = true;	
}
