/* 
 * Planetaki Ubiquity Verbs
 * 
 * 2008-08-28 Sam Lown
 * 
 * Based on James Simm's Add to google reader command:
 * http://jimmahdigital.com/ubiq/
 * 
 */

CmdUtils.CreateCommand({
  name: "planetaki",
  icon: "http://www.planetaki.com/favicon.ico",
  homepage: "http://www.planetaki.com",
  author: {name: "Samuel Lown", email: "sam@planetaki.com"},
  preview: "Add to my planet",
  contributors: ["Javier Cañada"],
  license: "MPL",
  description: "Quickly add the current news website to your planet on Planetaki.",
  help: "This adds the current website to your planet",
  preview: function(pblock) {
    var msg = '';
    var feed = this.detectLinks();
    
    if (!feed) {
      msg = 'Website cannot be read by Planetaki';
    } else {
      msg = 'Add <i>"' + feed.title + '"</i> to your Planet';
    }
    
    pblock.innerHTML = CmdUtils.renderTemplate(msg);
  },  
  execute: function() {
    var feed = this.detectLinks();
    if (!feed) { 
      displayMessage("Cannot be read by Planetaki");
    } else {
      var url = "http://www.planetaki.com/subscription/add";
      
      url = url + "?url=" + feed.url
      
      // openUrl(url);
      Utils.openUrlInBrowser(url);
    }
  },
  detectLinks: function() {
    var feeds = jQuery("link[rel='alternate']", context.focusedWindow.document);
    
    if (!feeds || feeds.length <= 0) {
      return false;
    }
    
    // return {url:jQuery(feeds[0]).attr("href"), title:jQuery(feeds[0]).attr("title")};
    return {url: context.focusedWindow.document.location, title: context.focusedWindow.document.title}
  }

})
