var wsServer = '';

var mediaArr = [];

function mediaObj(pId) {
	
  this.nodeId = pId;
  this.arrIdx = mediaArr.length;

  this.pluginHash = {
    video: {
      width: 391,
      height: (221 + 22),
      wmode: 'window'
    },
    audio: {
      width: 391,
      height: 20,
      wmode: 'window'
    }
  };

  this.init = function() {
    this.isOpened = false;
    this.node = document.getElementById(this.nodeId);

    this.contextArt = 'PodcastPlayer';
    var tmpA         = this.node.getElementsByTagName('a')[0];
    var tmpHref      = tmpA.getAttribute('href');
    this.playerQuery = (this.playerQuery != null)?this.playerQuery:tmpHref.substring(1 + tmpHref.indexOf('?'));

    this.mediaType   = (this.playerQuery.toLowerCase().indexOf('.flv') != -1)?'Video':(this.playerQuery.toLowerCase().indexOf('.mp4') != -1)?'Video':((this.playerQuery.toLowerCase().indexOf('.mp3') != -1)?'Audio':null);

    if(this.node.getElementsByTagName('a').length > 0) {
      this.headerText = this.getInnerHTML(this.node.getElementsByTagName('a')[0]);
    }

    this.srcText = (tmpA.getAttribute('title') != null)?tmpA.getAttribute('title').substring(0,tmpA.getAttribute('title').indexOf('; Flash')):'';

    this.playerNode = document.createElement('span');
    this.playerNode.className = 'ws' + this.mediaType + 'Player';

    if(this.mediaType != null) {
      this.setOnClickEvents();
    }

    this.tmpClassName = this.node.className;
  };

  this.open = function() {
    closeAllOpenPlayer();
    if(!this.isOpened) {
      this.isOpened = true;
      this.tmpNode = this.node.cloneNode(true);


      var tmpA = document.createElement('a');
      tmpA.innerHTML = 'X';
      tmpA.setAttribute('title','Player schließen');

      var tmpIdx = this.arrIdx;
      tmpA.onclick = function() {
        mediaArr[tmpIdx].close();
      };

      var tmpOuterA = document.createElement('span');
      tmpOuterA.className = 'wsClose';
      tmpOuterA.appendChild(tmpA);

      this.playerNode.innerHTML = getFlashPlayerHTML(this.playerQuery,
        'ws' + this.mediaType + '',
        (this.mediaType == 'Video')?this.pluginHash['video']:this.pluginHash['audio']
        );

      this.getSibling();

      if(this.mediaType == 'Audio') {
        this.playerNode.insertBefore(tmpOuterA,this.playerNode.firstChild);
        if(this.contextArt == 'PlayerModul') {
          this.node.getElementsByTagName('p')[0].innerHTML = this.node.getElementsByTagName('p')[0].getElementsByTagName('a')[0].innerHTML;

          this.node.getElementsByTagName('div')[1].getElementsByTagName('strong')[0].innerHTML = this.node.getElementsByTagName('div')[1].getElementsByTagName('strong')[0].getElementsByTagName('a')[0].innerHTML;

          this.node.className += ' wsAudioPlayerModul';
        }
        else {
          //var tmpSrcSpan = document.createElement('span');
          //tmpSrcSpan.className = 'wsAudioSrc';
          //tmpSrcSpan.innerHTML = '[' + this.srcText + ']';
          //this.playerNode.appendChild(tmpSrcSpan);
        }

        (this.sibling != 'append')?this.node.insertBefore(this.playerNode,this.sibling):this.node.appendChild(this.playerNode);

      }

      else if(this.mediaType == 'Video') {
        var tmpSquare = document.createElement('span');
        tmpSquare.setAttribute('id','wsInvSquare');
//gdp
        this.playerNode.insertBefore(tmpOuterA,this.playerNode.firstChild);
        if(this.sibling != 'append') {
          //this.node.insertBefore(tmpOuterA,this.sibling);
          this.node.insertBefore(this.playerNode,this.sibling);
        }
        else {
          //this.node.appendChild(tmpOuterA);
          this.node.appendChild(this.playerNode);
        }

//        if(this.node.getElementsByTagName('p').length > 0) {
//          this.node.getElementsByTagName('div')[1].getElementsByTagName('strong')[0].innerHTML = this.node.getElementsByTagName('div')[1].getElementsByTagName('strong')[0].getElementsByTagName('a')[0].innerHTML;
//          this.node.removeChild(this.node.getElementsByTagName('p')[0]);
//        }

        this.node.className += ' wsVideoPlayerModul';

        (this.node.nextSibling != null)?this.node.parentNode.insertBefore(tmpSquare,this.node.nextSibling):this.node.parentNode.appendChild(tmpSquare);
      }

    }
  };

  this.close = function() {
    if(this.isOpened) {
      var tmpPlugin = document.getElementById(this.nodeId).getElementsByTagName('object')[0];
      if(tmpPlugin != null && tmpPlugin.stopAllMedia != null && typeof tmpPlugin.stopAllMedia == 'function') {
        // TODO scheint zwar plausibel, wirft aber eine Exception. Mit Ralf klären, was hier ab geht.
        //tmpPlugin.stopAllMedia();
      }
      if(this.mediaType == 'Video') {
        this.node.parentNode.removeChild(document.getElementById('wsInvSquare'));
      }
      this.node.innerHTML = this.tmpNode.innerHTML;
      this.node.className = this.tmpClassName;

      this.playerNode.innerHTML = '';
      this.tmpUL = null;
      this.tmpNode = null;
      this.isOpened = false;

      //for(var i = 0; i < mediaArr.length; i++) {
      //mediaArr[i].init(i);
      //}
      this.init();
    //this.setOnClickEvents();
    }
  };

  this.setOnClickEvents = function() {
    var tmpIdx = this.arrIdx;
    this.node.getElementsByTagName('a')[0].onclick = function() {
      closeAllOpenPlayer(); mediaArr[tmpIdx].open(); return false;
    };
  };

  this.getInnerHTML = function(node) {
    return (typeof node == 'object')?node.innerHTML:'';
  };

  this.getSibling = function() {
    this.sibling = (this.node.getElementsByTagName('span')[0] != null
      && this.node.getElementsByTagName('span')[0].nextSibling != null)?this.node.getElementsByTagName('span')[0].nextSibling:'append';
  } ;

  this.playList = function(pLink,pCnt) {

    var tmpIH = pLink.innerHTML;
    var tmpTitle = (pLink.getAttribute('title') != null)?pLink.getAttribute('title').substring(0,pLink.getAttribute('title').indexOf('; Flash')):'';
    var tmpHref = pLink.getAttribute('href');
    var tmpQuery = tmpHref.substring(1 + tmpHref.indexOf('?'));

    if(!mediaArr[pCnt].isOpened
      || typeof mediaArr[pCnt].node.getElementsByTagName('object')[0].changeMedia != 'function') {
      mediaArr[pCnt].playerQuery = tmpQuery;
      mediaArr[pCnt].open();
    }
    else {
      var tmpLinkArr = tmpQuery.split('&');
      mediaArr[pCnt].node.getElementsByTagName('object')[0].changeMedia(
        tmpLinkArr[0].split('=')[1],
        tmpLinkArr[1].split('=')[1],
        '0'
        );
    }

    var tmpSpanArr = mediaArr[pCnt].node.getElementsByTagName('span');
    for(var i = 0; i < tmpSpanArr.length; i++) {
      if(tmpSpanArr[i].className == 'wsEPMSpan') {
        tmpSpanArr[i].getElementsByTagName('strong')[0].getElementsByTagName('span')[0].innerHTML = tmpIH;
      }
      else if(tmpSpanArr[i].className == 'wsMediaSource') {
        tmpSpanArr[i].innerHTML = tmpTitle;
      }
    }

  };
};

