$(document).ready(function() {

    /* Image loading */
    
    $('.loading').each(function(i) {
    
        var self = this;
        var oldImg = $(this).find('img').attr('src');
        var width = $(this).find('img').attr('width');
        var height = $(this).find('img').attr('height');
        $(this).css({'width': width, 'height': height});
        $(this).find('img').remove();
        var img = new Image();
        $(img).load(function() {
            $(self).hide().append(this).fadeIn();
            $(self).removeClass('loading');
        }).attr('src', oldImg);
        
    });
    
    
    /* Image slider */
    
    
    $('.image_slider').each(function(i) {
    
        var cache = [];
        var self = this;
        
        $(this).find('div:first-child').addClass('active');
        
        var list = $(this).find('div img');
        var list_len = list.length;
        
        //$(this).find('div img').remove();
        
        //preloadNextImageSlider(self, list, 0);
        
        activateImageSlider();

    
    }).click(function() {
        var current = ($(this).find('.active')) ? $(this).find('.active') : $(this).find(':first');
        
        var next = ((current.next().length) ? ((current.next().hasClass('active')) ? $(this).find(':first') : current.next()) : $(this).find(':first'));
        
        next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000);

        current.animate({opacity: 0.0}, 1000).removeClass('active');
        
        clearInterval(imageSliderInterval);
        
        activateImageSlider();

    });
    
    var imageSliderInterval;
    function activateImageSlider() {
        imageSliderInterval = window.setInterval(function() {
            $('.image_slider').click();
        }, 6000);
    }
    
});

