$(document).ready(function(){
    
    //setupScrollers();
    
    
    
    $("#container").css({"margin-top": "320px", "width":"1180px"}); 
   
    $("div.content_column div.content").hide();
    $("#content li.bordered").hide();
    $("#welcome").hide().width(60);
    
    setTimeout(animateIn, 400);
    
    $.address.change(address_change);
    
    
});

var has_animated = false;
var disable_interaction = true;

function address_change(val){
    if(has_animated){
        var url = val.pathNames[val.pathNames.length-1];
        //console.log("change address", url);
        find_address_reference("/" + url);
    }
}


function find_address_reference(url){
    
    if(url == "/"){
        $.address.value("/who");
    }else{
        
        var $found = $("#content").find("li.bordered h1").find('a[href="#'+url+'"]');
        if($found.length > 0){
            var $li = $found.parent().parent().parent().parent();
            disable_interaction = true;
            showItem($li);
        }else{
            $.address.value("/who");
        }
        
    }
}


function detect_address(){
    has_animated = true;
    disable_interaction = false;
    setupHover();
    find_address_reference($.address.value());
}


function setupScrollers(){
    
    
    $("body").css("overflow", "hidden");
    
    $("#content li.bordered div.content div.scrollable").each(function(idx, el){;
        createSlider($(el));
    });
    
}

function animateIn(){
    
    
    $("#welcome").fadeIn(1500, function(){
        var $wel = $(this);
        $wel.animate({width:200}, function(){
          var count = 0;
          $("#content li.bordered").each(function(idx, el){
                var $item = $(el);
                count = idx;
                setTimeout(function(){ $item.fadeIn(360); }, (idx * 180));
            });
          
          setTimeout(detect_address, count * 400);
                  
          }
          
        );
    });
    
}

function setupHover(){
    $("#content_list li.bordered").each(function(index, el){
        
        var $item = $(el);
        
        $item.data({"index": index, "w":$item.find("div.link").width()+8});
    }).hover(function(e){
        if(!disable_interaction){
           
           
           var $link = $(this).find("h1 a");
           var hash = $link.attr("href").slice(1,12);
           
           $.address.value(hash);
           
        }
    });
}

var selectedItem = -1;

function showItem($item){
    
    
    var idx = $item.data("index");
    
    if(selectedItem == idx){
        return;
    }
    
    
    selectedItem = idx;
    
    var $block = $item.find("div.content");
    $block.show(300, function(){
        disable_interaction = false;
    });
    $item.animate({"margin-top": "-300px", "width":"400px"}, 300);
    
    $("#content_list li.bordered").each(function(index, el){
        if(index != idx){
            hideItem($(el));
        }
    });
    
}

function hideItem($item){
    var w = $item.data("w");
    $item.find("div.content").hide(300);
    $item.animate({"margin-top": "0px", width:w+"px"}, 300);
}
