var next_slide = 0;

jQuery(document).ready(function()
{
    jQuery('#thumbnails a').click(function()
    {
        var href = jQuery(this).attr('href');
        
        if (   !href
            || !href.match(/img=/)
            || jQuery(this).hasClass('selected'))
        {
            return false;
        }
        
        // Remove selected status
        jQuery(this).parents('#thumbnails').find('a.selected, img.selected').removeClass('selected');
        
        // Add selected status to the current
        jQuery(this).addClass('selected');
        jQuery(this).find('img').addClass('selected');
        
        var regs = href.match(/img=([0-9a-f]+)/);
        var id = '#id_' + regs[1];
        
        // Hide the visible pictures
        jQuery('#show_picture div.container.visible').each(function(i)
        {
            // ...except for the one clicked, if it is already visible
            if ('#' + jQuery(this).attr('id') == id)
            {
                return;
            }
            
            // ... but continue still hiding the others
            jQuery(this)
                .removeClass('visible')
                .fadeOut(2000);
        });
        
        // Exists already
        if (jQuery(id).size() > 0)
        {
            // Is hidden, needs to be shown
            if (!jQuery(id).hasClass('visible'))
            {
                jQuery(id).fadeIn(2000);
            }
            
            // End running and don't follow the link
            return false;
        }
        
        // Link location
        var href = '/imagetag/' + regs[1] + '/';
        
        // Doesn't exist, create and load
        jQuery('<div></div>')
            .addClass('container')
            .addClass('visible')
            .css('display', 'none')
            .attr('id', id)
            .load(
                href,
                {},
                function()
                {
                    jQuery(this).fadeIn(2000);
                    var alt = jQuery(this).find('img').attr('alt');
                    
                    if (!alt)
                    {
                        return;
                    }
                    
                    var name = new RegExp(alt);
                    var src = jQuery(this).find('img').attr('src');
                    
                    if (src.match(name))
                    {
                        return;
                    }
                    
                    jQuery('<p></p>')
                        .html(alt)
                        .appendTo(jQuery(this));
                }
            )
            .appendTo(jQuery('#show_picture'));
        
        return false;
    });
});

jQuery.fn.initialize_slideshow = function(slides, link)
{
    var slideshow_container = jQuery(this);
    
    // Preload the slides
    for (var i = 0; i < slides.length; i++)
    {
        jQuery('<div></div>')
            .addClass('slide')
            .attr('id', 'slide_' + slides[i])
            .css('display', 'none')
            .load(
                '/imagetag/' + slides[i] + '/',
                {},
                function()
                {
                    jQuery(this).appendTo(slideshow_container);
                    jQuery('<p></p>')
                        .html(jQuery(this).find('img').attr('alt'))
                        .appendTo(jQuery(this));
                    
                    var height = jQuery(this).find('img').attr('height');
                    
                    if (height)
                    {
                        var offset_top = Math.round((400 - height) / 2);
                        jQuery(this).css('margin-top', offset_top + 'px');
                    }
                }
            );
    }
    
    jQuery(this).find('div.slide').each(function()
    {
        var height = jQuery(this).find('img').attr('height');
        
        if (height)
        {
            var offset_top = Math.round((400 - height) / 2);
            jQuery(this).css('margin-top', offset_top + 'px');
        }
    });
    
    jQuery(this).hover(
        function()
        {
            jQuery(this).addClass('hover');
        },
        function()
        {
            jQuery(this).removeClass('hover');
        }
    );
    
    jQuery(this).click(function()
    {
        window.location = link;
    });
    
    var index = 0;
    
    jQuery(this).everyTime(10000, function()
    {
        // Mouse over, don't change the slide
        if (jQuery(this).hasClass('hover'))
        {
            return;
        }
        
        var pack_size = jQuery(this).find('div.slide').size();
        
        jQuery(this).find('div.slide').each(function(i)
        {
            if (!jQuery(this).hasClass('visible'))
            {
                return;
            }
            
            next_slide = i + 1;
            
            if (next_slide == pack_size)
            {
                next_slide = 0;
            }
            
            jQuery(this).removeClass('visible').fadeOut(2000);
        });
        
        jQuery(this).find('div.slide').each(function(i)
        {
            if (i != next_slide)
            {
                return;
            }
            
            jQuery(this).addClass('visible').fadeIn(2000);
        });
    });
}