﻿var th=null;

// Additional string functions.
String.prototype.endsWith = function (str) {
    return (this.match(str + "$") == str);
}
String.prototype.startsWith = function (str) {
    return (this.match("^" + str) == str);
}
String.prototype.trim = function () {
    return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}

// object defs
function pane(id) {
    this.curFrame=0;
    this.curDiv=null;
    this.effects=null;
    this.id=id;
    this.stale=false;
    this.startPos=0;
    this.timer=null;
}

function paneAnimation(div, x, y, w, h, curX, curY, curW, curH) {
    this.curH = curH;
    this.curW = curW;
    this.curX = curX;
    this.curY = curY;
    this.div = div;
    this.h = h;
    this.w = w;
    this.x = x;
    this.y = y;

    this.vx = Math.round(Math.abs(curX - x) / 5);
    this.vy = Math.round(Math.abs(curY - y) / 5);

    th.putDebug('Move pane ' + div.id + ' from ' + curX + ',' + curY + ' (' + curW + ' x ' + curH + ') to ' + x + ',' + y + ' (' + w + ' x ' + h + ')');

    var intH = h - (th.skinMargin * 2);
    var intW = w - (th.skinMargin * 2);

    if (div.id.indexOf('video') != -1) {
        var st = getStyle(getObject('slvMain'));
        if (st) {
            st.height = intH + 'px';
            st.width = intW + 'px';
        }
        else if (getWmp()) with (getWmp()) {
            height = intH;
            width = intW;
        }
    }

    for (var i = 0; i < div.childNodes.length; i++) {
        var subDiv = div.childNodes[i];
        var cls = subDiv.getAttribute('class');
        var st = getStyle(subDiv);
        if (st) {
            var ctr = false;
            if (cls.indexOf('Bottom') != -1) st.top = (h - th.skinMargin) + 'px';
            if (cls.indexOf('Center') != -1) {
                st.width = intW + 'px';
                ctr = true;
            }
            if (cls.indexOf('Middle') != -1) {
                st.height = intH + 'px';
                if (ctr) this.resizeAssets(div, intW, intH);
            }
            if (cls.indexOf('Right') != -1) st.left = (w - th.skinMargin) + 'px';
            th.putDebug('Moved border div ' + cls + ' to ' + st.left + ', ' + st.top + ', (' + st.width + ' x ' + st.height + ')');
        }
    }
}

function theatre(debug) {
    this.animations = null;
    this.animTimer = null;
    this.combo_g = 0;
    this.combo_h = 0;
    this.combo_p = 0;
    this.combo_t = 0;
    this.combo_w = 0;
    this.curMk = null;
    this.dbgTxt='';
    this.debug=debug;
    this.divs=null;
    this.layoutId = 0;
    this.layouts = null;
    this.loadTime = null;
    this.maxPos = 0;
    this.mks=null;
    this.mediaSrc='';
    this.panes = null;
    this.player = null;
    this.quiz = false;
    this.retryInterval = 1000;
    this.skinMargin = 0;
    this.stageTimeout = 120;
    this.startTime = null;
    this.status='';
    this.statusId = '';
    this.tagged = false;
    this.timer=null;
    this.visitId = '';
    this.zoomJumpPos = 0;
    this.zoomLimitX = 0;
    this.zoomLimitY = 0;
    this.zoomTimer = null;
}