function checkClassName(pClassName,pStr) {
  var tmpArr = pClassName.split(' ');
  for(var i = 0; i < tmpArr.length; i++) {
    if(tmpArr[i] == pStr) {
      return true; break;
    }
  }
  return false;
};

function closeAllOpenPlayer() {
  for(var i = 0; i < mediaArr.length; i++) {
    if(mediaArr[i].isOpened) {
      mediaArr[i].close();
    }
  }
};

function stopOpenPlayer() {
  var objArr = document.getElementsByTagName('object');
  for(var i = 0; i < objArr.length; i++) {
    if(typeof objArr[i].stopAllMedia == 'function') {
      objArr[i].stopAllMedia();
    }
  }
}

addLoadEvent(
  function() {
    for(var i = 0; i < mediaArr.length; i++) {
      mediaArr[i].init(i);
    }
  }
  );


function getFlashPlayerHTML(pQuery,classStr,pHash) {

  //pQuery += '&amp;autoPlay=true&amp;red=tv_home';

  var lParams = {
    //base: 'http://www.wdr.de/tv/home/codebase/video/podcast/',
    quality: 'autolow',
    allowScriptAccess: 'always',
    allowFullScreen: 'true',

    wmode: pHash['wmode']
  };

  var lParamsStr = '';
  for(var prop in lParams) {
    lParamsStr += '<param name="' + prop + '" value="' + lParams[prop] +'" \/>' + "\n";
  }


  var lAltText = '';
  lAltText += '<p>Unsere Audios und Videos können Sie mit dem Flash-Player ab der Version 8.0 abspielen. Den neuesten Flash-Player können Sie beim Hersteller Adobe unter folgender Adresse kostenlos downloaden:<br />';
  lAltText += '<strong><a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" target="_blank">http://www.macromedia.com/go/getflashplayer_de<\/a><\/strong><\/p>';

  var lHTML = '';

  lHTML += '<object width="' + pHash['width'] + '" height="' + pHash['height'] + '"';
  if(isIE) {
    lHTML += ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
    lHTML += ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"';
  }
  else {
    lHTML += ' type="application/x-shockwave-flash"';
    lHTML += ' data="'+wsServer+'/themen/global/flashplayer/wsPlayer.swf"';
  }
  lHTML += ' class="' + classStr + '"';
  lHTML += '>';
  lHTML += "\n";
  if(isIE) {
    lHTML += '<param name="movie" value="'+wsServer+'/themen/global/flashplayer/wsPlayer.swf" \/>';
  }

  lHTML += '<param name="flashVars" value="';
  lHTML += pQuery;
  lHTML += '" />';

  lHTML += "\n";
  lHTML += lParamsStr;

  lHTML += "\n";
  lHTML += lAltText;

  lHTML += "\n";
  lHTML += '<\/object>';
  lHTML += "\n";

  return lHTML;
};

/* PopUp für FS */
function getDim()  {
  w = 800; h= 600;
  if(window.screen) {
    w = screen.availWidth; h = screen.availHeight;
  }
  return {
    width: w,
    height: h
  };
};

function openFullscreenFLV(url) {
  var str = '';
  str = 'width=' + getDim().width + ',height=' + getDim().height + ',top=0,left=0,resizable';
  var flvWin = window.open(url,'flvWin',str);
  if(window.focus) {
    flvWin.focus();
  }
};

function getScriptState() {
  return 'true';
};

/* Zählpixel Referer */
function getReferer() {
  return escape(location.href);
};


