﻿var parentElement;
var SLIR = {
    fadeSpeed:3,    
    waitLength:5,
    images:null,
    nextImage:0,
    
    createSLImageRotator:function(){
        Silverlight.createObject(
        ServerObjects.SLIRPath,           // Source property value.
        parentElement,                           // DOM reference to hosting DIV tag.
        "SLImageRotator",               // Unique plug-in ID value.
        {                               // Per-instance properties.
            width:'100%',                // Width of rectangular region of 
                                        // plug-in area in pixels.
            height:'100%',               // Height of rectangular region of 
                                        // plug-in area in pixels.
            inplaceInstallPrompt:false, // Determines whether to display 
                                        // in-place install prompt if 
                                        // invalid version detected.
            background:'#FFFFFF',       // Background color of plug-in.
            isWindowless:'false',       // Determines whether to display plug-in 
                                        // in Windowless mode.
            framerate:'24',             // MaxFrameRate property value.
            version:'1.0'               // Silverlight version to use.
        },
        {
            onError:null,               // OnError property value -- 
                                        // event handler function name.
            onLoad:null                 // OnLoad property value -- 
                                        // event handler function name.
        },
        null);
    },
    
    initSilverlight:function(){
        SLIR.images = ServerObjects.SLIRImagePaths;
        parentElement = document.getElementById('SLImageRotatorHost');
        if(Silverlight.isInstalled('1.0')){
            SLIR.createSLImageRotator();
        }
    },
    
    Fade1Completed:function(sender, args){
        var nextSource;
        //if we havent reached the end of the array yet, continue on.  else, start at 1;
        if(SLIR.nextImage < SLIR.images.length){
            nextSource = SLIR.images[SLIR.nextImage];
            SLIR.nextImage++;
        }else{
            nextSource = SLIR.images[0];
            SLIR.nextImage = 1;
        }
        sender.findName('Image1').Source = nextSource;
        sender.findName('Fade2').Begin();
    },
    
    Fade2Completed:function(sender, args){
        var nextSource;
        //if we havent reached the end of the array yet, continue on.  else, start at 1;
        if(SLIR.nextImage < SLIR.images.length){
            nextSource = SLIR.images[SLIR.nextImage];
            SLIR.nextImage++;
        }else{
            nextSource = SLIR.images[0];
            SLIR.nextImage = 1;
        }
        sender.findName('Image2').Source = nextSource;
        sender.findName('Fade1').Begin();
    },
    
    SLImageRotatorLoaded:function(sender, args){
        var fadeSpeedText = '0:0:' + SLIR.fadeSpeed;
        var waitLengthText = '0:0:' + SLIR.waitLength;        
        var sl = document.getElementById('SLImageRotator');
        var animation = sl.content.findName('Ani1');
        animation.Duration = fadeSpeedText;
        animation.BeginTime = waitLengthText;
        animation = sl.content.findName('Ani2');
        animation.Duration = fadeSpeedText;
        animation.BeginTime = waitLengthText;
        animation = sl.content.findName('Ani3');
        animation.Duration = fadeSpeedText;
        animation.BeginTime = waitLengthText;
        animation = sl.content.findName('Ani4');
        animation.Duration = fadeSpeedText;
        animation.BeginTime = waitLengthText;
        //we dont want to start the rotator if there are no images
        if(SLIR.images.length > 0){
            sender.findName('Image1').Source = SLIR.images[0];
            //we dont want to start the rotator if the image count is only 1
            if(SLIR.images.length > 1){
                sender.findName('Image2').Source = SLIR.images[1];
                //if we have three or more, set the nextImage to the 3rd one (index 2).  else, set it to the first (0).
                SLIR.nextImage = SLIR.images.length > 2 ? 2 : 0;
                sender.findName('Fade1').Begin();
            }
        }
    }
};

function SLImageRotatorLoaded(sender, args){
    SLIR.SLImageRotatorLoaded(sender, args);
}

function Fade1Completed(sender, args){
    SLIR.Fade1Completed(sender, args);
}

function Fade2Completed(sender, args){
    SLIR.Fade2Completed(sender, args);
}