// theatre prototype functions
function changeLayout(newId) {
    this.putDebug('Switch layouts from ' + this.layoutId +' to ' + newId);
    if (this.layoutId != newId) {
        // Obtain layout panes.
        var oldArray = null, newArray = null;
        for (var i=0; i < this.layouts.length; i+=2) {
            switch (this.layouts[i]) {
                case newId:
                    newArray = this.layouts[i+1];
                    break;
                case this.layoutId:
                    oldArray = this.layouts[i+1];
                    break;
            }
        }
        // Hide panes from old layout which are not in the new layout.
        if (oldArray && newArray) for (var i = 0; i < oldArray.length; i += 5) {
            var hideDiv = getElement(oldArray[i]);
            for (var j = 0; j < newArray.length - 1; j += 5) {
                if (oldArray[i] == newArray[j]) hideDiv = null;
            }
            if (hideDiv) {
                var pSt = getStyle(hideDiv);
                if (pSt) pSt.display = 'none';
            }  
        }
        // Move panes to new layout positions.
        this.animations = new Array(newArray.length-1);
        for (var i = 0; i < newArray.length; i += 5) {
            // Identify old pane so we know whether to resize it.
            var curH = 0, curW = 0, curX = 0, curY = 0;
            for (var j = 0; j < oldArray.length - 1; j += 5) {
                if (newArray[i] == oldArray[j]) {
                    curX = oldArray[j + 1];
                    curY = oldArray[j + 2];
                    curW = oldArray[j + 3];
                    curH = oldArray[j + 4];
                }
            }
            // Initiate a pane animation.
            this.animations[i] = new paneAnimation(getElement(newArray[i]), newArray[i + 1], newArray[i + 2], newArray[i + 3], newArray[i + 4], curX, curY, curW, curH);
        }
        this.doAnimations();
        // Set current layout.
        this.layoutId = newId;
    }
}

function doAnimations() {
    clearTimeout(this.animations);
    var pRs = true;
    if (this.animations) for (var i = 0; i < this.animations.length; i++) if (this.animations[i]) pRs = this.animations[i].animatePane() && pRs;
    if (pRs) for (var i = 0; i < this.animations.length; i++) {
        if (this.animations[i]) {
            var st = getStyle(this.animations[i].div);
            if (st) st.display = '';
        }
    }
    else this.animTimer = setTimeout('th.doAnimations()', 25);
}

function elapsedTime() {
    if (!this.loadTime) this.loadTime = (new Date()).getTime();
    return ((new Date()).getTime() - this.loadTime) / 1000;
}

function getDiv(key) {
    var pRs = null;
    if (this.divs) {
        for (var i=0;i<this.divs.length;i+=2) {
            if (this.divs[i] == key) {
                pRs = getElement(this.divs[i + 1]);
                break;
            }
        }
    }
    return pRs;
}

function getPane(At) {
    var pRs=null;
    if (this.panes) for (var i=0;i<this.panes.length;i++) {
        var pPn=this.panes[i];
        if (pPn&&pPn.id==At) {
            pRs=pPn;
            break;
        }
    }
    return pRs;
}

function getPosition() {
    if (this.player) return this.player.get_position();
    else if (getWmp() && getWmp().controls) return getWmp().controls.currentPosition;
    else if (this.startTime) return ((new Date()).getTime()-this.startTime)/1000;
    else return 0;
}

function getStatus() {
    if (this.player) return this.player.get_currentState();
    else return '';
}

function initPanes(ids) {
    if (ids) {
        this.panes=new Array(ids.length);
        for (var i=0;i<ids.length;i++) this.panes[i]=new pane(parseInt(ids[i]));
    }
}

function pause() {
    var pPl=this.player();
    if (pPl) pPl.Pause();
    else if (getWmp() && getWmp().controls) getWmp().controls.pause();
}

function play() {
    var pPl=this.player;
    if (pPl) pPl.play();
    else if (getWmp() && getWmp().controls) getWmp().controls.play();
}

function putDebug(msg) {
	//if (!window.onerror) 
	if (this.debug) this.dbgTxt+=showTime()+' '+msg+'\n<br />';
}

function reportAudit(At,Dt) {
    var img=new Image();
    var pPos=parseInt(this.getPosition());
    img.src='handlers/feedback.ashx?guid='+this.visitId+'&a='+At+(this.maxPos>pPos?'&m='+this.maxPos:'')+(pPos?'&p='+pPos:'')+(Dt?'&d='+encodeURIComponent(Dt):'');
}

function reportStart() {
    if (!this.startTime) {
        this.startTime=(new Date()).getTime();
        this.reportAudit('Start');
    }
}

