/*
 * javascript framework: MooTools, My Object Oriented Javascript Tools, <http://mad4milk.net>
 * version  : v1.11
 * used for : (1) dropdown menus in menu-block with (2) opacity, (3) opacity purple background nieuw-text & opdracht-text
*/

Element.extend( {
    hide: function() {
        this.setStyle('visibility', 'hidden');
    },

    show: function() {
        this.setStyle('visibility', 'visible');
    }
});
//save state for mouse over system.
var primaryLastULSelected = null;
var primaryLastASelected = null;
var secondairyLastULSelected = null;
var secondairyLastASelected = null;
//only used when in eos.
var tertiaryLastULSelected = null;
var tertiaryLastASelected = null;

//save state for entry page.
var primaryShowState = false;
var primaryShowElement = null;
var secondairyShowState = false;
var secondairyShowElement = null;
//only used when in eos.
var tertiaryShowState = false;
var tertiaryShowElement = null;

var DropdownMenuPrimary = new Class({
    initialize: function(element) {
        $A($(element).childNodes).each(function(el) {
            if(el.nodeName.toLowerCase() == 'li') {

                var hasUl = false;
                var lastA = null;
                $A($(el).childNodes).each(function(el2) {

                    if(el2.nodeName.toLowerCase() == 'a') {

                        lastA = $(el2);
                    }
                    if(el2.nodeName.toLowerCase() == 'ul') {
                        //er is een sub menu.
                        hasUl = true;
                        $(el2).hide();//hide submenu.
                        $A($(el2).childNodes).each(function(el3) {

                            if(el3.firstChild && el3.firstChild.nodeName.toLowerCase() == 'a'){

                                if($(el3.firstChild).hasClass('selected2')) {
                                    //this is a child page, so show selected submenu.
                                    primaryShowState = true;
                                    primaryShowElement = $(el2);
                                    $('menu-block-bg-secondary').show();
                                    $(el2).show();
                                    primaryLastULSelected = el2;
                                }
                            }

                        });

                        if(!primaryShowState) {
                            $('menu-block-bg-secondary').hide();
                        }

                        lastA.removeEvents();
                        lastA.addEvent('mouseover', function() {
                            //verwijder eerder geselecterde menus.
                            if(primaryLastULSelected != null) {

                                primaryLastULSelected.hide();
                            }
                            if( secondairyLastULSelected != null) {

                                secondairyLastULSelected.hide();
                                secondairyLastULSelected=null;
                                $('menu-block-bg-tertiary').hide();
                                if(secondairyLastASelected != null) {

                                    secondairyLastASelected.removeClass('selected');
                                }
                            }
                            if(tertiaryLastULSelected != null){

                                tertiaryLastULSelected.hide();
                                tertiaryLastULSelected=null;
                                $('menu-block-bg-quat').hide();
                            }
                            if(primaryLastASelected!=null){

                                primaryLastASelected.removeClass('selected');
                            }
                            //verwijder de door een geselecteerde pagina aangezette
                            //breadcrumb functie van het menu.
                            if(primaryShowElement != null && primaryShowElement != $(el2)) {

                                primaryShowElement.hide();
                                if(secondairyShowElement != null) {

                                    secondairyShowElement.hide();
                                    $('menu-block-bg-tertiary').hide();
                                }
                            }
                            //toon het nu geselecteerde menu. en set blijven tonen aan
                            $('menu-block-bg-secondary').show();
                            $(el2).show();
                            primaryLastULSelected = el2;
                            this.addClass('selected');
                            primaryLastASelected = this;

                            return false;
                        });
                    };
                    if(!hasUl) {

                        lastA.addEvent('mouseover', function() {
                            //verwijder eerder geselecteerde menus.
                            if(primaryLastULSelected != null) {

                                primaryLastULSelected.hide();
                                primaryLastULSelected=null;
                                $('menu-block-bg-secondary').hide();
                            }
                            if( secondairyLastULSelected != null) {

                                secondairyLastULSelected.hide();
                                secondairyLastULSelected=null;
                                $('menu-block-bg-tertiary').hide();
                                if(secondairyLastASelected != null) {

                                    secondairyLastASelected.removeClass('selected');
                                }
                            }
                            if(tertiaryLastULSelected != null){

                                tertiaryLastULSelected.hide();
                                tertiaryLastULSelected=null;
                                $('menu-block-bg-quat').hide();
                            }
                            if(primaryLastASelected!=null){

                                primaryLastASelected.removeClass('selected');
                            }
                            //verwijder de door een geselecteerde pagina aangezette
                            //breadcrumb functie van het menu.
                            if(primaryShowElement != null) {

                                primaryShowElement.hide();
                                $('menu-block-bg-secondary').hide();
                                if(secondairyShowElement != null) {

                                    secondairyShowElement.hide();
                                    $('menu-block-bg-tertiary').hide();
                                }
                            }

                            //toon het nu geselecteerde menu. en set blijven tonen aan
                            this.addClass('selected');
                            primaryLastASelected = this;

                            return false;
                        });
                    }
                });
            };
        });
        return this;
    }
});


