Site = {
    init : function() {
        new ThiemeMenu('navigation');
    },
    initFaq : function() {
        var nodes = $$('div.faq dl > *, dl.faq > *');
        if (nodes == null) {
            return;
        }
        for (var i = 0; i < nodes.length; i += 2) {
            var dt = nodes[i], dd = nodes[i + 1];
            if (dt.get("tag") == "dt" && dd.get("tag") == "dd") {
                var elements = $$([dt,dd]);
                elements.addClass("closed");
                dt.addEvent("click", function(e) {
                    e.preventDefault();
                    this.toggleClass("closed");
                }.bind(elements));
            } else {
                i -= 1;
            }
        }
    },
    initSitemap : function(id) {
        var map = $(id); //if(map==null){return;}
        map.removeClass("plain");
        map.addClass("js");
        map.addEvent("click", function(e) {
            var el = $(e.target);
            var p;
            if (el = el.hasClass("toggle") && el || el.getParent(".toggle")) {
                p = el.getParent("li");
                if (p.hasClass("open")) {
                    p.removeClass("open");
                } else {
                    p.addClass("open");
                }
            }
        });
    },
    initClickList : function(id) {
        var list = $(id);//if(list==null){return;}
        if ($defined(list)) {
            list.getChildren("li").each(function(li) {
                var a, url;
                if (a = li.getElement("a.more")) {
                    url = a.getProperty("href");
                    li.addClass("clickable");
                    li.addEvent("click", function() {
                        window.location = url;
                    })
                }
            })
        }
    }
};

var ThiemeMenu = new Class({
    Implements : Options,
    options : {
        openDelay : 500,
        closeDelay : 500
    },
    initialize : function(id) {
        this.element = $(id);
        this.element.removeClass("plain");
        this.element.addClass("js");
        this.setOptions(arguments[1]);

        this.active = false;
        this.current = null;

        this._addEvents();
    },
    over : function(li) {
        if (this.current) {
            this.out(this.current)
        }
        this.current = li;

        if (!this.active) {
            return;
        }
        li.addClass("open");
    },
    out : function(li) {
        li.removeClass("open");
    },
    delayedActivate : function() {
        this.activateTimer = (function() {
            this.active = true;
            if (this.current) {
                this.over(this.current);
            }
        }).delay(this.options.openDelay, this);
        $clear(this.deactivateTimer);
    },
    delayedDeactivate : function() {
        this.deactivateTimer = (function() {
            this.active = false
            if (this.current) {
                this.out(this.current);
            }
        }).delay(this.options.closeDelay, this);
        $clear(this.activateTimer);
    },
    _addEvents : function() {
        this.element.addEvents({
            mouseenter : this.delayedActivate.bind(this),
            mouseleave : this.delayedDeactivate.bind(this)
        });

        this.element.getChildren("li").each(function(li) {
            li.addEvents({
                mouseenter : this.over.bind(this, li)
            });
        }.bind(this));
    }
});

/*
 Script: BarackSlideshow.js
 Lightweight slideshow script, based on Fx.MorphList

 License:
 MIT-style license.

 Authors:
 Guillermo Rauch
 */

var ThiemeSlideshow = new Class({

    Extends: Fx.MorphList,

    options: {/*
     onShow: $empty,*/
        auto: false,
        autostart: false,
        autointerval: 2000,
        transition: 'fade',
        tween: { duration: 700 }
    },

    initialize: function(menu, images, options) {
        this.parent(menu, options);
        this.images = $(images);
        this.imagesitems = this.images.getChildren().fade('hide');
        this.loaded = true;
        if ($type(this.options.transition) != 'function') {
            this.options.transition = $lambda(this.options.transition);
        }
        if (this.current) {
            this.show(this.items.indexOf(this.current));
        } else if (this.options.auto && this.options.autostart) {
            this.progress();
        }
    },

    auto: function() {
        if (!this.options.auto) {
            return false;
        }
        $clear(this.autotimer);
        this.autotimer = this.progress.delay(this.options.autointerval, this);
    },

    onClick: function(event, item) {
        this.parent(event, item);
        event.stop();
        this.show(this.items.indexOf(item));
        this.options.auto = false;
        $clear(this.autotimer);
    },

    show: function(index) {
        if (!this.loaded) {
            return;
        }
        var image = this.imagesitems[index];
        if (image == this.curimage) {
            return;
        }
        image.set('tween', this.options.tween).dispose().inject(this.curimage || this.images.getFirst(), this.curimage ? 'after' : 'before').fade('hide');
        var trans = this.options.transition.run(null, this).split('-');
        switch (trans[0]) {
            case 'slide':
                var dir = $pick(trans[1], 'left');
                var prop = (dir == 'left' || dir == 'right') ? 'left' : 'top';
                image.fade('show').setStyle(prop, image['offset' + (prop == 'left' ? 'Width' : 'Height')] * ((dir == 'bottom' || dir == 'right') ? 1 : -1)).tween(prop, 0);
                break;
            case 'fade': image.fade('in'); break;
        }
        image.get('tween').chain(function() {
            this.auto();
            this.fireEvent('show', image);
        }.bind(this));
        this.curimage = image;
        this.setCurrent(this.items[index])
        this.morphTo(this.items[index]);
        return this;
    },

    progress: function() {
        var curindex = this.imagesitems.indexOf(this.curimage);
        this.show((this.curimage && (curindex + 1 < this.imagesitems.length)) ? curindex + 1 : 0);
    }

});


