Array.prototype.contains = function(item)
{
 return this.indexOf(item) != -1;
};

Array.prototype.indexOf = function(item)
{
 for (var i = 0; i < this.length; i++)
 {
  if (this[i] === item) return i;
 }
 return -1;
};

var Class = function(properties)
{
 var _class = function()
 {
  return this.initialize.apply(this, arguments);
 };

 _class.prototype = properties;
 _class.constructor = Class;
 return _class;
};

IMGFADE = 200
TEXTFADE = 80
TEXTPAUSE = 20

var ImgObject = new Class
(
 {
  initialize: function(img)
  {
   this._node = img;
   this._fade = new Spry.Effect.Fade
   (
    img,
    {
     duration: IMGFADE,
     from: 0,
     to: 100,
     toggle: true
    }
   );

   this._hidden = true;
  },

  hide: function()
  {
   this._fade.start();
   this._hidden = true;
  },

  isHidden: function()
  {
   return this._hidden;
  },

  isShown: function()
  {
   return !this._hidden;
  },

  prepare: function(data)
  {
   this._node.src = data.src;
   this._node.alt = data.alt;
  },

  show: function()
  {
   this._fade.start();
   this._hidden = false;
  }
 }
);

var Gallery = new Class
(
 {
    initialize: function(imgs, startWith)
    {
        var _this = this;
        this._currentIndex = -1;
        if (startWith !== undefined && startWith < imgs.length)
        {
         this._currentIndex = startWith - 1;
        }
        this._imgs = imgs;
        this._nodes =
        {
            img1: new ImgObject(document.getElementById('img1')),
            img2: new ImgObject(document.getElementById('img2')),
            imgDescription: document.getElementById('imgDescription'),
            imgHeadling: document.getElementById('imgHeadline'),
            imgSubline: document.getElementById('imgSubline'),
            imgText: document.getElementById('imgText'),
            imgBackward: document.getElementById('imgBackward'),
            imgForward: document.getElementById('imgForward')
        };

        this._textFadeOut = new Spry.Effect.Fade
        (
            this._nodes.imgDescription,
            {
                duration: TEXTFADE,
                from: 100,
                to: 0,
                toggle: false,
                finish: function()
                {
                    window.setTimeout
                    (
                     function()
                     {
                       _this._showText(_this._imgs[_this._currentIndex]);
                     },
                     TEXTPAUSE
                    );
                }
            }
        );
        this._textFadeIn = new Spry.Effect.Fade
        (
         this._nodes.imgDescription,
         {
          duration: TEXTFADE,
          from: 0,
          to: 100,
          toggle: false
         }
        );

        if (this._imgs) this._showImg(this._currentIndex + 1, true);
        this._addEvents();
        this._cacheImgs();
    },
    _addEvents: function()
    {
     var _this = this;
     this._nodes.imgBackward.onclick = function () {_this.showLastImg();}
     this._nodes.imgForward.onclick = function () {_this.showNextImg();}
    },
    _cacheImgs: function()
    {
        var _cache = [];
        for (var i = 0; i < this._imgs.length; i++)
        {
            _cache.push(new Image());
            _cache[i].src = this._imgs[i].src;
        }
    },
    _fixNavi: function()
    {
        if (this._currentIndex + 1 === this._imgs.length)
        {
         this._nodes.imgForward.style.display = 'none';
        }
        else
        {
         this._nodes.imgForward.style.display = 'block';
        }

        if (this._currentIndex === 0)
        {
         this._nodes.imgBackward.style.display = 'none';
        }
        else
        {
         this._nodes.imgBackward.style.display = 'block';
        }
    },
    _showText: function(data)
    {
     this._nodes.imgHeadling.innerHTML = data.headline;
     this._nodes.imgSubline.innerHTML = data.subline;
     this._nodes.imgText.innerHTML = data.text;
     this._textFadeIn.start();
    },
    _showImg: function(indexToShow, ignore)
    {
     var _this = this;
     var _hidden = this.getHiddenImg();
     var _shown = this.getShownImg();
     var _data = this._imgs[indexToShow];
     _hidden.prepare(_data);
     _hidden.show();
     this._currentIndex = indexToShow;

     if (ignore === undefined ? true : !ignore)
     {
      this._textFadeOut.start();
      _shown.hide();
     }
     else
     {
      this._showText(_data);
     }

     this._fixNavi();
    },
    getHiddenImg: function()
    {
     if (this._nodes.img1.isHidden())
     {
      return this._nodes.img1;
     }
     else
     {
      return this._nodes.img2;
     }
    },
    getShownImg: function()
    {
     if (this._nodes.img1.isShown())
     {
      return this._nodes.img1;
     }
     else
     {
      return this._nodes.img2;
     }
    },
    showLastImg: function()
    {
     this._showImg(this._currentIndex - 1);
    },
    showNextImg: function()
    {
     this._showImg(this._currentIndex + 1);
    },
    hideShownImg: function()
    {
     this.getShownImg().hide();
    }
});


var lightboxx = new function()
{

 this.init = function()
 {
  this.lbdiv = document.getElementById('lightbox');
 };

 this.lbstart = function (startwith, galName)
 {

  if (galName === undefined)
  {
   this._gallery = new Gallery(_imgs, startwith);
  }
  else if (galName == 'gal')
  {
   this._gallery = new Gallery(_gal, startwith);
  }
  else if (galName == 'img')
  {
   this._gallery = new Gallery(_img, startwith);
  }

//  var _gallery = new Gallery(_imgs, startwith);
  this.lbdiv.style.display = 'block';
  //console.log(_gallery);
 };

 this.lbhide = function()
 {
  this.lbout.start();
  setTimeout ('lightboxx.lbhide()', 200)
  //var _gallery = false;

  this.lbin = new Spry.Effect.Fade('lightbox', {duration: 500, from:0, to:100});
  this.lbout = new Spry.Effect.Fade('lightbox', {duration: 200, from:100, to:0});

 };

 this.lbclose = function()
 {
  this.lbdiv.style.display = 'none';
  this._gallery.hideShownImg();
 };

};