function rotateTheatre(ix) {
    var pMk=this.mks[ix+2]
    if (this.curMk!=pMk) {
        this.putDebug('rotateTheatre m'+pMk);
        this.curMk=pMk;
        for (var i=0;i<this.panes.length;i++) if (this.panes[i]) this.panes[i].stale=true;
        for (var i=ix;this.staleCount()&&i>=0;i-=4) {
            pMk=this.mks[i+2];
            var pAs=this.mks[i+3];
            for (var j=0;j<pAs.length;j+=2) {
                var pPn=this.getPane(pAs[j]);
                if (pPn&&pPn.stale) pPn.rotate(pMk,pAs[j+1]);
            }
        }       
        var pPos=parseInt(this.getPosition());
        if (this.maxPos<pPos) this.maxPos=pPos;
        return true;
    }
    else return false;
}

function setPosition(pos) {
    if (this.player) {
        this.player.set_position(pos);
        return true;
    }
    else if (getWmp() && getWmp().controls) {
        getWmp().controls.currentPosition = Pos;
        return true;
    }
    else return false;
}

function showDebug() {
	if (this.dbgTxt!='') {
		winDebug=window.open('','debug','');
		if (winDebug) {
			with (winDebug.document) {
				open();
				write(this.dbgTxt);
				close();
			}
		} 
		else alert(this.dbgTxt.replace(/<br ?\/>/g,'\n'));
	}
	this.dbgTxt='';
	return true;
}

function staleCount() {
    var pRs=0;
    if (this.panes) for (var i=0;i<this.panes.length;i++) if (this.panes[i]&&this.panes[i].stale) pRs++;
    return pRs;
}

// theatre prototype declarations
theatre.prototype.changeLayout = changeLayout;
theatre.prototype.doAnimations = doAnimations;
theatre.prototype.elapsedTime = elapsedTime;
theatre.prototype.getDiv=getDiv;
theatre.prototype.getPane=getPane;
theatre.prototype.getPosition=getPosition;
theatre.prototype.getStatus=getStatus;
theatre.prototype.initPanes=initPanes;
theatre.prototype.pause=pause;
theatre.prototype.play=play;
theatre.prototype.putDebug=putDebug;
theatre.prototype.reportAudit=reportAudit;
theatre.prototype.reportStart=reportStart;
theatre.prototype.rotate = rotateTheatre;
theatre.prototype.setPosition = setPosition;
theatre.prototype.showDebug=showDebug;
theatre.prototype.staleCount=staleCount;

// pane prototype functions
function doEffect(ix) {
    if (ix) ix -= 1;
    if (this.effects) {
        var pFn=0;
        if (ix>=0&&this.effects.length>ix*2) pFn=this.effects[1+ix*2];
        if (this.curDiv&&this.curFrame!=pFn) {
            th.putDebug('doEffect: '+ix);
            var anim=this.curDiv.getAttributeNode('anim');
            if (anim) animate(this.id,anim.value,pFn,0);
        }
    } 
}

function rotatePane(mk,eff) {
    this.stale=false;
    if (th) {
        var div=th.getDiv('p'+this.id+'m'+mk);
        if (div) {
            var pCd=this.curDiv;
            if (div!=pCd) {
                cancelTimer(this.timer);
                this.effects=eff;
                if (this.id==6) {
                    div.className='theatreTransOn';
                    if (pCd) pCd.className='theatreTransOff';
                    scrollTo(div);
                }
                else {
                    var anim,pSt;
                    // old div
                    if (pCd) {
                        anim=pCd.getAttributeNode('anim');
                        if (anim) animate(this.id,anim.value,-1,0);
                    }            
                    pSt=getStyle(pCd);
                    if (pSt) pSt.display='none';
                    // new div
                    pSt=getStyle(div);
                    if (pSt) pSt.display='';
                    anim=div.getAttributeNode('anim');
                    if (anim) {
                        this.curFrame=-1;
                        if (eff) {
                            for (var i=0;i<th.mks.length-4;i+=4) if (th.mks[i+2]==mk) {
                                this.startPos=th.mks[i];
                                break;
                            }
                        }
                        animate(this.id,anim.value,0,0);
                    }
                }
                this.curDiv=div;
                th.putDebug('rotatePane: '+div.id+', '+eff);
            }
        }
        else th.putDebug('div not found: p'+this.id+'m'+mk);
    }
}

