$(document).ready(function() {
    /*--------------------------------------
    Focus on username field on login by default
    --------------------------------------*/
    $("input[name=usr_username]").focus();
    
    /*--------------------------------------
    Continent sprite background-position change
    --------------------------------------*/
    $("a.the-americas").bind('mouseover', function() {
        $('div#world').css({backgroundPosition: '0px -1250px'});
    });
    $("a.europe").bind('mouseover', function() {
        $('div#world').css({backgroundPosition: '0px -1000px'});
    });
    $("a.asia-pacific").bind('mouseover', function() {
        $('div#world').css({backgroundPosition: '0px -500px'});
    });
    $("a.africa").bind('mouseover', function() {
        $('div#world').css({backgroundPosition: '0px -250px'});
    });
    $("a.middle-east").bind('mouseover', function() {
        $('div#world').css({backgroundPosition: '0px -750px'});
    });

    $("div#world a").bind('mouseout', function() {
        $('div#world').css({backgroundPosition: '0px 0px'});
    });

    $("div#content_right_empty").css("height", $("div#contentcontainer").css('height'));

    
    /*--------------------------------------
    bij bladeren naar eerste of laatste pagina gaan
    --------------------------------------*/
    $("div.paging div").click(function() {
        if ($(this).attr('id') == 'lt') {
            $("input#page").val(1);

        } else if($(this).attr('id') == 'gt') {
            $("div.paging div").last().html();
            $("input#page").val($("div.paging div").eq($("div.paging div").length - 2).attr('id'));

        } else {
            $("input[name=page]").val($(this).attr('id'));
        }

        $("div#content_center form").submit();
    });


    /*--------------------------------------
    mediacentre meer info dropdown
    --------------------------------------*/
    $("a.more_info").click(function() {
        $("div.long_description").slideUp("slow");
        $(this).parent().parent().children('.long_description').slideDown('slow');
        return false;
    });
    
    $("a.delete").click(function() {
        if (confirm('Are you sure you want to delete this user?')) {
            return true;
        }
        return false;
    });
    
    $("table.grid th").bind('click', function() {
        var direction = $("form input#direction").val();
        var newdirection = 'desc';

        
        if (direction) {
            if (direction == 'asc') {
                newdirection = 'desc';
            }

            if (direction == 'desc') {
                newdirection = 'asc';
            }
        }

        $("form#search_search_form input#direction").val(newdirection);
        $("form#search_search_form input#sort").val($(this).attr('name'));
        
        $("form#search_search_form").submit();
    });
    
    
    /*--------------------------------------
    News & Events popup
    --------------------------------------*/
    $('td.event_on').hoverIntent({
        over: startHover,
        out: endHover,
        timeout: 500
    });

    function startHover() {
        $('.events_popup', this).fadeIn();
    }

    function endHover() {
        $('.events_popup', this).fadeOut('slow');
    }
     
     
    /*--------------------------------------
    Library submenu close on 2nd click
    --------------------------------------*/
    $("a.active").bind("click", function() {
        if ($(this).hasClass("subnavigation")) {
            if ($(this).hasClass("hidden")) {
                $('a.subsubnavigation').slideDown();
                $(this).removeClass('hidden');
            } else {
                $('a.subsubnavigation').slideUp();
                $(this).addClass('hidden');
            }
        }
        return false;
    });
    
    
    /*--------------------------------------
    Grid hover full name message
    --------------------------------------*/
    $('table.grid tr td').hoverIntent({
        over: startGridHover,
        out: endGridHover,
        timeout: 1000
    });

    function startGridHover() {
        var element = $(this).parent().children("td");
        $(this).parent().children("td:first").html(element.html().replace("...", ""));
        element.children("span").fadeIn();
    }

    function endGridHover() {
        var element = $(this).parent().children("td");
        var first   = $(this).parent().children("td:first");
        var span    = element.children("span");

        span.fadeOut("slow", function() {
            if (span.text() != '') {
                first.html(element.html().replace("<span", "...<span"));
            }
        });
    }
    
    
    /*--------------------------------------
    FAQ vragen in/uitklappen
    --------------------------------------*/
    $("div.faq_question").bind('click', function() {
        if ($(this).siblings("div.faq_answer").css("display") == 'block') {
            $(this).siblings("div.faq_answer").fadeOut();
        } else {
            $(this).siblings("div.faq_answer").fadeIn();
        }
    });
});
