var wsServer = 'http://www.wdr.de';
var wsVideoGIF = "/studio/codebase/img/000000_ebf1f7/video.gif";

var mediaArr = [];

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

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

    this.pluginHash = {
        video: {
//            width: 512,
            width: 440,
//            height: (288 + 24),
            height: (248 + 24),
            wmode: 'window'
        },
        audio: {
            width: 400,
            height: 24,
            wmode: 'opaque'
        }
    };

    
    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 = tmpHref.substring(1 + tmpHref.indexOf('?'));
        this.mediaType = (this.playerQuery.toLowerCase().indexOf('.flv') != -1) ? 'Video' : ((this.playerQuery.toLowerCase().indexOf('.mp3') != -1) ? 'Audio' : null);
        
        this.headerText = (this.contextArt == 'Teaser') ? this.node.getElementsByTagName('h3')[0].getElementsByTagName('a')[0].innerHTML : ((this.contextArt == 'PlayerModul') ? this.node.getElementsByTagName('a')[1].innerHTML : this.node.getElementsByTagName('a')[0].innerHTML);
        this.srcText = 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']);
            
            if (this.contextArt == 'Teaser') {
                this.node.getElementsByTagName('h3')[0].innerHTML = this.node.getElementsByTagName('h3')[0].getElementsByTagName('a')[0].innerHTML;
                
                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);
                
                var tmpMore = document.createElement('span');
                tmpMore.innerHTML = this.node.getElementsByTagName('p')[0].getElementsByTagName('a')[0].innerHTML;
                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.node.getElementsByTagName('a')[0].innerHTML + '<\/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 = 'wsRubrik';
                        tmpRubrik.innerHTML = '<h2>Video<\/h2>';

						var tmpRubrikSub = document.createElement('div');
						tmpRubrikSub.className = 'wsRubrikThinSub';
                        
                        var tmpEPMSpan = document.createElement('span');
                        tmpEPMSpan.className = 'wsEPMSpan';
                        tmpEPMSpan.innerHTML += '<img src="'+ wsVideoGIF +'" alt="Video: " class="wsIcon" />';
                        tmpEPMSpan.innerHTML += '<strong><span>' + this.headerText + '<\/span><\/strong>';
                        tmpEPMSpan.innerHTML += '<span class="wsMediaSource">[' + this.srcText + ']<\/span>';
                        
                        
                        tmpSpan.appendChild(tmpRubrik);
                        tmpSpan.appendChild(tmpRubrikSub);						
                        tmpSpan.appendChild(tmpOuterA);
                        tmpSpan.appendChild(this.playerNode);
                        tmpSpan.appendChild(tmpEPMSpan);
                        
                        tmpSpan.className = 'wsBGLiteColor wsVideoPlayerModul';
                        
                        (this.sibling != 'append') ? this.node.insertBefore(tmpSpan, this.sibling) : this.node.appendChild(tmpSpan);
                        (this.sibling != 'append') ? this.node.insertBefore(tmpSquare, this.sibling) : this.node.appendChild(tmpSquare);
                    }
                    else {
                        (this.sibling != 'append') ? this.node.insertBefore(tmpOuterA, this.sibling) : this.node.appendChild(tmpOuterA);
                        (this.sibling != 'append') ? this.node.insertBefore(this.playerNode, this.sibling) : this.node.appendChild(this.playerNode);
                        
                        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.setOnClickEvents();									
        }
    };
    this.setOnClickEvents = function(){
        var tmpIdx = this.arrIdx;
        if (this.contextArt == 'Teaser') {
            this.node.getElementsByTagName('h3')[0].getElementsByTagName('a')[0].onclick = function(){
                mediaArr[tmpIdx].open();
                return false;
            };
            this.node.getElementsByTagName('img')[0].parentNode.onclick = function(){
                mediaArr[tmpIdx].open();
                return false;
            };
            this.node.getElementsByTagName('p')[0].getElementsByTagName('a')[0].onclick = function(){
                mediaArr[tmpIdx].open();
                return false;
            };
        }
        else 
            if (this.contextArt == 'PlayerModul') {
                this.node.getElementsByTagName('a')[0].onclick = function(){
                    mediaArr[tmpIdx].open();
                    return false;
                };
                this.node.getElementsByTagName('a')[1].onclick = function(){
                    mediaArr[tmpIdx].open();
                    return false;
                };
            }
            else 
                if (this.contextArt == 'Liste') {
                    this.node.getElementsByTagName('a')[0].onclick = function(){
                        mediaArr[tmpIdx].open();
                        return false;
                    };
                }
    };
    
    this.getSibling = function(){
        switch (this.contextArt) {
            case 'Teaser':
                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';
                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';
        }
    };
};

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');
    pQuery += '&amp;cfgFile=' + cfgFile;
    
    var lParams = {
        base: '' + wsServer + '/themen/global/flashplayer/',
        quality: 'autolow',
        allowScriptAccess: 'sameDomain',
        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 += 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);
}
