$(document).ready(
    function() {
        $('.galeria').children('img').each(function(index) {
            /*$(this).click(
                function() {
                    doCarousel();
                }
            );
            
            $(this).mouseover(
                function() {
                    clearInterval(interval);
                }
            );
            
            $(this).mouseout(
                function() {
                    var interval = setInterval("carousel_select()",7000);
                }
            );*/
            
            if(index > 0)    
                $(this).hide();
        });

       $('.carrusel_caption').each(function(index) {
            if(index > 0)    
                $(this).hide();
       });
        
        var interval = setInterval("carousel_select()",7000);
    }
);

function doCarousel() {
    $('.galeria').find('img').each(function(index) {
        $($(this).siblings())[index].fadeOut('slow');
        if($(this).is(':visible')) {
            $(this).fadeOut(
                'slow',
                function () {
                    if($(this).next().is('img')) {
                        $(this).next().fadeIn('slow');
                        $($(this).next().siblings())[0].fadeIn('slow');
                    } else {
                        $($('.galeria').find('img')[0]).fadeIn('slow');
                        $($($('.galeria').find('img')[0]).siblings()[0]).fadeIn('slow');
                    }
                }
            );
            return false;
        }
    });
}

function carousel_select(image_to_show) {
    if(!image_to_show) {
        image_to_show = 1;
        $('.galeria').children('img').each(function(index) {
            if($(this).is(':visible')) {
                image_to_show = index + 2;
            }
        });
    }
    
    image_to_show = (image_to_show < 1 || image_to_show > 7) ? 1 : image_to_show;

    $('.galeria').children('img:visible').each(function(index) {
        $(this).fadeOut(
            1000,
            function () {
                $($('.galeria').children('img')[image_to_show - 1]).fadeIn(1000);
                $($('.carrusel_caption')[image_to_show - 1]).fadeIn(1000);
            }
        );
    });
    
    $('.galeria_nav').find('img').each(function(index) {
        var url = $(this).attr('src');
        if(index == (image_to_show - 1)) {
            url = url.replace('pos.png', 'pos_actual.png');
        } else {
            url = url.replace('pos_actual.png', 'pos.png');
            $($('.carrusel_caption')[index]).fadeOut(1000);
        }
        $(this).attr('src', url);
    });
}