/*  -----------------------------presentation.js documentation-------------------------------<!--
Documentation:
This file is included into an HTML file in the document's head, <SCRIPT LANGUAGE="javascript 1.2" SRC="presentation.js"></SCRIPT>.  In the document's body, to include one or more presentations, use a script similar to the following:
<SCRIPT LANGUAGE="javascript">
createPresentation(name,top,left,restartDelay,numLoops);
name.createItem(name,pathType,startX,startY,endX,endY,speed,whenShow,whenHide);
</SCRIPT>
In the above script, parameters are as follows:
    createPresentation:
        name - what to name the presentation being created
        top - the distance of the top of the presentation from that edge of the screen
        left - the distance of the left of the presentation from that edge of the screen
        restartDelay - the time (in milliseconds) the presentation waits to restart when done
        numLoops - the number of times the presentation executes, or -1 for infinite.
    name.createItem:
        First, note that in "name.createItem", "name" must be the same as the name parameter in           the createPresentation() for the presentation that the item is to be added to.
        name - the name of the item in the presentation.  There must be, in the main body of the
		pathType - the shape of the path to be generated.
        document ahead of the script calling createItem, a DIV by the same name as this                   parameter.  This div must have its position defined in a stylesheet as absolute.
        startX, startY - the starting coordinates, as measured from the presentation's top and 
        left, of the item being created.
        endX, endY - the ending coordinates of the item being created.
        speed - the speed for the item to move from its start point to its end point.
        whenShow - when the item should appear and begin moving from its startpoint.
        whenHide - when the item should disappear.  You can set this to -1 to make the item stay 
        on the screen until the presentation is done executing.
Also, somewhere in the document, the presentation's startPresent method must be called.  This can be somewhere in the body after the presentation is created and its items are added, or it can be called in the onLoad event handler on the BODY tag.  For example,
<BODY  onLoad="yourPresentationName.startPresent();">
Once the presentation has been started, you may also pause the presentation at any time, calling yourPresentationName.pausePresent(1) to pause the presentation or yourPresentationName.pausePresent(0) to unpause the presentation.
The presentation will automatically stop after running through numLoops times.
end of documentation-->*/

presentations = new Array();
curPresNum = 0;
function createPresentation(name,top,left,restartDelay,numLoops){
        presString = name+" = new presentation(";
        presString += "name,curPresNum,top,left,restartDelay,numLoops);";
        eval(presString);
        presentations[curPresNum] = eval(name);
        curPresNum++;
        }
function presentation(name,number,left,top,restartDelay,numLoops){
       this.isNS4 = (document.layers) ? 1 : 0;
	this.name = name;
	this.number = number;
	this.left = left;
	this.top = top;
	this.items = new Array();
	this.isPaused = false;
	this.restartDelay = restartDelay;
	this.numLoops = numLoops;
	this.curLoop = 0;
	this.curItemNum = 0;
	this.createItem = createItem;
	this.startPresent = startPresent;
	this.pausePresent = pausePresent;
	this.restartPresent = restartPresent;
	this.resetPresent = resetPresent;
	this.readyToRestart = false;
	}
function createItem(name,pathType,startX,startY,endX,endY,speed,whenShow,whenHide){
	this.items[this.curItemNum] = new presItem(this,this.curItemNum,name,startX,startY,endX,endY,speed,whenShow,whenHide);
	this.items[this.curItemNum].buildPath(pathType,startX,startY,endX,endY);
	this.curItemNum++;
	}
function presItem(parent,number,name,startX,startY,endX,endY,speed,whenShow,whenHide){
	this.parent = parent;
	this.number = number;
	this.name = name;
	this.layer = (parent.isNS4) ? document.layers[name] : document.all[name];
	this.sty = (parent.isNS4) ? this.layer : this.layer.style;
	this.startX=startX;
	this.startY=startY;
	this.endX=endX;
	this.endY=endY;
	this.speed=speed;
	this.setRunTimer=setRunTimer;
	this.setStopTimer=setStopTimer;
	this.whenShow=whenShow;
	this.whenHide=whenHide;
	this.isDone = false;
	this.buildPath = buildPath;
	this.moveIt = moveIt;
	}
function startPresent(){
       this.curLoop++;
	for (itemCount=0;itemCount<this.items.length;itemCount++){
              this.items[itemCount].sty.top = this.items[itemCount].incs[0].yLoc;
              this.items[itemCount].sty.left = this.items[itemCount].incs[0].yLoc;
              this.items[itemCount].setRunTimer();
              this.items[itemCount].setStopTimer();
		}
        }
function setRunTimer(){
    runString = "presentations["+this.parent.number+"].items["+this.number+"].sty.visibility = (presentations["+this.parent.number+"].isNS4) ? 'show' : 'visible';";
    runString += "presentations["+this.parent.number+"].items["+this.number+"].moveIt(0);";
    clearTimeout(this.runTimer);
    this.runTimer = setTimeout(runString,this.whenShow);
    }
function setStopTimer(){
    stopString = "presentations["+this.parent.number+"].items["+this.number+"].sty.visibility = (presentations["+this.parent.number+"].isNS4) ? 'hide' : 'hidden';";
    clearTimeout(this.stopTimer);
    this.stopTimer = setTimeout(stopString,this.whenHide);
    }
function showStatus(str){
    //window.status = str;
    document.write(str);
    }


