//
// +-------------------------------------------------------+
// | pbot4.js : the javascript behind puzzlebot v4.0       |
// +-------------------------------------------------------+
// | copyright (c) 2008 Jared Russell                      |
// | email         me@jaredrussell.com                     |
// | web           http://jaredrussell.com/                |
// | license       BSD <http://jaredrussell.com/license/>  |
// +-------------------------------------------------------+
//

<!--
var _keyCodes = [8, 13, 27, 35, 36, 37, 38, 39, 40, 46];
var _frozen = false;
var _cursor = false;
var _position = 0;
var _text = "";

var _inputs = [];
var _input = 0;

function ScrollToBottom()
{
	scroll(0, $(document).height());
}

function ShowCursor()
{
	var html = "";

	if (_position < _text.length)
	{
		html = _text.slice(0, _position) + "<span id='cursor'>" + _text.slice(_position, _position + 1) + "</span>" + _text.slice(_position + 1);
	}
	else
	{
		html = _text + "<span id='cursor'>&nbsp;</span>";
	}

	$("div#terminal div:last span.text").html(html);
	_cursor = true;
}

function HideCursor()
{
	$("div#terminal div:last span.text").html(_text.length == 0 ? '&nbsp;' : _text);
	_cursor = false;
}

function SwapCursor()
{
	_cursor ? HideCursor() : ShowCursor();
}

function NewInput()
{
	HideCursor();

	_position = 0;
	_text = "";

	$("div#terminal").append("                   \
		<div class='input'>                      \
			<span class='prompt'>&gt;&gt;</span> \
			<span class='text'></span>           \
		</div>                                   \
	");

	ScrollToBottom();
}

function Respond(response)
{
	$("div#terminal").append("                       \
		<div class='response'>" + response + "</div> \
	");
}

function Remote(address, text)
{
	HideCursor();
	_frozen = true;

	$.get(address, {args: text}, function(text, textStatus)
	{
		HideCursor();
		Respond(text);

		$("div#terminal span.text:last").parent().remove();
		NewInput();

		_frozen = false;
	});

	_text = "";
	_position = 0;

	$("div#terminal").append("         \
		<div>                          \
			<span class='text'></span> \
		</div>                         \
	");

	ScrollToBottom();
}

function RemoteCommand(command)
{
	Remote("_php/command.php", command);
}

function Chat(text)
{
	Remote("_php/chat.php", text);
}

function RunCommand(command)
{
	var args = $.grep(command.split(" "), function(n, i) { return n != "" });

	switch(args.shift().toLowerCase())
	{
	case "clear":
	case "cls":
		$("div#terminal").children().not("h3").remove();
		break;

	case "echo":
		for (i in args)
		{
			args[i] = args[i].replace(/["']/g, "");
		}
		Respond(args.join(" "));
		break;

	default:
		RemoteCommand(command);
		return;
	}

	NewInput();
}

function ParseText()
{
	if (_text.length > 0)
	{
		_inputs.push(_text);
		_input = _inputs.length;
	}

	HideCursor();

	if (_text.substr(0, 1) == '!')
	{
		RunCommand(_text.slice(1));
	}
	else if (_text != "")
	{
		Chat(_text);
	}
	else
	{
		NewInput();
	}
}

function IsKeyCode(code)
{
	return $.inArray(code, _keyCodes) != -1;
}

function OnKeyCode(code)
{
	var result = false;

	switch (code)
	{
	case 8: // backspace
		if (_position > 0)
		{
			_text = _text.slice(0, _position - 1) + _text.slice(_position);
			_position--;
		}
		break;

	case 13: // enter
		ParseText();
		break;

	case 27: // escape
		_text = "";
		_position = 0;
		_input = _inputs.length;

	case 35: // end
		_position = _text.length;
		break;

	case 36: // home
		_position = 0;
		break;

	case 37: // left arrow
		if (_position > 0)
		{
			_position--;
		}
		break;

	case 38: // up arrow
		if (_input > 0)
		{
			_input--;
			_text = _inputs[_input];
			_position = _text.length;
		}
		break;

	case 39: // right arrow
		if (_position < _text.length)
		{
			_position++;
		}
		break;

	case 40: // down arrow
		if (_input < _inputs.length)
		{
			_input++;
			_text = _inputs[_input] || "";
			_position = _text.length;
		}
		break;

	case 46: // delete
		_text = _text.slice(0, _position) + _text.slice(_position + 1);
		if (_position > _text.length)
		{
			_position = _text.length;
		}
		break;

	default:
		result = true;
	}

	return result;
}

function OnKeyPress(char)
{
	_text = _text.slice(0, _position) + String.fromCharCode(char) + _text.slice(_position);
	_position++;
	return false;
}

$(document).keydown(function(e)
{
	if (e.ctrlKey || e.metaKey || _frozen)
	{
		return true;
	}

	if (($.browser.safari || $.browser.msie) && IsKeyCode(e.keyCode))
	{
		OnKeyCode(e.keyCode);
		ShowCursor();
		ScrollToBottom();
	}
});

$(document).keypress(function(e)
{
	if (e.ctrlKey || e.metaKey || _frozen)
	{
		return true;
	}

	var result = true;

	if (IsKeyCode(e.keyCode))
	{
		if ($.browser.mozilla)
		{
			OnKeyCode(e.keyCode);
		}

		result = false;
	}
	else
	{
		result = OnKeyPress(e.which || e.charCode);
	}

	if (!result)
	{
		ShowCursor();
		ScrollToBottom();
	}

	return result;
});

$(function()
{
	NewInput();
	setInterval("SwapCursor()", 500);
});
-->

