// wait for background image to load
$(document).bind('backgroundLoaded', function(){

    $('.career-content').css({display: 'none'});
    $('.careerlocation h2').css({display: 'none'});
    $('.careerlocation').css({display: 'none'});
    
    // initialize the page
    $('div.loading-container').fadeOut(500);
    $('div.careers-container').fadeIn(500);

    $('.contact-location-links p a').click(handleLocationClick);
    $('.career h3 a').click(handleCareerClick);
    
    $('.contact-location-links p a:first').click();
    
});

var bIsLocationChanging = false;
function handleLocationClick()
{
    oLink = $(this);
    oLocation = $('.careerlocation-'+oLink.text().toLowerCase().replace(' ', ''));
    
    if (!bIsLocationChanging)
    {
        if (!oLink.hasClass('on'))
        {
            bIsLocationChanging = true;
            
            $('.noopenings').css({display: 'none'});
            
            $('.career-content-visible').fadeOut('slow', function(){
                $('.contact-location-links p a').removeClass('on');
                oLink.addClass('on');
                
                $('.careerlocation').fadeOut('slow');

                oLocation.find('.career-collection h3 a').removeClass('on');
                    
                oLocation.fadeIn('slow', function() {
                    $('.noopenings').fadeIn('slow');
                    oLocation.find('.career-collection h3 a:first').click();                            
                    resetAllScrollbars();
                    bIsLocationChanging = false;                   
                });
            });
        }
    }
    
    oLink.blur();
}

var bIsCareerChanging = false;
function handleCareerClick()
{
    oLink = $(this);
    
    if (!bIsCareerChanging)
    {
        if (!oLink.hasClass('on'))
        {
            bIsCareerChanging = true;
                    
            $('.career h3 a').removeClass('on');
            oLink.addClass('on');

            $('.career-content-visible').fadeOut('slow', function(){
                $('.career-content-visible').html(oLink.parent().next().html());
                $('.career-content-visible').fadeIn('slow', function(){
                    resetAllScrollbars();
                    bIsCareerChanging = false;
                });
            });
        }    
    }
    
    oLink.blur();   
}