var Broadcast = new Class({
    initialize : function(title, content) {
        this.title = title;
        this.content = content;
    },
    open : function() {
        this._createElements();
        this.contentEl.getChildren().setStyle("display", "block");
    },
    close : function() {
        this.element.dispose();
    },
    _createElements : function() {

        this.element = new Element("dl", {"class" : "broadcast"});
        this.titleEl = new Element("span").set("html", this.title);
        this.contentEl = new Element("dd", { "class" : "content" });

        if ($type(this.content) == "element") {
            this.contentEl.adopt(this.content.clone(true))
        } else {
            this.contentEl.set("html", this.content)
        }

        this.element.adopt(
                new Element("dt").adopt(
                        this.titleEl,
                        (new Element("a", { "href" : "#", "class" : "close", "events" : {"click" : this.close.bind(this) } })).set("html", "x")
                        ),
                this.contentEl,
                (new Element("dd", { "class" : "footer" })).adopt(
                        (new Element("a", { "href" : "#",  "class" : "close", "events" : {"click" : this.close.bind(this) } })).set("html", "Sluit")
                        )
                );

        $(document.body).adopt(this.element);
    }
});

var Styleswitcher = {
    activate : function(title) {
        var i, a, main;
        for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
            if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
                a.disabled = true;
                if (a.getAttribute("title") == title) {
                    a.disabled = false;
                }
            }
        }
    },
    getActive : function() {
        var i, a;
        for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
            if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) {
                return a.getAttribute("title");
            }
        }
        return null;
    },
    getPreferred : function() {
        var i, a;
        for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
            if (a.getAttribute("rel").indexOf("style") != -1
                    && a.getAttribute("rel").indexOf("alt") == -1
                    && a.getAttribute("title")
                    ) {
                return a.getAttribute("title");
            }
        }
        return null;
    },
    createCookie : function(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else {
            expires = "";
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    },
    readCookie : function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1, c.length);
            }
            if (c.indexOf(nameEQ) == 0) {
                return c.substring(nameEQ.length, c.length);
            }
        }
        return null;
    },
    load : function() {
        var cookie = this.readCookie("style");
        var title = cookie ? cookie : this.getPreferred();
        this.activate(title);
    },
    unload : function() {
        this.createCookie("style", this.getActive(), 365);
    },
    init : function() {
        window.addEvent("domready", this.load.bind(this));
        window.addEvent("unload", this.unload.bind(this));

        var cookie = this.readCookie("style");
        var title = cookie ? cookie : this.getPreferred();
        this.activate(title);
    }
};

Styleswitcher.init();

// ========================
// = Font initializations =
// ========================

Cufon.replace("h1");
Cufon.replace("#home #content h2");
Cufon.replace("#navigation .extra dt");
Cufon.replace(".export h2");
Cufon.replace(".carousel h2");
Cufon.replace("#serviceblock dt");
Cufon.replace("dl.more dt");


// search box cleanup
function runConfig() {
    var query = $('query');
    if (!$defined(query)) {
        return;
    }
    var label = 'Type uw zoekterm(en)';
    if (query.get('value').trim() == '') {
        $('query').set('value', label);
    }
    query.addEvent('focus', function() {
        if ($('query').get('value').trim() == label) {
            $('query').set('value', '');
        }
    });
    $('query').addEvent('blur', function() {
        if ($('query').get('value').trim() == '') {
            $('query').set('value', label);
        }
    });
    //
    if ($defined($('pagesize'))) {
        $('pagesize').addEvent('change', onPageSize);

    }
    if ($defined($('pagesort'))) {
        $('pagesort').addEvent('change', onPageSort);
    }

    // enter button submit
    query.addEvent('keydown', function(e) {
        if (e.key == 'enter') {
            var form = $('search');
            if ($defined(form)) {
                form.submit();
            }
        }
    });
    var secondQuery = $('search_term');
    if ($defined(secondQuery)) {
        secondQuery.addEvent('keydown', function(e) {
            if (e.key == 'enter') {
                var form = $('search_form');
                if ($defined(form)) {
                    form.submit();
                }
            }
        });
    }
    var searchButton = $('header_search_button');
    if ($defined(searchButton)) {
        searchButton.addEvent('click', function(e) {
            var form = $('search');
            if ($defined(form)) {
                form.submit();
            }
        });
    }
    searchButton = $('search_button');
    if ($defined(searchButton)) {
        searchButton.addEvent('click', function(e) {
            var form = $('search_form');
            if ($defined(form)) {
                form.submit();
            }
        });
    }
}

