// Web command line.
// Russ Cox <rsc@swtch.com>, February 2007 
// IE syntax fixes (no trailing commas), March 2008
// Customization: Jesus Galan (yiyu) <yiyu.jgl@gmail.com>, October 2008, March 2009

// Simple shortcuts: name: url.
var shortcuts = {
	"9":	"http://plan9.bell-labs.com/plan9/",
	"gn":	"http://www.google.com/news",
//	"hm":	"http://www.hotmail.com/",
	"gm":	"https://mail.google.com/",
//	"mb":	"http://www.meebo.com/",
	"c":	"https://www.google.com/calendar",
	"da":	"http://www.diariodeavila.es",
	"r":	"http://reddit.com",
	"x":	"http://xkcd.com",
	"u":	"http://www.ugent.be/portal/en",
	"um": "https://webmail.ugent.be",
	"a": "http://athena.ugent.be/"
}

// Search shortcuts: name: [url, cgiparam, {extra cgi}]
var searches = {
	"g":	["http://www.google.com/search", "q"],
	"i":	["http://www.google.com/images", "q"],
	"m":	["http://maps.google.com/maps", "q"],
	"y":	["http://www.youtube.com/results", "search_query"],
	"cs":	["http://www.google.com/codesearch", "q"],
	"w":	["http://en.wikipedia.org/wiki/Special:Search", "search"],
	"aw":	["http://wiki.archlinux.org/index.php/Special:Search", "search"],
	"v":	["http://www.google.com/cse", "cx=partner-pub-2060328396151526%3Aea9sar-xttn&q"],
	"b":	["http://delicious.com/search", "lc=1&p"],
	"s":	["http://delicious.com/save", "url"]
}

// Translation shortcuts: name: url
var translators = {
	"es": "http://www.wordreference.com/es/translation.asp?tranword=",
	"se": "http://www.wordreference.com/es/en/translation.asp?spen="
}

// Help text to be displayed for shortcuts & commands.
var help = {
	"9":	"plan9 from bell Labs",
	"u":	"ugent",
	"b":	"delicious bookmarks",
	"s":	"save url in delicious",
	"aw":	"archlinux wiki",
	"g":	"google search (default)",
	"i":	"google images",
	"gm":	"google mail",
	"gn":	"google news",
	"hm":	"hotmail",
	"cs":	"google code search",
	"da":	"diariode&aacute;vila",
	"m":	"google maps",
	"mb":	"meebo.com (web im)",
	"r":	"reddit",
	"c":	"google calendar",
	"v":	"doc.cat-v.org",
	"w":	"wikipedia",
	"e":	"javascript evaluator",
	"x":	"xkcd",
	"y":	"youtube",
	"es":	"english to spanish",
	"se":	"spanish to english",
	"um": "ugent mail",
	"a": "athena.ugent.be"
}

// Commands: args are command name, arg text,
// and array of arg text split by white space.
// Commands must be named cmd_foo where
// foo is the actual command name.

// Evaluate an argument.
function cmd_e(cmd, arg, args)
{
	output(arg + " = " + eval(arg));
}


/////
///// Below here you should not need to fiddle with.
/////