function buildPath(pathType,startX,startY,endX,endY){
    this.incs = new Array();
    isVertical = (endX == startX);
    slope = (!isVertical) ? ((endY-startY)/(endX-startX)) : null;
    if (pathType.indexOf("_") != -1) {
        baseType = pathType.split("_")[0];
        param1 = pathType.split("_")[1];
        param2 = pathType.split("_")[2];
        } else {
        baseType = pathType;
        }
    if (isVertical) { 
        loopYEnd = (endY>=startY) ? "yLoop <= (endY-startY)" : "yLoop >= (endY-startY)";
        yIncrement = (endY>=startY) ? "yLoop++" : "yLoop--";
        for(yLoop=0;eval(loopYEnd);eval(yIncrement)){
            this.incs[Math.abs(yLoop)] = {xLoc : startX, yLoc : startY+yLoop};
            }
        return;
        }        
    if (slope <= 1) {
        loopXEnd = (endX>=startX) ? "xLoop <= (endX-startX)" : "xLoop >= (endX-startX)";
        xIncrement = (endX>=startX) ? "xLoop++" : "xLoop--";
        xLength = endX - startX;
        for (xLoop=0;eval(loopXEnd);eval(xIncrement)){
                xVal = xLoop;
                straightYVal = Math.round(slope * xLoop);
                switch (baseType) {
                    case "straight" : 
                        yVal = straightYVal;
                        break;
                    case "sine" : 
                        amplitude = param1;
                        yVal = straightYVal + Math.round(amplitude*Math.sin(xVal)*slope);
                        break;            
                    case "bounce":
                        amplitude = param1;
                        tYVal = Math.round(amplitude*Math.sin(xVal)*slope);
                        yVal = straightYVal + Math.abs(tYVal);
                        break;
                    default: 
                        window.alert("Invalid path type in presentation "+this.parent.number+" item "+this.number+".");
                        break;            
                    }
                dispX = this.parent.left + startX + xVal;
                dispY = this.parent.top + startY + yVal;
                this.incs[Math.abs(xLoop)] = {xLoc : dispX, yLoc : dispY};
                }
        } else {
        yLength = endY - startY;
        loopYEnd = (endY>=startY) ? "yLoop <= (endY-startY)" : "yLoop >= (endY-startY)";
        yIncrement = (endY>=startY) ? "yLoop++" : "yLoop--";
        for (yLoop=0;eval(loopYEnd);eval(yIncrement)){
                yVal = yLoop;
                straightXVal = Math.round(yVal / slope);
                switch (baseType) {
                    case "straight" :
                        xVal = straightXVal;
                        break;
                    case "sine" : 
                        amplitude = param1;
                        xVal = straightXVal+Math.round(amplitude*Math.sin(yVal)/slope);
                        break;
                    case "bounce":
                        amplitude = param1; 
                        tXVal = Math.round(amplitude*Math.sin(yVal)*slope);
                        xVal = straightXVal + Math.abs(tXVal);
                        break;                       
                    }
                dispX = this.parent.left + startX + xVal;
                dispY = this.parent.top + startY + yVal;
                this.incs[Math.abs(xLoop)] = {xLoc : dispX, yLoc : dispY};
                }                 
        }                   
    }
function moveIt(incNum){
    if (this.parent.isPaused) {
        incNum += 1;
        clearTimeout(this.movingTimer);
        repeatString = "presentations["+this.parent.number+"].items["+this.number+"].moveIt("+incNum+");";
        this.movingTimer =setTimeout(repeatString,100);
        return;
        }        
    if (incNum * this.speed > this.incs.length-1) {
        this.isDone = true;
        this.parent.restartPresent(); 
        return;
        }
    this.sty.top = this.incs[incNum*this.speed].yLoc;
    this.sty.left = this.incs[incNum*this.speed].xLoc; 
    incNum++;
    repeatString = "presentations["+this.parent.number+"].items["+this.number+"].moveIt("+incNum+");";
    clearTimeout(this.movingTimer);
    this.movingTimer =setTimeout(repeatString,1);
    }
function pausePresent(runSwitch){
    vis = (this.isNS4) ? "show" : "visible";
    invis = (this.isNS4) ? "hide" : "hidden";
    this.isPaused = runSwitch;
    for (setVis=0;setVis<this.items.length;setVis++){
        this.items[setVis].sty.visibility = (runSwitch) ? invis : vis;
        }
    }
function resetPresent(){
    for (itemCount=0;itemCount<this.items.length;itemCount++){
        this.items[itemCount].sty.visibility = (this.isNS4) ? "hide" : "hidden";
        this.items[itemCount].sty.left = this.left + this.items[itemCount].startX;
        this.items[itemCount].sty.top = this.top + this.items[itemCount].startY;
        this.items[itemCount].isDone = false;
        clearTimeout(this.items[itemCount].stopTimer);
        }
    }
function restartPresent(){
    this.readyToRestart = true;
    for (doneCheck=0;doneCheck<this.items.length;doneCheck++){
        if (!this.items[doneCheck].isDone) this.readyToRestart = false;
        }
    if ((this.curLoop > this.numLoops) && (this.numLoops != -1)) this.readyToRestart = false;
    if (this.readyToRestart) {
        restartString = "presentations["+this.number+"].resetPresent();";
        restartString += "presentations["+this.number+"].startPresent();";
        clearTimeout(this.restartTimer);
        this.restartTimer = setTimeout(restartString,this.restartDelay);
        }
    }
