function Lightbox() {
}

Lightbox.prototype = {
    $lightboxNode: null,
    $overlayNode: null,
    $closeNode: null,
    $imageNode: null,
    $imageContainerNode: null,
    $bottomNode: null,
    $titleNode: null,
    $descriptionNode: null,
    $prevNode: null,
    $nextNode: null,
    $nextPrevSeparatorNode: null,

    visible: false,
    clicked: false,

    animationTime: 500,

    init: function(lightboxNode, overlayNode) {
        if(typeof lightboxNode == 'undefined' || !lightboxNode.length) {
            return false;
        }
        this.$lightboxNode = $(lightboxNode);
        this.$overlayNode = $(overlayNode);
        this.$closeNode = $(this.$lightboxNode.find('span.close'))
        this.$imageNode = $(this.$lightboxNode.find('img.image'));
        this.$imageContainerNode = $(this.$lightboxNode.find('div.image-container'));
        this.$bottomNode = $(this.$lightboxNode.find('div.bottom'));
        this.$titleNode = $(this.$lightboxNode.find('div.title'));
        this.$descriptionNode = $(this.$lightboxNode.find('div.description'));
        this.$prevNode = $(this.$lightboxNode.find('span.prev'));
        this.$nextNode = $(this.$lightboxNode.find('span.next'));
        this.$nextPrevSeparatorNode = $(this.$lightboxNode.find('span.next-prev-separator'));

        var _this = this;
        this.$closeNode.click(function() {
            _this.hide();
        });
        this.$overlayNode.click(function() {
            _this.hide();
        });

        this.$imageNode.bind("load", function() {
            _this.center(_this.clicked);
            if(_this.clicked) {
                _this.$lightboxNode.css({opacity:100});
            }
            if ($.browser.msie) {
                setTimeout(function() { 
                    _this.center(_this.clicked)
                }, 200);
            }
        });

        $(window).resize(function() {
            _this.center(true);
        });

        this.$prevNode.hide();
        this.$nextNode.hide();
        this.$nextPrevSeparatorNode.hide();
    },

    openFromLink: function(aNode, click) {
        if(click) {
            this.clicked = true;
        } else {
            this.clicked = false;
        }

        var src = $(aNode).attr('href');
        var description = $(aNode).children('img').attr('alt');
        var galleryId = $(aNode).attr('rel');

        var aNodesInGallery = $(aNode).parent().parent().parent().find('a[rel="'+galleryId+'"]');

        var linksVisible = 0;
        var _this = this;
        for(var i=0; i<aNodesInGallery.length; i++) { 
            var node = aNodesInGallery[i];
            if(node === aNode) { 
                if(i < aNodesInGallery.length - 1) {
                    var nextNode = aNodesInGallery[i+1];
                    this.$nextNode.unbind().click(function() { 
                        _this.openFromLink(nextNode);
                    });
                    this.$nextNode.show();
                    linksVisible++;
                } else {
                    this.$nextNode.unbind().hide();
                }

                if(i > 0) {
                    var prevNode = aNodesInGallery[i-1];
                    this.$prevNode.unbind().click(function() { 
                        _this.openFromLink(prevNode);
                    });
                    this.$prevNode.show();
                    linksVisible++;
                } else {
                    this.$prevNode.unbind().hide();
                }
            }
        }

        if(linksVisible == 2) {
            this.$nextPrevSeparatorNode.show();
        } else {
            this.$nextPrevSeparatorNode.hide();
        }

        this._displayImage(src, $(aNode).attr('lightboxTitle'), description, galleryId);
    }, 

    _displayImage: function(src, title, description, galleryId) {
        this.$imageNode.attr('src', src);
        this.$titleNode.html(title);
        this.$descriptionNode.html(description);

        Cufon.refresh('div#lightbox div.title');

        this.show();

        if(this.clicked) {
            this.$lightboxNode.css({opacity:0});
        }
    },

    show: function() {
        if(!this.visible) {
            this.center(true);
            this.visible = true;
        }

        this.$lightboxNode.show();
        this.$overlayNode.show();
    },

    hide: function() {
        this.visible = false;

        this.$lightboxNode.hide();
        this.$overlayNode.hide();
    },

    center: function(skipAnimation) {
        var newImageHeight = this.$imageNode.height();
        var newImageWidth = this.$imageNode.width();

        var newLightboxHeight = newImageHeight + this.$bottomNode.outerHeight();
        var newLightboxWidth = newImageWidth + parseInt(this.$imageContainerNode.css("padding-right").replace("px", ""));

        var top = (($(window).height() - newLightboxHeight) / 2) + $(window).scrollTop() + "px";
        var left = (($(window).width() - newLightboxWidth) / 2) + $(window).scrollLeft() + "px";
        if(skipAnimation) {
            this.$imageContainerNode.css("height", newImageHeight);
            this.$imageContainerNode.css("width", newImageWidth);
            this.$lightboxNode.css("top", top);
            this.$lightboxNode.css("left", left); 
        } else {
            this.$imageContainerNode.animate({
                height: newImageHeight, 
                width: newImageWidth, 
            }, this.animationTime);

            this.$lightboxNode.animate({
                top: top,
                left: left
            }, this.animationTime);
        }
    },
}

