var next_slide = 0;

if (window.location.hash.match(/#!/))
{
    window.location.href = window.location.hash.replace(/#!/, '');
}

jQuery(document).ready(function()
{
    jQuery('#show_picture').init_flash_replace();
    
    jQuery('#show_picture')
        .bind('contextmenu', function()
        {
            return false;
        });
    
    jQuery('div.news-image ul.thumbnails li a')
        .click(function()
        {
            var href = jQuery(this).attr('href');
            
            jQuery(this).parent().siblings('.selected').removeClass('selected');
            jQuery(this).parent().addClass('selected');
            
            jQuery('div.news-image > img').attr('src', href);
            
            jQuery(this).trigger('blur');
            
            return false;
        });
    
    jQuery('#thumbnails a')
        .bind('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 false;
                }
                
                // ... but continue still hiding the others
                jQuery(this)
                    .removeClass('visible')
                    .css('display', 'none');
            });
            
            // Exists already
            if (jQuery(id).size() > 0)
            {
                // Is hidden, needs to be shown
                if (!jQuery(id).hasClass('visible'))
                {
                    jQuery(id).css('display', 'block');
                }
                
                // End running and don't follow the link
                return false;
            }
            
            window.location.hash = '#!' + jQuery(this).attr('href');
            
            jQuery.ajax
            (
                {
                    url: jQuery(this).attr('href'),
                    success: function(data)
                    {
                        var new_div = jQuery(data).find('#show_picture > div');
                        
                        if (!new_div.size())
                        {
                            return;
                        }
                        
                        new_div
                            .addClass('container')
                            .addClass('visible')
                            .appendTo(jQuery('#show_picture'));
                        
                        new_div.init_flash_replace();
                    }
                }
            );
            
            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)
        .bind('mouseover', function()
        {
            jQuery(this).addClass('hover');
        })
        .bind('mouseout', 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);
        });
    });
}

jQuery.fn.init_flash_replace = function()
{
    img = jQuery(this).find('img');
    
    if (!img.size())
    {
        return;
    }
    
    src = img.attr('rel');
    
    if (!src)
    {
        return;
    }
	
	var flashvars =
	{
    	bgcolor: '0x000000',
    	picurl: src,
    	fill: 'true'
	};
	
	var params =
	{
		menu: 'true',
		allowFullscreen: 'true',
		wmode: 'transparent'
	};
	
	var attributes = {};
	attributes.id = img.attr('id');
	
	var src = '/flash/pictureviewer.swf';
	
	// Lock the height to prevent flickering
	img.css('height', img.height() + 'px');
	
	var id = img.attr('id');
	
	if (!id)
	{
	    do
	    {
	        id = 'flash_replace_' + Math.round(Math.random() * 1000);
	    }
	    while (jQuery('#' + id).size());
	    
	    img.attr('id', id);
	}
	
	// Embed according to the image count
	swfobject.embedSWF
	(
	    src,
	    img.attr('id'),
	    img.width(),
	    img.height(),
	    "9.0.0",
	    false,
	    flashvars,
	    params,
	    attributes
	);
}

if (typeof console == 'undefined')
{
    var console =
    {
        log: function(input)
        {
            // alert(input.toString());
        }
    }
}
