var bugRiddenCrashPronePieceOfJunk = (
        navigator.userAgent.indexOf('MSIE 5') != -1
                &&
        navigator.userAgent.indexOf('Mac') != -1
        );

var W3CDOM = (!bugRiddenCrashPronePieceOfJunk &&
              document.getElementsByTagName && document.createElement);

var DEFAULT_POPUP_WINDOW_WIDTH = 600;
var DEFAULT_POPUP_WINDOW_HEIGHT = 400;
var POPUP = 'popup';

window.onload = initialize;

/* Why no window.onload = function () {} ? Because NN3 doesn't support the function
	constructor and gives an error message. This site must be accessible to NN3 */

function initialize() {


    /* Hide nifty stuff from old browsers */

    if (W3CDOM) {

        /* Go through all links. If any has a type="popup" write the popup function into its onclick
        Any external link gets a target='ppk'.
        Any link with a hreflang attribute gets an extra note with its value.
        */
        var langspan = document.createElement('span');
        langspan.className = 'smaller lang';

        var x = document.getElementsByTagName('a');
        for (var i = 0; i < x.length; i++) {
            var _size = POPUP.length;
            if (x[i].getAttribute('type'))
                if (x[i].getAttribute('type').substring(0, _size) == POPUP) {
                    var ttt = x[i].getAttribute('type');
                    if (ttt.length > _size) {
                        // TODO: find two numbers after popup, if only one number, set it to width, use default height
                        var w = ttt.substring(_size, ttt.indexOf('x'));
                        var h = ttt.substring(ttt.indexOf('x') + 1);
                        x[i].onclick = function () {
                            return pop(this.href, w, h)
                        }
                    } else {
                        x[i].onclick = function () {
                            return pop(this.href)
                        }
                    }
                    //x[i].innerHTML += '<span class="smaller"> (popup)</span>';
                }
        }

        /* End hide. This is for all browsers
        If the page has an init() function, execute it */

    }

    if (self.init) self.init();
}

window.onunload = remove;

function remove() {
    if (top.navi && top.navi.setNav) top.navi.setNav(location.href, '');
    top.setNav = '';
    if (self.exit) self.exit();
}

// Popup

var popUp = null;


function pop(url, width, height) {
    // default width and height
    if (!width)
        width = DEFAULT_POPUP_WINDOW_WIDTH;
    if (!height)
        height = DEFAULT_POPUP_WINDOW_HEIGHT;
    if (popUp && !popUp.closed)
        popUp.location.href = url;
    else
        popUp = window.open(url, 'popUp', 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,toolbar=yes,location=yes');
    popUp.focus();
    return false;
}


