/**
 * hAudio 0.9.1.
 *
 * see: http://microformats.org/wiki/haudio
 */
function hAudio(node) {
  if (node) {
    Microformats.parser.newMicroformat(this, node, "hAudio");
  }
}
hAudio.prototype.toString = function() {
  return this["fn"];
}

var hAudio_definition = {
  mfVersion:0.1,
  mfObject: hAudio,
  className: "haudio",
  required: ["fn"],
  properties: {
    "fn" : {
      required: true
    },
    "contributor" : {
      plural: true,
      subproperties : {
        "vcard" : {
          virtual: true,
          datatype: "microformat",
          microformat: "hCard"
        }
      }
    },
    "duration" : {
      datatype: "dateTime"
    },
    "published" : {
      datatype: "dateTime"
    },
    "category" : {
      plural: true
    },
    "price" : {
      plural: true,
      subproperties : {
        "money" : {
          virtual: true,
          datatype: "microformat",
          microformat: "money"
        }
      }
    },
    "enclosure" : {
      plural: true,
      rel: true,
      subproperties: {
        "link" : {
          virtual: true,
          datatype: "anyURI"
        },
        "text" : {
          virtual: true
        }
        // FIXME: add "type"
      }
    },
    "sample" : {
      plural: true,
      rel: true,
      subproperties: {
        "link" : {
          virtual: true,
          datatype: "anyURI"
        },
        "text" : {
          virtual: true
        }
        // FIXME: add "type"
      }
    },
    "payment" : {
      plural: true,
      rel: true,
      subproperties: {
        "link" : {
          virtual: true,
          datatype: "anyURI"
        },
        "text" : {
          virtual: true
        }
        // FIXME: add "type"
      }
    },
    "photo" : {
      plural: true,
      subproperties: {
        "link" : {
          virtual: true,
          datatype: "anyURI"
        },
        "text" : {
          virtual: true
        }
      }
    }
  }
};

Microformats.add("hAudio", hAudio_definition);

/* Figure out a guess for artist */
function hAudioArtist(node) {
  var fn = null;
  var contributors = node["contributor"];
  if (contributors) {
    // Find first contributor with "artist" role
    for (var c = 0; fn == null && c < contributors.length; c++) {
      var contributor = contributors[c];
      if (contributor.vcard && contributor.vcard.role) {
        for (var r = 0; fn == null && r < contributor.vcard.role.length; r++) {
          if (contributor.vcard.role[r].toLowerCase() == "artist") {
            // vcard is invalid without fn
            if (contributor.vcard.fn) {
              fn = contributor.vcard.fn;
            }
          }
        }
      }
    }
    // Default to first contributor if no artist present
    if (fn == null && contributors.length > 0) {
      fn = contributors[0].vcard.fn;
    }
  }
  return fn;
}

/* Join title and creator, if available, into a encoded string suitable
 * for use in a URI */
function encodedTitleAndArtist(node, quote) {
  if (!quote) {
    quote = "";
  }
  var str = quote + node["fn"] + quote;
  var fn = hAudioArtist(node);
  if (fn) {
    str += " " + quote + fn + quote;
  }
  return encodeURIComponent(str);
}

var audio_sample = {
  description: "Listen to audio sample",
  shortDescription: "Sample",
  scope: {
    semantic: {
      "hAudio" : "sample"
    }
  },
  doAction: function(semanticObject, semanticObjectType, propertyIndex) {
    var propertyIndex = (propertyIndex == null) ? 0 : propertyIndex;
    return semanticObject.sample[propertyIndex].link;
  },
  getActionName: function(semanticObject, semanticObjectType, propertyIndex) {
    var propertyIndex = (propertyIndex == null) ? 0 : propertyIndex;
    var text = semanticObject.sample[propertyIndex].text;
    if (!text) {
      text = "Sample #" + (propertyIndex + 1);
    }
    return text;
  }
};
SemanticActions.add("audio_sample", audio_sample);

