﻿
function Gallery()
{
    var _this = this;
    var _display = null;
    var _items = null;
    var _images = null;
    var _buttons = null;
    var _currentIndex = 0;
    var _currentItem = null;
    var _isLoadingImages = false;
    var _loader = null;
    var _loadedImagesCount = 0;
    
    var MAX_IMAGE_HEIGHT = 600;
    var IMAGE_HEIGHT_OFFSET = 110;
    var BUTTON_OFFSET = 2;
    

    _getItems = function()
    {
        if (_items == null)
            _items = _display.find('.image');
        return _items;
    }
    
    _getImages = function()
    {
        if (_images == null)
            _images = _display.find('img');
        return _images;
    }
    
    _getButtons = function()
    {
        if (_buttons == null)
            _buttons = $('.gallery .bottom .button');
        return _buttons;
    }
    
    _getItem = function(index)
    {
        return $(_getItems()[index]);
    }
    
    _getMaxIndex = function()
    {
        return _getItems().length - 1;
    }

    _setCurrentButton = function(index)
    {
        var buttons = _getButtons();

        buttons
            .css({ color: '#444242', backgroundColor: '#c3c2c2' })
            .eq(index + BUTTON_OFFSET).css({ color: '#fff', backgroundColor: '#ce2f2a' });
    }
    
    _setCurrentIndex = function(index)
    {
        _currentIndex = index;
    }

    _setCurrentItem = function(index)
    {
        _setCurrentIndex(index);
        _setCurrentButton(index);
        _centerItem();
    }

    _centerItem = function()
    {
        var currentItem = _getItem(_currentIndex);

        var left = _display.attr("scrollLeft") + currentItem.position().left;
        _display.animate({ scrollLeft: left }, 650);
    }

    _updateLoader = function(value)
    {
        var count = !value ? _loadedImagesCount : value;
        var percent = Math.round((count / _getImages().length) * 100);
        _loader.html("Loading Gallery " + percent + "%");
    }

    _checkLoader = function()
    {
        var loadedCount = 0;
        var cycleCount = 0;

        var intervalID = setInterval(function()
        {
            if (cycleCount == 5 && loadedCount == _loadedImagesCount)
            {
                clearInterval(intervalID);

                _updateLoader(_getImages().length);
                _loader.hide();
                _this.sizeImages();

                if (!_display.is(":visible"))
                {
                    _display.show('slide', { direction: 'right' });
                }
                return;
            }

            if (cycleCount > 10)
            {
                clearInterval(intervalID);
            }

            loadedCount = _loadedImagesCount;
            cycleCount++;

        }, 200);
    }

    // Public Functions
    this.sizeImage = function(image)
    {
        var adjustedHeight = _display.height() - IMAGE_HEIGHT_OFFSET;

        if (adjustedHeight > MAX_IMAGE_HEIGHT)
        {
            adjustedHeight = MAX_IMAGE_HEIGHT;
        }
        
        image.parent().css('height', 'auto');
        image.show().attr('loaded', 'true').height(adjustedHeight);
    }
    
    this.sizeImages = function()
    {
        _getImages().each(function() { _this.sizeImage($(this)); });
    }

    this.initialize = function()
    {
        _display = $('.gallery .display');
        _loader = $('.gallery .loader');

        var images = _display.find('img');

        images.load(function()
        {
            _isLoadingImages = true;
            _loadedImagesCount++;

            _this.sizeImage($(this));

            _updateLoader();

            if (_loadedImagesCount == _getImages().length)
            {
                _loader.hide();

                if (!_display.is(":visible"))
                {
                    _this.sizeImages();
                    _display.show('slide', { direction: 'right' });
                }
            }
        });

        images.each(function()
        {
            var image = $(this);

            if (!image.is("[loaded='true']"))
            {
                _this.sizeImage(image);
            }
        });

        $(window).resize(function()
        {
            _this.sizeImages();
        });

        _checkLoader();
    }

    this.getItem = function(index)
    {
        return _getItem(index);
    }

    this.getItems = function()
    {
        return _getItems();
    }

    this.preview = function(itemIndex)
    {
        if (itemIndex <= 0)
        {
            itemIndex = 0;
        }
        else if (itemIndex > _getMaxIndex())
        {
            itemIndex = 0;
        }

        _setCurrentItem(itemIndex);
    }

    this.previous = function()
    {
        _this.preview(_currentIndex - 1);
    }

    this.next = function()
    {
        _this.preview(_currentIndex + 1);
    }

}