// pane prototype declarations
pane.prototype.doEffect=doEffect;
pane.prototype.rotate=rotatePane;

// paneAnimation functions
function animatePane() {
    var st = getStyle(this.div);
    if (st) {
        th.putDebug('Moving '+this.div.id + ' to ' + st.left + ',' + st.top);
        if (Math.abs(this.curX - this.x) < 2) this.curX = this.x;
        else this.curX += (this.curX < this.x ? this.vx : 0 - this.vx);
        if (Math.abs(this.curY - this.y) < 2) this.curY = this.y;
        else this.curY += (this.curY < this.y ? this.vy : 0 - this.vy);
        st.left = this.curX +'px';
        st.top = this.curY + 'px';
        if (this.vx > 1) this.vx *= 0.8;
        if (this.vx < 1) this.vx = 1;
        if (this.vy > 1) this.vy *= 0.8;
        if (this.vy < 1) this.vy = 1;
        return this.animFinished();
    }
    else return true;
}

function animFinished() {
    return this.curX == this.x && this.curY == this.y;
}

function resizeAsset(st, maxW, maxH, nativeW, nativeH) {
    if (st && maxW && maxH) {
        if (nativeH && nativeW) {
            var pRatioH = maxH / nativeH;
            var pRatioW = maxW / nativeW;
            var pRatio = pRatioH < pRatioW ? pRatioH : pRatioW;
            st.height = (pRatio * nativeH) + 'px';
            st.width = (pRatio * nativeW) + 'px';
        }
        else {
            st.height = (maxH) + 'px';
            st.width = (maxW) + 'px';
        }
    }
}

function resizeAssets(div, maxW, maxH) {
    if (maxH < 1) maxH = 1;
    if (maxW < 1) maxW = 1;
    if (th) th.putDebug('Resize ' + div.id + ' to ' + maxW + 'x' + maxH);
    var imgs = div.getElementsByTagName('img');
    for (var i = 0; i < imgs.length; i++) {
        var img = imgs[i];
        resizeAsset(getStyle(img), maxW, maxH, img.getAttribute('podia-w'), img.getAttribute('podia-h'));
    }
    var objs = div.getElementsByTagName('object');
    for (var i = 0; i < objs.length; i++) if (objs[i].id && objs[i].id.indexOf('swf') >= 0) {
        objs[i].height = maxH;
        objs[i].width = maxW;
        if (objs[i].Redraw) objs[i].Redraw();
        //if (th) th.putDebug('Resize ' + objs[i].id + ' to ' + maxW + 'x' + maxH);
    }
    var ifs = div.getElementsByTagName('iframe');
    for (var i = 0; i < ifs.length; i++) {
        ifs[i].height = maxH;
        ifs[i].width = maxW;
        if (th) th.putDebug('Resize ' + ifs[i].id + ' to ' + maxW + 'x' + maxH);
    }
}

// paneAnimation prototype declarations
paneAnimation.prototype.animatePane = animatePane;
paneAnimation.prototype.animFinished = animFinished;
paneAnimation.prototype.resizeAssets = resizeAssets;

//Globals
function animate(at,name,fn,retry) {
	var pOk=false;
    var pPn=th.getPane(at);
    if (pPn) cancelTimer(pPn.timer);
    var pFl=getObject(name);
    if (pFl) {
        try {
			if (fn<0) pFl.Stop();
			else {
			    if (fn) pFl.GotoFrame(fn);
			    else pFl.Rewind();
                pFl.Play();
                pPn.curFrame=fn;
				effectTicker(at);
    		}
			pOk=true;
			if (th) th.putDebug('animate: '+name+', '+fn+', '+retry);
        }
        catch (ex) {
			if (th) {
			    th.putDebug('animate error for '+name+': '+ex.message);
			    if (pPn&&fn>=0&&retry<10) pPn.timer=setTimeout('animate('+at+',"'+name+'",'+fn+','+(retry+1)+')',250);
			    else th.reportAudit('Error',ex.message);			    
			}	        
        }
    }
    else if (th) th.putDebug('animation not found: '+name);
    return pOk;
}

