// Selectors that apply functions to elements in EBK.

$(function () {
    // IE 4 and 5 seem to attempt to run the code, but have various problems.
    // Therefore, we detect them and get out.
    if ( $.browser.msie && $.browser.version <6 ) return;
    

    // This is the on ready function. All selectors should go in here:

    // =============================================================
    // Global behaviors (well almost global due to optimisation)

    // Any <a class="newwindow"> should open in a new window.
    // This is because target attribute is disallowed in strict
    // (X)HTML.
    // 
    // Optimisation: This used to apply to ALL a.newwindow 
    // Now, to make it faster we only look in areas we know 
    // are going to contain it: header, footer, article main text
    
    // =============================================================
    // Search form behaviour
    //

    // Pre-fill the Quick and Advanced search input boxes
    //  with the last search if saved in the cookie.
    $('#searchform input#query').preFillValue('last_search');
    $('body#advancedsearch input#field-query').preFillValue('last_search');

    // Select the contents of the query box on focus 
    // (so the hint or last search can be cleared easily)
    $('body#advancedsearch input#field-query').selectOnFocus();
    $('#searchform input#query').selectOnFocus();

    // initialise the state of sidebar
    if ( window.location.pathname.search(/(book|view_pdf)$/) >= 0 ) {
        EBK.initialiseSidebar();
        prepareBookToc(jQuery.url.param("id"));
    }
    // register click event handlers for sidebar show/hide
    $('#sidebarHide a').hideSidebar();
    $('#sidebarShow a').showSidebar();
    
    // initialise the state of entry details panel
    if ( window.location.pathname.search(/view_pdf$/) >= 0 ) {
        EBK.initialiseEntryDetails();
    }
    // register click event handlers for entry details show/hide
    $('#detailsHide a').hideDetails();
    $('#detailsShow a').showDetails();
    
    if ( window.location.pathname.search(/search_results$/) >= 0 ) {
        prepareSearchResults();
    }
    
    // Advanced search "select all/none" controls for subject list 
    $('#subject_controls').show();
    
    // Make all anchors with rel="external" open in a new browser window/tab
    $('a[rel=external]').attr('target', '_blank');
    
    // Add HTML tooltip to the 'i' link on the advanced search page
    if ( window.location.pathname.search(/advanced_search$/) >= 0 ) {
        $('a.helpinfo').tooltip({
                        delay: 0,
                        showURL: false,
                        fade: 150,
                        track: true,
                        top: -17,
                        left: 19,
                        bodyHandler: function() {
                            return $('#help_i').html();
                        }
        });
    }
    
});


