var content;
var submenu;

function initialize_menu() {
    content = jQuery('#content');
    submenu = jQuery('#submenu');
    
    jQuery('#menu').delegate('a', 'click', function() {
        window.location.hash = jQuery(this).attr('href').replace('.htm', '');
        return false;
    });
    jQuery('#launch_site').click(function() {
        window.location.hash = jQuery(this).attr('href').replace('.htm', '');
        return false;
    });
    jQuery('#launch_site_index').click(function() {
        window.location.hash = 'work';
        return false;
    });
}

function launch_site(url) {
    jQuery('#main').parent().load(url + ' #main', function() {
        initialize_menu();
        jQuery('.slideshow').cycle({
    		fx: 'fade'
    	});
    });
}

jQuery(document).ready(function() {
    initialize_menu();

    jQuery('.hidden_if_js').hide();

    jQuery(window).bind( 'hashchange', function(){
        new_hash = window.location.hash.substring(1);
        if(new_hash) {
            if(jQuery('#index').length) {
                launch_site(new_hash + '.htm');
                return;
            }

            content.find('#content_loadable').fadeOut(200, function() {
                content.hide().load(new_hash + '.htm #content_loadable', function() {
                    jQuery('.slideshow').cycle({
                		fx: 'fade'
                	});
                	content.fadeIn(200);
                });
            });
            submenu.find('#submenu_loadable').fadeOut(200, function() {
                submenu.hide().load(new_hash + '.htm #submenu_loadable', function() {
                    submenu.fadeIn(200);
                });
            });
            jQuery("#menu a").removeClass("selected");
            jQuery("#menu a[href=" + new_hash + ".htm]").addClass("selected");
        }
    });
    
    $(window).trigger('hashchange');

});
