;(function($) {
    
    $(document).ready(function() {
        var currentTimeout = false,
            currentIndex   = 0,
            paused         = false;
        
        function wait() {
            currentTimeout = setTimeout(function() {
                currentIndex++;
                
                if (!paused) {
                    updateDot();
                    slideImage();
                }
            }, 7000);
        };
        
        function updateDot() {
            $('.splashContainer .indexDotts li').addClass('inactive');
            var dot = $('.splashContainer .indexDotts li').get(currentIndex % $('.splashContainer .item').length);
            
            $(dot).removeClass('inactive');
        };
        
        function slideImage() {
            var prevItem = $('.splashContainer .item').get((currentIndex - 1) % $('.splashContainer .item').length);
            var nextItem = $('.splashContainer .item').get(currentIndex % $('.splashContainer .item').length);
            
            $(prevItem).animate({'left': '-990px'}, 1000, function() {
                $(this).css('left', '990px');
            });
            $(nextItem).animate({'left': '0'}, 1000, function() {
                wait();
            });
        };
        
        function bindPausing() {
            $('.splashContainer .indexDotts li').click(function(e) {
                e.stopPropagation();
                e.preventDefault();
                
                clearTimeout(currentTimeout);
                paused = true;
				
				var index = $('.splashContainer .indexDotts li').index($(this));
                
                if (currentIndex != index) {
                    var prevItem = $('.splashContainer .item').get(currentIndex % $('.splashContainer .item').length);
                    var nextItem = $('.splashContainer .item').get(index % $('.splashContainer .item').length);

                    $('.splashContainer .indexDotts li').addClass('inactive');
                    var dot = $('.splashContainer .indexDotts li').get(index % $('.splashContainer .item').length);
                    
                    $(dot).removeClass('inactive');
                    
                    $(prevItem).animate({'left': '-990px'}, 1000, function() {
                        $(this).css('left', '990px');
                    });
                    $(nextItem).animate({'left': '0'}, 1000);
                    
                    currentIndex = index;
                }
            });
        };
        
        function startDisplay() {
            bindPausing();
            wait();
        };
        
        var srcs = $('.splashContainer .item[data-src]').get();
        
        if (srcs.length == 0) {
        	return;
        }
        
        function loadImages() {
            if (srcs.length == 0) {
                startDisplay();
                return;
            }
            
            var current  = srcs.pop();
            var image    = new Image();
            image.src    = $(current).attr('data-src');
            image.onload = loadImages;
            
            $(current).find('.img').append(image);
        };
        
        function animateStartScreen() {
            var item     = $('.splashContainer .startScreen[data-preload-src]');
            
            if (item.length == 0) {
                loadImages();
            }
            
            var image    = new Image();
            image.src    = item.attr('data-preload-src');
            $(image).css('opacity', '0');
            
            image.onload = function() {
                $(this).animate({'opacity': '1'}, 1000, function() {
                    loadImages();
                });
            };
            
            $('.splashContainer .startScreen').append(image);
        };
        
        animateStartScreen();
        
    });
    
})(jQuery);