function cancelTimer(t) {
    if (t) window.clearTimeout(t);
}

function cancelTimers() {
    if (th) {
        cancelTimer(th.timer);
        if (th.panes) for (var i=0;i<th.panes.length;i++) {
            var pPn=this.panes[i];
            if (pPn) cancelTimer(pPn.timer);
        }
    }
}

function checkReady() {
    var pPc=percentLoaded();
    displayStatus(pPc.toFixed(1)+'% complete'); //localisation
    if (th && !th.quiz && (pPc == 100 || th.elapsedTime() > th.stageTimeout)) {
        if (th.player) {
            th.player.set_mediaSource(th.mediaSrc);
            th.retryInterval = null;
        }
        else if (getWmp()) {
            getWmp().Enabled = true;
            if (getWmp().controls) {
                getWmp().controls.play();
                th.retryInterval = null;
            }
        }
        if (th.retryInterval) {
            setTimeout('checkReady()', th.retryInterval);
            th.retryInterval += 1000;
        }
    }
    else setTimeout('checkReady()', 1000);
    return pPc;
}

function displayStatus(status) {
    var div = getElement(th && th.statusId != '' ? th.statusId : 'theatreStatus');
    if (div&&div.innerHTML!=status) div.innerHTML=status;
    if (th) th.putDebug(status);
}

function effectTicker(id) {
    if (th&&!th.tagged) {
        var pPn=th.getPane(id);
        if (pPn) {
            cancelTimer(pPn.timer);
            if (pPn.effects) {
                var pPos=th.getPosition()-pPn.startPos;
                th.putDebug('effectTicker: '+pPos.toFixed(3));
                var ix=-1;
                for (var i=0;i<pPn.effects.length;i+=2) {
                    var pEs=pPn.effects[i];
                    if (pPos<pEs) {
                        pPn.timer=setTimeout('effectTicker('+id+')',1000*(0.05+pEs-pPos));
                        break;
                    }
                    else ix=i/2;
                }
                if (ix>=0) pPn.doEffect(ix);
            }
        }
    }
}

function eventTagger(tag) {
    if (th) {
        th.putDebug('eventTagger: '+tag);
	    if (tag.substring(0,1)=="$") {
	        if (tag.substring(1,10)=="PODIA_END") {
	            th.reportAudit('End');
	        }
	        else if (tag.substring(1,12)=="PODIA_START") {
	            th.reportStart();
	        }
/*	        
	        else if (tag.substring(1,11)=="PODIA_TIME") {
		        th.startTime=(new Date()).getTime()-(parseInt(Cmd.substring(11))*1000);
	        }
	        eventTicker();
*/	        
        }
        else {
            var pEv=tag.split('|');
            for (var i=0;i<th.mks.length-4;i+=4) {
                var pTg=th.mks[i+1];
                if (pTg&&pEv.length&&pEv[0].toLowerCase()==pTg) {
                    if (!th.tagged) {
                        cancelTimer(th.timer);
                        th.tagged=true;
                    }
                    th.rotate(i);
                    if (pEv.length>1) {
                        var pAs=th.mks[i+3];
                        for (var i=0;i<pAs.length;i+=2) {
                            var pPn=th.getPane(pAs[i]);
                            if (pPn&&pPn.effects) pPn.doEffect(parseInt(pEv[1]));
                        }
                    }
                    break;
                } 
            }       
        }
    }
}

function eventTicker() {
    if (th) {
        cancelTimer(th.timer);
        if (th.mks&&!th.tagged) {
            var pPos=th.getPosition();
            th.putDebug('eventTicker ' + pPos.toFixed(3));
            var lastMs = -1;
            for (var i=0;i<th.mks.length;i+=4) {
                var pMs=th.mks[i];
                if (pMs>pPos||i==th.mks.length-1) {
                    if (i>=4 && lastMs != -1) th.rotate(i-4);
                    if (th.status=='Playing'&&pMs>pPos) th.timer=setTimeout('eventTicker()',1000*(0.05+pMs-pPos));
                    break;
                }
                lastMs = pMs;
            }
        }
    }
}

