/* Released: 2010-03-18 15:56:43 */

/**
 * @author Martijn
 */

function LightBox() {
    var me = this;
    if (typeof gLightBoxImages == "undefined" || gLightBoxImages.length == 0)
        return;
    this._maxIndex = gLightBoxImages.length;
    $gJQ('.gallery-background').height($gJQ('body').height()).click(this.close);
    $gJQ('#lightbox-close').click(this.close);
    $gJQ('.gallery-active .next').click(function(e) {
        me.gotoNext();
        e.preventDefault();
    });
    $gJQ('.gallery-active .previous').click(function(e) {
        me.gotoPrevious();
        e.preventDefault();
    });
    if (gLightBoxImages.length > 1) {
        this._buildFilmstrip();
    } else {
        // Hiding filmstrip if array with images has length 1
        $gJQ('#lightbox-filmstrip').css('visibility','hidden');
        $gJQ('.gallery-active .next').css('visibility','hidden');
        $gJQ('.gallery-active .previous').css('visibility','hidden');
    }
    this._buildPreview();
}

LightBox.prototype = {

    _index : 0,
    _minIndex : 0,
    _maxIndex : 0,

    show : function(imgId) {
        var me = this;
        $gJQ('body').addClass('gallery-active');
        for (var i = 0; i < this._maxIndex; i++) {
            if (gLightBoxImages[i].id == imgId) {
                me.gotoIndex(i);
            }
        }
    },

    close : function() {
        $gJQ('body').removeClass('gallery-active');
    },

    gotoIndex : function(idx) {
        this._index = idx;
        this._moveSelectionToIndex(this._index);
        this._toggleArrowButtons(this._index);
        this.showContent(idx);
    },

    gotoNext : function() {
        if (this._index == this._maxIndex - 1)
            return;
        this._index++;
        this._moveSelectionToIndex(this._index);
        this._toggleArrowButtons(this._index);
        this.showContent(this._index);
    },

    gotoPrevious : function() {
        if (this._index == this._minIndex)
            return;
        this._index--;
        this._moveSelectionToIndex(this._index);
        this._toggleArrowButtons(this._index);
        this.showContent(this._index)
    },

    _moveSelectionToIndex : function(index) {
      var filmstrip = document.getElementById("lightbox-filmstrip");
      var activeFrame = filmstrip.getElementsByTagName('li')[index];
      $gJQ('.strong', filmstrip).removeClass('strong');
      $gJQ('img', activeFrame).addClass('strong');
    },

    _toggleArrowButtons : function(index) {
        $gJQ('.gallery-active .next').css('visibility','visible');
        $gJQ('.gallery-active .previous').css('visibility','visible');
        if (index == this._maxIndex - 1)
            $gJQ(".gallery-active .next").css('visibility','hidden');
        if (index == this._minIndex)
            $gJQ('.gallery-active .previous').css('visibility','hidden');
    },
    
    showContent : function(idx) {
        $gJQ('#content-here').empty();
        var contentObj = gLightBoxImages[idx];
        if (contentObj.type == 'video') {
            var lightboxContent = document.getElementById('content-here');
            var swfcontainer = document.createElement('div');
            swfcontainer.id = 'swfcontainer';
            lightboxContent.appendChild(document.createElement('span'));
            lightboxContent.appendChild(swfcontainer);
            swfobject.embedSWF(contentObj.imageurl, 'swfcontainer', contentObj.width, contentObj.height, contentObj.flashversion,"swfobject/expressInstall.swf", {}, {}, {})
        }
        if (contentObj.type == 'image') {
        	$gJQ("#content-here").html("<span></span><img src='"+contentObj.imageurl+"' alt='' />");
        }
    },

    _buildFilmstrip : function() {
        var me = this;
        for (var i = 0; i < gLightBoxImages.length; i++)
            (function(index){
                var x = gLightBoxImages[i];
				var playIcon = (x.type == 'video') ? "<span></span>" : "";

				$gJQ("<li><a href='#'><img src='"+x.thumbnail+"' />"+playIcon+"</a></li>").appendTo("#lightbox-filmstrip")
                	.find("a").click(function(e){
                		me.gotoIndex(index);
                		e.preventDefault();
                	});
            })(i)
    },
    
    _buildPreview : function() {
        var me = this;
        var maxItem = gLightBoxImages.length > 3 ? 3 : gLightBoxImages.length;
        for (var i = 0; i < maxItem; i++) {
            (function(index) {
                var previewItem = gLightBoxImages[index];

                $gJQ("<li><a href='#'><img src='"+previewItem.thumbnail+"' /></a></li>").appendTo('#lightbox-preview')
                	.find("a").click(function(e){
	                	me.show(previewItem.id);
	                	e.preventDefault();
	                })
            })(i)
        }
    }
};
