/*
 * jQuery based slideshow v0.1
 * http://borkedlabs.com/
 *
 * Copyright (c) 2009 Marek Roszko
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 *
 */
 
 
    function slidey()
    {
        this.activeslide = '';
        this.activeslideindex = 0;
        this.slides = new Array;
        this.slideslength = 0;
        this.slidestest = new Array;
    }
    
    slidey.prototype.init = function()
    {
        this.slides = $('#slides div.slide');
        this.slideslength = this.slides.length;
        var thisObj = this;
        for( i=0; i < this.slideslength; i++)
        {  
            var indicator = $('<img>').attr('src', '/img/slide_notcurrent.gif');
            indicator.attr('class', 'clickable');
            if( this.activeslide == '' )
            {
                this.activeslideindex = i;
                this.activeslide = this.slides[i];
                $(this.activeslide).css('display', 'block');
                indicator.attr('src', '/img/slide_current.gif');
                indicator.attr('class', '');
            }
            
            //js will update the set index in previously set
            //click events but not if we move it outside the loop
            this.setindicatorclick( indicator, i);
            
            this.slides[i].indicator = indicator;
            $('#indicators').append( indicator );
            
        }
        setInterval( function() { thisObj.nextslide(); }, 20000 );
    }
    
    slidey.prototype.setindicatorclick = function( indicator, slide)
    {
        var thisObj = this;
            indicator.click( function() {
                if( thisObj.activeslideindex != slide )
                {
                    thisObj.switchslides( slide );
                }
            });
    }
    
    slidey.prototype.nextslide = function()
    {
        if( this.activeslideindex+1 >= this.slideslength )
        {
            this.switchslides( 0 );
        }
        else if( this.activeslideindex+1 < this.slideslength )
        {
            this.switchslides( this.activeslideindex+1);
        }
    }
    
    slidey.prototype.switchslides = function( slideindex )
    {
        $(this.activeslide).fadeOut('fast');
         this.activeslide.indicator.attr('src', '/img/slide_notcurrent.gif');
         this.activeslide.indicator.attr('class', 'clickable');
        
        this.activeslideindex = slideindex;
        this.activeslide = this.slides[this.activeslideindex];
        
        this.activeslide.indicator.attr('src', '/img/slide_current.gif');
         this.activeslide.indicator.attr('class', '');
         $(this.activeslide).fadeIn('fast');
    }
