// Author:: Christian Meyer (mailto:meyer@burda-ic.com)
// Copyright:: Copyright (c) 2008, Burda:ic GmbH
// Codebase: Lightbox 2.04
//

// monkey hack to support:
// - an hidden span tag which contains the title stuff
// - support partly empty content-parts
//
Object.extend(Lightbox.prototype, {
  start: function(imageLink) {
    $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
    // stretch overlay to fill page and fade in
    var arrayPageSize = this.getPageSize();
    $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
    new Effect.Appear(this.overlay, { duration: this.overlayDuration, from: 0.0, to: LightboxOptions.overlayOpacity });
    this.imageArray = [];
    var imageNum = 0;
    if ((imageLink.rel == 'lightbox')){
        // if image is NOT part of a set, add single image to imageArray
        this.imageArray.push([imageLink.href, imageLink.down('span').title]);         
    } else {
        // if image is part of a set..
        this.imageArray = 
            $$(imageLink.tagName + '[href][rel="' + imageLink.rel + '"]').
            collect(function(anchor){ return [anchor.href, anchor.down('span').title]; }).
            uniq();
        
        while (this.imageArray[imageNum][0] != imageLink.href) { imageNum++; }
    }
    // calculate top and left offset for the lightbox 
    var arrayPageScroll = document.viewport.getScrollOffsets();
    var lightboxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
    var lightboxLeft = arrayPageScroll[0];
    this.lightbox.setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();
    this.changeImage(imageNum);
  },
  updateDetails: function() {
    this.caption.update(this.imageArray[this.activeImage][1]).show();
    // if image is part of set display 'Image x of x' 
    if (this.imageArray.length > 1){
        this.numberDisplay.update( LightboxOptions.labelImage + ' ' + (this.activeImage + 1) + ' ' + LightboxOptions.labelOf + '  ' + this.imageArray.length).show();
    }
    new Effect.Parallel(
      [ 
        new Effect.SlideDown(this.imageDataContainer, { sync: true, duration: this.resizeDuration, from: 0.0, to: 1.0 }), 
        new Effect.Appear(this.imageDataContainer, { sync: true, duration: this.resizeDuration }) 
      ], 
      { 
        duration: this.resizeDuration, 
        afterFinish: (function() {
          // update overlay size and update nav
          var arrayPageSize = this.getPageSize();
          this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
          this.updateNav();
        }).bind(this)
      } 
    );
  }
});
