﻿$(document).ready(function() {
    timer = null;
    index = null;
    timerOn = true;
    maxHeight = 0;
    numberOfItems = $('#nieuws_home ul li').length;

    showDesc(0);

    $('#nieuws_home ul').hover(function() {
        if (timer) {
            clearTimeout(timer);
            timerOn = false;
        }
        $('a.nieuwslink').hover(function() {
            if (timerOn) {
                clearTimeout(timer);
            }
            index = $(this).parent().prevAll().length + 1;
            $('.description').hide();
            $('.nieuwslink').removeClass('selected');
            $(this).siblings('.description').show();
            $(this).addClass('selected');
        }, function() {
        });
    }, function() {
        // zet timer weer aan...
        if (!index || index >= numberOfItems) { index = 0; }
        timer = setTimeout('showDesc(' + index + ')', 2000);
        timerOn = true;
    });

    $('.nieuwsImage').click(function() {
        window.location = $(this).siblings('.moreinfo').children('a.leesverder').attr('href');
        return false;
    });
    
    // Voer dit pas uit als alle images geladen zijn:
    $("img").one('load', function() {
        // Pas groote div aan a.d.h.v. grootste item  ;
        var nieuwsKaderHeight = $('#home_kader').height();
        $('.description').each(function() {
            myHeight = $(this).height() + 15;
            if (myHeight >= nieuwsKaderHeight) {
                $('#home_kader').css('height', myHeight);
                nieuwsKaderHeight = myHeight;
            }
        });
    }).each(function() {
        if (this.complete) $(this).load();
    });
    

});

function showDesc(nr) {
    $('.description').hide();
    $('#nieuws_home li a:not(.selected) > .description').hide();
    $('.nieuwslink').removeClass('selected');
    $('.nieuwslink:eq(' + nr + ')').addClass('selected');
    $('.description:eq(' + nr + ')').fadeIn('slow', function() {
        aantal = $('.description').size();
        next = nr + 1;
        if (next >= aantal) next = 0;
        if (timerOn) {
            timer = setTimeout('showDesc(' + next + ')', 3000);
        }
    });
}