var audio_download = {
  description: "Download audio",
  shortDescription: "Download",
  scope: {
    semantic: {
      "hAudio" : "enclosure"
    }
  },
  doAction: function(semanticObject, semanticObjectType, propertyIndex) {
    var propertyIndex = (propertyIndex == null) ? 0 : propertyIndex;
    return semanticObject.enclosure[propertyIndex].link;
  },
  getActionName: function(semanticObject, semanticObjectType, propertyIndex) {
    var propertyIndex = (propertyIndex == null) ? 0 : propertyIndex;
    var text = semanticObject.enclosure[propertyIndex].text;
    if (!text) {
      text = "Download #" + (propertyIndex + 1);
    }
    return text;
  }
};
SemanticActions.add("audio_download", audio_download);

var audio_buy = {
  description: "Buy audio",
  shortDescription: "Buy",
  scope: {
    semantic: {
      "hAudio" : "payment"
    }
  },
  doAction: function(semanticObject, semanticObjectType, propertyIndex) {
    var propertyIndex = (propertyIndex == null) ? 0 : propertyIndex;
    return semanticObject.payment[propertyIndex].link;
  },
  getActionName: function(semanticObject, semanticObjectType, propertyIndex) {
    var propertyIndex = (propertyIndex == null) ? 0 : propertyIndex;
    var text = semanticObject.payment[propertyIndex].text;
    if (!text) {
      text = "Location #" + (propertyIndex + 1);
    }
    return text;
  }
};
SemanticActions.add("audio_buy", audio_buy);

var audio_search_google = {
  description: "Find audio on Google",
  shortDescription: "Google",
  icon: "http://www.google.com/favicon.ico",
  scope: {
    semantic: {
      "hAudio" : "fn"
    }
  },
  doAction: function(semanticObject, semanticObjectType) {
    var url = "http://www.google.com/search?q=";
    var str = encodedTitleAndArtist(semanticObject, "\"");
    return url + str;
  }
};
SemanticActions.add("audio_search_google", audio_search_google);

var audio_search_musicbrainz = {
  description: "Find audio on MusicBrainz",
  shortDescription: "MusicBrainz",
  icon: "http://musicbrainz.org/favicon.ico",
  scope: {
    semantic: {
      "hAudio" : "fn"
    }
  },
  doAction: function(semanticObject, semanticObjectType) {
    var url = "http://musicbrainz.org/search/textsearch.html?query=";
    var title = semanticObject["fn"];
    url += encodeURIComponent("\"" + title + "\"");
    var fn = hAudioArtist(semanticObject);
    if (fn) {
      url += encodeURIComponent(" AND artist:\"" + fn + "\"");
    }
    url += "&type=track&adv=on&handlearguments=1"
    return url;
  }
};
SemanticActions.add("audio_search_musicbrainz", audio_search_musicbrainz);

var audio_search_skreemr = {
  description: "Find audio on Skreemr",
  shortDescription: "SkreemR",
  icon: "http://skreemr.com/favicon.ico",
  scope: {
    semantic: {
      "hAudio" : "fn"
    }
  },
  doAction: function(semanticObject, semanticObjectType) {
    var url = "http://skreemr.com/results.jsp?q=";
    var str = encodedTitleAndArtist(semanticObject, "\"");
    return url + str;
  }
};
SemanticActions.add("audio_search_skreemr", audio_search_skreemr);


var extract_podcast = {
  description: "Extract Podcast",
  scope: {
    semantic: {
      "hAudio" : "hAudio"
    }
  },
  doAction: function(semanticObject, semanticObjectType) {
    if (semanticObjectType == "hAudio") {
      var url = "http://microform.at/?type=haudio-rss&url=";
      var pageurl;
      if (semanticObject.node.ownerDocument) {
        pageurl = semanticObject.node.ownerDocument.URL;
      } else {
        pageurl = semanticObject.node.URL;
      }
      url += encodeURIComponent(pageurl);
      return url;
    }
  }
};
SemanticActions.add("extract_podcast", extract_podcast);
