/*
 * This is a prototype script, here are some thoughts
 * for future development : 
 * 
 * 1. The uri building technique in execute() is weak. 
 *    Would be better to call click() on the jQuery obj. 
 *    My initial attempt at that failed.
 * 2. a:contains() selector is case sensitive - should replace.
 * 3. noun_type_clickable should really apply to form 
 *    buttons and other 'clickables'
 * 4. Could also search alt tags of images within anchors to
 *    make those clickable?
 */
var noun_type_clickable = {
	_name: "Text link",
	suggest: function(text, html){
	var suggestions = [];
	var doc =  CmdUtils.getDocument();
	jQuery(doc.body).find("a:contains('" + text + "')").each(function(i){
		var item = jQuery(this);
		suggestions.push(CmdUtils.makeSugg(item.text()));
	});
	return suggestions.splice(0, 8);
	}
}

CmdUtils.CreateCommand({
	name : "click",
	homepage: "http://ethanmiller.name/notes/ubiquity_click/",
	author: { name: "Ethan Miller", email: "thnntn@gmail.com"},
	license: "MPL",
	description: "Finds links to click.",
	takes : {"link text" : noun_type_clickable},
	help: "Start typing text that matches links on the page, then hit return to click.",
	preview : function(pblock, link_txt){
		pblock.innerHTML = "Click on link with text : " + link_txt.text;
	},
	execute : function(link_txt){
		var win = CmdUtils.getWindow();
		var doc = CmdUtils.getDocument();
		var item = jQuery(doc.body).find("a:contains('" + link_txt.text + "')").eq(0);
		var uri = Utils.url({uri : item.attr('href'), base : win.location.protocol + '//' + win.location.host})
		Utils.openUrlInBrowser(uri.spec);
	}
})

