$(document).ready(function() {

    var contentWrapID = '___content-wrapper';
    
    $('#content').wrap('<div id=\"' + contentWrapID + '\"></div>');
    
    function showNewContent() {
        $("#" + contentWrapID).show();
        $('#load').fadeOut();
    }
    
    function pageload(hash) {
        if(hash) {
            $("#" + contentWrapID).load(hash + " #content",'',function(){
            	$("#top a > span").removeClass("selected");
				Shadowbox.clearCache();
				Shadowbox.setup($("#nav1 a[rel^=\'shadowbox\']"));
				if (hash == "photos.html") {
					Shadowbox.setup($("#content a[rel^=\'shadowbox\']"), {gallery: "bandpics"});
					Shadowbox.setup($("#content area[rel^=\'shadowbox\']"), {gallery: "bandmembers"});
				} else if (hash == "shows.html") {
					Shadowbox.setup($("#content a[rel^=\'shadowbox\']"), {gallery: "posters"});
				} else if (hash == "news.html") {
					Shadowbox.setup($("#content a[rel^=\'shadowbox\']"), {});
				} else if (hash == "discography.html") {
					Shadowbox.setup($("#content a[rel^=\'shadowbox\']"), {});
				}
				if (hash == "photos.html") {
					$("title").replaceWith("<title>The Mean - Photos</title>");
					$("#top a > span:contains(\'Photos\')").addClass("selected");
				} else if (hash == "shows.html") {
					$("title").replaceWith("<title>The Mean - Shows</title>");
					$("#top a > span:contains(\'Shows\')").addClass("selected");
				} else if (hash == "news.html") {
					$("title").replaceWith("<title>The Mean - News</title>");
					$("#top a > span:contains(\'News\')").addClass("selected");
				} else if (hash == "discography.html") {
					$("title").replaceWith("<title>The Mean - Records</title>");
					$("#top a > span:contains(\'Records\')").addClass("selected");
				} else if (hash == "index.html") {
					$("title").replaceWith("<title>The Mean - Music</title>");
				} else if (hash == "videos.html") {
					$("title").replaceWith("<title>The Mean - Videos</title>");
					$("#top a > span:contains(\'Videos\')").addClass("selected");
				} else if (hash == "merch.html") {
					$("title").replaceWith("<title>The Mean - Merch</title>");
					$("#top a > span:contains(\'Merch\')").addClass("selected");
				} else if (hash == "links.html") {
					$("title").replaceWith("<title>The Mean - Links</title>");
					$("#top a > span:contains(\'Links\')").addClass("selected");
				} else if (hash == "list.html") {
					$("title").replaceWith("<title>The Mean - Mailing List</title>");
				}
				if (hash == "index.html") {
					$("body").addClass("home");
				} else {
					$("body").removeClass("home");
				}
                if($('img:last',this).get(0)) {
                    $('img:last',this).load(function(){
                        showNewContent();
                    });
                } else {
                    showNewContent();
                }
            });
        } else {
            $("#" + contentWrapID).load("index.html #content");
        }
	}
    $.historyInit(pageload);
    
    $('#top a[href!=contact.html]').click(function(){
        
        var hash = $(this).attr('href');
        hash = hash.replace(/^.*#/, '');
        $("#" + contentWrapID).hide("fast",function(){
            $.historyLoad(hash);
        });
        if(!$('#load').get(0)) {
            $('#wrapper').append('<span id=\"load\">LOADING...</span>');
        }
        $('#load').fadeIn('normal');
        
        return false;
    
    });
});