/*
    NSG Slideshow class
        version 2.0 (20070220) : complete generalized rewrite to replace the
          custom slideshow code that was scattered around various sites.
*/

function Slideshow(tag, contentVarName) {
    // object properties set by constructor
    this.tag = tag; // I'd rather just get the name of the instance, but this.name doesn't work.  We have to know our name to set up timers to call our member functions, since settimeout appears to see only the global scope
    this.contentVarName = contentVarName;

    // object properties with reasonable defaults - override before starting if you like
    this.interval = 8000;
    this.transition = 'fade';

    // object static work variables
    this.nextDiv = 0;
    this.nextContent = 0;
    this.divNewName = '';
    this.divOldName = '';

    // object member functions
    this.buildNewElement = function() {}
    this.start = function(initialTransition) {
        this.nextRunner(initialTransition);
    }
    this.setOpacity = function(e, o) {
        e.style.opacity = o;
        e.style['-moz-opacity'] = o;
        e.style['-khtml-opacity'] = o;
        e.style.filter = 'alpha(opacity='+(o*100)+')';
    }
    this.nextRunner = function(currentTransition) {
        // Wait to evaluate content until we actually run.  This allows us to
        // instantiate the Slideshow before the content is defined, plus it
        // also lets us change the content while the Slideshow is running, if
        // we're feeling really crazy.
        var content = eval(this.contentVarName);

        // Figure out which direction we are transitioning
        var divNewNum = this.nextDiv;
        var divOldNum = (this.nextDiv + 1) % 2;
        this.divNewName = this.tag + divNewNum;
        this.divOldName = this.tag + divOldNum;
        var elementNew = document.getElementById(this.divNewName);
        var elementOld = document.getElementById(this.divOldName);

        // Get ready - make sure we're at a known state
        // Some browsers flicker at 0.99 to 1.00 transition, so stay at max 0.99
        this.setOpacity(elementNew, 0.00);
        this.setOpacity(elementOld, 0.99);

        // user-supplied function to build content
        this.buildNewElement(this.nextDiv, content[this.nextContent]);

        // Run the transition
        eval("this."+currentTransition+"Transition()");

        // Set next content
        this.nextContent++;
        if (this.nextContent >= content.length) this.nextContent = 0;

        // Set next div
        this.nextDiv = (this.nextDiv + 1) % 2;

        // Let's do this again soon...
        setTimeout(this.tag+".nextRunner('"+this.transition+"')", this.interval);
    }

    // transition functions

    this.fadeIn = function(divName) {
        var div = document.getElementById(divName);
        var currentOpacity = parseFloat(div.style.opacity);
        if (currentOpacity < 0.99) {
            var newOpacity = currentOpacity + 0.05;
            if (newOpacity > 0.99) newOpacity = 0.99;
            this.setOpacity(div, newOpacity);
            setTimeout(this.tag+".fadeIn('"+divName+"')", 50);
        }
    }

    this.fadeOut = function(divName) {
        var div = document.getElementById(divName);
        var currentOpacity = parseFloat(div.style.opacity);
        if (currentOpacity > 0) {
            var newOpacity = currentOpacity - 0.05;
            if (newOpacity < 0) newOpacity = 0;
            this.setOpacity(div, newOpacity);
            setTimeout(this.tag+".fadeOut('"+divName+"')", 50);
        }
    }

    this.fadeTransition = function() {
        var divNew = document.getElementById(this.divNewName);
        var divOld = document.getElementById(this.divOldName);
        divNew.style.zIndex = 1;
        divOld.style.zIndex = 0;
        this.fadeIn(this.divNewName);
    }

    this.crossfadeTransition = function() {
        this.fadeIn(this.divNewName);
        // We normally wouldn't want to access content here, but crossfade is
        // a special case where a fade is still visible if you're doing a
        // transition from A to A.
        var content = eval(this.contentVarName);
        if (content.length > 1) this.fadeOut(this.divOldName);
    }

    this.swapTransition = function() {
        var divNew = document.getElementById(this.divNewName);
        var divOld = document.getElementById(this.divOldName);
        divNew.style.zIndex = 1;
        divOld.style.zIndex = 0;
        this.setOpacity(divNew, 0.99);
        this.setOpacity(divOld, 0);
    }

}
