﻿function Gallery(fadespeed, imageElementIdArray, 
    thumbnailElementId, statementElementId, buttonElementIdArray, buttonImagePath)
{
    this._fadespeed = fadespeed;
    this._imageElementIdArray = imageElementIdArray;
    this._thumbnailElementId = thumbnailElementId;
    this._statementElementId = statementElementId;
    this._buttonElementIdArray = buttonElementIdArray;
    this._buttonImagePath = buttonImagePath;
   
    this._currIndex = -1;
    this._fading = false;
}

Gallery.prototype.HideImage = function(index, onDone)
{                                                                  
    this._currIndex = -1;               
    opacity(this._imageElementIdArray[index], 100, 0, this._fadespeed, onDone);
}
Gallery.prototype.ShowImage = function(index)
{              
    var o = this;
    opacity(this._imageElementIdArray[index], 0, 100, this._fadespeed, 
        function() { o._fading = false; });
    this._currIndex = index;
}
Gallery.prototype.ChangeImage = function(index)
{
    this._fading = true;
    if (this._currIndex != -1) 
    { 
        var o = this;        
        this.HideImage(this._currIndex, function() { o.ShowImage(index); }); 
    }
    else
    {
        this.ShowImage(index);
    }
}

Gallery.prototype.PrevImage = function() 
{
    if (this._fading) { return false; }
    if (this._currIndex == -1) { return false; }
    this.ChangeImage((this._currIndex == 0)? this._imageElementIdArray.length - 1 : (this._currIndex - 1));
    return false;
}
Gallery.prototype.NextImage = function() 
{
    if (this._fading) { return false; }
    if (this._currIndex == -1) { return false; }
    this.ChangeImage((this._currIndex == this._imageElementIdArray.length - 1)? 0 : (this._currIndex + 1));
    return false;
}
Gallery.prototype.ChangeButtonVisibilty = function(v)
{
    var prevButton = $get(this._buttonElementIdArray[0]);
    prevButton.style.visibility = v;
    var nextButton = $get(this._buttonElementIdArray[1]);
    nextButton.style.visibility = v;
    var thumbButton = $get(this._buttonElementIdArray[2]);
    thumbButton.style.visibility = v;
}
Gallery.prototype.ShowThumbnail = function()
{
    var o = this;
    opacity(this._thumbnailElementId, 0, 100, this._fadespeed, function() { o._fading = false; });
    return false;
}
Gallery.prototype.ChangeThumbnail = function()
{
    if (this._fading) { return false; }
    if (this._currIndex == -1) { return false; }
    this.ChangeButtonVisibilty("hidden");
    var o = this;
    this.HideImage(this._currIndex, function() { o.ShowThumbnail(); }); 
    return false;
}
Gallery.prototype.ClickThumbnail = function(index)
{
    if (this._fading) { return false; }
    var o = this;
    this.ChangeButtonVisibilty("visible");
    opacity(this._thumbnailElementId, 100, 0, this._fadespeed, 
        function() { o.ShowImage(index); });
    return false;
}
Gallery.prototype.ShowStatement = function()
{
    if (this._statementElementId == null)
    {
        return false;
    }
    if (this._fading) { return false; }
    this._fading = true;
    opacity(this._statementElementId, 0, 100, this._fadespeed);
    return false;
}
Gallery.prototype.HideStatement = function()
{   
    var o = this;
    opacity(this._statementElementId, 100, 0, this._fadespeed, 
        function() { o._fading = false; });
    return false;
}
Gallery.prototype.MouseOverPrev = function()
{
    if (this._fading || this._currIndex == -1) { return; }
    var prevButton = $get(this._buttonElementIdArray[0]);
    prevButton.src = this._buttonImagePath[0][1];
}
Gallery.prototype.MouseOutPrev = function()
{
    var prevButton = $get(this._buttonElementIdArray[0]);
    prevButton.src = this._buttonImagePath[0][0];
}
Gallery.prototype.MouseOverNext = function()
{
    if (this._fading || this._currIndex == -1) { return; }
    var nextButton = $get(this._buttonElementIdArray[1]);
    nextButton.src = this._buttonImagePath[1][1];
}
Gallery.prototype.MouseOutNext = function()
{
    var nextButton = $get(this._buttonElementIdArray[1]);
    nextButton.src = this._buttonImagePath[1][0];
}
Gallery.prototype.MouseOverThumbnail = function()
{
    if (this._fading || this._currIndex == -1) { return; }
    var thumbButton = $get(this._buttonElementIdArray[2]);
    thumbButton.src = this._buttonImagePath[2][1]
}
Gallery.prototype.MouseOutThumbnail = function()
{
    var thumbButton = $get(this._buttonElementIdArray[2]);
    thumbButton.src = this._buttonImagePath[2][0];
}