var page; /*Let loose the jQuery!*/
$(function () {
   AudioPlayer.setup("/js/player.swf", {  
                width: 290  
            });  

 $('#news')
            .tumblr({
                url:        'http://alexwand.tumblr.com',
                loading:    '#loading'
            });

    $('a[title=audio]').each(function(){
        $(this).attr('id',$(this).html());
        AudioPlayer.embed($(this).attr('id'), {soundFile: $(this).attr('href'),titles:$(this).html()});  
    });
    $('.page').hide();
    $('.page[rel!=hidden]').each(function () {
        $(this).hide();
        var navigation = $('#Navigation');
        var id = $(this).attr('id');
   'fast'     //If the page is in the hash ie(we are either loading the page, or creating the page as we click)
        if (document.location.hash.slice(2) == id) {
            setTimeout(function () {
                page = id;
                $('#' + id).fadeIn('fast');
            }, 200);
        }
        //Create a button to the page
        var button = $("<a href='#" + id + "'>" + id.replace(/-/gi, " ") + "   </a></br>");
        button.id = id;
        button.attr('rel', id);
        button.click(function () {
            changePage($(this).attr('rel'))
        });
        navigation.prepend(button);
    }); 
    $("a[title=link]").each(function(){
        var link=$(this);
        link.attr('rel', link.attr('href').toString().replace('#',''));
        link.click(function(event){
            changePage($(this).attr("rel"));
        });  
    });
    $('body').hide();
    setTimeout('$("body").fadeIn();', 100);

 $.history.init(function(hash){
        if(hash == "") {
            var pages=$(".page");
            var landing=pages[pages.length-1].id;
            changePage(landing)
        } else {
            changePage(hash);
        }
    });


    function changePage(pg){

        jQuery.history.load(pg);

            if (page == pg) {
                return true;
            }
            if (page == undefined){
                page = pg;
                $('#' + page).fadeIn('fast');
                return true;
            }
            last='#'+page
            page = pg;
            console.log()
            $(last).fadeOut('fast',function(){
                $('#' + page).fadeIn();
            });
 
    }

});




