(function($){
    $(function(){
        // css helpers
        $('ul.nav').find('li:last').addClass('last');
        
        // add transparent gradients
        $('#PageContentContainer').prepend('<div id="gradientOverlay-top">');
        $('#pageFooter').prepend('<div id="gradientOverlay-bottom">');
        
        // placeholder text
        $('#searchContainer input[type=text]').placeholder();
        
        // slideshow
        var SLIDES = 3,
            FADE_SPEED = 2500;
        $('div.slideshow').each(function(){
            var container = $(this);
            var slideshow = $('<div id="slideshow">');
            var slide;

            var i;
            for (i=1; i < (SLIDES + 1); i++) {
                slide = $('<div>', {
                    'id': 'slideshowSlide-' + i,
                    'css': {
                        'position': 'absolute',
                        'opacity': 1
                    }
                });
                slideshow.append(slide);
            };
            
            slideshow.prependTo(container);
            slideshow.find('div:last').addClass('active');
            slideshow.find('> div').css({
                'height': container.height(),
                'width': container.innerWidth()
            });
            
            window.setInterval(function() {
                var active = slideshow.find('.active');
                var next = active.next();
                if (!next.length) {
                    next = slideshow.find(':first');
                }
                active.fadeOut();
                next.fadeIn();
                
                active.removeClass('active');
                next.addClass('active');
            }, FADE_SPEED);
            container.addClass('slideshowLoaded');
        });
        
        // feature tooltips
        var COMBINED_BORDER_WIDTH = 2;
        $('#featured.slideshowLoaded li').each(function(){
            var li = $(this);
            var ul = li.closest('ul');
            var parent = ul.parent();
            var content = $('<p>', {
                'css': {
                    'position': 'relative',
                    'text-align': 'right'
                },
                'class': 'tooltip',
                'text': li.find('a').attr('title')
            });
            var container = $('<div>', {
                'css': {
                    'position': 'absolute',
                    'top': ul.position().top,
                    'width': parent.width()
                }
            });
            var background = $('<div>', {
                'css': {
                    'background-color': 'black',
                    'position': 'absolute',
                    'margin-left': COMBINED_BORDER_WIDTH / 2,
                    'width': parent.width() - COMBINED_BORDER_WIDTH
                }
            });
            
            // clear title
            li.find('a').attr('title', '');
            
            li.hover(function(event){
                var offset = 0;
                var opacity = 0.3;
                var speed = 200;
                var centreIndex = Math.floor(($(this).parent().children().length + 1) / 2);
                content.css('text-align', centreIndex > $(this).index() ? 'left' : 'right');
                
                if (event.type === 'mouseenter') {
                    offset = '-' + background.height();
                    opacity = 0.78;
                    speed = 500;
                }
                
                container.stop();
                container.animate({
                    'margin-top': offset,
                    'opacity': opacity
                }, speed);
            });
            
            container.append(background, content);
            parent.append(container);
            background.height(content.outerHeight());
        });
    });
})(jQuery);

// placeholder plugin

(function(jQuery){

jQuery.fn.placeholder = function(elements) {
    $( this ).filter( 'input[type=text]:not(input[title=""])' ).each( function() {
        var EMPTY_CLASSNAME = 'empty';
        $( this ).data( 'placeholder', $( this ).attr( 'title' ));
        $( this ).blur( function() {
            if ( $( this ).val() === '' || $( this ).val() === $( this ).data( 'placeholder' )) {
                $( this ).addClass( EMPTY_CLASSNAME );
                $( this ).val( $( this ).data( 'placeholder' ));
            } else {
                $( this ).removeClass ( EMPTY_CLASSNAME );
            }
        });
        $( this ).focus( function() {
            if ($( this ).hasClass( EMPTY_CLASSNAME )) {
                $( this ).val( '' );
                $( this ).removeClass( EMPTY_CLASSNAME );
            }
        });
        $( this ).triggerHandler( 'blur' );
    });
};

})(jQuery);
