$(init);

var currentIndex = -1;

function init()
{
    reset(true);

    $(window).resize(resize);
    resize();

    $('nav a').click(navigationHandler);
    $('.close').click(reset);
    $('.pressLink').click(function()
    {
        currentIndex = 1;
        destinationHandler()
    });


    $('.thepress').mouseenter(function()
    {
        $(this).children().attr('src', 'media/images/nav_press_ovr.png')
    }).mouseleave(function()
    {
        $(this).children().attr('src', 'media/images/nav_press.png')
    });


    $('.thejob').mouseenter(function()
    {
        $(this).children().attr('src', 'media/images/nav_job_ovr.png')
    }).mouseleave(function()
    {
        $(this).children().attr('src', 'media/images/nav_job.png')
    });


    $('header').fadeIn('slow');
    $('nav').css('right', -$('nav').width() + 'px').show().animate(
    {
        right : 0
    },
    {
        duration : 200
    });
    $('footer').fadeIn('slow');

    twitter();

    var hash = location.href.split('#')[1];
    switch (hash)
    {
        //case 'TheStory': currentIndex = 0; break;
        case 'ThePress':
            currentIndex = 0;
            break;
        case 'TheJob':
            currentIndex = 1;
            break;
        default:
            currentIndex = -1;
    }

    destinationHandler();
}

function resize()
{
    $('header').css('top', (($(window).height() - $('header').height() - $('footer').height()) / 2) + 'px');
    $('header').css('left', (($(window).width() - $('header').width()) / 2) + 'px');
    $('section').css('left', ((($(window).width() - $('section').width()) / 2) - ($('nav').width() + 35)) + 'px');
    resizeContent();
}

function reset(bFirstTime)
{
    var dist = $(window).height();
    if(bFirstTime)
    {
        $('section').css('top', dist + 'px');
        return;
    }

    $('section').animate(
    {
        top : dist
    },
    {
        duration : 50
    });
}

function navigationHandler(e)
{
    reset();
    currentIndex = $(e.target).parent().index();

    $('section:eq(' + currentIndex + ')').show().animate(
    {
        top : 40
    },
    {
        duration : 500,
        complete : resizeContent
    });

    var href = location.href.split('#')[0];
    var hash;
    switch (currentIndex)
    {
        case 0:
            hash = '#ThePress';
            break;
        case 1:
            hash = '#TheJob';
            break;
        default:
            hash = '#';
    }
    location.href = href + hash;
    return false;
}

function destinationHandler()
{
    if(currentIndex != -1)
    {
        reset();
        $('section:eq(' + currentIndex + ')').show().animate(
        {
            top : 40
        },
        {
            duration : 500,
            complete : resizeContent
        });
    }
}

function resizeContent()
{
    var $currentSection = $('section:eq(' + currentIndex + ')');
    var $currentContent = $currentSection.find('.content');

    if($currentContent.length > 0)
    {
        var contentHeight = $(window).height() - $('footer').height() - ($currentContent.position().top + $currentSection.position().top + 40);
        //Magic number is padding
        $currentContent.height(contentHeight);
    }
}

function goToByScroll(id)
{
    console.log("goToByScroll");
    var scrollY = $("#" + id).offset().top - 120;
    var speed = Math.abs( scrollY * .7 );
    $("#jobsSection").stop().animate(
    {
        scrollTop : scrollY
    }, speed, JQueryEasing.easeOutExpo);
}

function twitter()
{
    $('#tweets').tweet(
    {
        avatar_size : null,
        count : 2,
        username : ['zeidel', 'sylvainperron', 'sonicboomcm'],
        loading_text : 'Loading tweets...',
        refresh_interval : 60,
        template : "{time}<br/>{text}"
    }).bind("loaded", function()
    {
        $(this).find("a").attr("target", "_blank");
    });

}
