﻿// JavaScript Flash Player Simulation Routines

window.players = [];

// Constructor for ViewPlayer
function ViewPlayer(screenDiv) {
    this.name=screenDiv;
    this.container=null;
    this.panes=[];
    this.paneIndex=0;
    this.paneCount=0;
    this.opacity=1;
    this.delay=10;
    this.timeout=5000;
    this.loaded=false;
    
    this.attach(screenDiv);
}

ViewPlayer.prototype.attach = function(screenDiv) {
    //alert("ViewPlayer.prototype.attach");
    if (screenDiv!=null) {
        this.name=screenDiv;
        this.container = document.getElementById(screenDiv);
        if (this.container!=null) {
            this.loaded = true;
            window.players[this.name] = this;
        }
    }
}

ViewPlayer.prototype.createPane = function(paneDiv) {
    //alert("ViewPlayer.prototype.addPane");
    if (this.loaded) {
        var paneContainer = document.createElement("div");
        if (paneContainer!=null) {
            if (paneDiv==null) paneDiv="vwplayer_pane"+this.paneCount;
            paneContainer.id=paneDiv;
            paneContainer.style.position = "absolute";
            paneContainer.style.pixelLeft = this.container.offsetLeft;
            paneContainer.style.pixelTop = this.container.offsetTop;
            paneContainer.style.pixelWidth = this.container.offsetWidth;
            paneContainer.style.pixelHeight = this.container.offsetHeight;
            paneContainer.style.backgroundColor = this.container.style.backgroundColor;
            paneContainer.style.borderTop = this.container.style.borderTop;
            paneContainer.style.borderBottom = this.container.style.borderBottom;
            paneContainer.style.borderLeft = this.container.style.borderLeft;
            paneContainer.style.borderRight = this.container.style.borderRight;
            paneContainer.style.overflow = "hidden";
            paneContainer.style.visibility = "hidden";
            paneContainer.style.zIndex=20;
            document.getElementsByTagName('body')[0].appendChild(paneContainer);
            //alert(paneContainer.style.pixelTop + "; " + this.container.style.pixelTop + "; " + this.container.offsetTop);
            
            this.panes[this.paneCount] = paneContainer;
            this.paneCount++;
            
            return paneContainer;
        }
    }
    return null;
}

ViewPlayer.prototype.addPane = function(paneDiv) {
    //alert("ViewPlayer.prototype.addPane");
    if (paneDiv!=null) {
        var paneContainer = document.getElementById(paneDiv);
        if (paneContainer!=null) {
            paneContainer.style.position = "absolute";
            paneContainer.style.pixelLeft = this.container.offsetLeft;
            paneContainer.style.pixelTop = this.container.offsetTop;
            paneContainer.style.visibility = "hidden";
            paneContainer.style.zIndex=0;
            //alert(paneContainer.style.pixelTop + "; " + this.container.style.pixelTop + "; " + this.container.offsetTop);
//            paneContainer.style.zIndex = 100;
            this.panes[this.paneCount] = paneContainer;
            this.paneCount++;
        }
    }
}

ViewPlayer.prototype.removePane = function (paneDiv) {
    //alert("ViewPlayer.prototype.removePane");
    if (paneDiv != null) {
        var newpanes = [];
        var paneContainer = document.getElementById(paneDiv);
        if (paneContainer != null) {
            var j = 0;
            for (var i = 0; i < this.paneCount; i++) {
                if (this.panes[i] == paneContainer) continue;
                newpanes[j] = this.panes[i];
                j++;
            }
            this.paneCount = j;
        }
    }
}

ViewPlayer.prototype.getNextPane = function () {
    var nextPane=this.paneIndex+1;
    if (nextPane>=this.paneCount) nextPane=0;
    return nextPane;    
}

ViewPlayer.prototype.setOpacity = function(paneNo, opacity) {
    //alert("ViewPlayer.prototype.fade");
    if (this.container!=null) {
        var paneObj = this.panes[paneNo];
        if (paneObj==null) return;
        if (opacity<0) opacity=0;
        else if (opacity>1) opacity=1;
        if (browser.isIE()) paneObj.style.filter="alpha(opacity=" + (opacity*100) + ")";
        else paneObj.style.opacity=""+opacity;
    }
}

ViewPlayer.prototype.setRepeatTime = function(timeout) {
    //alert("ViewPlayer.prototype.setRepeatTime");
    this.timeout = timeout;
}

ViewPlayer.prototype.reduceOpacity = function() {
    //alert("ViewPlayer.prototype.reduceOpacity");
    if (this.container!=null) {
        var topPane = this.panes[this.paneIndex];
        if (topPane==null) return 0;
        
        this.opacity=this.opacity-0.01;
        if (this.opacity<0) this.opacity=0;
        else if (this.opacity>1) this.opacity=1;
        
        //alert("reduceOpacity: " + topPane.id + " - " + this.opacity);
        
        if (browser.isIE()) topPane.style.filter="alpha(opacity=" + (this.opacity*100) + ")";
        else topPane.style.opacity=""+this.opacity;
        return this.opacity;
    }
    return 0;
}

ViewPlayer.prototype.animate = function(screenDiv) {
    //alert("ViewPlayer.prototype.animate");
    var thisPlayer = window.players[screenDiv];
    if (thisPlayer==null) return;
    if (thisPlayer.container!=null) {
        var topPane = thisPlayer.panes[thisPlayer.paneIndex];
        if (topPane==null) return;
        thisPlayer.setOpacity(thisPlayer.paneIndex, 1);
        topPane.style.zIndex=100;
        topPane.style.visibility="visible";
        
        var bkgPane = thisPlayer.panes[thisPlayer.getNextPane()];
        if (bkgPane==null) return;
        thisPlayer.setOpacity(thisPlayer.getNextPane(), 1);
        bkgPane.style.zIndex=0;
        bkgPane.style.visibility="visible";

        thisPlayer.opacity=1;
        setTimeout("ViewPlayer.prototype.fade('" + thisPlayer.name + "')", thisPlayer.timeout);
    }
}

ViewPlayer.prototype.fade = function(screenDiv) {
    //alert("ViewPlayer.prototype.fade: " + screenDiv);
    var thisPlayer = window.players[screenDiv];
    if (thisPlayer==null) return;
    var opacity = thisPlayer.reduceOpacity();
    if (opacity>0) {
        setTimeout("ViewPlayer.prototype.fade('" + thisPlayer.name + "')", thisPlayer.delay);
    } else {
        var topPane = thisPlayer.panes[thisPlayer.paneIndex];
        if (topPane==null) return;
        topPane.style.visibility="hidden";
        topPane.style.zIndex=0;
        //var nextPane = thisPlayer.getNextPane();
        //thisPlayer.opacity=1;
        //thisPlayer.setOpacity(nextPane, thisPlayer.opacity);
        thisPlayer.paneIndex=thisPlayer.getNextPane();
        thisPlayer.animate(thisPlayer.name);
        //setTimeout("ViewPlayer.prototype.animate('" + thisPlayer.name + "')", thisPlayer.timeout);
    }
}

