var cc_kaktus_fimm_search_text = '';
var ie6_mode = false;

jQuery(document).ready(function()
{
    Cufon.replace('#content-navigation ul li a, #navigation > ul > li > a, #content-right h2, #content-text h2', {fontFamily: 'Fedra Sans Std Demi'});
    Cufon.replace('#content-text h1, #slogan h3, .fedrasans', {fontFamily: 'Fedra Sans Std Book'});
    Cufon.replace('#slogan h2', {fontFamily: 'Fedra Sans Std Bold'});
    
    jQuery('#search input.submit').attr('value', '');
    jQuery('#search input.text')
        .bind('focus', function()
        {
            if (jQuery(this).attr('value') == jQuery(this).attr('rel'))
            { 
                jQuery(this).attr('value', '');
            }
        })
        .bind('blur', function()
        {
            if (jQuery(this).attr('value') == '')
            {
                jQuery(this).attr('value', jQuery(this).attr('rel'));
            }
        });
    
    jQuery('#mainpage_slideshow').slideshow_initialize();
    
    jQuery('a[target="_blank"] img').each(function()
    {
        var href = jQuery(this).parent().attr('href');
        
        if (   !href
            || !href.match(/\.(jpe?g|gif|png)$/))
        {
            return;
        }
        
	jQuery(this).parent().addClass('thickbox');
    });
    
    jQuery('#content-text ul, #content-text ol').each(function()
    {
        if (jQuery('body').hasClass('frontpage'))
        {
            return;
        }
        
        if (jQuery(this).attr('class'))
        {
            return;
        }
        
        jQuery(this).addClass('jquery');
        
        jQuery(this).find('li').each(function()
        {
            var html = jQuery(this).html();
            jQuery(this).html('<div class="wrapper">' + html + '</div>');
        });
    });
    
    if (ie6_mode)
    {
        jQuery('img[@src$=".png"]').each(function(i)
        {
            var src = jQuery(this).attr('src');
            src = src.replace(/\.png$/, '.gif');
            jQuery(this).attr('src', src);
        });
    }
});

jQuery.fn.slideshow_toggle = function()
{
    jQuery(this).bind('click', function()
    {
        window.location.hash = '#!' + jQuery(this).attr('href');
        
        jQuery('#mainpage_slideshow > *').fadeTo(500, 0.5);
        
        jQuery(this).oneTime(500, function()
        {
            jQuery.ajax
            (
                {
                    url: jQuery(this).attr('href'),
                    dataType: 'html',
                    success: function(data)
                    {
                        jQuery('#container').find('#mainpage_slideshow > *').remove();
                        jQuery(data).find('#mainpage_slideshow > *').appendTo('#mainpage_slideshow');
                        jQuery('#mainpage_slideshow').slideshow_initialize();
                    }
                }
            );
        });
        
        return false;
    });
}

jQuery.fn.slideshow_initialize = function()
{
    Cufon.replace('.fedrasans', {fontFamily: 'Fedra Sans Std Book'});
    
    jQuery('div.slideshow')
        .bind('mouseover', function()
        {
            jQuery(this).addClass('hover');
        })
        .bind('mouseout', function()
        {
            jQuery(this).removeClass('hover');
        })
        .everyTime(3000, function()
        {
            if (jQuery(this).hasClass('hover'))
            {
                return;
            }
            
            var index = jQuery(this).find('img.selected').prevAll('img').size();
            var next = index + 1;
            
            if (next > jQuery(this).find('img').size() - 1)
            {
                next = 0;
            }
            
            // Fade out the current image
            jQuery(this).find('img').eq(index)
                .removeClass('selected');
            
            // Fade in next image
            jQuery(this).find('img').eq(next)
                .addClass('selected');
        });
    
    jQuery('#mainpage_slideshow').find('a.navigation').slideshow_toggle();
}

var hash = String(window.location.hash);

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