function getElement(en) {
    var pEl = null;
    var d=window.document;
    if (d&&en&&en!='') {
        if (d.getElementById) pEl=d.getElementById(en);
        else if (d.all) pEl=d.all[en];
        else pEl=d[en];
    }
	return pEl;
}
/*
function getEmbed(en) {
    var d=window.document;
    if (d) return d.embeds&&d.embeds[en]?d.embeds[en]:d[en]?d[en]:getElement(en);
}
*/
function getMouseX(e) {
    if (e.pageX) return e.pageX;
    else if (e.clientX) return e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    else return 0;
}

function getMouseY(e) {
    if (e.pageY) return e.pageY;
    else if (e.clientY) return e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    else return 0;
}

function getObject(en) {
    var objs = window.document.getElementsByTagName('object');
    for (var i = 0; i < objs.length; i++) if (objs[i].id && objs[i].id.endsWith(en)) return objs[i];
}

function getStyle(el) {
	if (el&&el.style) return el.style;
	else return el;
}

function getWmp() {
    return getElement('wmpMain');
}

function handleError(url, lineno, msg) {
    displayStatus(msg);
	if (th) {
	    th.putDebug(url+' ('+lineno+') '+msg);
	    th.reportAudit('Error',lineno+': '+msg);
	}
    return true;
}

function jumpPos() {
    if (th) th.setPosition(th.zoomJumpPos);
}

function onPreQuizClose(oWnd, args) {
    if (args) {
        var arg = args.get_argument();
        if (arg) {
            //var parm = arg.parm;
            if (th) th.quiz = false;
        }
    }
}

function onPostQuizClose(oWnd, args) {
}

function padLeft(Mask, Msg) {
	var s=Mask+Msg;
	return s.substr(s.length-Mask.length,Mask.length);
}

function percentLoaded() {
	var pDone=0,pSrc=null,pTot=0;
    var imgs=document.images;
    if (imgs) {
		pTot+=imgs.length;
		for (var i = 0; i < imgs.length; i++) {
		    if (imgs[i].complete) pDone++; 
            else pSrc = imgs[i].src;
		}
    }
    if (th&&th.divs) {
	    th.putDebug('Image progress: '+pDone+' out of '+pTot+(pSrc ? ' ('+pSrc+')' : ''));
		for (var i=0;i<th.divs.length;i++) {
		    var div=getElement(th.divs[i]);
		    if (div) {
                var anim=div.getAttributeNode('anim');
                if (anim) {
                    var pFl=getObject(anim.value);
                    if (pFl) {
                        pTot++;
					    try {
						    pDone+=pFl.PercentLoaded()/100;
						    th.putDebug('Flash progress: '+name+' '+pFl.PercentLoaded()+'%');
					    }
					    catch (ex) {
						    pDone++;
						    th.putDebug('Flash error: '+ex.message);
					    }
                    }
                }
		    }
		}
    }
    if (pTot) return 100*pDone/pTot;
    else return 100;
}

function resizeMedia(width) {
    if (th) th.putDebug('Resize media to ' + width);
    var slv = getObject('slvMain');
    if (slv) resizeSlv(slv, width);
    else resizeWmp(getWmp(), (width < 1 ? 1 : width));
}

function resizeSlides(width, pos) {
    var div = th.getDiv('p5');
    st = getStyle(div);
    if (st && th) {
        st.left = (pos + th.combo_g) + 'px';
        st.width = width + 'px';
        resizeAssets(div, width, th.combo_h);
    }
}

function resizeSlv(slv, width) {
    var st = getStyle(slv);
    if (st) {
        if (th && th.player && th.combo_h) {
            var slvH = width * 9 / 16;  //3/4;
            slvH = (slvH > th.combo_h ? th.combo_h : slvH);
            var slvW = slvH * 16 / 9; // 4/3;
            st.height = slvH + 'px';
            st.width = slvW + 'px';
            if (width > slvW) st.left = (width - slvW) / 2 + 'px';
            else st.left = '0px';
            if (th.combo_h > slvH) st.top = th.combo_t + (th.combo_h - slvH) / 2 + 'px';
            th.putDebug('Resize slv to ' + slvW + 'x' + slvH + ' at ' + st.left + ',' + st.top);
        }
        else st.width = width + 'px';
    }
}

