$(document).ready(function() {
    // Set the width of the content (i.e. text) so that it fits to the left of the menu.
    $('#content').width($('#content_wrap').outerWidth() - $('#submenu').outerWidth() - 50);
    // Make sure that the content is at least as high as the viewport
    var windowHeight = $(window).height();
    if ($('#content').height() < windowHeight) {
        $('#content').height(windowHeight);
    }
    // Make menu and content equal height.
    $('#content_wrap').equalChildrensHeights(true, 100);

    // Firefox does not display the background image correctly, due to height of html/body/form,
    //  which are the height of the viewport.
    // Since we're using Javascript for this, we might as well fix that here too.
    // This assignment must come after the above code, because the height of the content_wrap must be set first.
    $('#container').height(
        $('#header').outerHeight() +
        $('#menu_top').outerHeight() +
        $('#content_wrap').innerHeight());
});

hideTimer = setTimeout('', 0);
activeMenu=null;
activeAanroep=null;

// Extend Number type with a method that converts from pixels to em.
// Source online: Unknown
Number.prototype.pxToEm = String.prototype.pxToEm = function(settings) {
    //set defaults
    settings = jQuery.extend({
        scope: 'body',
        reverse: false
    }, settings);

    var pxVal = (this == '') ? 0 : parseFloat(this);
    var scopeVal;
    var getWindowWidth = function() {
        var de = document.documentElement;
        return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
    };

    /* When a percentage-based font-size is set on the body, IE returns that percent of the window width as the font-size. 
    For example, if the body font-size is 62.5% and the window width is 1000px, IE will return 625px as the font-size. 	
    When this happens, we calculate the correct body font-size (%) and multiply it by 16 (the standard browser font size) 
    to get an accurate em value. */

    if (settings.scope == 'body' && $.browser.msie && (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(1) > 0.0) {
        var calcFontSize = function() {
            return (parseFloat($('body').css('font-size')) / getWindowWidth()).toFixed(3) * 16;
        };
        scopeVal = calcFontSize();
    }
    else { scopeVal = parseFloat(jQuery(settings.scope).css("font-size")); };

    var result = (settings.reverse == true) ? (pxVal * scopeVal).toFixed(2) + 'px' : (pxVal / scopeVal).toFixed(2) + 'em';
    return result;
};

// Extend the document with a function that compares the height of all children (of the object on which the function was
// evoked) and sets all heights equal to the tallest child.
// TODO: Supply list of objects to be made of equal height.
$.fn.equalChildrensHeights = function(px, padding_bottom) {
    $(this).each(function() {
        var tallest = 0;
        $(this).children().each(function(i) {
            if ($(this).height() > tallest) {
                tallest = $(this).height();
            }
        });
        // Convert to em.
        if (!px || !Number.prototype.pxToEm)
            tallest = tallest.pxToEm();

        // Set all children's heights to be the tallest.
        $(this).children().css({ 'height': tallest + padding_bottom });
    });
    return this;
};

function openMenu(menu) {
    clearTimeout(hideTimer);
    closeMenu();

	document.getElementById('divMenu' + menu).style.visibility = "visible";

	TopObjects('hidden');
	activeMenu = menu;
    
    return false;
}

function closeMenu() {
	if(activeMenu!=null)
	{
        document.getElementById('divMenu' + activeMenu).style.visibility = "hidden";

		TopObjects('visible');
        activeMenu = null;
    }
}

function TopObjects(sVisibility) {
	oSelects = document.getElementsByTagName('SELECT');
	if (oSelects.length > 0) {
		for (i = 0; i < oSelects.length; i++) {
			oSelects[i].style.visibility = sVisibility;
		}
	}

	oObjects = document.getElementsByTagName('OBJECT');
	if (oObjects.length > 0) {
		for (i = 0; i < oObjects.length; i++) {
			oObjects[i].style.visibility = sVisibility;
		}
	}
}


/* POPUP STUFF */
function popupMapWindow( Name, width, height, top, left ) {
		style = "height=" + height + ",width=" + width + ", top=" + top + ", left=" + left + ", status=no, toolbar=no, menubar=no, location=no, scrollbars=no, resizable=no";
		if (document.getElementsByTagName('base')[0] && Name.substring(0, 1) != "/") {
		    var prefix = document.getElementsByTagName('base')[0].href;
		}
		else {
		    var prefix = "";
		}
		open(prefix + Name, 'GeoMapWindow', style );
}
		
function popupWindow(Name, width, height) {
    style = "height=" + height + ",width=" + width + ", status=no, toolbar=no, menubar=no, location=no, scrollbars=yes, resizable=no";
    if (document.getElementsByTagName('base')[0] && Name.substring(0, 1) != "/") {
        var prefix = document.getElementsByTagName('base')[0].href;
    }
    else {
        var prefix = "";
    }
    open(prefix + Name, '_blank', style);
}

function MapPopup(Name) {
    style = "height=640, width=960, top=100, left=100, status=no, toolbar=no, menubar=no, location=no, scrollbars=no, resizable=no";
    if (document.getElementsByTagName('base')[0] && Name.substring(0, 1) != "/") {
        var prefix = document.getElementsByTagName('base')[0].href;
    }
    else {
        // if localhost "/" ET 20110406 "/arcade" ervoor
        //var prefix = "/arcade";
        var prefix = "";
    }
    
    open(prefix + Name, 'GeoMapWindow', style);
}
