$(document).ready(function() {

    var contentWrapID = '___content-wrapper';
    
    $('#content').wrap('<div id=\"' + contentWrapID + '\"></div>');
    
    function showNewContent() {
        $("#" + contentWrapID).show();
        $('#load').fadeOut();
    }
    
    function pageload(hash, data) {
        var mainPage = window.location.pathname.replace(/.*\//, '').replace(/#.*$/, '');
        var contentPage = hash;
        if (contentPage == null || contentPage == "") {
        	contentPage = mainPage;
        }
        if (data == null) {
            data = 'ajax=true';
        } else {
            data += '&ajax=true';
        }
        $("#" + contentWrapID).load(contentPage + ' #content',data,cleanupPage);
        function cleanupPage(){
			$("#top a > span").removeClass("selected");
			Shadowbox.clearCache();
			//Shadowbox.setup($("#nav1 a[rel^=\'shadowbox\']"));
			if (hash == "photos") {
				Shadowbox.setup($("#content a[rel^=\'shadowbox\']"), {gallery: "bandpics"});
				//Shadowbox.setup($("#content area[rel^=\'shadowbox\']"), {gallery: "bandmembers"});
			} else if (hash == "shows") {
				Shadowbox.setup($("#content a[rel^=\'shadowbox\']"), {gallery: "posters"});
			} else if (hash == "news") {
				Shadowbox.setup($("#content a[rel^=\'shadowbox\']"), {});
			} else if (hash == "discography") {
				Shadowbox.setup($("#content a[rel^=\'shadowbox\']"), {});
			}
			if (contentPage == "photos") {
				$("title").replaceWith("<title>The Mean - Photos</title>");
				$("#top a[href='photos'] > span").addClass("selected");
			} else if (contentPage == "shows") {
				$("title").replaceWith("<title>The Mean - Shows</title>");
				$("#top a[href='shows'] > span").addClass("selected");
			} else if (contentPage == "news") {
				$("title").replaceWith("<title>The Mean - News</title>");
				$("#top a[href='news'] > span").addClass("selected");
			} else if (contentPage == "discography") {
				$("title").replaceWith("<title>The Mean - Records</title>");
				$("#top a[href='discography'] > span").addClass("selected");
			} else if (contentPage == "videos") {
				$("title").replaceWith("<title>The Mean - Videos</title>");
				$("#top a[href='videos'] > span").addClass("selected");
			} else if (contentPage == "merch") {
				$("title").replaceWith("<title>The Mean - Merch</title>");
				$("#top a[href='merch'] > span").addClass("selected");
			} else if (contentPage == "contact") {
				$("title").replaceWith("<title>The Mean - About/Contact</title>");
				$("#top a[href='contact'] > span").addClass("selected");
			} else if (contentPage == "links") {
				$("title").replaceWith("<title>The Mean - Links</title>");
				$("#top a[href='links'] > span").addClass("selected");
			} else if (contentPage == "download") {
				$("title").replaceWith("<title>The Mean - Download</title>");
				    $('#submitDownloadCode').unbind('submit');
					$('#submitDownloadCode').submit(function(){
					    var data = $(this).serialize();
                        var dlhash = 'download';
                        $("#" + contentWrapID).hide("fast",function(){
                            pageload(dlhash, data);
                        });
                        if(!$('#load').get(0)) {
                            $('#wrapper').append('<span id=\"load\">LOADING...<\/span>');
                        }
                        $('#load').fadeIn('normal');
                        return false;
                    });
				    $('#requestDownloadBegin').unbind('submit');
					$('#requestDownloadBegin').submit(function(){
					    var remaining = parseInt($("#remainingDownloadInputField").attr('value'));
					    if (remaining < 1) {
					        return false;
					    }
                        $("#remainingDownloadInputField").attr('value', function(i, val){
                            var newRemaining = parseInt(val) - 1;
                            $("#remainingDownloads").empty().append(newRemaining);
                            return newRemaining;
                        });
                        return true;
                    });
			} else if (contentPage == "list") {
				$("title").replaceWith("<title>The Mean - Mailing List</title>");
			} else if (contentPage == "") {
				$("title").replaceWith("<title>The Mean - Music</title>");
				$("#top a[href=''] > span").addClass("selected");
			}
			if($('img:last',this).get(0)) {
                $('img:last').one('load', function() {
                    showNewContent();
                }).each(function() {
                    if(this.complete) $(this).load();
                });				
			} else {
				showNewContent();
			}
		}
	}
	
    $.history.init(pageload);
        
    $('#top a[href!="contact.html"]').click(function(){
        var hash = $(this).attr('href');
        hash = hash.replace(/^.*#/, '');
        if ($("#top a[href=\""+hash+"\"]").length > 0 && !$("#top a[href=\""+hash+"\"] > span").hasClass("selected")) {
			$("#" + contentWrapID).hide("fast",function(){
				$.history.load(hash);
			});
			if(!$('#load').get(0)) {
				$('#wrapper').append('<span id=\"load\">LOADING...</span>');
			}
			$('#load').fadeIn('normal');
        }
        
        return false;
    
    });
});