function resizeWmp(wmp, width) {
    if (wmp) {
        if (!width) width = wmp.width;
        if (th && th.combo_h && wmp.currentMedia && wmp.currentMedia.imageSourceWidth) {
            var wmpH = 64 + width * wmp.currentMedia.imageSourceHeight / wmp.currentMedia.imageSourceWidth;
            wmp.height = (wmpH > th.combo_h ? th.combo_h : wmpH);
            var wmpW = (wmp.height - 64) * wmp.currentMedia.imageSourceWidth / wmp.currentMedia.imageSourceHeight;
            wmp.width = (wmpW > 0 ? wmpW : 1);
            var st = getStyle(wmp);
            if (st) {
                if (th.combo_h > wmpH) st.top = th.combo_t + (th.combo_h - wmpH) / 2 + 'px';
                if (width > wmpW) st.left = (width - wmpW) / 2 + 'px';
                else st.left = '0px';
            }
        }
        else wmp.width = width;
    }
}

function showTime() {
	var now=new Date();	
	return '('+padLeft('00',now.getHours())+':'+padLeft('00',now.getMinutes())+':'+padLeft('00',now.getSeconds())+'.'+padLeft('000',now.getMilliseconds())+') ';
}

function sliderValueChanging(sender, args) {
    var pPos = args.get_newValue();
    if (th) {
        var mediaWidth = pPos - th.combo_g;
        if (mediaWidth < 0) mediaWidth = 0;
        var slideWidth = th.combo_w - pPos - th.combo_g;
        if (slideWidth < 0) slideWidth = 0;

        if (pPos > th.combo_p) resizeMedia(mediaWidth);
        resizeSlides(slideWidth, pPos);
        if (pPos < th.combo_p) resizeMedia(mediaWidth);

        th.combo_p = pPos;
    }
}

function slvCurrentStateChanged(sender,EventArgs) {
    if (sender&&th) {
        var status=sender.get_currentState();
        displayStatus(status);
        th.status=status;
        eventTicker();
        /*
        switch (status) {
            case 'Playing':
                eventTicker();
                break;
            default:
                cancelTimers();
        }
        */
    }
    return true;
}

function slvMarkerReached(sender,EventArgs) {
    var marker=EventArgs.get_marker();
    if (marker&&marker.Type=='TEXT') eventTagger(marker.Text);
    else if (th) th.putDebug('Unused marker: '+marker.Type+', '+marker.Text)
    return true;
}

function slvMediaEnded(sender,EventArgs) {
    if (th) {
        th.putDebug('media ended: '+sender.get_currentState());
        th.reportAudit('End');
    }
    return true;
}

function slvMediaFailed(sender,EventArgs) {
    var err=EventArgs.get_error();
    if (err) {
        displayStatus(err.errorMessage);
	    if (th) th.reportAudit('Error',err.errorMessage);
    }
    return true;
}

function slvMediaOpened(sender,EventArgs) {
    if (th) with (th) {
        putDebug('media opened: ' + sender.get_currentState());
        if (!player) player=sender;
        reportStart();
    }
    return true;
}

function slvPluginError(sender,EventArgs) {
    var err=EventArgs.get_error();
    if (err) {
        displayStatus('Error: '+err.errorMessage);
	    if (th) th.reportAudit('Error',err.errorMessage);
    }
    if (th&&!th.player) th.player=sender;
    return true;
}

function slvPluginLoaded(sender,EventArgs) {
    if (th&&!th.player) th.player=sender;
}

function slvVolumeChanged(sender,EventArgs) {
    writeCookie('podiaVolume',sender.get_volume(),30);
    return true;
}

