/*
    SLIDESHOW JSLibrary by Dušan Šild
    ver: 0.6
    
    Vstupní parametry :
    
    - Efekt přechodu obrázků
    - objekt obrázku / obrázků
    - nadřazený div obrázku ( pro barevny přechod )
    - pole obrázků k zobrazení
    - způsob výběru obrázku z pole
    - rychlost slideshow ( doba zobrazení jednoho obrázku )
    - rychlost efektu přechodu obrázku
    - výchozí průhlednost obrázku ( průhlednost s jakou má být obrázek zobrazen normálně ) - ne pro efekt 'Prolnutí'


    Návrh:
    
    Efekty přechodu:
        - Bez efektu - obrázek se změní okamžitě bez efektu
        - Přechod přes barvu - obrázek se postupně ztratí, čímž odkryje barevné pozadí za ním. Pak se následují obrázek postupně objeví.
        - Přechod prolnutím - nový obrázek se postupně zneprůhlední před původním. Je nutné mít dva objekty obrázků <img>
        - Přechod zakrytím - nový obrázek překryje původní posunutím. Je nutné mít dva objekty obrázků <img>
        
    Výběr obrázků z pole:
        - Sekvenční - obrázky jsou vybírány popořadě
        - Náhodný - obrázky jsou vybírány náhodně
        
    Další nastavení:
        - tyto nastavení lze v průběhu slideshow běžně měnit
        - Nastavení efektů - nastavení barvy u přechodu 'Přes barvu'. Nastavení směru překrývání u přechodu 'Zakrytím'
        - Nastavení sekvečního výběru - od prvního po poslední nebo opačně
*/
var Slideshows = new Array();


function Slideshow(setEff, setImg1, setImg2, setImgBG, setImages, setImgSelection, setSlideShowSpeed, setEffSpeed, setNormalOpacity)
{
    this.Effect = setEff;
    this.Img1 = setImg1;
    this.Img2 = setImg2;
    this.ImgBG = setImgBG;
    this.Images = setImages;
    this.ImgSelection = setImgSelection;
    this.NormalOpacity = setNormalOpacity;
    this.State = 0;
    this.Pruhlednost = 1;
    this.Vyber = 0;
    this.timer = null;
    this.id = Slideshows.length;
    Slideshows[this.id] = this;
    
    if ( setSlideShowSpeed != 'rand' )
        this.SlideShowSpeed = setSlideShowSpeed * 1000;     // převod ze vteřin na milisekundy
    else
        this.SlideShowSpeed = 'rand';
    
    if ( this.Effect == 'fade')
        this.EffectFunc = EffectFade;
    else if ( this.Effect == 'cross')
        this.EffectFunc = EffectFade;
    else if ( this.Effect == 'slide')
        this.EffectFunc = EffectSlide;
    
    this.SelSpeed = function()
    {
        var temp = (Math.floor(Math.random() * 3) + 2) * 1000;    // nastavi nahodne cislo od 2 do 5s
        return temp;
    }
    
    this.StopSlideShow = function()
    {
        this.State = -1;
    }
    
    switch(setEffSpeed)
    {
        case 'slow':    this.EffSpeed = 0.01;
                        break;
        case 'fast':    this.EffSpeed = 0.04;
                        break;
        case 'superfast':   this.EffSpeed = 0.1;
                            break;
        default:    this.EffSpeed = 0.02;
                    break;
    }
    
    this.SelectImage = function()
    {
        if ( this.ImgSelection == 'rand' )
        {
            this.Vyber = Math.floor(Math.random() * this.Images.length);
            return true;
        }
        else if ( this.ImgSelection == 'seq' )
        {
            this.Vyber++;
            if ( this.Vyber > this.Images.length - 1 )
                this.Vyber=0;
            return true;
        }
        return false;
    }
    
    
    this.FadingSlideShow = function()
    {
        if( this.State == 0 )
        {
            var x = this.EffectFunc(0, 'out', this.Img1);
            if ( x == true )
                this.State = 1;
        }
        else if ( this.State == 1 )
        {
            this.SelectImage();
            this.Img1.src = this.Images[this.Vyber].src;
            this.State = 2;
        }
        else if ( this.State == 2 )
        {
            var x = this.EffectFunc(1, 'in', this.Img1);
            if ( x == true )
                this.State = 3;
        }
        else if ( this.State == 3 )
        {
            clearTimeout(this.timer);
            this.State = 0;
            if ( this.SlideShowSpeed == 'rand' )
                var tempSpeed = this.SelSpeed();
            else
                var tempSpeed = this.SlideShowSpeed;
                     
            this.timer = setTimeout('Slideshows[' + this.id + '].FadingSlideShow()', tempSpeed);
            return;
        }
        else
        {
            clearTimeout(this.timer);
            return;
        }
        this.timer = setTimeout('Slideshows[' + this.id + '].FadingSlideShow()', 10);
    }

    this.CrossfadeSlideShow = function()
    {
        if ( this.State == 0 )   // zmizeni druheho obrazku
        {        
            var x = this.EffectFunc(0, 'out', this.Img2);
            if ( x == true )
                this.State = 1;
        }
        else if ( this.State == 1 ) // druhy obrazek uplne pruhledny
        {
            //alert(obj.Img1.src);
            this.SelectImage();
            this.Img2.src = this.Images[this.Vyber].src;
            this.State = 2;
            
            if ( this.SlideShowSpeed == 'rand' )
                var tempSpeed = this.SelSpeed();
            else
                var tempSpeed = this.SlideShowSpeed;
                
            this.timer = setTimeout('Slideshows[' + this.id + '].CrossfadeSlideShow()', tempSpeed);
            return;
        }
        else if ( this.State == 2 ) // objeveni druhehe obrazku
        {
            var x = this.EffectFunc( 1, 'in', this.Img2);
            if ( x == true )
                this.State = 3;
        }
        else if ( this.State == 3 ) // druhy obrazek plne viditelny
        {
            this.SelectImage();
            this.Img1.src = this.Images[this.Vyber].src;
            this.State = 0;
            
            if ( this.SlideShowSpeed == 'rand' )
                var tempSpeed = this.SelSpeed();
            else
                var tempSpeed = this.SlideShowSpeed;
                
            this.timer = setTimeout('Slideshows[' + this.id + '].CrossfadeSlideShow()', tempSpeed);
            return;
        }
        else
        {
            clearTimeout(this.timer);
            return;
        }
    
        this.timer = setTimeout('Slideshows[' + this.id + '].CrossfadeSlideShow()', 10);
    }
    
    this.RunSlideShow = function()
    {
        if ( this.Effect == 'none')
            ClearSlideShow();
        else if ( this.Effect == 'fade')
            this.FadingSlideShow();
        else if ( this.Effect == 'cross')
            this.CrossfadeSlideShow();
        else if ( this.Effect == 'slide')
            SlidingSlideShow();
    }
}

