﻿
(function($) {
    var specialTableClass = 'umcg';
    //add extra class to the html tag to enable css tweaks
    var extraClass = '';
    if ($.browser.msie) {
        extraClass = 'ie ie' + parseInt($.browser.version, 10);
    } else if ($.browser.mozilla) {
        extraClass = 'gecko';
    } else if ($.browser.webkit) {
        extraClass = 'webkit';
    } else if ($.browser.opera) {
        extraClass = 'opera';
    }
    if ($.support.boxModel) {
        extraClass += ' boxmodel';
    }
    if (extraClass != '') {
        $('html').addClass(extraClass);
    }



    /** Global utility object  */
    Site = {
        MailTo: function(encodedMail) {
            document.location.href = 'mailto:' + encodedMail.join('').split(' ').join('').replace('[at]', '@');
        },

        searchUrl: false,
        /** activates the search field */
        initExtendedSearch: function() {
            //.extendedsearchSelector
            //.documentsearch
            if ($('.extendedsearchSelector').val() != 'document') {
                $('.documentsearch').hide();
            }
            $('.extendedsearchSelector').bind('change', function() {
                if ($(this).val() == 'document') {
                    $('.documentsearch').show();
                } else {
                    $('.documentsearch').hide();
                }
            });
        },
        createSearch: function() {
            var wm = 'Zoeken';
            var txt = $('.searchbox input');
            txt.val(wm);
            txt.bind('keydown', function(e) {
                if (e.which == 13) {
                    var v = txt.val();
                    if (v.length > 0) {
                        //go to the search page
                        var url = Site.searchUrl.replace(/\$\{query\}/, v);
                        document.location.href = url + '#results';
                    }
                    e.preventDefault();
                    e.stopPropagation();
                    return false;
                }
            }).bind('focus', function(e) {
                if (txt.val() == wm) {
                    txt.val('');
                }
            }).bind('blur', function(e) {
                if (txt.val() == '') {
                    txt.val(wm);
                }
            });
            $('.searchbox .searchbutton').css('cursor', 'pointer').click(function() {
                var v = txt.val();
                if (v.length > 0 && v != wm) {
                    //go to the search page
                    var url = Site.searchUrl.replace(/\$\{query\}/, v);
                    document.location.href = url + '#results';
                }
            });
        },

        updateColumnHeights: function() {
            //make sure all .column elements inside the same  .columns element are the same height
            var bodyHeight = $('#body').height();
            $('.columns').each(function() {
                var me = $(this),
                columns = me.children('.column'),
                maxHeight = bodyHeight;
                columns.each(function() {
                    maxHeight = Math.max(maxHeight, $(this).height());
                });
                columns.css('min-height', maxHeight + 'px');
            });
        },

        onContentChanged: function() {
            this.updateColumnHeights();
            this.updateTableStyle();
        },




        initMenu: function() {
            var activeMenu = false, activeMenuParent = false;
            var hideTimeout = false;

            function isActiveItem(item) {
                if (!activeMenu) { return false; }
                var aid = activeMenu.data('itemId');
                var mid = item.data('itemId');
                return aid === mid;

            }
            function hide() {
                //hide the active menuitem
                if (activeMenu) {
                    activeMenu.hide();
                    activeMenu = false;
                    hideTimeout = false;
                }
            }
            $('#nav ul.submenu').each(function(index) {
                //set a unique number for each submenu
                $(this).data('itemId', index);
            });

            //process hover
            $('#nav ul.menu').delegate('li.item', 'mouseover mouseout', function(e) {
                if (e.type == 'mouseout') {
                    if (!hideTimeout) { //schedule hide
                        hideTimeout = setTimeout(hide, 250);
                    }
                } else if (e.type == 'mouseover') {
                    if (hideTimeout) { clearTimeout(hideTimeout); hideTimeout = false; }
                    var target = $(this).children('ul.submenu');
                    if (!isActiveItem(target)) {
                        hide();
                        activeMenu = target;
                        activeMenu.show();
                    }
                }
                e.preventDefault();
            });
            $('#nav ul.menu').delegate('ul.submenu', 'mouseover mouseout', function(e) {
                if (e.type == 'mouseout') {
                    if (!hideTimeout) {
                        hideTimeout = setTimeout(hide, 250);
                    }
                } else if (e.type == 'mouseover') {
                    if (hideTimeout) { clearTimeout(hideTimeout); hideTimeout = false; }
                    target = $(this);
                    if (!isActiveItem(target)) {
                        hide();
                        activeMenu = target;
                        activeMenu.show();
                    }
                }
                e.preventDefault();
            });

            $('#footer ul.menu li.item:first-child').addClass('first');
            $('#footer ul.menu > li.item').each(function(index) {
                var w = 140;
                switch (index) {
                    case 0: w = 135; break;
                    case 1: w = 144; break;
                    case 2: w = 140; break;
                    case 3: w = 144; break;
                    case 4: w = 140; break;
                    case 5: w = 144; break;
                    default: break;
                }
                w -= 10;
                $(this).width(w + 'px');

            });
        },

        //evenly spread all items
        positionMenu: function() {
            var menu = $('#nav ul.menu');
            var items = menu.children('.item');
            var w = parseInt(menu.width(), 10);
            //var logoutW = parseInt(menu.children('.logout').width(), 10);
            var available = w; // - logoutW;
            //items.hide();
            var itemWidth = 0;
            var itemMargin = 0;
            items.each(function(idx) {
                var me = $(this);
                if (idx == 0) {
                    me.css('margin-left', '5px');
                }
                itemWidth += parseInt(me.width(), 10);
                itemMargin += parseInt(me.css('margin-left'), 10);
            });
            available -= itemWidth;
            available -= itemMargin;
            items = items.not('.logout,.profiel');

            var margin = Math.floor(available / (items.length - 1));
            //items.not(':first').css('padding-left', margin + 'px');
            items.not(':last').css('padding-right', margin + 'px');


        },
        updateTableStyle: function() {
            var replacedHeaders = false;
            $('table.' + specialTableClass).each(function() {
                var table = $(this);
                var th = table.find('th,tr');
                if (th.length > 0) {
                    th = $(th.get(0));
                    th.addClass('umcgHeader');
                    th.find('td').each(function(i) {
                        var td = $(this);
                        td.addClass((i % 2 == 0) ? 'odd' : 'even');
                        if (td.find('h1,h2,h3,h4,h5,h6,h7').length == 0) {
                            td.html('<h3>' + td.html() + '</h3>');
                            replacedHeaders = true;
                        }
                    });
                    var allTr = table.find('tr');  // get all tablerows
                    for (i = 0; i < allTr.length; i++) {
                        if (!$(allTr.get(i)).hasClass('umcgHeader')) {   // if it already has this class its the header, dont change it
                            // change tablerow class accordingly
                            $(allTr.get(i)).addClass((i % 2 == 0) ? 'odd' : 'even');
                        }
                    }
                }
            });
            Cufon.replace('table.umcg h3');
        }
    };

    /** initialize columns */
    $(function() {
        Site.onContentChanged();
        Site.createSearch();
        Site.initMenu();
        Site.positionMenu();

        if ($.browser.msie) {
            //alert($('#header input.searchField'));
            var sf = $('#header input.searchField');
            sf.css({
                'height': (parseInt(sf.height(), 10) - 3) + 'px',
                'padding-top': '3px'
            }).val('' + sf.val());
        }

    });

    //Cufon.replace('#nav .menu .item > a, #nav .itemsub > a', { hover: true });
    Cufon.replace('#nav .menu a', { hover: true });
    Cufon.replace('h1,h2,h3,h4,h5, .button');
    //Cufon.replace('h1, .button, #nav .submenu a');


})(jQuery);

