
function initNav() {

    $(document).ready(function() {

        // hide the books subsections
        $('#books').find('dd').css({ "display": "none" });
        $('#books').find('dt').css({ "cursor": "pointer" });

        // give the dt some click events
        $('#books').find('dt').click(function() {

            // reset
            $('#books').find('dd').css({ "display": "none" });

            $(this).nextAll().each(function() {

                if (this.tagName.toLowerCase() == 'dd') {
                    $(this).slideDown('slow');
                }
                else if (this.tagName.toLowerCase() == 'dt') {
                    return false;
                }
            });
        });

        var url = window.location.href;
        var host = window.location.host;

        if (url.indexOf('localhost', 0) > 0) {
            host = 'localhost/hodderfaith';
        }
        else if (url.indexOf('lbsltd', 0) > 0) {
            host = 'www1dev.lbsltd.co.uk/hodderfaith';
        }

        var endposition = url.indexOf(host, 0) + host.length
        var remain = url.slice(endposition + 1);
        var arrURL = remain.split('/');
        var firstpart = arrURL[0];

        if (firstpart.indexOf('.aspx', 0) > 0) {
            firstpart = firstpart.split('.')[0];
        }

        $('.element').each(function() {

            $(this).find('dl').css({ "display": "none" });

            if (firstpart.toLowerCase() == $(this).attr('id').toLowerCase()) {

                if (firstpart.toLowerCase() != "books") {
                    $(this).find('dl').slideDown("slow");
                }
                else {

                    if (remain.toLowerCase().indexOf('genreid') > 0) {
                        $(this).find('dl').css({ "display": "block" });
                    }
                    else {
                        $(this).find('dl').slideDown("slow");
                    }
                }

            }
        });


        // expanding the genre sections
        if (firstpart.toLowerCase() == "books") {
            if (remain.toLowerCase().indexOf('genreid') > 0) {

                var GenreID = getParameterByName("genreid");

                var books = new Array(1, 2, 3, 4, 5, 6, 25, 8, 9, 10, 11, 12, 13, 14);
                var bibles = new Array(16, 17, 18, 19, 20, 21, 22, 23, 24);

                for (i = 0; i < books.length; i++) {
                    if (GenreID == books[i]) {
                        $('#g_books').nextAll().each(function() {

                            if (this.tagName.toLowerCase() == 'dd') {
                                $(this).css({ "display": "block" });
                            }
                            else if (this.tagName.toLowerCase() == 'dt') {
                                return false;
                            }
                        });
                    }
                }

                for (i = 0; i < bibles.length; i++) {
                    if (GenreID == bibles[i]) {
                        $('#g_bibles').nextAll().each(function() {

                            if (this.tagName.toLowerCase() == 'dd') {
                                $(this).css({ "display": "block" });
                            }
                            else if (this.tagName.toLowerCase() == 'dt') {
                                return false;
                            }
                        });
                    }
                }

            }
        }

    });

}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href.toLowerCase());
    if (results == null)
        return "";
    else
        return results[1];
} 