function startTimers() {
    if (th&&th.panes) for (var i=0;i<th.panes.length;i++) {
        var pPn=this.panes[i];
        if (pPn) {
            if(pPn.effects) effectTicker(pPn.id);
            else cancelTimer(pPn.timer);
        } 
    }
}

function theatreClose() {
    if (th) th.reportAudit('Close');
    return true;
}

function wmpBuffering(starting) {
    if (th) th.putDebug('WMP Buffering:' + starting);
    return true;
}

function wmpEndOfStream(result) {
    if (th) th.reportAudit('End');
    return true;
}

function wmpError() {
    if (th) {
        if (getWmp()) with (getWmp()) {
            if (error && error.item(0)) {
                var errDesc = error.item(0).errorDescription;
                displayStatus(errDesc);
                th.reportAudit('WMP Error: ' + errDesc);
            }
        }
    } 
    return true;
}

function wmpStateChange(newSt) {
    if (th) {
        var status = wmpTranslatePlaystate(newSt);
        displayStatus(status);
        th.status = status;
        if (newSt == 3) {
            reportStart();
            resizeWmp(getWmp());
        }
        eventTicker();
    }
    return true;
}

function wmpReadyStateChange(readyState) {
    if (th) th.putDebug('WMP ReadyStateChange:' + readyState);
    eventTicker();
    return true;
}

function wmpScriptCommand(type, command) {
    if (type == 'TEXT') eventTagger(command);
    else if (th) th.putDebug('Unused marker: ' + type + ', ' + command);
    return true;
}

function wmpTranslatePlaystate(wmpState) {
    var res = 'Undefined';
    switch (wmpState) {
        case 1:
            res = 'Stopped';
            break;
        case 2:
            res = 'Paused';
            break;
        case 3:
            res = 'Playing';
            break;
        case 4:
            res = 'Forward';
            break;
        case 5:
            res = 'Rewind';
            break;
        case 6:
            res = 'Buffering';
            break;
        case 7:
            res = 'Waiting';
            break;
        case 8:
            res = 'Ended';
            break;
        case 9:
            res = 'Opening';
            break;
        case 10:
            res = 'Ready';
            break;
        case 11:
            res = 'Reconnecting';
            break;
    }
    return res;
}

function wmpWarning(type, param, description) {
    if (th) th.putDebug('WMP Warning:' + type + ',' + param + ',' + description);
    return true;
}

function writeCookie(name, value, days) {
    if(name&&value&&days) {
        var expire = new Date((new Date()).getTime() + days * 86400000);
        //alert(name + ',' + value + ',' + expire);
        window.document.cookie = name + '=' + escape(value) + '; expires=' + expire.toGMTString();
    }
}

function zoomOut() {
    if (th) th.zoomTimer = setTimeout('zoomThumbnail()', 50);
}

function zoomOver() {
    cancelTimer(th.zoomTimer);
}

function zoomThumbnail(syncTime, imgId, h, w, e) {
    cancelTimer(th.zoomTimer);
    var pDiv = getElement('zoomDiv');
    var pStyle = getStyle(pDiv);
    if (pStyle && th) {
        if (imgId) {
            var pImg = getElement('zoomImage');
            var pLnk = getElement('zoomLink');
            var pSrc = getElement(imgId);
            if (pImg && pSrc) {
                pImg.height = h;
                pImg.src = pSrc.src;
                pImg.width = w;
                if (pLnk) {
                    th.zoomJumpPos = syncTime;
                    pLnk.onclick = jumpPos;
                }
                if (!e) e = window.event;
                if (e) {
                    var x = getMouseX(e), y = getMouseY(e);
                    pStyle.left = x - (x > th.zoomLimitX && th.zoomLimitX ? w : 0) + 'px';
                    pStyle.top = y - (y > th.zoomLimitY && th.zoomLimitY ? h : 0) + 'px';
                    pStyle.zIndex = 28000;
                    th.putDebug('zoomThumbnail: ' + imgId + ' ' + pStyle.left + ',' + pStyle.top);
                }
                else th.putDebug('zoomThumbnail: no window event');
                pStyle.visibility = 'visible';
            }
        }
        else pStyle.visibility = 'hidden';
    }
}

