/*global window, document, navigator, location, ActiveXObject */ /*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 createaud(e, div, sfx) {
    var obj, uri, ns;
    uri = div.href;
    ns = document.createElementNS ? div.namespaceURI : false;
    obj = createElement(ns, 'audio');
    obj.className = "audiojs1";
    obj.src = sfx ? uri + sfx : uri;
    //obj.src += "xxx"; // this line for testing onerror condition
    obj.title = div.title;
    obj.controls = "controls";
    obj.onerror = function () {window.addEventListener('unload', function () {}, false); location.href = uri; };
    div.parentNode.replaceChild(obj, div);
    obj.play();
    if (e && e.preventDefault) {e.preventDefault(); }
    return false;
  }
  function CreateMarkAud(e) {
    return createaud(e, this);
  }
  function CreateMarkOgg(e) {
    return createaud(e, this, ".ogg");
  }

  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, audmp3supported, aud3, audoggsupported;
    if (initdone) {return; }
    initdone = true;
    if ((!document.getElementById) || (!document.createElement && !document.createElementNS)) {return; }
    aud3 = document.createElement('audio');
    audmp3supported = aud3 && aud3.canPlayType && aud3.canPlayType('audio/mpeg');
    audoggsupported = aud3 && aud3.canPlayType && aud3.canPlayType('audio/ogg');
    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.substr(0, 6) === "repcvt") {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.substr(0, 3) === "rep") {
        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 (audmp3supported && (ai.id.substr(3, 1) === "a" || ai.id.substr(3, 1) === "o")) {
            ai.addEventListener('click', CreateMarkAud, false);
          } else if (audoggsupported && ai.id.substr(3, 1) === "o") {
            ai.addEventListener('click', CreateMarkOgg, false);
          } else if (mp3supported) {
            ai.addEventListener('click', CreateMarkMp3, false);
          } else if (mp2mp3) {
            ai.addEventListener('click', CreateMarkMplayer2, false);
          }
        }
      }
    }
  }

  function createMidiIE4(ai, player, width, height) {
    var clsids, filenames, starts, autostart = "true", endtag = (document.body.nodeName === "body") ? "/>" : ">";
    switch (player) {
    case 0: clsids = "6BF52A52-394A-11D3-B153-00C04F79FAA6"; filenames = "url"; starts = "AutoStart"; break;
    case 1: clsids = "22d6f312-b0f6-11d0-94ab-0080c74c7e95"; filenames = "Filename"; starts = "AutoStart"; break;
    case 3: clsids = "CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"; filenames = "src"; starts = "AutoStart"; break;
    default: clsids = "02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"; filenames = "src"; starts = "autoplay";
    }
    ai.outerHTML = "<object width='" + width + "' height='" + height + "' title='" + ai.title +
      "' classid='clsid:" + clsids + "'>" +
      "<param name='" + filenames + "' value='" + ai.href + "'" + endtag +
      "<param name='" + starts + "' value='" + autostart + "'" + endtag +
      "<a href='" + ai.href + "'>Play</a>" +
      "</object>";
    return false;
  }
  function clickfun(aiid, player, width, height) {
    return function () {
      return createMidiIE4(document.getElementById(aiid), player, width, height);
    };
  }
  function initIEMid4() {
    var a, ai, h, i, whichplayer, x = null, aud3, audmp3supported,
      objects = ["WMPlayer.OCX.7",
        "MediaPlayer.MediaPlayer.1",
        "QuickTimeCheckObject.QuickTimeCheck.1",
        "rmocx.RealPlayer G2 Control.1"];
    aud3 = document.createElement('audio');
    audmp3supported = aud3 && aud3.canPlayType && aud3.canPlayType('audio/mpeg');
    for (whichplayer = 0; whichplayer < objects.length; whichplayer += 1) {
      /*@cc_on @*/ /*@if (@_jscript_version < 5.6) continue;
      @else @*/
      try {x = new ActiveXObject(objects[whichplayer]); } catch (e) {x = null; }
      /*@end @*/
      if (x) {break; }
    }
    if (whichplayer >= objects.length) {return; }
    a = document.getElementsByTagName('a');
    h = (whichplayer === 2) ? "16" : "60";
    for (i = 0; i < a.length; i += 1) {
      ai = a[i];
      if (whichplayer === 3 && ai.id.substr(0, 6) === "repcvt") {ai.href += ".mp3"; }
      if (audmp3supported && (ai.id.substr(0, 4) === "repa" || ai.id.substr(0, 4) === "repo") && /\.mp3$/.test(ai.href)) {
        ai.addEventListener('click', CreateMarkAud, false);
      } else if (/\.(mid|mp3)$/.test(ai.href)) {
        if (ai.id.substr(0, 3) === "rep" && (whichplayer !== 3 || !/\.mid$/.test(ai.href))) {
          ai.attachEvent('onclick', clickfun(ai.id, whichplayer, "120", h));
        }
      }
    }
  }

  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);
    }
  } else if (document.getElementsByTagName && window.ActiveXObject) {
    if (document.getElementsByTagName('a').length) {
      initIEMid4();
    } else if (document.addEventListener) {
      document.addEventListener("DOMContentLoaded", initIEMid4, false);
    } else if (window.attachEvent) {window.attachEvent("onload", initIEMid4); }
  }
}());

