/*global window, document, navigator */ /*jslint indent : 2 */
(function () {
  "use strict";
  var safaritype = 0, midheight = "60", mp3height = "60", initdone = false;
  /* see http://simonwillison.net/2003/Jun/15/javascriptWithXML/ for explanation of following function */
  function createElement(ns, element) {
    return ns ? document.createElementNS(ns, element) : document.createElement(element);
  }
  function createMarkup(div, mimetype, auto, autoon, autooff, width, height, e) {
    var obj, oldsafari, newid, uri, ns, p_m, p_2, textnode, a, clickhere;
    if (safaritype === 0) {
      if ((!navigator.vendor) || (navigator.vendor.indexOf('Apple') === -1)) {
        safaritype = 1;
      } else {
        window.addEventListener("unload", function () {}, false);
        oldsafari = navigator.userAgent.indexOf('Safari');
        if (oldsafari === -1) {
          safaritype = 2;
        } else if (parseInt(navigator.userAgent.substring(oldsafari + 7, navigator.userAgent.length), 10) < 312) {
          safaritype = 3;
        } else {safaritype = 4; }
      }
    }
    newid = div.id + "noautoplay";
    uri = div.href;
    ns = document.createElementNS ? div.namespaceURI : false;
    if (safaritype === 3) { /* safari 10.2.8 can't handle javascript dom, but can handle innerHTML */
      if (!document.links) {return true; }
      obj = createElement(ns, "span");
      obj.innerHTML = "<object type='" + mimetype + "' data='" + uri + "' height='" + height + "' width='" + width +
        "' title='" + div.title + "'><param id='" + newid + "' name='" + auto + "' value='" + autoon + "'><\/object>";
      div.parentNode.replaceChild(obj, div);
      return false;
    }
    obj = createElement(ns, 'object');
    obj.width = width;
    obj.height = height;
    obj.data = uri;
    obj.type = mimetype;
    obj.title = div.title;
    p_m = createElement(ns, 'param');
    p_m.setAttribute('name', auto);
    p_m.setAttribute('value', autoon);
    p_m.id = newid;
    obj.appendChild(p_m);
    if ((safaritype !== 1) || (mimetype === "application/x-mplayer2")) {
      p_2 = createElement(ns, 'param');
      p_2.setAttribute('name', 'src');
      p_2.setAttribute('value', uri);
      obj.appendChild(p_2);
    }
    textnode = document.createTextNode("Click to ");
    obj.appendChild(textnode);
    a = createElement(ns, 'a');
    a.setAttribute('href', uri);
    clickhere = document.createTextNode("play");
    a.appendChild(clickhere);
    obj.appendChild(a);
    div.parentNode.replaceChild(obj, div);
    if (safaritype === 1) {
      if (navigator.platform.indexOf("Mac") === -1 && navigator.platform.indexOf("Win") === -1) {
        window.addEventListener("unload", function () {}, false);
      } else {window.setTimeout('document.getElementById("' + newid + '").value="' + autooff + '"', 1000); }
    }
    if (e && e.preventDefault) {e.preventDefault(); }
    return false;
  }

  function CreateMarkMid(e) {return createMarkup(this, 'audio/x-midi', 'autoplay', 'true', 'false', '120', midheight, e); }
  function CreateMarkMp3(e) {return createMarkup(this, 'audio/mpeg', 'autoplay', 'true', 'false', '120', mp3height, e); }
  function CreateMarkMplayer2(e) {return createMarkup(this, 'application/x-mplayer2', 'autostart', '1', '0', '120', '60', e); }
  function initOthMid() {
    var i, midsupported, mp3supported, mp2supported, a, mp2mid, mp2mp3, mp2suf, ai;
    if (initdone) {return; }
    initdone = true;
    if ((!document.getElementById) || (!document.createElement && !document.createElementNS)) {return; }
    midsupported = navigator.mimeTypes['audio/x-midi'];
    if (midsupported) {midsupported = midsupported.enabledPlugin; }
    if (midsupported && midsupported.name.indexOf("QuickTime Plug-in") !== -1) {midheight = "16"; }
    mp3supported = navigator.mimeTypes['audio/mpeg'];
    if (mp3supported) {mp3supported = mp3supported.enabledPlugin; }
    if (mp3supported && mp3supported.name.indexOf("QuickTime Plug-in") !== -1) {mp3height = "16"; }
    mp2supported = navigator.mimeTypes['application/x-mplayer2'];
    if (mp2supported && mp2supported.enabledPlugin) {
      mp2suf = mp2supported.suffixes;
      mp2mid = (/\*/.test(mp2suf) || /mid/.test(mp2suf));
      mp2mp3 = (/\*/.test(mp2suf) || /mp3/.test(mp2suf)); // alert("mp2mid/mp3mid "+mp2mid+" "+mp2mp3);
    }
    a = document.getElementsByTagName('a');
    if (!midsupported && !mp2mid) {
      for (i = 0; i < a.length; i += 1) {
        ai = a[i];
        if (ai.id.indexOf("repcvt") > -1) {ai.href = ai.href + ".mp3"; }
      }
    }
    if ((navigator.platform.indexOf("Mac") === -1) &&
        (navigator.platform.indexOf("Win") === -1) &&
        (navigator.platform.indexOf("Linux") === -1)
        ) {return; }
    if (navigator.vendor === "KDE") {return; }
    for (i = 0; i < a.length; i += 1) {
      ai = a[i];
      if (ai.id.indexOf("rep") > -1) {
        if (/\.mid$/.test(ai.href)) {
          if (midsupported) {
            ai.addEventListener('click', CreateMarkMid, false);
          } else if (mp2mid) {
            ai.addEventListener('click', CreateMarkMplayer2, false);
          }
        } else if (/\.mp3$/.test(ai.href)) {
          if (mp3supported) {
            ai.addEventListener('click', CreateMarkMp3, false);
          } else if (mp2mp3) {
            ai.addEventListener('click', CreateMarkMplayer2, false);
          }
        }
      }
    }
  }

  if (document.getElementsByTagName && navigator.mimeTypes && navigator.mimeTypes.length && window.addEventListener) {
    if (document.getElementsByTagName('a').length) {
      initOthMid();
    } else {
      document.addEventListener("DOMContentLoaded", initOthMid, false);
      window.addEventListener("load", initOthMid, false);
    }
  }
}());