function onPageSize(formId) {
    var val = $('pagesize').get('value');
    if ($defined($('size'))) {
        $('size').set('value', val);
    }
    $('search_form').submit();
}

function onPageSort(formId) {
    var val = $('pagesort').get('value');
    if ($defined($('sort'))) {
        $('sort').set('value', val);
    }
    $('search_form').submit();
}

function setTaxonomyFilters(removeAll, allSelected) {
    if (removeAll) {
        $('catfilter').set('value', '');
    } else {
        var catFilter = '';
        var hasKeys = false;
        $$('input[class="taxonomy"]').each(function(obj) {
            // we submit checked items
            if (obj.checked) {
                if (catFilter.length > 0) {
                    catFilter += ",";
                }
                catFilter += obj.value;
                hasKeys = true;
            }
        });
        if(hasKeys && allSelected){
            catFilter += ",invert";
        }

        $('catfilter').set('value', catFilter);
    }
}

function selectAllFilters() {
    $$('input[class="taxonomy"]').each(function(obj) {
        $(obj).setProperty('checked', 'checked');
    });
    setTaxonomyFilters(false, true);
}
// methods and subject overview page events
function runOverviewConfig() {
    if ($defined($('paging_controls'))) {
        var inverse = true;
        var selector1 = $('clearallfilters-top');
        if (!$defined(selector1)) {
            selector1 = $('selectallfilters-top');
            inverse = false;
        }
        var selector2 = $('clearallfilters-bottom');
        if (!$defined(selector2)) {
            selector2 = $('selectallfilters-bottom');
            inverse = false;
        }
        if ($defined(selector1) && $defined(selector2)) {
            selector1.addEvent("click", function() {
                if (!inverse) {
                    selectAllFilters();
                } else {
                    setTaxonomyFilters(true);
                }
                $('paging_controls').submit();
                return false;
            });
            selector2.addEvent("click", function() {
                if (!inverse) {
                    selectAllFilters();
                } else {
                    setTaxonomyFilters(true);
                }
                $('paging_controls').submit();
                return false;
            });
            $$('input[class="taxonomy"]').addEvent("click", function() {
                setTaxonomyFilters();
                $('paging_controls').submit();
            });
        }
        if ($defined($('control_sort'))) {
            $('control_sort').addEvent("change", function() {
                $('paging_controls').submit();
            });
        }
        if ($defined($('control_perpage'))) {
            $('control_perpage').addEvent("change", function() {
                $('paging_controls').submit();
            });
        }
    }
}

function runBroadcastConfig() {
    if ($defined($('broadcast'))) {

        var broadcastId = $('broadcastId');
        if (!$defined(broadcastId)) {
            return;
        }
        broadcastId = broadcastId.get("value");
        var cookieValue = Cookie.read(broadcastId);
        if (cookieValue == "show") {
            $('broadcast').setStyle('visibility', 'visible');
        } else {
            $('broadcast').setStyle('visibility', 'hidden');
        }
        $('broadcast').getElements('a[class=close]').each(function(obj) {
            obj.addEvent("click", function() {
                Cookie.write(broadcastId, 'closed');
                $('broadcast').setStyle('visibility', 'hidden');
                return false;
            });
        });
    }
}

var last_open_menu_item = null;
function setupNavigation() {
    $$('li.closed').each(function(el) {
        var nav = $('navigationcolumn');
        var main = el.getFirst();
        main.addEvent('click', function(e) {
            var holder = this.getParent();
            if (last_open_menu_item) {
                last_open_menu_item.removeClass('open');
                last_open_menu_item.addClass('closed');
            }
            holder.removeClass("closed");
            holder.addClass("open");
            last_open_menu_item = holder;
            return false;
        }.bind(main));
    });
}
function setupGA() {
    try {
        //$("a[href*=.pdf], a[href*=.doc], a[href*=.ppt]").attr("target", "_blank").attr("onclick", "_gaq.push(['_trackPageview', '/downloads/'+this.href]);");
    } catch(e) {
        // ignore
    }
}
function siteSetup() {
    //Site.init.bind(Site);
    runConfig();
    runOverviewConfig();
    runBroadcastConfig();
    setupNavigation();
    setupGA();
}

window.addEvent("domready", Site.init.bind(Site));
window.addEvent("domready", siteSetup);






