var DxImageSlideShows = [];

function DxImageSlideShowImage( src, width, height, wrapperPaddingTop, href, text, alt )
{
    var imgDom = document.createElement("img");
    imgDom.alt = alt;
    imgDom.src = src;
    this.Img = $(imgDom);

    this.Src = src;
    this.Text = text;
	this.Href = href;
	this.Width = width;
	this.Height = height;
	this.WrapperPaddingTop = wrapperPaddingTop;
}

function DxImageSlideShow( id, interval, autoStart, showControlPanel, height )
{
	this.Id = id;
	this.Images = new Array();
	this.CurrentImageIndex = 0;
	this.Interval = interval;
	this.IsPlaying = false;
	this.TimeoutId = null;
	this.ShowControlPanel = showControlPanel;
	this.Height = height;

	this.__Image = null;
	this.__Link = null;
	this.__Text = null;
	this.__Dom = null;
	this.__StartButton = null;
	this.__Status = null;
	this.__ImageBox = null;
	
	if (autoStart)
	{
		this.Start();
	}
}

DxImageSlideShow.prototype.__GetDom = function()
{
    if (this.__Dom === null)
    {
        this.__Dom = $("#" + this.Id );
    }
    return this.__Dom;
}

DxImageSlideShow.prototype.__GetImageBox = function()
{
    if (this.__ImageBox === null)
    {
        this.__ImageBox = this.__GetDom().find(".dxImageBox");
    }
    return this.__ImageBox;
}

DxImageSlideShow.prototype.__GetImage = function()
{
    if (this.__Image === null)
    {
        this.__Image = this.__GetDom().find(".dxImage");
    }
    return this.__Image;
}

DxImageSlideShow.prototype.__GetStatus = function()
{
    if (this.__Status === null)
    {
        this.__Status = this.__GetDom().find(".dxImageSlideShowStatus");
    }
    return this.__Status;
}

DxImageSlideShow.prototype.__GetText = function()
{
    if (this.__Text === null)
    {
        this.__Text = this.__GetDom().find(".dxImageText");
    }
    return this.__Text;
}

DxImageSlideShow.prototype.__GetStartButton = function()
{
    if (this.__StartButton === null)
    {
        this.__StartButton = this.__GetDom().find(".dxImageSlideShowStartStop");
    }
    return this.__StartButton;
}

DxImageSlideShow.prototype.__GetLink = function()
{
    if (this.__Link === null)
    {
        this.__Link = this.__GetDom().find("a");
    }
    return this.__Link;
}

DxImageSlideShow.prototype.ImageCount = function()
{
	return this.Images.length;
}


DxImageSlideShow.prototype.ChangeImage = function(delta)
{
    this.CurrentImageIndex += delta;
    if (this.CurrentImageIndex >= this.ImageCount())
        this.CurrentImageIndex = 0;
    else if (this.CurrentImageIndex < 0)
        this.CurrentImageIndex = this.ImageCount() - 1;

    var img = this.__GetImage();
    var self = this;
    //img.fadeOut( 150, function() {self.__ChangeImage2(); } );
    self.__ChangeImage2();
}

DxImageSlideShow.prototype.__ChangeImage2 = function()
{
    var imgE = this.__GetImage();

    var img = this.Images[this.CurrentImageIndex];
    img.Img.attr( "class", imgE.attr( "class" ) );
    //img.Img.css("display", "none");

    imgE.replaceWith(img.Img);
    //img.Img.fadeIn(150);
    this.__Image = img.Img;

    this.__GetLink().attr("href", img.Href);

    var imageBox = this.__GetImageBox();
    imageBox.css("padding-top", img.WrapperPaddingTop + "px");
    imageBox.css("height", (this.Height - img.WrapperPaddingTop) + "px");

    this.SetText(img.Text);

    if (this.ShowControlPanel)
    {
        var statusHtml = DxImageSlideShowResources.Status.replace("{0}", this.CurrentImageIndex + 1).replace("{1}", this.ImageCount());
        this.__GetStatus().html(statusHtml);
    }
}

DxImageSlideShow.prototype.SetText = function(text)
{
    this.__GetText().html(text);
}

DxImageSlideShow.prototype.Start = function()
{
    if (this.IsPlaying)
    {
        this.__StopTimeout();
        this.IsPlaying = false;
        if (this.ShowControlPanel)
            this.__GetStartButton().attr( "value", DxImageSlideShowResources.Start);
    }
    else
    {
        this.IsPlaying = true;
        if (this.ShowControlPanel)
            this.__GetStartButton().attr( "value", DxImageSlideShowResources.Stop );
        this.__StartTimeout();
    }
}

DxImageSlideShow.prototype.__StartTimeout = function()
{
    var self = this;
    this.TimoutId = window.setTimeout(function() { self.Play() }, this.Interval);
}

DxImageSlideShow.prototype.__StopTimeout = function()
{
    window.clearTimeout(this.TimoutId);
}

DxImageSlideShow.prototype.Play = function()
{
    if (this.IsPlaying)
        this.ChangeImage(1);

    this.__StartTimeout();
}

if (typeof (DxNotifyScriptsLoaded) !== "undefined" && typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();