/* slideshow helper classes
 *  requires jquery 1.3+
 *
 * base class 'slideShow()' -
 *  given a ul w/ an id, 'new slideShow(id)' will display one li at a time,
 *  fading from one to the next.
 *
 *  css requirements...
 *      ul must have {position: relative} set
 *      li must have {position: absolute; left: 0; top: 0} set
 *      li.current must have {z-index: ??? }
 *      li.next must have {z-index: ???+1 }
 *
 *      all other styles are up to user.
 */

function slideShow(root_id, slide_speed, transition_speed, mode){
    /* constructor for new slideshow object */
    var self = this;
    this.root = "#" + root_id;
    this.mode = mode || "fade";
    this.slide_speed = slide_speed || 3000;
    this.transition_speed = transition_speed || 1000;

    //toggle hovering flag to disable advance on mouse over
    this.hovering = false;

    $(this.root).hover(
        function () {
            self.hovering = true;
        },
        function () {
            self.hovering = false;
        }
    );

    //reset slideshow & begin timer
    this.reset();
}

slideShow.prototype.reset = function () {
    /* called to reset slideshow to base state */
    var self = this;

    //clear opacity & class-flags for all entries
    $(this.root + ' li').removeClass('current').removeClass('next').css({opacity: 0.0});

    //set first entry as current
    $(this.root + ' li:first').addClass('current').css({opacity: 1.0});

    setTimeout(function(){ self.advance(); }, this.slide_speed);
}

slideShow.prototype.advance = function () {
    /* callback used to advance to next image */
    var self = this;

    if(this.hovering){
        setTimeout(function () { self.advance(); }, this.slide_speed/2);
        return;
    }

    //find current entry
    var current = $(this.root + ' li.current');
    if(!current){
        this.reset();
        return;
    }

    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    var next = current.next();
    if(!next.length){
        next = $(this.root + ' li:first');
    }

    //prep function to transition next -> current
    function after_transition () {
        current.removeClass('current').css({opacity: 0.0});
        next.css({opacity:1.0}).addClass('current').removeClass('next');
        setTimeout(function () { self.advance(); }, self.slide_speed);
    }

    //Set the fade in effect for the next image, show class has higher z-index
    if(this.mode == "fade-down"){
        next.css({opacity: 0.0, top: "-100%"}).addClass('next');
        next.animate({opacity: 1.0, top: "0" }, this.transition_speed, undefined, after_transition);
    }else if(this.mode == "slide-left"){
        next.css({opacity: 1.0, left: "100%"}).addClass('next');
        current.animate({left: "-100%" }, this.transition_speed, undefined);
        next.animate({left: "0" }, this.transition_speed, undefined, after_transition);
    }else{
        next.css({opacity: 0.0}).addClass('next');
        next.animate({opacity: 1.0}, this.transition_speed, undefined, after_transition);
    }
}
