
SlideShow = function() { this.init.apply(this, arguments); }

SlideShow.prototype = {
    init: function(options) {
        var dOptions = {
            autoStart: true,
            interval: 7000,
            selector: '#ss',
            stopOnHover: true,
            fade: 700
        };
        this.options = jQuery.extend(dOptions, options);
        this.go();
    },
    
    go: function() {
        $(this.options.selector).css('position', 'relative');
        this.divs = $(this.options.selector + '> div').not('.pagination').css({
            display: 'none',
            position: 'absolute'
        }).get();
        this.count = this.divs.length;
        // console.log(this.options.selector + '!' + this.count);
        this.divs[0].style.display = 'block';
        this.curDiv = this.divs[0];
        this.initEvents();
        $('a[href$="' + this.options.selector + '"]:first').addClass('active');
        if (this.options.autoStart) {
            this.start();
        }
    },
    
    initEvents: function() {
        var self = this;
        
        $(this.options.selector + '-controls .ss-next').click(function() {
            self.next.apply(self);
            return false;
        });
        
        $(this.options.selector + '-controls .ss-prev').click(function() {
            self.prev.apply(self);
            return false;
        });
        
        $(this.options.selector + '-controls .ss-start').click(function() {
            self.start.apply(self);
            return false;
        });
        
        $(this.options.selector + '-controls .ss-stop').click(function() {
            self.stop.apply(self);
            return false;
        });
        
        $('a[href$="' + this.options.selector + '"]').click(function() {
            self.stop();
            var cClass = this.className;
            // alert(this + ', ' + cClass);
            var selector = self.options.selector + ' .' + cClass;
            var switchTo = $(selector).get(0);
            
            if (switchTo == self.curDiv) {
                return false;
            }
            
            self.switchPanes.apply(self, [switchTo]);
            
            // Remove active classes from everything and add them back to the current one
            $('a[href$="' + self.options.selector + '"]').removeClass('active');
            $(this).addClass('active');
            
            return false;
        });
        
        if (this.options.stopOnHover) {
            $(this.options.selector + ' div,' + this.options.selector + '-controls .dots').hover(function() { self.stop.apply(self); }, function() { self.start.apply(self); });
        }
    },
    
    getNext: function(div) {
        for (var i = 0; i < this.count; i++) {
            if (this.divs[i] == div) break;
        }
        
        if (i == this.count - 1) return this.divs[0];
        return this.divs[i + 1];
    },
    
    getPrev: function(div) {
        for (var i = 0; i < this.count; i++) {
            if (this.divs[i] == div) break;
        }
        
        if (i == 0) return this.divs[this.count - 1];
        return this.divs[i - 1];
        
    },
    
    start: function() {
        var self = this;
        if (!this.theInterval)
        this.theInterval = setInterval(function() { self.next.apply(self); }, this.options.interval);
    },
    
    stop: function() {
        clearInterval(this.theInterval);
        this.theInterval = 0;
    },
    
    next: function() {
        this.switchPanes(this.getNext(this.curDiv));
    },
    
    prev: function() {
        this.switchPanes(this.getPrev(this.curDiv));
    },
    
    switchPanes: function(newDiv) {
        var self = this;
        // console.log(this.options.selector + ': newDiv:' + newDiv);
        
        // Set appropriate zIndices so that the new one is above the old one.
        this.curDiv.style.zIndex = 50;
        newDiv.style.zIndex = 100;
        
        // console.log('CurDiv zIndex: ' + this.curDiv.style.zIndex + ', newDiv.zIndex: ' + newDiv.style.zIndex);
        
        // Cancel all animations
        // ignore anims for now
        // $(this.curDiv).stop().css('opacity', 1).css('display', 'block');
        
        // Run the function
        if (this.curDiv.finalize) this.curDiv.finalize.apply(this.curDiv);
        // if (this.prevDiv.makeNew) this.prevDiv.makeNew.apply(this.curDiv);
        
        // console.log('switching panes from ' + this.curDiv.className + ' to ' + newDiv.className);
        
        newDiv.finalized = false;
        
        newDiv.finalize = function() {
            // console.log(' Finalizing:' + newDiv.className + ', before finalized');
            if (this.finalized) return false;
            // console.log(' Finalizing:' + newDiv.className);
            // Hide the old div
            $(self.prevDiv).css('display', 'none');
            // Show the current, new div
            $(this).css('display', 'block');//.css('opacity', 1);
            
            this.finalized = true;
            
            // Get rid of this function; we do not need it any more.
            //this.finalize = null;
        };
        
        // CORRECT AT YOUR OWN RISK, SEEMS TO WORK FINE WITH THE TYPO
        $(self.curDid).stop();
        
        self.prevDiv = self.curDiv;
        self.curDiv = newDiv;
        
        $(newDiv)
            .css({'display': 'block', 'opacity': 0})
            .animate({
                'opacity': 1
            }, self.options.fade, newDiv.finalize);
        
        // Remove active classes from all and add them back to just the current one
        $('a[href$="' + self.options.selector + '"]')
            .removeClass('active')
            .each(function() {
                if ($(this).hasClass(newDiv.className)) $(this).addClass('active');
            });
        
        this.stop();
        this.start();
        
    },
    
    getAnchor: function(s) {
        var strReturn = '';
        if ( s.indexOf('#') > -1 ) {
            strReturn = s.substr(s.indexOf('#') + 1).toLowerCase();
        }
        return strReturn;
    }
};
