/* Check if afw exists, create if it doesn't */
if( afw === undefined ) var afw = {};
if( afw.image === undefined ) afw.image = {};
afw.image.slider = 
{
    /* Various script settings, default values defined in afw.image.slider.functions.initiate() */
    settings : 
    {
        area : { width : null, height : null }
        ,delay : null
        ,maxRuns : null
        ,fadeStep : null
    }
    
    
    /* Variables used and modified throughout runtime */
    ,runtime : 
    {
        slideCount : 0
        ,isLoaded : false
        ,isDelayed : false
        ,opacityValue : 0
        ,objectIndex : 0
    }
    
    
    /* DOM */
    ,dom : 
    {
        container : null
        ,currentObject : null
        ,previousObject : null
    }
    
    
    /* Preloaded images */
    ,preload : []
    
    
    /* Image list */
    ,objects : []
    
    
    /* Functions used by script */
    ,functions :
    {
        /* Initial function */
        initiate : function()
        {            
            // Check settings and determine default values
            afw.image.slider.settings.area.width = afw.image.slider.methods.checkInt(afw.image.slider.settings.area.width, 600);
            afw.image.slider.settings.area.height = afw.image.slider.methods.checkInt(afw.image.slider.settings.area.height, 250);
            afw.image.slider.settings.delay = afw.image.slider.methods.checkInt(afw.image.slider.settings.delay, 3000);
            afw.image.slider.settings.maxRuns = afw.image.slider.methods.checkInt(afw.image.slider.settings.maxRuns, 1000);
            afw.image.slider.settings.fadeStep = afw.image.slider.methods.checkInt(afw.image.slider.settings.fadeStep, 2);
            
            // Get and manipulate DOM elements
            afw.image.slider.dom.container = afw.image.slider.methods.getElement('afw_imageslider_container');
            afw.image.slider.dom.container.style.width = afw.image.slider.settings.area.width + 'px';
            afw.image.slider.dom.container.style.height = afw.image.slider.settings.area.height + 'px';
            
            // Start slider
            afw.image.slider.functions.run();
        }
        
        /* Advance slider */
        ,run : function()
        {
            // Terminate script if no objects has been set
            if(!afw.image.slider.objects.length || afw.image.slider.runtime.slideCount > afw.image.slider.settings.maxRuns)
                return;
            
            // Halt script if paused
            if(afw.image.slider.runtime.isPaused)
            {
                setTimeout('afw.image.slider.functions.run()', 1000);
                return;
            }
            
            afw.image.slider.runtime.slideCount++;
            afw.image.slider.runtime.isDelayed = false;
            if(afw.image.slider.dom.currentObject === null)
            {
                afw.image.slider.runtime.isDelayed = true;
                afw.image.slider.functions.checkPreload(afw.image.slider.objects[0]);
            }
            else
            {
                setTimeout(function()
                {
                    afw.image.slider.runtime.isDelayed = true;
                }, afw.image.slider.settings.delay);
                afw.image.slider.runtime.objectIndex++;
                if(afw.image.slider.runtime.objectIndex + 1 > afw.image.slider.objects.length)
                    afw.image.slider.runtime.objectIndex = 0;
                if(afw.image.slider.dom.previousObject !== null)
                    afw.image.slider.functions.remove();
                afw.image.slider.dom.previousObject = afw.image.slider.dom.currentObject;
                afw.image.slider.functions.checkPreload(afw.image.slider.objects[afw.image.slider.runtime.objectIndex]);
            }
        }
        
        /* Check if object is preloaded */
        ,checkPreload : function(src)
        {
            var done = false;
            for(var i = 0, m = afw.image.slider.preload.length; i < m; ++i)
            {
                if(afw.image.slider.preload[i] == src)
                {
                    done = true;
                    break;
                }
            }
            if(!done)
            {
                afw.image.slider.runtime.isLoaded = false;
                afw.image.slider.functions.preload(src);
            }
            else
            {
                afw.image.slider.runtime.isLoaded = true;
                afw.image.slider.functions.create(src);
            }
        }
        
        /* Preload object */
        ,preload : function(src)
        {
            var img = new Image(50, 50);
            img.src = src;
            afw.image.slider.preload.push(src);
            afw.image.slider.methods.checkPreload(img);
        }
        
        /* Create object */
        ,create : function(src)
        {
            var img = document.createElement('IMG');
            img.src = src;
            img.id = 'afw_imageslider_' + afw.image.slider.runtime.slideCount;
//            img.style.margin = ((afw.image.slider.settings.area.height / 2) - img.height / 2) + 'px 0px 0px ' + ((afw.image.slider.settings.area.width / 2) - img.width / 2) + 'px';
            afw.image.slider.runtime.opacityValue = 0;
            afw.image.slider.methods.setOpacity(img, afw.image.slider.runtime.opacityValue);
            afw.image.slider.dom.container.appendChild(img);
            afw.image.slider.dom.currentObject = img;
            afw.image.slider.runtime.isLoaded = true;
            afw.image.slider.functions.fade();
        }
        
        /* Remove object */
        ,remove : function()
        {
            afw.image.slider.dom.previousObject.parentNode.removeChild(afw.image.slider.dom.previousObject);
        }
        
        /* Fade object */
        ,fade : function()
        {
            if(!afw.image.slider.runtime.isDelayed || !afw.image.slider.runtime.isLoaded)
                setTimeout('afw.image.slider.functions.fade()', 30);
            else
            {
                afw.image.slider.runtime.opacityValue += afw.image.slider.settings.fadeStep;
                if(afw.image.slider.runtime.opacityValue > 100)
                    afw.image.slider.runtime.opacityValue = 100;
                if(afw.image.slider.dom.currentObject !== null)
                    afw.image.slider.methods.setOpacity(afw.image.slider.dom.currentObject, afw.image.slider.runtime.opacityValue);
                if(afw.image.slider.dom.previousObject !== null)
                    afw.image.slider.methods.setOpacity(afw.image.slider.dom.previousObject, 100 - afw.image.slider.runtime.opacityValue);
                if(afw.image.slider.runtime.opacityValue != 100)
                    setTimeout('afw.image.slider.functions.fade()', 30);
                else
                    afw.image.slider.functions.run();
            }
        }
    }
    
    
    /* Methods used in script */
    ,methods : 
    {
        setOpacity : function(o, i){ o.style.opacity = i / 100; o.style.filter = 'alpha(opacity=' + i +')'; }
        ,getElement : function(id){ var o = null; if(document.getElementById){ if(document.getElementById(id)){ o = document.getElementById(id); }} return o; }
        ,checkPreload : function(img){ if(!img.complete){ setTimeout(function(){afw.image.slider.methods.checkPreload(img);}, 250); } else { afw.image.slider.functions.create(img.src); } }
        ,checkInt : function(i, dValue){ if(!i.toString().match(/^[0-9]+$/)){ return dValue; } else { return parseInt(i); } }
    }
};


// Initiate script when page is fully loaded
AFW_addEvent(window, 'load', afw.image.slider.functions.initiate);