$.fn.highlightThumbnail = function() {
    $(this).each(function(){
        var self = $(this);
        if (self.find('span').length == 0){
            self = $(this).parent().prev();
        }
    	$(this).hover(function(){
			self.find("span").stop();
			self.find('span:not(:animated)').animate({opacity: 1}, 200);
		}, function(){
			self.find("span").animate({opacity: 0},200);
		});        
    });
};

$(function(){
    
    $('div#arts > div > a:first-child').each(function(){    
        spanFader = document.createElement('span');
    	myBG = $(this).css("background-image");
    	
    	$(this).append(spanFader);
        spanWidth = $(this).css("width");
        spanHeight = $(this).css("height");
    				
    	$(this).find("span").css({
    		backgroundImage:myBG,
    		backgroundPosition:"bottom right",
    		position:"absolute",
    		display:"block",
    		cursor:"pointer",
    		top:"0px",
    		left:"0px",
    		width:spanWidth,
    		height:spanHeight,
    		opacity:0,
    		visibility:"visible"
    	});
    })
    .highlightThumbnail(); // thumbnail hover
    
    $('div#arts > div > div > a').highlightThumbnail(); // text link hover
  
    // Vertical centering for arts images
    $('div#arts > div > div').each(function(){
        $(this).css({top: (90 / 2) - (this.offsetHeight / 2) + 2});
    });
    
    // Featured item links
    var featuredLinks = $('#featuredLinks a');
    featuredLinks.each(function(i){
        $(this).click(function(){
            featuredLinks.removeClass('selected');
            $(this).addClass('selected');
            $('div#featured > div:not("#featuredLinks")').hide();
            $('div#feature' + (i + 1)).fadeIn();
            return false;
        });
        
    });
    
    // Initialize rotate until user selects something
    $('#featuredLinks a').eq(0).triggerHandler('click');
    var i = 0;
    var rotateFeatures = setInterval(function(){

        if (i == 2) {
            i = 0;
        } else {
            i++;
        }
        $('#featuredLinks a').eq(i).triggerHandler('click', true);
    }, 7000);
    
    $('a').click(function(e, fakeClick){
         if (!fakeClick) clearInterval(rotateFeatures);
    });
    
    $.localScroll();

});