//Globals
var storedLinkBkup;

// wait for background image to load
$(document).bind('backgroundLoaded', function(){

    
    $('.office').css({display: 'none'});
    $('.contact-location-links').css({display: 'none'});
                
    
    if (jQuery.support.opacity)
    {
        // initialize the page
        $('div.loading-container').fadeOut(500);
        $('div.contact-container').fadeIn(500);


        $('.contact-location-links').fadeIn('slow', function(){
            $('.contact-location-links p a').click(handleLocationClick);
            $('.contact-location-links p a:first').click();
        });

    }else{
    
        // initialize the page
        $('div.loading-container').hide();
        $('div.contact-container').show();

        $('.contact-location-links').show().find('p a').click(handleLocationClick);
        $('.contact-location-links p a:first').click();

    }
    
	$('.image-collection-slideshow').cycle({
	    fx: 'fade', 
        speed: 1500 
	});
	
})

function handleLocationClick(e)
{
    var clickedLink = $(this);
    var officeToShow = $('.office-'+clickedLink.text().toLowerCase().replace(' ', ''));
    
    //Remove ON state of previous link and add its href back
    $('.contact-location-links p a.on').removeClass('on').attr("href",storedLinkBkup);
    
    //Store the clicked link href, remove it, set link state to ON 
    storedLinkBkup = clickedLink.attr("href");
    clickedLink.removeAttr("href");
    clickedLink.addClass('on');
    
    //Handle transition and show/hide

    if (jQuery.support.opacity)
    {

        $('.office-collection').fadeOut("slow", function() {
            $('.office:visible').hide();
            officeToShow.show();
            resetAllScrollbars();
            $(this).fadeIn("slow");
        });

    
    }else{
        $('.office:visible').hide();
        officeToShow.show();
    
    }
    
}
