/*
jQuery UI: Core, Widget, Accordion
*/
(function($) {
    $(function() {
        // fix browser annoyances
        $.fixBrowsers()
        // setup external links
        $.setupExternalLinks()
        
        $.Sidenav();

        $('#nav li.AspNet-Menu-WithChildren').each(function() {
            var $this = $(this);
            $this.mouseenter(function() {
                clearTimeout($this.data('timeout'));
                if (!$this.hasClass('open')) {
                    $this.addClass('open');
                }
            }).mouseleave(function() {
                var $this = $(this)
                $this.data('timeout', setTimeout(function() {
                    $this.removeClass('open');
                }, 250))
            });
        });

        // Newsticker on homepage
        $('div.introtekst ul.ModuleChildOverview').bxCarousel({
            display_num: 1,
            move: 1,
            auto: true,
            controls: false,
            margin: 10,
            auto_hover: true,
            auto_interval: 5000
        });
      
        // Accordion on sidenavigation
        $('#sidenav .AspNet-Menu li.AspNet-Menu-WithChildren ul').accordion({
            active:false,
            event:'mouseover',
            navigation:true
        });

        // For homepage
        /*$('.HomepageButtons').accordion({
            //collapsible: true,
            active:false,
            event:'mouseenter'
        }).bind('mouseleave',function() {
            // We switchen collapsible, omdat dit nodig is om alles te sluiten
            // als dit altijd aan staat werkt het niet goed
            $(this).accordion('option','collapsible',true);
            $(this).accordion('activate',false);
            $(this).accordion('option','collapsible',false);
            //console.log($(this).accordion('option','active'));
        });*/
     
        // Searchbox label in searchbox  
        if ($('search')) {
            var label = $('#search label').text();
            var box = $('#search .searchBox');
            box.val(label);
            box.click(function() {this.val('');});
        }

        // Contact altijd in beeld
        var contact = $('.contactInfo');
        contact.floatingBox();
    })

    $.fixBrowsers = function() {
        // CSS3 hack for IE6 and IE7
        if($.browser.msie) {
            $('#nav .AspNet-Menu-Horizontal li:last-child').css('border-right','none');
            if ($('#language')) $('#language li:last-child').css('border-right','none');
        }

        // replace all rel=external links with target=_blank
        $('a[rel=external]').attr('target', '_blank');
    }

    $.setupExternalLinks = function() {
        // Links to external domain
        $('a[href^=http]').each(function() {
            if ($(this).attr('href').indexOf(location.hostname) == -1) {
                $(this).attr('target', '_blank');
            }
        });

        // replace all rel=mediaitem links (media related to page) with target=blank
        $('a[rel=mediaitem]').attr('target', '_blank');
    }
    
    // Make a floating box
    // borrowed from: http://www.webanddesigners.com/floating-share-box
    $.fn.floatingBox = function(options) {
        settings = jQuery.extend({
            topPadding: 15
        }, options);

        var box = $(this);
        var offset = box.offset();

        $(window).scroll(function() {
            if ($(window).scrollTop() > offset.top) {
                box.stop().animate({
                    marginTop: $(window).scrollTop() - offset.top + settings.topPadding
                });
            } else {
                box.stop().animate({
                    marginTop: 0
                });
            }
        });
    }

    $.Sidenav = function() {
        $('.HomepageButtons .hpItem').mouseenter(function() {
            $(this).find('.hpContent').slideDown();
        }).mouseleave(function() {
            $(this).find('.hpContent').slideUp();
        });
    
        $('.HomepageButtons .hpContent').each(function() {
            $(this).show().hide();
            $(this).find('a').show();
        });
    }

})(jQuery);