/* 
 * 
 */


$(function(){
    $('#content').append('<div id="navigation"><ul></ul></div>');
    $('#content').find('.gallery').each(function(){
        formatContent($(this));
    });
    $(window).hashchange( function(){
        // Alerts every time the hash changes!
        parseHash();
    })
    $(window).hashchange();
    
    new iScroll('content');
});

function formatContent(g){
    wd = 400;
    $(g).find('img').each(function(){
        wd += parseInt($(this).attr('width'));
    });
    
    $(g).css('width', wd+'px');
    $('#navigation ul').append('<li class="'+$(g).attr('id')+'"><a href="#/'+$(g).attr('id')+'">'+$(g).attr('id')+'</a></li>');  
}

function parseHash(){
    temp = decodeURIComponent((location.hash).replace(/\+/g, '%20'));
    hash = temp.split("/");
    //$("#container p.loading").hide();
    if(hash.length > 1){
        navigate(hash);
    }else{
        h = $('#content').find('.gallery').eq(0).attr('id');
        window.location = "#/"+h;
    }
	
}

function navigate(hash){
    $('#content').find('.gallery').hide();
    $('#content').find('#'+hash[1]).show();
    
    $('#navigation li a').removeClass('active');
    $('#navigation li.'+hash[1]+' a').addClass('active');
}
