

function barTop() {
    $("#header-target").animate({
        top: 0
    });
}

function movecontactlist( leftValue, topValue ) {
    $("#contactlist").animate({
        top: topValue,
        left: leftValue
    });
}

function smalllogo(leftValue) {
    $("#CHHS").animate({
        width: 280,
        left: leftValue
    });
}

function hideEvents() {
    $("#upcoming").animate({
        left: -800
    });
}

function contentFadeOut() {
    $("#coveragearea").animate({
        top: -500
    });
    $("#bio, #about, #contact, #coveragearea, #events, #links").fadeOut();
    
}

var $nav;
    
$.fn.animationClick = function(callback, target) {

    var stringTarget = target,
        $target = target ? $(target) : null;
    
    return this.click(function(e) {
        
        if (!$(this).hasClass("current")) {
                
            $nav.removeClass("current");
            $(this).addClass("current");
            
            contentFadeOut();
            barTop();
            
            callback();
            
            hideEvents();
            
            if ($target) $target.fadeIn();
            
            window.location.hash = stringTarget;
            
        }
        
        return false;
                
    });
    
}

$(function() {


   

    var $logo = $("#CHHS"),
        $list = $("#contactlist"),
        $header = $("#header-target"),
        $upcoming = $("#upcoming");
      
    
    $nav = $("nav a");

    var origlogoWidth = 540, // or could test after window.load
        origlogoLeft = $logo.position().left,
        origcontactlistTop = $list.position().top,
        origcontactlistLeft = $list.position().left,
        origHeaderTop = $header.css("top"),
        origUpcomingLeft = $upcoming.position().left;
    
    $(".about-link").click(function() {
        if (!$(this).hasClass("current")) {
            $("nav a").removeClass("current");
            $("nav .about-link").addClass("current");
            contentFadeOut();
            barTop();
            movecontactlist(541,0);
            smalllogo(520);
            hideEvents();
            $("#about").fadeIn(); 
        }
    });

    $(".home-link").click(function(e) {
        if (!$(this).hasClass("current")) {
            $("nav a").removeClass("current");
            $("nav .home-link").addClass("current");
            contentFadeOut();
            $logo.animate({
                width: origlogoWidth,
                left: origlogoLeft
            });
            $list.animate({
                top: origcontactlistTop,
                left: origcontactlistLeft
            });
            $header.animate({
                top: origHeaderTop
            });
            $upcoming.animate({
                left: origUpcomingLeft
            });
        }
    });
    
    var $eventsLoad = $("#events ul").html();
    $("#events-target")
        .append($eventsLoad)
        .find("li:gt(1)").remove();
        
        
    $("#nav-bio").animationClick(function() {
        movecontactlist(541, 0);
        smalllogo(520);
    }, "#bio");
    
    $("#nav-contact").animationClick(function() {
        movecontactlist(541, 0);
        smalllogo(520);
    }, "#contact");
    
    $("#nav-coveragearea").animationClick(function() {
        movecontactlist(20, 0);
        smalllogo(20);
        $("#coveragearea").fadeIn().animate({ top: 0 });
    }, "#coveragearea");
    
    $(".events-link").animationClick(function() {
        movecontactlist(20, 0);
        smalllogo(20);
        $("#events").fadeIn().animate({ left: 375 }); 
    }, "#events");
    
    $("#nav-links").animationClick(function() {
        movecontactlist(541, 0);
        smalllogo(520);
    }, "#links");
    
    
    var hash = window.location.hash;
    hash = hash.replace(/^#/, '');
    switch (hash) {
        case 'about':
            $("." + hash + "-link:first").trigger("click");
            break;
        case 'events':
            $("." + hash + "-link:first").trigger("click");
            break;
        case 'contact':
            $("#nav-" + hash ).trigger("click");
            break;
        case 'coveragearea':
            $("#nav-" + hash ).trigger("click");
            break;
        case 'bio':
            $("#nav-" + hash ).trigger("click");
            break;
        case 'links':
            $("#nav-" + hash ).trigger("click");
            break;
    }
    
         
       });

