$(document).ready(function(){

    // forgot password slide-in
    $("a#forgot_password").click(function(){
        $("div#password_form").show(250);
    });

    // navigation

    // disable non-js pulldown hover to avoid flicker
    $("#navigation li").children("ul").css("display", "none");

    // navigation menu pulley
    $("#navigation li").hoverIntent(downer, upper);

    // article filter

    // filter: collapse all filters on page start
    $("#filter > li").addClass("collapsed");

    // filter: toggle filters on click
    $("#filter > li > h2").click(function(){
        if ($(this).parent("li").is(".collapsed")) {
            $(this).next("div").show("fast").parent("li").removeClass("collapsed").addClass("expanded");
        }
        else {
            $(this).next("div").hide("fast").parent("li").removeClass("expanded").addClass("collapsed");
        }
        return (false);
    });

    // faq: collapse all filters on page start
    $("#sub_categories li").addClass("collapsed");

    // faq: toggle filters on click
    $("#sub_categories li h2 a").click(function(){
        if ($(this).parent('h2').parent('li').is('.collapsed')) {
            $(this).parent('h2').parent('li').removeClass('collapsed').addClass('expanded');
        }
        else {
            $(this).parent('h2').parent('li').removeClass('expanded').addClass('collapsed');
        }
        return false;
    });

    $("table.table_default tbody tr:even").addClass("even");

    // Google toolbar override

    $("div.overview_wide").addClass("collapsed");
    $("div.overview_wide").hoverIntent(expander, collapser);
    $("div.filter_item_body li label").click(function(){
        $(this).addClass('altered');
    });

    $(".begrippen_item").addClass("collapsed");
    $(".begrippen_item").hoverIntent(begrip, begreep);

    customBannerOverlay();
});

/**
 * This custom overlay is because all overlays today are only compatible with jquery version
 * 1.3.2 so this does the trick
 */
function customBannerOverlay() {
 
    function closeBanner() {
        $('#natuurkunde_overlay_banner_expose, #natuurkunde_banner_content, #close_banner').fadeOut(125);
        $.cookie(bannerCookieName, 'true');
    }

    // Only show banner when cookie is not set
    var bannerCookieName = 'banner_visited';
    if(!$.cookie(bannerCookieName)) {

        // a expose is the black transparant background
        $('<div id="natuurkunde_overlay_banner_expose"></div>').css({
            'z-index': 10000,
            'position': (typeof document.body.style.maxHeight === "undefined") ? 'absolute' : 'fixed',
            'background-color': '#000',
            'top': 0,
            'left': 0,
            'opacity': 0.75,
            'height': '100%',
            'width': '100%'
        }).appendTo('body');

        // the window
        $(' <div id="natuurkunde_banner_content">' +
                '<a href="http://www.sciencespace.nl/" target="_blank">' +
                    '<img src="./gfx/sciencespace.jpg" alt="banner" />' +
                '</a>' +

           '</div>')
       .css({
            'z-index': 10001,
            'background-color': '#FFF',
            'position': 'absolute',
            'top': '20%',
            'left': '50%',
            'margin-left': '-400px',
            'height': '200px',
            'width': '200px'
        })
        .appendTo('body');

        // a close button
        $('<a href="#" id="close_banner"></a>')
        .css({
            'z-index': 10002,
            'background-color': '#000',
            'opacity': 0,
            'position': 'absolute',
            'top': '20%',
            'left': '50%',
            'margin-left': '430px',
            'height': '20px',
            'width': '20px'
        })
        .appendTo('body')
        .click(function(e) {

            // onclick this overlay will be closed and a cookie will remember to not open this banner again
            e.preventDefault();
            closeBanner();
        });


        setTimeout(function() {
            closeBanner();
        }, 10000);

        $("#natuurkunde_overlay_banner").fadeIn(125);
    }
}

function expander(){
    $(this).children("div.overview_details").slideDown(180);
}

function collapser(){
    $(this).children("div.overview_details").slideUp(180);
}

function begrip(){
    $(this).removeClass("collapsed").children("div.begrippen_item_wrapper").children("div.begrippen_extended").slideDown("fast");
}

function begreep(){
    $(this).addClass("collapsed").children("div.begrippen_item_wrapper").children("div.begrippen_extended").slideUp("fast");
}

function downer(){
    $(this).children("ul").slideDown(120);
}

function upper(){
    $(this).children("ul").slideUp(120);
}
/*
$(function(){
    $('th').click(function(){
        $(this).highlightFade({
            color: 'white',
            speed: 1000,
            iterator: 'sinusoidal'
        });
    });

});
*/