var DropdownMenuSecondary = new Class({
    initialize: function(element) {
        $A($(element).childNodes).each(function(el) {
            if(el.nodeName.toLowerCase() == 'li') {
                $A($(el).childNodes).each(function(el2) {
                    if(el2.nodeName.toLowerCase() == 'ul') {
                        $A($(el2).childNodes).each(function(el3) {
                            if(el3.nodeName.toLowerCase() == 'li') {

                                var hasUl = false;
                                var lastA = null;
                                $A($(el3).childNodes).each(function(el4) {

                                    if(el4.nodeName.toLowerCase() == 'a') {

                                        lastA = $(el4);
                                    }
                                    if(el4.nodeName.toLowerCase() == 'ul') {

                                        hasUl = true;
                                        $(el4).hide();
                                        $A($(el4).childNodes).each(function(el5) {

                                            if(el5.firstChild && el5.firstChild.nodeName.toLowerCase() == 'a'){

                                                if($(el5.firstChild).hasClass('selected2')) {

                                                    secondairyShowState = true;
                                                    secondairyShowElement = $(el4);
                                                    $('menu-block-bg-tertiary').show();
                                                    $(el4).show();
                                                    secondairyLastULSelected = $(el4);
                                                }
                                            }

                                        });

                                        if(!secondairyShowState) {

                                            $('menu-block-bg-tertiary').hide();
                                        }

                                        lastA.removeEvents();
                                        lastA.addEvent('mouseover', function() {
                                            //verwijder de door een geselecteerde pagina aangezette
                                            //breadcrumb functie van het menu.
                                            if(secondairyShowElement!= null && secondairyShowElement!=$(el4)) {

                                                secondairyShowElement.hide();
                                            }
                                            //verwijder eerder geselecteerde menus.
                                            if(secondairyLastULSelected != null) {

                                                secondairyLastULSelected.hide();
                                            }
                                            if(tertiaryLastULSelected != null){

                                                tertiaryLastULSelected.hide();
                                                tertiaryLastULSelected=null;
                                                $('menu-block-bg-quat').hide();
                                            }
                                            if(secondairyLastASelected!=null){

                                                secondairyLastASelected.removeClass('selected');
                                            }
                                            //toon het nu geselecteerde menu. en set blijven tonen aan
                                            $('menu-block-bg-tertiary').show();
                                            $(el4).show();
                                            secondairyLastULSelected = $(el4);
                                            this.addClass('selected');
                                            secondairyLastASelected = this;

                                            return false;
                                        });

                                    };
                                    if(!hasUl) {

                                        lastA.addEvent('mouseover', function() {
                                            //verwijder de door een geselecteerde pagina aangezette
                                            //breadcrumb functie van het menu.
                                            if(secondairyShowElement != null) {

                                                secondairyShowElement.hide();
                                                $('menu-block-bg-tertiary').hide();
                                            }
                                            //verwijder eerder geselecteerde menus.
                                            if(secondairyLastULSelected != null) {

                                                secondairyLastULSelected.hide();
                                                secondairyLastULSelected=null;
                                            }
                                            if(tertiaryLastULSelected != null){

                                                tertiaryLastULSelected.hide();
                                                tertiaryLastULSelected=null;
                                                $('menu-block-bg-quat').hide();
                                            }
                                            if(secondairyLastASelected!=null){

                                                secondairyLastASelected.removeClass('selected');
                                            }
                                            //toon het nu geselecteerde menu. en set blijven tonen aan
                                            this.addClass('selected');
                                            secondairyLastASelected = this;

                                            return false;
                                        });
                                    }
                                });

                            };
                        });
                    };
                });
            };
        });
        return this;
    }
});


