﻿function ImageRotator(containerId)
{
    this.Container = $('#' + containerId);
    this.Interval = 5000;
    this.ImageFiles = [];
    this.AllImages = [];
    this.Running = false;
    this.CurrentIndex = 0;
    this.CurrentFrontIndex = 0;
    this.Timer = null;
    
    this.FlipIt = function()
        {
            var index = 0;
	    this.AllImages.each(Function.createDelegate(this, function() { if (index != this.CurrentFrontIndex) this.AllImages.eq(index).css('z-index', 99); index ++; }));

            var back = this.AllImages.eq(this.CurrentFrontIndex);
            this.CurrentFrontIndex = (this.CurrentFrontIndex + 1) % this.AllImages.length;
            var front = this.AllImages.eq(this.CurrentFrontIndex);

            front.hide();
            back.css('z-index', '100');
            front.css('z-index', '666');

            this.CurrentIndex = (this.CurrentIndex + 1) % this.AllImages.length;
                
            front.fadeIn('slow');
        }
        
    this.Start = function()
        {
            if (!this.Running)
            {
                this.Timer = setInterval(Function.createDelegate(this, this.FlipIt), this.Interval);
                this.Running = true;
                this.FlipIt();
            }
        }
        
    this.Stop = function()
        {
            clearInterval(this.Timer);
        }

    this.Init = function()
        {
            var z = 666;
            for (var i = 0; i < this.ImageFiles.length; i ++)
            {
                this.Container.append('<img src="' + this.ImageFiles[i] + '" style="z-index: ' + z + '" />');
                z = 99;
            }
            this.AllImages = this.Container.find('img');
        }
}
