jQuery(function()
{
    jQuery('#search input[type="text"]')
        .focus(function()
        {
            if (jQuery(this).attr('rel') == jQuery(this).attr('value'))
            {
                jQuery(this).attr('value', '');
            }
        })
        .blur(function()
        {
            if (!jQuery(this).attr('value'))
            {
                jQuery(this).attr('value', jQuery(this).attr('rel'));
            }
        });
    
    Cufon.replace
    (
        '#content-text h1, #content-text h2.section, #content-text .helvetica, #category_chooser ul li a, #content-text div.item-list p.label a, #content-text ul.actions h2',
        {
            fontFamily: 'HelveticaNeue LT 45 Light'
        }
    );
    Cufon.replace
    (
        'ul.fi_protie_navigation li a, #footer a',
        {
            fontFamily: 'HelveticaNeue LT 67 MdCn'
        }
    );
    
    Cufon.replace
    (
        '#fi_dermalogica_speedmapping_results h1',
        {
            fontFamily: 'HelveticaNeue LT 35 Thin'
        }
    );
    
    jQuery('#content-text div.cc_kaktus_shop.item-list a[title]')
        .each(function()
        {
            var title = jQuery(this).attr('title');
            
            if (!title)
            {
                return;
            }
            
            // Remove trailing semicolon
            title = title.replace(/:\s*$/, '');
            
            // If there is a semicolon, remove everything before it
            if (title.match(/:/))
            {
                title = title.replace(/^.+?:\s*/g, '');
            }
            
            // No title, no tooltip needed
            if (!title)
            {
                return;
            }
            
            jQuery('<span class="title">' + title + '</span>')
                .appendTo(jQuery(this));
            
            jQuery(this).attr('title', '');
        });
    
    jQuery('ul.fi_protie_navigation').find('li:first').addClass('first');
    
    // Change the product picture according to the size
    jQuery('#cc_kaktus_shop_item div.left-column div.sizes ol li a').bind('click', function()
    {
        jQuery(this).blur();
        if (jQuery(this).parent().hasClass('selected'))
        {
            return false;
        }
        
        var rel = jQuery(this).attr('rel');
        
        if (!rel)
        {
            return true;
        }
        
        var index = Number(rel.replace(/image:/i, ''));
        
        jQuery('#cc_kaktus_shop_item div.product-image').find('div').each(function()
        {
            if (jQuery(this).css('display') == 'block')
            {
                jQuery(this).fadeOut(500);
            }
        });
        jQuery('#cc_kaktus_shop_item div.product-image').find('div').eq(index).fadeIn(500);
        
        jQuery(this).parents('div.sizes').find('li.selected').removeClass('selected');
        jQuery(this).parent().addClass('selected');
        
        return false;
    });
    
    // Display additional product information
    jQuery('#cc_kaktus_shop_item ul.actions li a.action').bind('click', function()
    {
        var href = jQuery(this).attr('href');
        
        if (href.match(/^http:\/\//i))
        {
            return true;
        }
        
        jQuery(this).blur();
        if (jQuery(this).hasClass('selected'))
        {
            return false;
        }
        
        jQuery(this).parents('ul').find('li.selected').removeClass('selected');
        jQuery(this).parent().addClass('selected');
        
        // Place a wrapper all around
        jQuery('<a id="body_wrapper"></div>')
            .insertBefore('#container')
            .bind('click', function()
            {
                // Close all everything
                jQuery('#cc_kaktus_shop_item ul.actions li.selected a.close').click();
                return false;
            });
        
        return false;
    });
    
    // Display additional product information
    jQuery('#cc_kaktus_shop_item ul.actions li a.close').bind('click', function()
    {
        jQuery('#body_wrapper').remove();
        jQuery(this).parents('li.selected').removeClass('selected');
        return false;
    });
    
    jQuery('#mainpage_wrapper').find('.mainpage-item').addClass('visible');
    
    if (jQuery('#mainpage_wrapper').find('.selected').size() == 0)
    {
        jQuery('#mainpage_wrapper').find('.mainpage-item').eq(0).addClass('selected');
    }
    
    jQuery('#mainpage_wrapper')
        .css('width', (jQuery(this).width() * (jQuery(this).find('.mainpage-item').size() + 1)) + 'px')
        .hover
        (
            function()
            {
                jQuery(this).addClass('hover');
            },
            function()
            {
                jQuery(this).removeClass('hover');
            }
        )
        .everyTime(8000, function()
        {
            if (jQuery(this).hasClass('hover'))
            {
                return;
            }
            
            var all = jQuery(this).find('.mainpage-item').size()
            var next = all - jQuery(this).find('.selected').nextAll('.mainpage-item').size();
            
            if (next >= all)
            {
                next = 0;
            }
            
            jQuery('#mainpage_navigation li.selected').removeClass('selected')
            jQuery('#mainpage_navigation li').eq(next).addClass('selected');
            
            jQuery(this)
                .oneTime(800, function()
                {
                    jQuery(this).find('.selected').removeClass('selected');
                    jQuery(this).find('.mainpage-item').eq(next).addClass('selected');
                });
            
            var margin = '-' + (jQuery(this).find('.mainpage-item').eq(next).width() * (next)) + 'px';
            
            jQuery(this)
                .animate
                (
                    {
                        marginLeft: margin
                    },
                    800
                );
            
            
        });
    
    jQuery('#mainpage_navigation li a')
        .click
        (
            function()
            {
                var index = jQuery(this).parent().prevAll('li').size();
                var margin = '-' + (jQuery('#mainpage_wrapper').find('.mainpage-item').eq(index).width() * (index)) + 'px';
                
                jQuery('#mainpage_wrapper').stopTime();
                jQuery('#mainpage_wrapper')
                    .animate
                    (
                        {
                            marginLeft: margin
                        },
                        800
                    );
                
                jQuery(this).parents('ul').find('li.selected').removeClass('selected');
                jQuery(this).parent().addClass('selected');
                jQuery(this).blur();
                
                return false;
            }
        );
});

