var wsServer = 'http://www.wdr.de';

var mediaArr = [];

function mediaObj(pId) {
	this.nodeId = pId;
	this.arrIdx = mediaArr.length;
	
	this.pluginHash = {
		video: {
			width: 512,
			height: (288 + 24),
			wmode: 'window'			
		},
		audio: { 
			width: 400,
			height: 24,
			wmode: 'window'
		}
	};	
	//alert("asdasd");
	this.init = function() {
		
	  this.isOpened = false;
      this.node = document.getElementById(this.nodeId);
	  			
      this.contextArt = (this.node.tagName == 'DIV' && checkClassName(this.node.className,'wsTeaser'))?'Teaser':((this.node.tagName == 'DIV' && checkClassName(this.node.className,'wsEmbedPlayerModul'))?'PlayerModul':((this.node.tagName == 'SPAN' && (checkClassName(this.node.className,'wsILLInner') || checkClassName(this.node.className,'wsLLInner')))?'Liste':null));
	  var tmpA = this.node.getElementsByTagName('a')[0];								
      var tmpHref      = tmpA.getAttribute('href');	
      this.playerQuery = (this.playerQuery != null)?this.playerQuery:tmpHref.substring(1 + tmpHref.indexOf('?'));

      if(this.playerQuery.toLowerCase().indexOf('.mp4') != -1 || this.playerQuery.toLowerCase().indexOf('.flv') != -1){
        this.mediaType = 'Video';
      }else if(this.playerQuery.toLowerCase().indexOf('.mp3') != -1){
        this.mediaType = 'Audio';
      }else{
        this.mediaType = null;
      }

      if(this.contextArt == 'Teaser' && this.node.getElementsByTagName('h3').length > 0) { 
        this.headerText = this.getInnerHTML(this.node.getElementsByTagName('h3')[0].getElementsByTagName('a')[0]);
      } else if(this.contextArt == 'PlayerModul' && this.node.getElementsByTagName('a').length > 0) { 
        this.headerText = this.getInnerHTML((this.node.getElementsByTagName('a').length > 1)?this.node.getElementsByTagName('a')[1]:this.node.getElementsByTagName('a')[0]);
      } else 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();
									if(this.node.parentNode.className.indexOf('wsVideoListCont') != -1) {
										this.initializeMediaList();
								  }									
								}
								
								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']
																												 				);
								
									if(this.contextArt == 'Teaser') {
											if(this.node.getElementsByTagName('h3').length > 0) {
											  this.node.getElementsByTagName('h3')[0].innerHTML = this.getInnerHTML(this.node.getElementsByTagName('h3')[0].getElementsByTagName('a')[0]);
											}
											if(this.node.getElementsByTagName('img').length > 0) {	
											   var tmpImg = this.node.getElementsByTagName('img')[0].cloneNode(true);
											   this.node.insertBefore(tmpImg,this.node.getElementsByTagName('img')[0].parentNode);
											   this.node.removeChild(this.node.getElementsByTagName('img')[1].parentNode);
										  }
											if(this.node.getElementsByTagName('p').length > 0
											&& this.node.getElementsByTagName('p')[0].getElementsByTagName('a').length > 0) {
											  var tmpMore = document.createElement('span');
											  tmpMore.innerHTML = this.getInnerHTML(this.node.getElementsByTagName('p')[0].getElementsByTagName('a')[0]);
											  this.node.getElementsByTagName('p')[0].insertBefore(tmpMore,this.node.getElementsByTagName('p')[0].getElementsByTagName('a')[0]);
											  this.node.getElementsByTagName('p')[0].removeChild(this.node.getElementsByTagName('p')[0].getElementsByTagName('a')[0]);
										  }
									}
									else if(this.contextArt == 'Liste') {
									  this.listenArt = (this.node.parentNode.parentNode.parentNode.parentNode.parentNode != null
														&& this.node.parentNode.parentNode.parentNode.parentNode.parentNode.tagName == 'DIV'
														&& this.node.parentNode.parentNode.parentNode.parentNode.parentNode.className == 'wsService'
														)?'halbeListe':'dflt';
											if(
										 			this.listenArt != null
										 	 &&	this.listenArt == 'halbeListe'
										 	 && this.node.parentNode.parentNode != null
											 && this.node.parentNode.parentNode.tagName == 'UL'
											){ this.tmpUL = this.node.parentNode.parentNode.cloneNode(true); }
											
											this.node.innerHTML = '<span class="wsMediaTitle"><strong>' + this.getInnerHTML(this.node.getElementsByTagName('a')[0]) + '<\/strong><\/span>';
									}
									
									if(
											this.listenArt != null
									 && this.listenArt == 'halbeListe'
									 && this.node.parentNode != null
									 && this.node.parentNode.style != null
									){
										this.node.parentNode.style.width = '85%';
										this.node.parentNode.style.cssFloat = 'none';
										this.node.parentNode.style.clear = 'both';
										this.node.parentNode.style.display = 'block';
										if(
												 this.node.parentNode.nextSibling != null
											&& this.node.parentNode.nextSibling.style != null
											) {											
											this.node.parentNode.nextSibling.style.cssFloat = 'none';
											this.node.parentNode.nextSibling.style.clear = 'both';
											this.node.parentNode.nextSibling.style.display = 'block';
										}
										
										
										
										
									}
									
									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');
										
										if(this.contextArt != 'PlayerModul') {
											var tmpSpan;
											tmpSpan = document.createElement('div');
											
											var tmpRubrik = document.createElement('div');
											tmpRubrik.className = 'wsRubrikVideo';
											tmpRubrik.innerHTML = '<strong>Video<\/strong>';
											
											var tmpEPMSpan = document.createElement('span');
											tmpEPMSpan.className = 'wsEPMSpan';											
											tmpEPMSpan.innerHTML += '<img src="/codebase/nav/2006/fbeed7/video.gif" alt="Video: " class="wsIcon" />';
											tmpEPMSpan.innerHTML += '<strong><span>' + this.headerText + '<\/span><\/strong>';
											
											
											tmpSpan.appendChild(tmpRubrik);
											tmpSpan.appendChild(tmpOuterA);
											tmpSpan.appendChild(this.playerNode);
											tmpSpan.appendChild(tmpEPMSpan);
											
											tmpSpan.className = 'wsBGLiteColor wsVideoPlayerModul';										
											if(this.sibling != 'append') { 
												this.node.insertBefore(tmpSpan,this.sibling);
												if(this.node.parentNode.className.indexOf('wsVideoListCont') != -1) {
													 this.sibling.className += ' wsMediaListe';
												}
												else {
													this.node.insertBefore(tmpSquare,this.sibling);
												}												
											}
											else { this.node.appendChild(tmpSpan); this.node.appendChild(tmpSquare); }  
										}
										else { 											
											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(typeof tmpPlugin.stopAllMedia == 'function') { tmpPlugin.stopAllMedia(); }
									if(this.mediaType == 'Video' && this.contextArt == 'PlayerModul') { 
											this.node.parentNode.removeChild(document.getElementById('wsInvSquare'));
									}
									/*
									else if(this.mediaType == 'Video' && this.contextArt == 'Teaser') { }
									else if(this.mediaType == 'Video' && this.contextArt == 'Liste')  { }
									*/
									this.node.innerHTML = this.tmpNode.innerHTML;									
									this.node.className = this.tmpClassName;
									
									if(
											this.listenArt != null
									 && this.listenArt == 'halbeListe'
									 && this.tmpUL != null
									){ 
										this.node.parentNode.parentNode.innerHTML = this.tmpUL.innerHTML;										
									}								
									
									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;
		if(this.contextArt == 'Teaser') {
				if(this.node.getElementsByTagName('h3').length > 0) {
				  this.node.getElementsByTagName('h3')[0].getElementsByTagName('a')[0].onclick = function() { mediaArr[tmpIdx].open(); return false; };
				}
				if(this.node.getElementsByTagName('img').length > 0
				&& this.node.getElementsByTagName('img')[0].parentNode.tagName.toLowerCase() == 'a') { 
					this.node.getElementsByTagName('img')[0].parentNode.onclick = function() { mediaArr[tmpIdx].open(); return false; };
				}
				if(this.node.getElementsByTagName('p').length > 0
				&& this.node.getElementsByTagName('p')[0].getElementsByTagName('a').length > 0
				) {
				  this.node.getElementsByTagName('p')[0].getElementsByTagName('a')[0].onclick = function() { mediaArr[tmpIdx].open(); return false; };
				}
		}
		else if(this.contextArt == 'PlayerModul') {
			if(this.node.getElementsByTagName('a').length > 0) {
				this.node.getElementsByTagName('a')[0].onclick = function() { mediaArr[tmpIdx].open(); return false; };
				if(this.node.getElementsByTagName('a').length > 1) {
				   this.node.getElementsByTagName('a')[1].onclick = function() { mediaArr[tmpIdx].open(); return false; };
			  }
			}
		}
		else if(this.contextArt == 'Liste') {
			  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() {
			switch(this.contextArt) {
				case 'Teaser':
				if(this.node.getElementsByTagName('p').length > 0) {
					this.sibling = (this.node.getElementsByTagName('p')[(this.node.getElementsByTagName('p').length - 1)] != null
											 && this.node.getElementsByTagName('p')[(this.node.getElementsByTagName('p').length - 1)].nextSibling != null)?this.node.getElementsByTagName('p')[(this.node.getElementsByTagName('p').length - 1)].nextSibling:'append';
				}
				else if(this.node.parentNode.className.indexOf('wsVideoListCont') != -1) { 
					
					this.sibling = this.node.parentNode.getElementsByTagName('ul')[0];
					
				}
				break;
				case 'PlayerModul':
					this.sibling = (this.node.getElementsByTagName('div')[1] != null)?this.node.getElementsByTagName('div')[1]:'append';
				break;
				case 'Liste':
				default:
					this.sibling = (this.node.getElementsByTagName('span')[0] != null
											 && this.node.getElementsByTagName('span')[0].nextSibling != null)?this.node.getElementsByTagName('span')[0].nextSibling:'append';
			}
	}	;
	this.initializeMediaList = function() {
		var tmpLinks = this.node.getElementsByTagName('ul')[0].getElementsByTagName('a');
		for(var i = 0; i < tmpLinks.length; i++) { 
			var tmpIdx = this.arrIdx;
			tmpLinks[i].onclick = function() { mediaArr[tmpIdx].playList(this,tmpIdx); return false; };
		}
	};
	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(); }
	}
}




function getFlashPlayerHTML(pQuery,classStr,pHash) {

	//var cfgFile = escape('' + wsServer + '/themen/global/flashplayer/skins/wdrde/skin.cfg');
	var cfgFile = escape('/codebase/flashplayer/skins/player.cfg');
	
	pQuery += '&amp;cfgFile=' + cfgFile + '&amp;autoCount=true&amp;autoPlay=true';
	
	var lParams = {
		//base: ''+wsServer+'/themen/global/flashplayer/',
		base: '/codebase',
		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="movie" value="'+wsServer+'/themen/global/flashplayer/wsPlayer.swf?';
		lHTML += pQuery;
		*/
  	lHTML += '" \/>';
	}
	else {}
		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); };