function MakeOpaque(element, val) {
    $A(element).each(function(el) {
        if($chk($(el))) {
            el.setOpacity(val);
        };
    });
};

function init_click_img( aImageCollection){
    var imgCollection = $(aImageCollection);
    if ($chk( imgCollection)){
        var validExt = new Array('png', 'gif', 'jpg', 'jpeg', 'bmp');
        var imageList = $$('#'+aImageCollection+' #aImg');
        for (var i = 0; i < imageList.length; i++) {
            var aImg = imageList[i];
            var href = aImg.href;

            if (href == window.location.href){
                aImg.setStyle('cursor', 'arrow');
                aImg.removeAttribute("href");
                continue;
            }

            if (href.indexOf( document.domain) == -1){
                //external link
                aImg.setProperty('target', '_blank');
                continue;
            }

            var extension = href.substr( href.length - 3 ).toLowerCase();
            if (validExt.indexOf(extension) != -1){
                aImg.setProperty('rel', 'lightbox');
            }
        }
    }
}


function initAccordion(){
	if ($('accordion')) {
		var accNewsItems = new Accordion('div.orange-bold', 'div.text', {opacity: false}, $('accordion'));
		
		// add event to togle summery
		accNewsItems.addEvent('onActive', function(header) {
			var summery = header.getLast();
			// check if there is an actual summery
			if (summery.hasClass('summaryText') === true) {
				accordionToggleSummery(false, summery);
			}
		});
		
		accNewsItems.addEvent('onBackground', function(header) {
			var summery = header.getLast();
			// check if there is an actual summery
			if (summery.hasClass('summaryText') === true) {
				accordionToggleSummery(true, summery);
			}
		});
		
		
		// Set all summerytexts except the first one to visable
		$$('div.summaryText').each(
			function(item, index) {
				if (index != 0) {
					accordionToggleSummery(true, item);
				} else {
					accordionToggleSummery(false, item);
				}
			}
		);
	}
}

function accordionToggleSummery(show, item) {
	if (show === true) {
		item.setStyle('visibility', 'visible');
		item.setStyle('display', 'block');
		item.setStyle('height', 'auto');
	} else {
		item.setStyle('visibility', 'hidden');
		item.setStyle('display', 'none');
		item.setStyle('height', 0);
	}
}

Window.onDomReady(function() {StartsIFR()});
Window.onDomReady(function() {initAccordion()});
Window.onDomReady(function() {MakeOpaque($$('div.opdracht-bg', 'div.nieuws-bg', '#menu-block-bg-secondary'), 0.7)});
Window.onDomReady(function() {MakeOpaque($$('#menu-block-bg-tertiary'), 0.4)});
Window.onDomReady(function() {new DropdownMenuPrimary($E('ul', 'menu-block'))});
Window.onDomReady(function() {new DropdownMenuSecondary($E('ul', 'menu-block'))});

