﻿function ImageRotator(ImageList, LinkURLList, TooltipList, FadeImages, ImageFadeSpeed, ImageDisplayTime, AnchorTagId, ImageTagId,
        PreloaderImageTagId, RotatorId, PreviewMode) {
        
    this.ImageList = ImageList;
    this.LinkURLList = LinkURLList;
    this.TooltipList = TooltipList;
    
    this.FadeImages = FadeImages;
    this.ImageFadeSpeed = ImageFadeSpeed;
    this.ImageDisplayTime = ImageDisplayTime;
    this.PreviewMode = PreviewMode;
    
    this.AnchorTagId = AnchorTagId;
    this.ImageTagId = ImageTagId;
    this.PreloaderImageTagId = PreloaderImageTagId;
    this.RotatorId = RotatorId;

    this.Images = this.ImageList.split(';');
    this.LinkURLs = this.LinkURLList.split(';');
    this.ToolTips = this.TooltipList.split(';');

    var currentIndex = 0;
    var rotator = this;

    if (typeof (rotator.ImageFadeSpeed) == 'string')
        rotator.ImageFadeSpeed = rotator.ImageFadeSpeed.replace("'", "");

    this.SwitchImage = function() {
        //Check if we still have an image for the current index
        if ((currentIndex + 1) > rotator.Images.length)
            currentIndex = 0;

        var sImg = $('#' + rotator.ImageTagId);

        //Chnage the image tag to insert into the a Tag
        sImg.attr("src", rotator.Images[currentIndex]);
        sImg.attr("alt", rotator.ToolTips[currentIndex]);
        sImg.attr("title", rotator.ToolTips[currentIndex]);

        var aTag = $('#' + rotator.AnchorTagId);
        aTag.attr("href", rotator.LinkURLs[currentIndex]);

        //Preload the next image in the queue
        var pImg = $('#' + rotator.PreloaderImageTagId);
        if (rotator.Images[currentIndex + 1] != null)
            pImg.attr("src", rotator.Images[currentIndex + 1]);
        //else we have reached the end of the queue and the browser should have
        //cached all the images in the array
        
        currentIndex += 1;
    };

    this.ManageRotationTimeout = function() {
        //Fade the current image opacity to almost black
        //If that option has been selected
        var sImg = $('#' + rotator.ImageTagId);

        if (rotator.FadeImages) {
            if (typeof (rotator.ImageFadeSpeed) == 'number')
                sImg.animate({ opacity: 0.1 }, parseInt(rotator.ImageFadeSpeed),
                    function() {
                        // Animation complete.
                        rotator.SwitchImage();
                    }
                );
            else
                sImg.animate({ opacity: 0.1 }, rotator.ImageFadeSpeed,
                    function() {
                        // Animation complete.
                        rotator.SwitchImage();
                    }
                );
        } else rotator.SwitchImage();

        //Fade the new image back in
        if (rotator.FadeImages) {
            if (typeof (rotator.ImageFadeSpeed) == 'number')
                sImg.animate({ opacity: 1.0 }, parseInt(rotator.ImageFadeSpeed));
            else
                sImg.animate({ opacity: 1.0 }, rotator.ImageFadeSpeed);
        }
    };

    //Initialize the first image
    rotator.SwitchImage();

    //Set the interval to run the timeout handler
    if (!rotator.PreviewMode)
        this.intervalControl = setInterval(rotator.RotatorId + "_Rotator.ManageRotationTimeout()", rotator.ImageDisplayTime);
    else this.intervalControl = null;

    this.ClearIntervalTimer = function() {
        if (rotator != null && rotator.intervalControl != null) {
            clearInterval(rotator.intervalControl);
            rotator = null;
        }
    };

    this.SetIntervalTimer = function() {
        rotator.intervalControl = setInterval(rotator.RotatorId + "_Rotator.ManageRotationTimeout()", rotator.ImageDisplayTime);
    };
}
