﻿function SlideShow(name)
{
    this.AnchorElement = null;
    this.CurrentElement = null;
    this.CurrentElementIndex = 0;
    this.Elements = new Array();
    this.Enabled = true;
    this.Initialized = false;
    this.Interval = 5000;
    this.PreviousElement = null;
    this.Name = name;
    this.Started = false;
    this.TransitionDuration = 2000;
}

SlideShow.prototype.AddElement = function(elementId, href)
{
    var element;
    
    element = document.getElementById(elementId);    
    element.AnchorHref = href;    
    this.Elements[this.Elements.length] = element;
}

SlideShow.prototype.SetAnchorHref = function(element)
{
    if (element.AnchorHref && this.AnchorElement) 
    {
        this.AnchorElement.href = element.AnchorHref
    }
}

SlideShow.prototype.ShowImage = function(elementId)
{
    var element, idx, isRunning; 
    
    isRunning = true;
    
    if (this.CurrentElement)
    {
        if (this.CurrentElement.FadeInFx) isRunning = this.CurrentElement.FadeInFx.isRunning;
    }
    
    for (idx = 0; idx < this.Elements.length; idx++)
    {
        element = this.Elements[idx]; 
        
        if (!isRunning)
        {
            if (element.id == elementId)
            {
                clearTimeout(this.Timeout);
                this.CurrentElementIndex = idx - 1;
                this.ShowNext();
            }
        }
    }
}

SlideShow.prototype.ShowNext = function()
{  
    if (this.Started && this.Enabled)
    {   
        this.CurrentElementIndex++;
                
        if (this.CurrentElementIndex >= this.Elements.length) this.CurrentElementIndex = 0;
                
        this.PreviousElement = this.CurrentElement;
        if (this.PreviousElement) this.PreviousElement.FadeOutFx.start();
        
        this.CurrentElement = this.Elements[this.CurrentElementIndex];
        this.CurrentElement.FadeInFx.start();       
        this.SetAnchorHref(this.CurrentElement);        
        
        if (this.OnCurrentIndexChanged) this.OnCurrentIndexChanged(this.CurrentElement);
        
        this.Start();
    }
}

SlideShow.prototype.Start = function()
{
    if (!this.Initialized)
    {           
        for (idx=0; idx<this.Elements.length; idx++)
        {        
            element = this.Elements[idx];            
            element.FadeInFx = new Spry.Effect.Fade(element.id, {duration: this.TransitionDuration, from: '0%', to: '100%'});
            element.FadeOutFx = new Spry.Effect.Fade(element.id, {duration: this.TransitionDuration, from: '100%', to: '0%'});
            
            if (idx == 0) 
            {
                element.FadeInFx.start();
                this.SetAnchorHref(element);
            }
        }
                
        this.Initialized = true;
    }

     this.Timeout = setTimeout(this.Name + '.ShowNext();', this.Interval);  
     this.Started = true; 
}

SlideShow.prototype.Stop = function()
{
    clearTimeout(this.Timeout);
    this.Started = false;
}

SlideShow.prototype.constructor = SlideShow; 