Components.utils.import("resource://gre/modules/Microformats.js");

CmdUtils.CreateCommand({
    name: "get-atom",
    icon: "http://transformr.co.uk/favicon.ico",
    author: {name: "Martin McEvoy", email: "weborganics@googlemail.com"},
    help: "Subscribe to Atom feeds using the <a href=\"http://microformats.org/wiki/hatom\">hAtom</a> Microformat.",
    preview: function(pblock) {
    doc = Application.activeWindow.activeTab.document;
      var entry = doc.getElementsByClassName('hentry');
        if (entry.length < 1) {
           pblock.innerHTML ="<p>Sorry no <a style=\"color:#679A06;\" href=\"http://microformats.org/wiki/hatom\">hAtom</a> found here!</p>";
      }else{
      pblock.innerHTML ="<p>Summary of <a style=\"color:#679A06;\" href=\"http://microformats.org/wiki/hatom\">hAtom</a> to Atom feed</p>";
      var href = "http://transformr.co.uk/hatom/";
      href += CmdUtils.getWindowInsecure().location;
       jQuery.ajax({
       async: false,
       type: "GET",
       url: href,
       dataType: "xml",
       success: function(xml) {
       xsltProcessor = new window.XSLTProcessor();
       xsl = '<?xml version="1.0" encoding="utf-8"?>' +
       '<xsl:stylesheet version="1.0"' +
       '     xmlns:atom="http://www.w3.org/2005/Atom"' +
       '     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"' +
       '     xmlns:dc="http://purl.org/dc/elements/1.1/">' +
       '     <xsl:template match="/atom:feed">' +
       '     <p title="Click To Subscribe"><a style="color:#679A06" href="http://transformr.co.uk/hatom/{atom:id}"><xsl:value-of select="atom:title[1]"/></a></p>'+
       '         <ul style="list-style-type:none;">' +
       '             <xsl:apply-templates select="atom:entry"/>' +
       '         </ul>' +
       '     </xsl:template>' +
       '     <xsl:template match="atom:entry">' +
       '         <li><a href="{atom:link/@href}"><xsl:value-of select="atom:title"/></a></li>' +
       '     </xsl:template>' +
       '</xsl:stylesheet>';
        xslDocument = new window.DOMParser().parseFromString(xsl, 'application/xml');
        xsltProcessor.importStylesheet(xslDocument);
          var result = xsltProcessor.transformToFragment(xml, pblock.ownerDocument);
          pblock.appendChild(result);
        }
      });
    }
  },
    execute: function(statusText) {}
})

CmdUtils.CreateCommand({
  icon: "http://transformr.co.uk/favicon.ico",
  name: "get-vcard",
  author: {name: "Martin McEvoy", email: "weborganics@googlemail.com"},
  help: "Transforms the <a href=\"http://microformats.org/wiki/hcard\">hCard</a> Microformat to a vCard file.",
  preview: function ( pblock ) {
  pblock.innerHTML = "Download vCard files to your address book"; 
   },
  execute: function() {
  var doc = Application.activeWindow.activeTab.document;
  var mFcount = Microformats.count('hCard', doc,{ showHidden : true });
  if (mFcount > 0) {
    var url = "http://transformr.co.uk/hcard/";
     url += CmdUtils.getWindowInsecure().location ;
     Utils.openUrlInBrowser(url);
  }else{
    displayMessage('Sorry No hCards Found!');
  }
}
})

CmdUtils.CreateCommand({
  icon: "http://transformr.co.uk/favicon.ico",
  name: "get-webcal",
  author: {name: "Martin McEvoy", email: "weborganics@googlemail.com"},
  help: "Subscribe to a webcal feed using the <a href=\"http://microformats.org/wiki/hcalendar\">hCalendar</a> Microformat.",
  preview: function ( pblock ) {
  pblock.innerHTML = "Subscribe to web calendar"; 
   },
  execute: function() {
  var doc = Application.activeWindow.activeTab.document;
  var mFcount = Microformats.count('hCalendar', doc,{ showHidden : true });
  if (mFcount > 0) {
    var url = "webcal://transformr.co.uk/hcalendar/";
     url += CmdUtils.getWindowInsecure().location ;
     Utils.openUrlInBrowser(url);
  }else{
    displayMessage('Sorry No hCalendar Events Found!');
  }
}
})

CmdUtils.CreateCommand({
  icon: "http://transformr.co.uk/favicon.ico",
  name: "get-podcast",
  author: {name: "Martin McEvoy", email: "weborganics@googlemail.com"},
  help: "Transforms the <a href=\"http://microformats.org/wiki/haudio\">hAudio</a> Microformat to RSS2 podcast.",
  preview: function ( pblock ) {
    doc = Application.activeWindow.activeTab.document;
      var audio = doc.getElementsByClassName('haudio');
        if (audio.length < 1) {
           pblock.innerHTML ="<p>Sorry no <a style=\"color:#679A06;\" href=\"http://microformats.org/wiki/haudio\">hAudio</a> found here!</p>";
        }else{
        pblock.innerHTML ="<p>Hit enter for a <a style=\"color:#679A06;\" href=\"http://microformats.org/wiki/haudio\">hAudio</a> to RSS2 Podcast.</p>";
      }
   },
   execute: function(){
   var url = "http://transformr.co.uk/haudio-rss2/";
   url += CmdUtils.getWindowInsecure().location ;
   Utils.openUrlInBrowser(url);
  } 
})
