var fadeIndex = 0;
var fadeImgsHome = ["/media/images/imagefade/fade-home-1.jpg","/media/images/imagefade/fade-home-2.jpg","/media/images/imagefade/fade-home-3.jpg"];

var fadeTimer = null;

jQuery(function(){
    $("#navigation li:last").addClass("last");
	// External Links
	$('A[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
						   
	// Homepage Fader
	$(".homepage #imagefade").load(function(){
		var img = $('<img src="" alt="" />');
		$(img).attr("src",fadeImgsHome[0]);
		$(this).append(img);
		fadeTimer = setInterval("slideshow(fadeImgsHome)",5000);
	}).trigger("load");
	
	// Navigation
    $("#navigation li").hover(
        function() { 
            if($(this).find('ul:first').css('display')=='none') { 
                $(this).find('ul:first').show(); 
                if(!$(this).find('a:first').hasClass("selected"))
                    $(this).find('a:first').addClass("selected hoverselected");
            }
        },
        function() { 
            $(this).find('ul:first').hide(); 
            if($(this).find('a:first').hasClass("hoverselected"))
                $(this).find('a:first').removeClass("selected hoverselected");
        }
    );  
    
    



});
					
function slideshow(arr) {
	total = $(arr).length;
	fadeIndex += 1;
	if(fadeIndex>=total)
		fadeIndex = 0;		
		$("#imagefade img:first").attr("src",arr[fadeIndex]);
		$("#imagefade img:last").fadeOut("slow",function(){
		$("#imagefade img:last").attr("src",arr[fadeIndex]);
		$("#imagefade img:last").show();
	});
}
