// QuickSearch.js - by Dominic Szablewski - http://www.phoboslab.org
// Based on Shaun Inman's Shortwave - http://shortwaveapp.com/

// All command definitions are in the following format: 
// COMMAND: ['Description', 'Target URL'],
// %s in the URL is replaced by optional search terms
QS_cmds = {
	g:	['Google Search', 		'http://www.google.com/search?q=%s'],
	map:	['Google Maps', 		'http://www.google.com/maps?q=%s'],
	w:	['Wikipedia Search', 		'http://en.wikipedia.org/wiki/%s'],
	php:	['PHP Function Search', 	'http://php.net/search.php?show=quickref&pattern=%s'],
	a:	['Amazon Search', 		'http://www.amazon.com/s/?field-keywords=%s'],
	imdb:	['IMDB Search', 		'http://www.imdb.com/find?s=all&q=%s'],
	nf:	['Netflix Search', 		'http://www.netflix.com/Search?v1=%s'],
	f:	['Flickr Search', 		'http://flickr.com/search/?q=%s'],
	t: 	['Twitter User', 		'http://twitter.com/%s'],
	s:	['Summize Twitter Search',	'http://summize.com/search?q=%s'],
	yt:	['YouTube Search', 		'http://www.youtube.com/results.php?search_query=%s'],
	v:	['Vimeo Search', 		'http://vimeo.com/videos/search:%s'],
	ip:	['IP Address', 			'http://www.whatismyip.org/'],
	whois:	['whois Search', 		'http://www.whois-search.com/whois/%s']
	// Dont forget to add a ',' at the end of the previous line when
	// defining new commands!
};

function QS_listCommands() {
	var cmdlist = 'Available commands: ';
	for( var cmd in QS_cmds ) {
		cmdlist += '\n' + cmd + ': ' + QS_cmds[cmd][0];
	}
	alert( cmdlist );
}

function QS_search() {
	var statement = window.prompt( 'Type `help` for a list of commands:' );
	if( !statement ) {
		return; 
	}
	var words = statement.split( ' ' );
	var cmd = words.shift();
	var args = words.join( ' ' );
	if( cmd == 'help' ) {
		QS_listCommands();
	} else if( typeof QS_cmds[cmd] != 'undefined' ) {
		window.location.href = QS_cmds[cmd][1].replace( '%s', escape(args) );
	} else {
		alert( 'The command ' + cmd + ' is not defined!' );
	}
}
QS_search();
