function updateCalendar() {
    var datumDagValue = [];
    $(".agendaItems li.agendaLine .datumBlock .datumDag").each(function () { datumDagValue.push($(this).text()) });

    $(datumDagValue).each(function (intIndex, objValue) {
        dateLoop(objValue);
    });

    initDateClickEvents();
    updateFoldoutButton();
}

function dateLoop(objValue) {
    $('.calendar td a').each(function () {
        if ($(this).text() == objValue) {
            $(this).addClass('activeDate');
        }
    });

    $(".ui-state-default, .ui-datepicker-unselectable, .ui-datepicker-calendar").click(function () {
        return false;
    });
}

function getMonthName(month) {
    switch (month) {
        case 0: return "januari";
        case 1: return "februari";
        case 2: return "maart";
        case 3: return "april";
        case 4: return "mei";
        case 5: return "juni";
        case 6: return "juli";
        case 7: return "augustus";
        case 8: return "september";
        case 9: return "oktober";
        case 10: return "november";
        case 11: return "december";
        default: return "onbekend";
    }
}


$(document).ready(function () {

	$("font").removeAttr("face");

    $("#zoekveld").Watermark("Snelzoek", "#8f8d8d");

    if ($("#nieuwsbriefInput").length > 0) {
        $("#nieuwsbriefInput").Watermark("E-mailadres", "#8f8d8d");
    }

    if ($("#trefwoordZoek").length > 0) {
        $("#trefwoordZoek").Watermark("Vul een trefwoord in", "#8f8d8d");
    }

    // Overlays 

    $(".overlay").hide();

    $(".helpButton").toggle(function () {
        $(this).parent().find(".overlay:first").fadeIn(200);
        $(this).addClass("activeHelp");
        return false;
    }, function () {
        $(this).parent().find(".overlay:first").fadeOut(200);
        $(this).removeClass("activeHelp");
        return false;
    });

    $(".overlay .sluitButton").click(function () {
        $(this).parent().parent().find(".helpButton").click();
        return false;
    });

    // formToggle 

    $(".foldout").hide();

    $(".toggle").toggle(function () {
        $(this).parent().find(".foldout:first").fadeIn(200);
        $(this).text("Sluit venster");
        $(this).addClass("toggleActive");
        return false;

    }, function () {
        $(this).parent().find(".foldout:first").fadeOut(200);
        $(this).text("Stel uw vraag");
        $(this).removeClass("toggleActive");
        return false;
    });
    
    // themas
    
    $(".themas").toggle(function () {
        $(this).parent().find(".foldout:first").show(200);
        $(this).find("span").text("Toon alleen actuele projecten");
        $(this).addClass("themasActive");
        return false;

    }, function () {
        $(this).parent().find(".foldout:first").hide(200);
        $(this).find("span").text("Toon afgesloten projecten");
        $(this).removeClass("themasActive");
        return false;
    });

    // inline formToggle 

    $(".foldout").slideUp();

    $(".inlineToggle").toggle(function () {
        var elm = $(this).parent().parent().find(".foldout:first");
        elm.slideDown(200);
        $(this).text("Verberg tekst");
        return false;

    }, function () {
        var elm = $(this).parent().parent().find(".foldout:first");
        elm.slideUp(200);
        $(this).text("Lees meer");
        return false;
    });
    
    
    // slideshare toggle
    
    $(".foldout").hide();
    
    $(".slideshare a").toggle(function () {
    	$(this).parent().parent().find(".foldout:first").show(200);
        return false;
    }, function () {
        $(this).parent().parent().find(".foldout:first").hide(200);
        return false;
    });

    // Tabs

    if ($("#tabContainer").length > 0) {
        $('#tabButtons > ul').tabs();
    }

    // Carousel

    function mycarousel_initCallback(carousel) {
        carousel.buttonNext.bind('click', function () {
            carousel.startAuto(0);
        });

        carousel.buttonPrev.bind('click', function () {
            carousel.startAuto(0);
        });

        carousel.clip.hover(function () {
            carousel.stopAuto();
        }, function () {
            carousel.startAuto();
        });
    };

    if ($(".carousel").length > 0) {
        jQuery('.carousel').jcarousel({
            vertical: true,
            scroll: 2,
            auto: 4,
            wrap: "last",
            animation: "slow",
            initCallback: mycarousel_initCallback
        }
    	);
    }

    // Hoofdmenu hover 
	if ($("#mainMenu li:first-child a").hasClass('active')) {
		$("#mainMenu li").hover(function () {
			$(this).find("ul").fadeIn(80);
			$(this).addClass("activated");
		}, function () {
			$(this).find("ul").fadeOut(80);
			$(this).removeClass("activated");
		});
	}

    // Accordion 

    if ($(".accordion").length > 0) {
        $(".accordion").accordion(
			{ autoHeight: false }
		);
    }

    // Kalender functie

    if ($(".kalender").length > 0) {
        $(".kalender").datepicker({
            changeMonth: true,
            monthNamesShort: ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
            dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
            firstDay: 1,
            onChangeMonthYear: function (year, month) {
                refreshEventList(year, month);
            }
        });

        curDate = new Date();
        refreshFoldoutButtonText(curDate.getMonth());
        updateCalendar();
		$('div.calendar.hidden').removeClass('hidden');


        // Toggle voor Agenda dagen 

        $(".foldout").hide();

        $(".calendar .foldoutButton").click(function () {
            $(this).parent().find(".foldout:first").fadeIn(500);
            $(".agendaItems li.agendaLine").show(500);
            return false;
        });
    }

});

function initDateClickEvents() {
    $(".activeDate").click(function () {

        var requestedDate = $(this).text();

        var datumDagValue = [];
        $(".agendaItems li.agendaLine .datumBlock .datumDag").each(function () {
            datumDagValue.push($(this).text());
        });

        $(datumDagValue).each(function (intIndex, objValue) {
            var givenDate = objValue;

            if (givenDate == requestedDate) {
                $(".agendaItems li.agendaLine .datumBlock .datumDag").each(function () {
                    if ($(this).text() != givenDate) {
                        $(this).parent().parent().hide();
                    } else {
                        $(this).parent().parent().show();
                    }
                });
            }
        });
        $(".calendar .foldout").show(400);
        return false;
    });
}

function updateFoldoutButton() {
    var curItems = $('ul.agendaItems');
    var button = $(".calendar .foldoutButton");
    var foldout = $(".calendar .foldout");
    if (curItems.length == 0) {
        button.hide();
        foldout.hide();
    } else {
        button.show();
    }
}

function refreshFoldoutButtonText(month) {
    var button = $(".calendar .foldoutButton");
    button.text("Bekijk alle events van " + getMonthName(month));
}

function refreshEventList(year, month) {
    refreshFoldoutButtonText(month-1);
    $.ajaxSetup({ error: noData });
    var url = '/?id=FO_HTMLCALITEMS&j=' + year + '&m=' + month;
    $.get(url, function (data) {
        renderEventsList(data);
    });
}

function renderEventsList(data) {
    //data = data.trim();
    var curItems = $('ul.agendaItems');
    if (curItems.length == 0) {
        $('div.foldout').append(data);
    } else {
        curItems.replaceWith(data);
    }
    updateCalendar()
}

function noData(XMLHttpRequest, textStatus, errorThrown) {
    $('ul .agendaItems').replaceWith('');
    updateCalendar()
}
