var codespero = { };

codespero.ajaxLoad = function(url)
{
	if(url.length == 0)
		return;
	
	// start the transition animation
	var pieces = url.split("/");
	var page = pieces[pieces.length-1];
	try { $("#flash-anim")[0].sendPage(page); }
	catch(e) { }

	// get the page json data and load it
	$.getJSON(url + "?json", function(data)
	{
        $("#stage-header .stage-title").html(data.stage_title);
        $("#stage-header #stagenav").remove();
        $("#stage-header").append(data.stagenav);
        $("#content .content-main").html(data.body);
        
        $("#mainnav a").removeClass("active");
        $("#mainnav a[href=" + url + "]").addClass("active");
        
        // rerun document ready
        codespero.documentready();
	});
};


codespero.portfolio = { }

codespero.portfolio.updatebuttons = function(part)
{
    var client = part.closest(".client");
    var clientNexts = client.nextAll(".client");
    var clientPrevs = client.prevAll(".client");
    var nexts = part.nextAll(":not('.info')")
        .add(part.closest(".client").nextAll(".client").children(":not('.info')"));
    var prevs = part.prevAll(":not('.info')")
        .add(part.closest(".client").prevAll(".client").children(":not('.info')"));
    
    var btnPrev = $(".content-bottombar .button-prev");
    var btnNext = $(".content-bottombar .button-next");
    
    if(nexts.length > 0)
    {
        // activate the next button
        btnNext.removeClass("button-next-inactive");
        btnNext.removeClass("inactive-button");
    }
    else
    {
        // deactivate the next button
        btnNext.addClass("button-next-inactive");
        btnNext.addClass("inactive-button");
    }
    
    if(prevs.length > 0)
    {
        // activate the prev button
        btnPrev.removeClass("button-prev-inactive");
        btnPrev.removeClass("inactive-button");
    }
    else
    {
        // deactivate the prev button
        btnPrev.addClass("button-prev-inactive");
        btnPrev.addClass("inactive-button");
    }
}

codespero.portfolio.showpart = function(part)
{
    if(!part || !part.length || part.length == 0)
        return;

    // get the piece of this part
    var piece = part.closest(".client");
    
    if(piece.not("visible").length > 0)
    {
        // the part's piece is hidden (part is in a new piece)
        
        // get the associated stagenav item
        var name = piece.attr("id").substr(7);
        var snItem = $("#stagenav a[rel=" + name + "]").closest("li");
        
        // remove active from other stagenav items
        $("#stagenav > li").removeClass("active");
        
        // set it as the active item
        snItem.addClass("active");
        
        // scroll to it
        codespero.stagenav.scrollTo(snItem);
        
        // hide all other pieces
        $(".content-main .client").hide();
        
        // show this piece
        piece.show();
        
        // hide all parts of the piece
        $(">*", piece).hide();
        
        // show the piece info in the bottombar
        var pieceinfo = $(".info", piece);
        $(".content-bottombar .info").empty().append($(">*", pieceinfo).clone());
    }
    
    // hide all parts of the piece
    $(">*", piece).hide();
    
    part.show();
    codespero.portfolio.updatebuttons(part);
}


codespero.stagenav = { }

codespero.stagenav.scrollTo = function(item)
{
    if(!item || !item.length || item.length == 0)
        return;

    var curScroll = codespero.stagenav.container.scrollLeft();
    var mid = codespero.stagenav.width/2;
    var offset = curScroll + item.position().left;
    var newScroll = (offset - mid) + item.outerWidth()/2;

    if(newScroll > codespero.stagenav.maxScroll)
        newScroll = codespero.stagenav.maxScroll;
    if(newScroll < 0)
        newScroll = 0;
        
    newScroll = Math.round(newScroll);
    
    codespero.stagenav.container.scrollTo(newScroll, 500, {axis: "x"});
}


codespero.documentready = function()
{
    /** Contact **/
    var lastActive;
    $("#topnav-contact a").click(function()
    {
        lastActive = $("#topnav a.active");
        $("#topnav a").removeClass("active");
        $(this).addClass("active");
        $("#contact-box").slideDown();
        return false;
    });
    $("#contact-box .button-close").click(function()
    {
        $("#topnav a").removeClass("active");
        if(lastActive)
            lastActive.addClass("active");
        $("#contact-box").hide();
        return false;
    });
    
    
    /** Stagenav **/
	sn = codespero.stagenav;
	sn.container = $("#stagenav-container");
	sn.items = $("#stagenav > li");
	sn.width = sn.container.width();
	sn.maxScroll = (sn.items.filter(":last").position().left + sn.items.filter(":last").outerWidth() + 2) - sn.width;
	
	/*
	// find the last stagenav item that can be fully displayed
	var i = sn.items.length-1;
	while(true)
	{
	    var cur = sn.items.eq(i);
	    if(cur.length == 0)
	        break;
	    cur.addClass("last");
	    if(cur.position().left + cur.outerWidth() < sn.width)
	        break;
        i -= 1;
	}

    // show only items that can be fully displayed
    sn.items.removeClass("last");
    sn.items.eq(i).addClass("last");
    sn.container.width(sn.items.eq(i).position().left + sn.items.eq(i).outerWidth());
    */

    
    /** Portfolio **/
    
    // hide all pieces and parts first
    $(".content-main .client").hide();
    $(".content-main .client > *").hide();
    
    // show the first part of the first piece
    codespero.portfolio.showpart($(".content-main .client:first :first"));

    // Portfolio stagenav clicks
	$(".stagenav-portfolio a").click(function()
	{
        codespero.portfolio.showpart($(".content-main #client-" + $(this).attr("rel") + " :first"));
        return false;
	});
	
	// Portfolio next button
	$(".content-bottombar .button-next").click(function()
	{
	    var cur = $(".content-main .client:visible > :visible");
	    var next = cur.next(":not('.info')");
	    if(next.length == 0)
	        next = cur.closest(".client").next(".client").children(":not('.info'):first");
	        
	    codespero.portfolio.showpart(next);
	    
	    return false;
	});
	
	// Portfolio prev button
	$(".content-bottombar .button-prev").click(function()
	{
	    var cur = $(".content-main .client:visible > :visible");
	    var prev = cur.prev(":not('.info')");
	    if(prev.length == 0)
            prev = cur.closest(".client").prev(".client").children(":not('.info'):last");

	    codespero.portfolio.showpart(prev);
	    
	    return false;
	});
}




jQuery(function($)
{
    // ajax history
    /*
	$.historyInit(codespero.ajaxLoad);
	$("#mainnav a").click(function()
	{
		$.historyLoad($(this).attr("href"));
		return false;
	});
	*/
	
	codespero.documentready();
});