//v1.0
//embedded media player

function MediaPlayer(id) {
  this.id = id;
  this.html = document.getElementById(this.id);
  this.mediaFormat = 'wmv';
  this.framed = false;
  this.autostart = true;
  this.externalIcon = false;
  this.embeddedExtraTop = 20;
  this.embeddedMediaDimensions = [360, 288, 512, 288, 440, 352];
  
  this.hiddenPageElements = new Array();

  this.mpIconEmp = '/binaries/common/button/mediaplayer_btn_emp.gif';
  this.mpIconExt = '/binaries/common/button/mediaplayer_btn_ext.gif';
  this.mpIconCls = '/binaries/common/button/mediaplayer_btn_cls.gif';

  this.externalTemplate = '<player />';

  this.frameTemplate = '<div style="width: <width+6 />px;" class="mediaPlayer">\
      <div style="width: 134px; height: 19px; background-image: url(/binaries/img/mediaplayer_bg_tl.gif);"></div>\
      <div style="width: <width-218 />px; height: 19px; background-image: url(/binaries/img/mediaplayer_bg_tc.gif);"></div>\
      <div class="icons" id="<id />MediaPlayerIcons" style="width: 90px; height: 19px; background-image: url(/binaries/img/mediaplayer_bg_tr.gif);"></div>\
      <div class="edge" style="width: 3px; height: <height />px; background-image: url(/binaries/img/mediaplayer_bg_ml.gif);"></div>\
      <div id="<id />MediaPlayer" style="width: <width />px; height: <height />px;"><player /></div>\
      <div style="width: 3px; height: <height />px; background-image: url(/binaries/img/mediaplayer_bg_mr.gif);"></div>\
      <div class="edge" style="width: 23px; height: 4px; background-image: url(/binaries/img/mediaplayer_bg_bl.gif);">&nbsp;</div>\
      <div style="width: <width-40 />px; height: 4px; background-image: url(/binaries/img/mediaplayer_bg_bc.gif);">&nbsp;</div>\
      <div style="width: 23px; height: 4px; background-image: url(/binaries/img/mediaplayer_bg_br.gif);">&nbsp;</div>\
    </div>';

  this.noPlayerTemplate = '<div class="noPlayerInfo" style="width: <width />px; height: <height />px;"><div>zainstaluj Windows Media Player lub RealPlayer,<br />by obejrzeć wideo</div></div>';
  this.noPlayerControlsHeight = 0;
  this.noWmvPlayerTemplate = '<div class="noPlayerInfo" style="width: <width />px; height: <height />px;"><div>zainstaluj Windows Media Player,<br />by obejrzeć wideo</div></div>';
  this.noWmvPlayerControlsHeight = 0;
  this.noRealPlayerTemplate = '<div class="noPlayerInfo" style="width: <width />px; height: <height />px;"><div>zainstaluj RealPlayer,<br />by obejrzeć wideo</div></div>';
  this.noRealPlayerControlsHeight = 0;

  this.wmvControlsHeight = document.all?64:68;
  this.wmvTemplate = '\
    <object width="<width />" height="<height />"\
        classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"\
        codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715"\
        standby="Loading Microsoft Windows Media Player components..."\
        type="application/x-oleobject">\
      <param name="windowlessVideo" value="true">\
      <param name="stretchToFit" value="true">\
      <param name="currentposition" value="<currentPosition />">\
      <param name="playCount" value="<playCount />">\
      <param name="autostart" value="<autostart />">\
      <param name="url" value="<src />">\
      <param name="showcontrols" value="true">\
      <param name="showstatusbar" value="true">\
      <embed type="application/x-mplayer2"\
        pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"\
        src="<src />"\
        width="<width />"\
        height="<height />"\
        windowlessVideo="1"\
        stretchToFit="true"\
        currentposition="<currentPosition />"\
        playCount="<playCount />"\
        autostart="<autostart />"\
        showcontrols="1"\
        showstatusbar="1">\
      </embed>\
    </object>';
  
  this.realControlsHeight = 30;
  this.realTemplate = '\
    <object id="<id />RealVideoO" width="<width />" height="<heightVideo />"\
        classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA">\
      <param name="controls" value="ImageWindow">\
      <param name="nologo" value="true">\
      <param name="console" value="Clip1">\
      <param name="autostart" value="false">\
      <embed id="<id />RealVideoO" type="audio/x-pn-realaudio-plugin"\
        console="Clip1"\
        controls="ImageWindow"\
        width="<width />"\
        height="<heightVideo />"\
        nologo="true"\
        autostart="false">\
      </embed>\
    </object><br />\
    <object id="<id />RealControl" width="<width />" height="<heightControls />"\
        classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA">\
      <param name="controls" value="ControlPanel">\
      <param name="console" value="Clip1">\
      <param name="autostart" value="<autostart />">\
      <param name="src" value="<src />">\
      <embed type="audio/x-pn-realaudio-plugin"\
        src="<src />"\
        console="Clip1"\
        controls="ControlPanel"\
        width="<width />"\
        height="<heightControls />"\
        autostart="<autostart />"\
        nojava="true">\
      </embed>\
    </object>';

  this.nop = nop;
  this.writePlayer = writePlayer;
  this.writeBarkerPlayer = writeBarkerPlayer;
  this.openExternalEmbeddedPlayer = openExternalEmbeddedPlayer;
  this.openPlayerInLayer = openPlayerInLayer;
  this.coverBackground = coverBackground;
  this.uncoverBackground = uncoverBackground;
  
//nic
  function nop() {}

//osadzanie playera
  function writePlayer(src, width, height, currentPosition, playCount) {
    var icons, icon;
    if(currentPosition == undefined)
      currentPosition = 0;
    if(playCount == undefined)
      playCount = 1;
    if(width == undefined)
      width = this.embeddedMediaDimensions[0];
    if(height == undefined)
      height = this.embeddedMediaDimensions[1];
    document.write(getPreparedPlayerObject(this, src, width, height, currentPosition, playCount));
    icons = document.getElementById(this.id+'MediaPlayerIcons');
    if(icons != undefined) {
      icon = document.createElement('img');
      icon.className = 'edge';
      icon.src = this.mpIconEmp;
      icons.appendChild(icon);
      if(this.externalIcon) {
        icon = document.createElement('img');
        icon.className = 'button';
        icon.src = this.mpIconExt;
        icon.onclick = new Function('event', this.id+'.openExternalEmbeddedPlayer();');
        icons.appendChild(icon);
      }
    }
  }
  
//osadzanie playera barkera
  function writeBarkerPlayer(src, width, height) {
    if(width == undefined)
      width = this.embeddedMediaDimensions[4];
    if(height == undefined)
      height = this.embeddedMediaDimensions[5];
    var req = extractReqParams(window.location.href);
    try {
      if(req.p2.match(/^\d{1,2}(:\d{1,2})?$/)) {
        req.p2 = req.p2.split(':');
        if(req.p2.length == 1)
          req.p1 = (parseFloat(req.p2[0])*60)+'';
        else
          req.p1 = (parseFloat(req.p2[0])*60+parseFloat(req.p2[1]))+'';
      }
    } catch(ex) {}
    try {
      if(!req.p1.match(/^\d{1,4}$/))
        req.p1 = 0;
    } catch(ex) {
      req.p1 = 0;
    }
    try {
      if(req.p3 == undefined) {
        req.p3 = false;
      }
    } catch(ex) {
      req.p3 = false;
    }
    if(document.all && req.p3)
      document.write('<img id="'+this.id+'PlayerImgLid" style="position: absolute; margin-top: 60px;" alt="" src="'+req.p3+'"/>');
    document.write('<div id="'+this.id+'barkerPlayer">');
    this.writePlayer(src, width, height, req.p1, 999);
    document.write('</div><div>&nbsp;</div>');
    document.getElementById(this.id+'barkerPlayer').childNodes[0].id = this.id+'barkerPlayerObject';
    document.writeln('<script language="JavaScript" type="text/javascript" for="'+this.id+'barkerPlayerObject" event="playStateChange(ps)">');
    document.writeln('try {');
    document.writeln('if(document.getElementById("'+this.id+'barkerPlayerObject").started) {');
    document.writeln('if(ps == 3) document.getElementById("'+this.id+'PlayerImgLid").style.visibility = "hidden";');
    document.writeln('} else {');
    document.writeln('if(ps == 3) document.getElementById("'+this.id+'barkerPlayerObject").started = true;');
    document.writeln('}');
    document.writeln('} catch(ex) {}');
    document.writeln('</script>');
    try {
      if(document.getElementById(this.id+'barkerPlayer').childNodes[0].playState != undefined) {
        if(document.getElementById(this.id+'barkerPlayer').childNodes[0].playState == 3)
          document.getElementById(this.id+'PlayerImgLid').style.visibility = 'hidden';
        Object.prototype.started = null;
        document.getElementById(this.id+'barkerPlayerObject').started = false;
      } else {
        setTimeout('try { document.getElementById("'+this.id+'PlayerImgLid").style.visibility = "hidden"; } catch(ex) {}', 5000);
      }
    } catch(ex) {}
  }
  
//otwieranie playera w popupie
  function openExternalEmbeddedPlayer(o, src, dim, mediaW, mediaH, cp) {
    try {
      var t, tb;
      if(o != undefined) {
        this.framed = false;
        var req = extractReqParams(src);
        cp += '';
        try {
          if(cp.match(/^\d{1,2}(:\d{1,2}){0,2}$/)) {
            cp = cp.split(':');
            if(cp.length == 1)
              cp = (parseFloat(cp[0]))+'';
            else if(cp.length == 2)
              cp = (parseFloat(cp[0])*60+parseFloat(cp[1]))+'';
            else
              cp = (parseFloat(cp[0])*60*60+parseFloat(cp[1])*60+parseFloat(cp[2]))+'';
          } else {
            cp = 0;
          }
        } catch(ex) {
          cp = 0;
        }
        if(req != null) {
          if(dim == undefined) dim = 0;
          var winW = document.all?(document.documentElement&&document.documentElement.clientWidth)?document.documentElement.clientWidth-20:document.body.clientWidth-20:window.innerWidth-16;
          var winH = document.all?(document.documentElement&&document.documentElement.clientHeight)?document.documentElement.clientHeight-20:document.body.clientHeight-20:window.innerHeight-16;
          var w = mediaW ? mediaW : this.embeddedMediaDimensions[2*dim];
          var h = mediaH ? mediaH : this.embeddedMediaDimensions[2*dim+1];
          if(req.p1.indexOf('wmv') != -1 && req.p4.indexOf('wmv') != -1)
            this.mediaFormat = 'wmv';
          else if(req.p1.indexOf('real') != -1 && req.p4.indexOf('real') != -1)
            this.mediaFormat = 'real';
          else if(req.p1.indexOf('wmv') != -1 && req.p1.indexOf('real') != -1 && req.p4.indexOf('wmv') == -1 && req.p4.indexOf('real') == -1)
            this.mediaFormat = 'noPlayer';
          else if(req.p1.indexOf('wmv') != -1 && req.p4.indexOf('wmv') == -1)
            this.mediaFormat = 'noWmvPlayer';
          else if(req.p1.indexOf('real') != -1 && req.p4.indexOf('real') == -1)
            this.mediaFormat = 'noRealPlayer';
          t = getPreparedPlayerObject(this, src, w, h, cp, 1);
          if(this.mediaFormat == 'wmv')
            mediaH += this.wmvControlsHeight;
          tb = 'width='+mediaW+',height='+mediaH+',scrollbars=no,resizable=yes,toolbar=no,userbar=no,location=no,status=no,menubar=no,screenX=100,screenY=100';
        }
      } else {
        var div = document.getElementById(this.id+'MediaPlayer');
        t = this.externalTemplate.replace(/<player *\/>/gi, div.innerHTML.replace(/Clip1/g, 'Clip'+(new Date()).getTime()));
        tb = 'width='+div.offsetWidth+',height='+div.offsetHeight+',scrollbars=no,resizable=yes,toolbar=no,userbar=no,location=no,status=no,menubar=no,screenX=100,screenY=100';
      }
      if(t) {
        var windowPlayer = window.open('', 'windowPlayer'+(new Date()).getTime(), tb);
        windowPlayer.document.writeln('<html><head><title>Odtwarzacz video</title>');
        windowPlayer.document.writeln('<link rel="stylesheet" type="text/css" href="/binaries/common/css/style.css" />');
        windowPlayer.document.writeln('</head><body bgcolor="white" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">');
        windowPlayer.document.writeln(t);
        windowPlayer.document.writeln('</body></html>');
        windowPlayer.document.close();
      }
    } catch(ex) {}
}

//odgrywanie embedded media (real,wmv) na warstwie
  function openPlayerInLayer(o, src, dim, mediaW, mediaH, cp) {
    this.openExternalEmbeddedPlayer(o, src, dim, mediaW, mediaH, cp);
//    src = src.replace('p1=real,wmv', 'p1=real,wmv');
//    src = src.replace('p4=real,wmv', 'p4=');
/*
    var id = o.id;
    o.blur();
    var req = extractReqParams(src);
    cp += '';
    try {
      if(cp.match(/^\d{1,2}(:\d{1,2}){0,2}$/)) {
        cp = cp.split(':');
        if(cp.length == 1)
          cp = (parseFloat(cp[0]))+'';
        else if(cp.length == 2)
          cp = (parseFloat(cp[0])*60+parseFloat(cp[1]))+'';
        else
          cp = (parseFloat(cp[0])*60*60+parseFloat(cp[1])*60+parseFloat(cp[2]))+'';
      } else {
        cp = 0;
      }
    } catch(ex) {
      cp = 0;
    }
    if(req != null) {
      if(dim == undefined) dim = 0;
      var winW = document.all?(document.documentElement&&document.documentElement.clientWidth)?document.documentElement.clientWidth-20:document.body.clientWidth-20:window.innerWidth-16;
      var winH = document.all?(document.documentElement&&document.documentElement.clientHeight)?document.documentElement.clientHeight-20:document.body.clientHeight-20:window.innerHeight-16;
      var w = mediaW ? mediaW : this.embeddedMediaDimensions[2*dim];
      var h = mediaH ? mediaH : this.embeddedMediaDimensions[2*dim+1];
      if(req.p1.indexOf('wmv') != -1 && req.p4.indexOf('wmv') != -1)
        this.mediaFormat = 'wmv';
      else if(req.p1.indexOf('real') != -1 && req.p4.indexOf('real') != -1)
        this.mediaFormat = 'real';
      else if(req.p1.indexOf('wmv') != -1 && req.p1.indexOf('real') != -1 && req.p4.indexOf('wmv') == -1 && req.p4.indexOf('real') == -1)
        this.mediaFormat = 'noPlayer';
      else if(req.p1.indexOf('wmv') != -1 && req.p4.indexOf('wmv') == -1)
        this.mediaFormat = 'noWmvPlayer';
      else if(req.p1.indexOf('real') != -1 && req.p4.indexOf('real') == -1)
        this.mediaFormat = 'noRealPlayer';
      this.coverBackground();
      var embeddedMedia = document.createElement("div");
      embeddedMedia.id = 'embeddedMedia';
      if(navigator.appVersion.indexOf('MSIE 6')!=-1) {
        embeddedMedia.style.position = 'absolute';
        embeddedMedia.style.left = document.documentElement.scrollLeft+Math.floor((winW-w)/2)+'px';
        embeddedMedia.style.top = document.documentElement.scrollTop+Math.floor((winH-h)/2)+'px';
      } else {
        embeddedMedia.style.position = 'fixed';
        embeddedMedia.style.left = Math.floor((winW-w)/2)+'px';
        embeddedMedia.style.top = Math.floor((winH-h)/2)+'px';
      }
      embeddedMedia.style.left = Math.floor((winW-w)/2)+'px';
      embeddedMedia.style.top = (Math.floor((winH-h)/2) - this.embeddedExtraTop)+'px';
      embeddedMedia.style.zIndex = 301;
//      embeddedMedia.style.width = w + 'px';
//      embeddedMedia.style.height = h + 'px';
      embeddedMedia.innerHTML = getPreparedPlayerObject(this, src, w, h, cp, 1);
      document.body.appendChild(embeddedMedia);
      icons = document.getElementById(this.id+'MediaPlayerIcons');
      if(icons != undefined) {
        icon = document.createElement('img');
        icon.className = 'edge button';
        icon.src = this.mpIconCls;
        icon.onclick = new Function('event', this.id+'.uncoverBackground();');
        icons.appendChild(icon);
        if(this.externalIcon) {
          icon = document.createElement('img');
          icon.className = 'button';
          icon.src = this.mpIconExt;
          icon.onclick = new Function('event', this.id+'.openExternalEmbeddedPlayer(); '+this.id+'.uncoverBackground();');
          icons.appendChild(icon);
        }
      }
      if(this.mediaFormat == 'real' && this.autostart) {
        try {
          setTimeout("try { document.getElementById('"+this.id+"RealVideoO').childNodes[document.getElementById('"+this.id+"RealVideoO').childNodes.length-2].DoPlay();} catch(ex) {}", 1000);
        } catch(ex) { alert(ex); }
      }
    }
*/
  }
  
//przykrycie strony (dezaktywacja)
  function coverBackground() {
    var elms = document.getElementsByTagName('select');
    for(var i = 0; i < elms.length; i++)
      if(elms[i].style.visibility != 'hidden') {
        elms[i].style.visibility = 'hidden';
        this.hiddenPageElements.push(elms[i]);
      }
    elms = document.getElementsByTagName('div');
    for(var i = 0; i < elms.length; i++)
      if(elms[i].className == 'banner' && elms[i].style.visibility != 'hidden') {
        elms[i].style.visibility = 'hidden';
        this.hiddenPageElements.push(elms[i]);
      }
    var pageLid = document.createElement("div");
    pageLid.id = 'pageLid';
    pageLid.style.position = document.all?'absolute':'fixed';
    pageLid.style.top = '0px';
    pageLid.style.left = '0px';
    pageLid.style.backgroundColor = 'silver';
    pageLid.style.opacity = .5;
    pageLid.style.filter = 'alpha(opacity=50)';
    pageLid.style.zIndex = 300;
    pageLid.style.width = '100%';
    pageLid.style.height = document.all ? document.body.clientHeight+'px' : '100%';
    pageLid.onclick = new Function('event', this.id+'.uncoverBackground();');
    document.body.appendChild(pageLid);
  }

//odkrycie strony (aktywacja)
  function uncoverBackground() {
    var elm;
    while(this.hiddenPageElements.length > 0) {
      elm = this.hiddenPageElements.pop();
      elm.style.visibility = 'visible';
    }
    try {
      for(i = document.getElementById('embeddedMedia').childNodes[0].childNodes.length-1; i >= 0; i--)
        try {
          document.getElementById('embeddedMedia').childNodes[0].childNodes[i].controls.stop();
        } catch(ex) {}
      document.getElementById('embeddedMedia').innerHTML = '';
      document.body.removeChild(document.getElementById('embeddedMedia'));
    } catch(ex) {}
    try {
      document.body.removeChild(document.getElementById('pageLid'));
    } catch(ex) {}
  }

//prywatne --------------------------------------------
//ustawianie formatu mediów
  function sanitizeMediaFormat(o) {
    if(o.mediaFormat.toLowerCase() == 'real')
      o.mediaFormat = 'real';
    else if(o.mediaFormat.toLowerCase() == 'rm')
      o.mediaFormat = 'real';
    else if(o.mediaFormat.toLowerCase() == 'wmv')
      o.mediaFormat = 'wmv';
    else if(o.mediaFormat.toLowerCase() == 'noplayer')
      o.mediaFormat = 'noPlayer';
    else if(o.mediaFormat.toLowerCase() == 'norealplayer')
      o.mediaFormat = 'noRealPlayer';
    else if(o.mediaFormat.toLowerCase() == 'nowmvplayer')
      o.mediaFormat = 'noWmvPlayer';
    else
      o.mediaFormat = 'wmv';
  }

//przygotowanie playera z szablonu
  function getPreparedPlayerObject(o, s, w, h, cp, pc) {
    sanitizeMediaFormat(o);
    var t = eval('o.'+o.mediaFormat+'Template');
    var hv = h;
    var hc = eval('o.'+o.mediaFormat+'ControlsHeight');
    h = hv + hc;
    if(o.framed)
      t = o.frameTemplate.replace(/<player *\/>/gi, t);
    return t.replace(/<width *\/>/gi, w).
          replace(/<height *\/>/gi, h).
          replace(/<width\+6 *\/>/gi, w+6).
          replace(/<width\-218 *\/>/gi, w-218).
          replace(/<width\-40 *\/>/gi, w-40).
          replace(/<currentPosition *\/>/gi, cp).
          replace(/<playCount *\/>/gi, pc).
          replace(/<src *\/>/gi, s).
          replace(/<id *\/>/gi, o.id).
          replace(/<autostart *\/>/gi, o.autostart ? 1 : 0).
          replace(/<heightVideo *\/>/gi, hv).
          replace(/<heightControls *\/>/gi, hc);
  }

}