// Compute help text.
function helptext()
{
	var a;
	var i = 0;
	var s = "";
	
	s += "<table cellspacing=0 cellpadding=0 border=0>";
	a = new Array();
	for(var k in searches)
		a[i++] = k;
	a.sort();
	s += "<tr><td colspan=6><b>Searches:</b><br>\
		<em><font size=1>http://c.4l77.com/[CMD]/search terms</font></em>";
	for(i=0; i<a.length; i++){
		if(i%2 == 0)
			s += "<tr align=left>"
		var h = help[a[i]];
		if(h == undefined)
			h = searches[a[i]][0];
		h = "<a href=\"" + searches[a[i]][0] + "\">" + h + "</a>";
		s += "<td><b>" + a[i] + "</b><td width=10><td>" + h + "\n";
	}
	s += "<tr height=10>\n";

	a = new Array();
	i = 0;
	for(var k in shortcuts)
		a[i++] = k;
	a.sort();

	s += "<tr><td colspan=6><b>Shortcuts:</b>";
	for(i=0; i<a.length; i++){
		if(i%2 == 0)
			s += "<tr align=left>"
		var h = help[a[i]];
		if(h == undefined)
			h = shortcuts[a[i]];
		h = "<a href=\"" + shortcuts[a[i]] + "\">" + h + "</a>";
		s += "<td><b>" + a[i] + "</b><td width=10><td>" + h + "\n";
	}
	s += "<tr height=10>\n";
	
	a = new Array();
	i = 0;
	for(var k in translators)
		a[i++] = k;
	s += "<tr><td colspan=6><b>Translators (wordreference):</b>";
	for(i=0; i<a.length; i++){
		if(i%2 == 0)
			s += "<tr align=left>"
		var h = help[a[i]];
		if(h == undefined)
			h = translators[a[i]];
		h = "<a href=\"" + translators[a[i]] + "\">" + h + "</a>";
		s += "<td><b>" + a[i] + "</a></b><td width=10><td>" + h + "\n";
	}
	s += "<tr height=10>\n";
	
	a = new Array();
	i = 0;
	for(var k in window)
		if(k.substr(0,4) == "cmd_")
			a[i++] = k.substr(4);
	a.sort();
	s += "<tr><td colspan=6><b>Additional Commands:</b>";
/*
	for(i=0; i<a.length; i++){
		var h = help[a[i]];
		if(h == undefined)
			h = "???";
		s += "<tr><td><b>" + a[i] + "</b><td width=10><td>" + h + "\n";
	}
*/
	h = help["e"];
	s += "<tr align=left><td><b>e</b><td width=10><td>" + h + "\n";
	s += "<tr><td><td width=10><td>URLs are allowed too.\n";

	s += "</table>\n";
	return s;
}

// Run command.
function runcmd(cmd)
{
	// Check for URL.
	var space = cmd.indexOf(' ');
	if(space == -1 && (cmd.indexOf('/') != -1 || cmd.indexOf('.') != -1)){
		// No spaces, has slash or dot: assume URL.
		if(cmd.indexOf('://') == -1)
			cmd = "http://" + cmd;
		window.location = cmd;
		return false;
	}
	if(space == -1){
		arg = "";
		args = new Array();
	}else{
		arg = cmd.substr(space+1);
		cmd = cmd.substr(0, space);
		args = toarray(arg.split(/\s+/));
	}

	var fn;
	if(searches[cmd] != undefined)
		fn = search;
	else if(shortcuts[cmd] != undefined)
		fn = shortcut;
	else if(translators[cmd] != undefined)
		fn = translate;
	else{
		fn = window["cmd_" + cmd];
		if(fn == undefined){
//			error("no command: " + arg);
			if(cmd.length > 0)
				search("g", cmd + " " + arg, args);
			else
				search("g", "In the beginning was the command line");
			return false;
		}
	}
	fn(cmd, arg, args);
	return false;
}

// Print output on page.
function output(s)
{
	document.getElementById("output").innerHTML += s + "<br>";
}

// Print error on page.
function error(s)
{
	document.getElementById("error").innerHTML += s + "<br>";
}

// Convert whatever split returns into an Array.
function toarray(args)
{
	var a = new Array();
	for(var i = 0; i < args.length; i++)
		a[i] = args[i];
	return a;
}

// Return a URL with some CGI args.
function cgiurl(base, params)
{
	var url = base + "?";
	for(var k in params)
		url += k + "=" + escape(params[k]) + "&";
	return url;
}

// Handle search shortcut.
function search(cmd, arg, args)
{
	var a = searches[cmd][2];
	if(a == undefined)
		a = {};
	a[searches[cmd][1]] = arg;
	window.location = cgiurl(searches[cmd][0], a);
}

// Handle simple shortcut.
function shortcut(cmd, arg, args)
{
	window.location = shortcuts[cmd];
}

// Handle translator shortcut.
function translate(cmd, arg, args)
{
	window.location = translators[cmd] + arg;
}

