
function ImageGalleryLightbox() {
    var _this = this;
    var _images = new Array();
    var _imageThumbs = new Array();

    var _lightbox = null;

    var _template = null;
    var _templateImg = null;
//    var _templatePrevBtn = null;
//    var _templateNextBtn = null;
    var _templateImageList = null;

    var _actImagePosition = 0;

    var _maxThumbHeight = 80;
    var _maxThumbWidth = 80;

    this.setImagesByName = function (properties) {
        var images = document.getElementsByName(properties.imgName);

        if (images && images.length > 0) {
            var img = null;

            for (var i = 0; i < images.length; i++) {
                img = images[i];

                img.gallery = this;
                img.position = i;
                img.className = properties.cssClass;

                img.onclick = function() {
                    this.gallery.open(this.position);
                }

                if (properties.cssHover) {
                    img.onmouseover = function() {
                        this.className = properties.cssHover;
                    }

                    img.onmouseout = function() {
                        this.className = properties.cssClass;
                    }
                }
            }
        }

        _images = images;
        this.prepareImageThumList();
    }

    this.setTemplate = function(template) {
        var templateElement = document.getElementById(template.gallery);

        if (templateElement != null) {
            _template = templateElement;
            
            var templateImgElement = document.getElementById(template.image);
            _templateImg = templateImgElement;

            var templateCloseBtn = document.getElementById(template.close);
            templateCloseBtn.gallery = this;
            templateCloseBtn.style.cursor = 'pointer';
            templateCloseBtn.onclick = function() {
                this.gallery.close();
            }

//            _templatePrevBtn = document.getElementById(template.previous);
//            _templatePrevBtn.gallery = this;
//            _templatePrevBtn.style.cursor = 'pointer';
//            _templatePrevBtn.onclick = function() {
//                this.gallery.setPreviousImg();
//            }
//
//            _templateNextBtn = document.getElementById(template.forward);
//            _templateNextBtn.gallery = this;
//            _templateNextBtn.style.cursor = 'pointer';
//            _templateNextBtn.onclick = function() {
//                this.gallery.setNextImg();
//            }

            _templateImageList = document.getElementById(template.thumbList);
            _templateImageList.gallery = this;
        }
    }

    this.setPreviousImg = function() {
        if (_actImagePosition > 0) {
            _actImagePosition--;

            var img = new Image();
            img.gallery = this;
            img.onload = function() {
                this.gallery.setImg(this.src);
                this.gallery.resize();
            }
            img.src = _images[_actImagePosition].getAttribute('open');

//            this.paintPrevNextBtn();
        }
    }

    this.setNextImg = function() {
        if (_actImagePosition < (_images.length - 1)) {
            _actImagePosition++;

            var img = new Image();
            img.gallery = this;
            img.onload = function() {
                this.gallery.setImg(this.src);
                this.gallery.resize();
            }
            img.src = _images[_actImagePosition].getAttribute('open');

//            this.paintPrevNextBtn();
        }
    }

    this.paintPrevNextBtn = function() {
//        if (_actImagePosition == (_images.length - 1)) {
//            _templateNextBtn.style.display = 'none';
//        } else {
//            _templateNextBtn.style.display = '';
//        }

//        if (_actImagePosition == 0) {
//            _templatePrevBtn.style.display = 'none';
//        } else {
//            _templatePrevBtn.style.display = '';
//        }
    }

    this.setImg = function(imgSource) {
        _templateImg.src = imgSource;
        this.paintImageThumbList(_actImagePosition);

        return;
    }

    this.open = function(position) {
        _actImagePosition = position;

        var img = new Image();
        img.gallery = this;
        img.onload = function() {
            this.gallery.openLightbox(this.src);
        }
        img.src = _images[_actImagePosition].getAttribute('open');
//        this.paintPrevNextBtn();
    }

    this.prepareImageThumList = function() {
        var imgCount = _images.length;

        for (var i = 0; i < imgCount; i++) {
            var imgThumb = new Image();
            imgThumb.maxHeight = _maxThumbHeight;
            imgThumb.maxWidth = _maxThumbWidth;
            imgThumb.onload = function() {
                var imgWidth = this.width;
                var imgHeight = this.height;

                var maxWidth = this.maxWidth;
                var maxHeight = this.maxHeight;

                if (imgWidth > maxWidth || imgHeight > maxHeight) {
                    if (imgWidth >= imgHeight) {
                        this.width = maxWidth;
                        this.height = (this.width / imgWidth) * imgHeight;
                    } else {
                        this.height = maxHeight;
                        this.width = (this.height / imgHeight) * imgWidth;
                    }
                }
            }
            imgThumb.src = _images[i].src;

            _imageThumbs[i] = imgThumb;
        }
    }

    this.setGalleryPosition = function(position) {
        _actImagePosition = position;
    }

    this.paintImageThumbList = function(position) {
        removeChildrensFromElement(_templateImageList);

        var imgThumbContainer = document.createElement('div');
        var imgThumbCount = _imageThumbs.length;

        for (var i = 0; i < imgThumbCount; i++) {
            var imgThumb = document.createElement('img');
            imgThumb.gallery = this;
            imgThumb.position = i;
            imgThumb.src = _imageThumbs[i].src;

            imgThumb.className = (i == _actImagePosition) ? 'OpacityNone' : 'OpacityHalf';
            imgThumb.style.width = _imageThumbs[i].width + "px";
            imgThumb.style.height = _imageThumbs[i].height + "px";
            imgThumb.style.cursor = 'pointer';
            imgThumb.style.marginLeft = '5px';

            imgThumb.onclick = function() {
                this.gallery.setGalleryPosition(this.position);

                var img = new Image();
                img.gallery = this.gallery;
                img.onload = function() {
                    this.gallery.setImg(this.src);
                    this.gallery.resize();
                }
                img.src = _images[this.position].getAttribute('open');

                this.gallery.paintPrevNextBtn();
            }

            imgThumbContainer.appendChild(imgThumb);
        }

        _templateImageList.appendChild(imgThumbContainer);
    }

    this.openLightbox = function(imgSource) {
        this.setImg(imgSource);

        _lightbox = new cLightbox();
        _lightbox.setContent(_template);
        _lightbox.open();

        _lightbox._onclick('close');
    }

    this.resize = function() {
        if (_lightbox != null) {
            _lightbox.resize();
        }
    }

    this.close = function() {
        if (_lightbox != null) {
            _lightbox.close();
        }
    }
}

