// Functions for use in EBK
// Please just define functions in here, and apply them in the 
// "ebk-selectors.js" file. With meaningful names, this should 
// make it easier to understand what's going on...

var EBK = {};

EBK.initialiseEntryDetails = function () {
    if ( $.cookie('show_entry_details') && $.cookie('show_entry_details') == 0 ) {
        EBK.hideEntryDetails();
    } else {
        // this is the default if no cookie is set
        EBK.showEntryDetails();
    }
}

EBK.hideEntryDetails = function () {
    $('#entry_details p, #entry_details h2').hide();    
    $('#detailsShow').show();
    $('#detailsHide').hide();
    /* set cookie to reflect new state*/
    $.cookie('show_entry_details', 0);
    return false;
}

EBK.showEntryDetails = function() {
    $('#entry_details p, #entry_details h2').show();    
    $('#detailsShow').hide();
    $('#detailsHide').show();
    $.cookie('show_entry_details', 1);
    return false;
}

EBK.initialiseSidebar = function () {
    if ( $.cookie('show_sidebar') && $.cookie('show_sidebar') == 0 ) {
        EBK.hideMySidebar($('#sidebarHide a')[0], 1);
    } else {
        // this is the default if no cookie is set
        EBK.showMySidebar($('#sidebarShow a')[0]);
    }
}

EBK.hideMySidebar = function (that, init) {
    if (init) {
        // for initialisation, just set things as quickly as possible
        $(that).parents('#extra').children('.greyBox, #toc, #searchnav').hide();
        $('#content').width("90%");
        $('#extra').width('2%');
        $('#sidebarShow').show();
        $('#sidebarHide').hide();
    } else {
        // animate
        $(that).parents('#extra').children('.greyBox, #toc, #searchnav').hide('slow', function() {
            $('#content').width("90%");
            $('#extra').width('2%');
            $('#sidebarShow').show();
            $('#sidebarHide').hide();
        });
        $.cookie('show_sidebar', 0);
        return false;
    }
}

EBK.showMySidebar = function () {
    $('#content').width("67%");
    $('#extra').width('25%');
    $('#sidebarShow').hide();
    $('#sidebarHide').show();
    $('#extra').children('.greyBox, #toc, #searchnav').show('slow');
    $.cookie('show_sidebar', 1);
    return false;
}

// prefill an input box from a named cookie
jQuery.fn.preFillValue = function (cookie_name) {
    if ($.cookie(cookie_name)) {
        $(this).val($.cookie(cookie_name));
    }
    return this;
};

// select the text in an input box on focus
jQuery.fn.selectOnFocus = function () {
    $(this).focus(function () {
        this.select();
    });
}

jQuery.fn.hideDetails = function () {
    $(this).click( EBK.hideEntryDetails );
}

jQuery.fn.showDetails = function () {
    $(this).click( EBK.showEntryDetails );
}

jQuery.fn.hideSidebar = function () {
    var that = this[0];
    $(this).click( function() { EBK.hideMySidebar(that); return false; } );
}

jQuery.fn.showSidebar = function () {
    $(this).click( EBK.showMySidebar );
}