function ClearSlideShow(obj)
{
    if ( obj.State == 0 )
    {
        obj.SelectImage();
        obj.Img1.src = obj.Images[obj.Vyber].src;
        obj.timer = setTimeout("ClearSlideShow(obj)", obj.SlideShowSpeed);
        return;
    }
    else
    {
        clearTimeout(obj.timer);
        return;
    }
}





function EffectFade(Desired, Type, Img)
{
    if ( Type == 'out')
    {
        if ( this.Pruhlednost > Desired )
        {
            this.Pruhlednost = this.Pruhlednost - this.EffSpeed;
            if ( this.Pruhlednost < Desired )
                this.Pruhlednost = Desired;
            
            Img.style.opacity = ( this.Pruhlednost );
            Img.style.MozOpacity = ( this.Pruhlednost );
            Img.style.KhtmlOpacity = ( this.Pruhlednost );
            Img.style.filter = "alpha(opacity=" + (this.Pruhlednost * 100.0) + ")";                                               
        }
        else
            return true;
    }
    else
    {
        if ( this.Pruhlednost < Desired )
        {
            this.Pruhlednost = this.Pruhlednost + this.EffSpeed;
            if ( this.Pruhlednost > Desired )
                this.Pruhlednost = Desired;
                
                Img.style.opacity = ( this.Pruhlednost );
                Img.style.MozOpacity = ( this.Pruhlednost );
                Img.style.KhtmlOpacity = ( this.Pruhlednost );
                Img.style.filter = "alpha(opacity=" + (this.Pruhlednost * 100.0) + ")";
                //obraz.style.webkit-opacity = ( pruhl );
        }
        else
            return true;
    }
    return false;
}
