MilwaukeeTool = window.MilwaukeeTool || {};

MilwaukeeTool.ImageGallery = function() {
    var _images = [];
    var _config = {};
    var _initialized = false;
    var _total_batches = 0;
    var _current_batch = 0;
    var _current_index = 0;

    function _preload_images() {
        for (var h = 0; h < _images.length; h++) {
            $.preloadImages(_images[h].src);
        }
    }

    function _init_navigation() {
        $('#' + _config.gallery_root + ' .image-gallery-next:first').click(function() {
            if ((_current_batch + 1) < _total_batches) {
                _current_batch++;
                _load_batch();
            }

            return false;
        });

        $('#' + _config.gallery_root + ' .image-gallery-prev:first').click(function() {
            if (_current_batch > 0) {
                _current_batch--;
                _load_batch();
            }

            return false;
        });
    }

    function _init_thumbs() {
        $('#' + _config.gallery_root + ' ul li a').each(function(i) {
            $(this).click(function() {
                //update current index
                for (var j = 0; j < _images.length; j++) {
                    if ($(this).attr('href') == _images[j].href) {
                        _current_index = j;
                    }
                }

                if (this.title != '') {
                    cmCreatePageElementTag(this.title, "Thumbnail Clicked");
                }

                _config.thumb_click_callback(this);
                return false;
            });
        });
    }

    function _load_batch() {
        var start_index = (_current_batch * _config.batch_size);
        var loop_limit = _config.batch_size;

        if (_images.length < _config.batch_size) {
            loop_limit = _images.length;
        } else {
            if (_images.length - start_index > _config.batch_size) {
                loop_limit = start_index + _config.batch_size;
            } else {
                loop_limit = _images.length;
            }
        }

        var li, anchor, img, text;
        var image_list = $('#' + _config.gallery_root + ' ul:first')[0];

        $(image_list).empty();

        for (var i = start_index; i < loop_limit; i++) {
            li = document.createElement('li');

            anchor = document.createElement('a');
            $(anchor).attr('href', _images[i].href);

            if (_images[i].description != '') {
                $(anchor).attr('title', _images[i].description);
            }

            // if additional attributes were passed, add them to the anchor element
            if (_images[i].attributes !== undefined) {
                for (var custom_attr in _images[i].attributes) {
                    $(anchor).attr(custom_attr, _images[i].attributes[custom_attr]);
                }
            }

            if (i == _current_index) {
                $(li).attr('class', 'selected');
            }

            img = document.createElement('img');

            $(img).attr('src', _images[i].src);

            anchor.appendChild(img);
            li.appendChild(anchor);

            if (_config.show_caption) {
                text = document.createTextNode(_images[i].title);

                anchor = document.createElement('a');
                $(anchor).attr('href', _images[i].href);

                if (_images[i].description != '') {
                    $(anchor).attr('title', _images[i].description);
                }

                // if additional attributes were passed, add them to the anchor element
                if (_images[i].attributes !== undefined) {
                    for (var custom_attr in _images[i].attributes) {
                        $(anchor).attr(custom_attr, _images[i].attributes[custom_attr]);
                    }
                }

                anchor.appendChild(text);
                li.appendChild(anchor);
            }

            image_list.appendChild(li);
        }

        $('#' + _config.gallery_root + ' .image-gallery-current-page:first').text(_current_batch + 1);

        _init_thumbs();

        if (!_initialized) {
            if (_config.init_callback ? true : false) {
                _config.init_callback();
            }

            _initialized = true;
        }
    }

    return {
        init: function(image_array, config) {
            _images = image_array;
            _config = config;
            _total_batches = (Math.ceil(_images.length / _config.batch_size));
            $('#' + _config.gallery_root + ' .image-gallery-total-pages:first').text(_total_batches);
            _preload_images();
            _load_batch();
            _init_navigation();
        }
    }
};