$(function () {
    var tabContainers = $('.section');
    var tabId = '#' + $(window.location.hash).parent().attr('id');
    
    // manage page tabs on page load
    if(window.location.hash) {
        $('.section').not('#'+window.location.hash).hide();
        $(tabId).show();
        $('a[href=\#products]').addClass('selected');
    } else {
        $('div.page-tabs ul a').each(function(){
            $(this).removeClass('selected');
            if($(this).attr('href') == tabId){
                $(this).addClass('selected');                        
            }
        });   
    }
    
    // manage page tabs on click
    $('div.page-tabs ul a').click(function () {
        tabContainers.hide().filter(this.hash).show();
        $('div.page-tabs ul a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    });

    // if there is no hash in the url
    if(!window.location.hash) {
        // click the first tab to reveal the content
        $('div.page-tabs ul a').filter(':first').click();                    
    } else {
        // otherwise (if there is a hash) click the tab that matches the hash 
        $('div.page-tabs ul a').removeClass('selected').filter('a[href='+tabId+']').addClass('selected');
    }
    
    // My Conference Selection block links affect the page tabs
    $('#content-heading #myconference-selections a').each(
        function() {             
            if(this.hash) {
                $(this).click(function() {
                    var tabId = '#' + $(this.hash).parent().attr('id');
                    // hide/show the appropriate containers
                    tabContainers.hide();
                    $(this.hash).parent().show();
                    // switch the tabs
                    $('div.page-tabs ul a').removeClass('selected');
                    $('div.page-tabs ul a').each(function(){
                        if($(this).attr('href') == tabId) {
                            $(this).addClass('selected');
                        }
                    });
                })
            }
        }
    );
    
    $('.section-heading h4').remove();
});
