// Open Links plugin for Ubiquity by Vladimir Prelovac
// http://www.prelovac.com/vladimir
// version
// 0.3 





// previously selected href
var lasthref=undefined;
var lasthrefcss=undefined;


// define custom type for the link
var noun_type_link = {
  _name: "link name",

  // returns all links on the page
  getLinks: function(){ 
  	// get the doc yikes
		var doc = Application.activeWindow.activeTab.document;  
    var links = {};

    for(var i=0;i<doc.links.length;i++) {
             //CmdUtils.log(doc.links[i].href);
             links[doc.links[i].text]=doc.links[i].href;                                               
    } 

    return links;
  },

  suggest: function( text, html ) {
    
    var suggestions  = [];
    var links = noun_type_link .getLinks();
   
    for ( var linkName in links ) {      
      if (linkName.match(text, "i"))
	 suggestions.push( CmdUtils.makeSugg(linkName) );
    }

    suggestions.push( CmdUtils.makeSugg(text));

    // return a list of input objects, limited to at most five
    return suggestions.splice(0, 5);
  }
}


CmdUtils.CreateCommand({
  name: "open",
  homepage: "http://www.prelovac.com/vladimir/ubiquity-plugins/open-links",
  author: { name: "Vladimir Prelovac", email: "vprelovac@gmail.com"},
  locale: "en-US",
  license: "MPL",
  description: "Opens URL or a link on a page.",
  help: "Navigate the Web with your keyboard! Just type the name of the link on the page or an URL you want to visit.",
  icon: "chrome://ubiquity/content/icons/tab_go.png",
  takes: {"link name": noun_type_link},

preview: function( pblock, theLink ) {
   
   // do nothing if empty
   if (theLink.text=='')  return;
   var str='';
   
   var doc = Application.activeWindow.activeTab.document;    
   var links = noun_type_link.getLinks();
   var href=links[theLink.text];
   if (!href)   
      href=theLink.text;
    
   if (lasthref!=undefined && lasthref!=href)
   {      
       //CmdUtils.log('returning '+lasthref);
       jQuery(doc.body).find('a[href$='+lasthref+']').css({"background-color": lasthrefcss}); 
   }   
   lasthref=href;
   lasthrefcss=jQuery(doc.body).find('a[href$='+href+']').css('background-color');

    //CmdUtils.log('replacing '+href);
   jQuery(doc.body).find('a[href$='+href+']').css({"background-color": "yellow"});  

   str+='Opens <span style=\"color:yellow\">'+href+'</span><br>';                              

   pblock.innerHTML = str;
},

execute: function(theLink) {
        
        var doc = Application.activeWindow.activeTab.document;
        var links = noun_type_link.getLinks();  
                 
        // if the link is from the page
        if (links[theLink.text])                
          // open the link in current window 
          doc.location.href = links[theLink.text];

        // else open the typed link
        else {
            var link=theLink.text;

            if (!link.match("http://", "i"))
               link="http://"+link.text;
            
            doc.location.href = link;
        }
}
});

