/*
 * this file only has functions
 * see menu.js for variables
 *
 */
function objById(id) {
    var rv = null;
    if( document.getElementById )
        rv = document.getElementById(id);
    else if( document.all )
        rv = document.all[id];
    else if( document.layers )
        rv = document.layers[id];
    return rv;
}

/*
 * Load the correct timeline image
 */
function timeline(i) {
    var tl = objById('tijdlijn');
    if( tl ) {
        tl.src=i;
    }
}

/* zooming for arbitrary zoomimgs */
function zoomimage(url, im, div) {
    var img = new Image;
    img.onload = function() {
        /* ok, image has been loaded -
         * onload method may go */
        img.onload = null;
        /* and perform the actual zoom */
        do_zoomimage(url, im, div, img);
        delete img;
    }
    img.src = url;
}
function do_zoomimage(url, im, div, img) {
    var zi = objById(im);
    var zd = objById(div);
    var h = "auto",w = "100%";

    if( img && zd ) {
        var s, sw, sh;
        sw = parseFloat(zd.clientWidth)/img.width;
        /*sh = parseFloat(zd.clientHeight)/im.height;*/
        /* actual scale will be the 
         * lowest of width/heigth scale */
        /* s = Math.min(sw, sh);*/
        s = sw;
        /* Now we know the actual scale, we
         * can properly size the image into 
         * the containing div */
        w = Math.round(img.width*s).toString()+"px";
        h = Math.round(img.height*s).toString()+"px";
        /* alert("D:w="+zd.clientWidth+", D:h="+zd.clientHeight+
           ", Sw="+sw+", Sh="+sh+" W="+w+", H="+h); */
    }

    /* load url into <img id=im> */
    if( zi ) {
        /*zi.style.width = '100%'*/;
        /*zi.style.width = 'auto';*/
        /*zi.style.height = 'auto';*/
        zi.style.width  = w;
        zi.style.height = h;

        /* load target img */
        zi.src = url;

        zi.style.display = 'block';
        zi.style.visibility = 'visible';
        zi.style.zIndex = 1;
        zi.style.margin = "0px auto";
    }
    /* remove <div id='zoomdiv'> */
    if( zd ) {
        zd.style.visibility = 'hidden';
        zd.style.zIndex = -1;
    }
}

/* 
 * resize img to zero & push backwards
 * in z-order
 */
function unzoomimage(zi, div) {
    var zd = objById(div);
    /* "remove" the image */
    if( zi ) {
        zi.style.width = 0;
        zi.style.height = 0;
        zi.style.visibility = 'hidden';
        zi.style.display = 'none';
        zi.style.zIndex = -1;
        zi.src = null;
    }
    /* make the div re-appear */
    if( zd ) {
        /* and div back to background */
        zd.style.visibility = 'visible';
        zd.style.zIndex = 0;
    }
}


/* 
 * shorthands for readability 
 */
function id(m, n) {
    return "td_"+m+"_"+n;
}
function act(m, n) {
    return "onmouseover='javascript:act_m(this, "+m+","+n+");'";
}
function klik(m, n) {
    return "onclick='javascript:klik_m("+m+","+n+");'";
}


/* activate the current menu-item in DOM element e.
 * We ignore the first mouse-over event such that switching
 * menus does not confuse the user by activating a 'tab'
 * that you're not on.
 * Note: this method will be called by
 * "activate_default_menu_item" with an extra parameter
 * and we should *never* ignore that call, only those coming
 * from a mouseevent.
 */
ignore = 1;

/* only change style when loaded
 * i.e. when act_m is called with "opt" being
 * defined. Otherwise only change text */
function act_m(e, m, n, opt) {
    var deactivate = false;
    /* ignore if 'opt' not is set and first 
       time called */
    if( typeof(opt)=='undefined' && typeof(ignore)!='undefined' ) {
        delete ignore;
        return;
    }
    /* ok, so, either this is not the first time called
     * or it was called from "activate_default_menu" */
    var i;
    var longtxt = objById('section_title');

    /* update the 'long' menu text */
    if( longtxt )
        longtxt.innerHTML = menu[m][n].m_long;

    /* Now, only change style if opt != undefined */
    if( typeof(opt)=='undefined' )
        return;
    if( typeof(opt)=='boolean' )
        deactivate = opt;
    /* optionally deactivate everything */
    if( deactivate ) {
        for(m in menu) {
            for(n in menu[m]) {
                var o = objById( id(m, n) );
                if( o ) {
                    o.style.background = defbg;
                    /*
                    o.style.borderRight = 'none';
                    o.style.borderLeft = 'none';*/
                    /*o.style.borderBottom = '2px solid black';*/
                    /*o.style.borderTop = 'none';*/
                    o.style.cursor = 'pointer';
                }
            }
        }
    }
    /* and activate current element */
    e.style.background = '#FF6600';
    /*e.style.borderRight = '1px solid black';
    e.style.borderLeft = '1px solid black';*/
    /*e.style.borderBottom = 'none';*/
    /*e.style.borderTop = '2px solid black';*/
    e.style.cursor = 'pointer';
}

/* Load the new URL into the window */
function klik_m(m,n) {
    window.location = menu[m][n].url;
}


/* 
 * Actually do write out all the 'td' elements
 */
function write_menu() {
    var m, n;
    document.write('<table id="menu" style="width: 100%;" cellspacing="0">\n');

    /* menu is now an array itself with the actual items in an array in
     * the positions:
     * menu[0] = array of menuitems for level 0
     * menu[1] = array of menuitems for level 1
     */
    for (m in menu) {
        var items = menu[m];
        document.write('<tr class="menurow row'+m+'" ');
        document.write('style="');
        document.write('width: 100%; ');
        document.write('">\n');
        document.write('<td style="width: 100%; padding: 0 0 0 0; margin:0 0 0 0;">\n');
        document.write('<table class="menurow" cellspacing="0" style="margin: 0 0 0 0; width: 100%;">\n');
        document.write('<tr class="menurow" id="items">\n');
        /*var w = "style=\"width: \" ";*/
        for(n in items) {
            document.write("<td "
                        + "id=\"" + id(m, n) + "\" "
                        + act(m, n) + " "
                        + klik(m, n) + ">"+items[n].m_short
                        + "</td>\n");
        }
        document.write("</tr>\n");
        document.write('</table>\n');
        document.write('</td>\n');
        document.write('</tr>\n');
    }
    document.write('<tr class="menurow" style="width: 100%; margin-top: -2px;padding: 2px;">\n');
    document.write('<td class="section" style="width: 100%">');
    document.write('<span id="section_title">&nbsp;</span></td></tr></table>');
}

function activate_default_menu_item() {
    var items = new Array(), l = null, p = null, o;

    /* find out which page we're on -
     * that is to say: the page within the
     * current directory [the part after the last /
     * (if any)
     */
    if( (l = window.location.href)!=null ) {
        var slash;
        if( (slash = l.lastIndexOf('/'))!=-1 )
            l = l.substring(slash);
        var question;
        if( (question = l.indexOf('?'))!=-1 )
            l = l.substring(0, question);
    }
    /* Also look for parenturl if it's set and not the
     * current url
     */
    if( l && parenturl ) {
        if( l.indexOf(parenturl)==-1 )
            p = parenturl;
    }
    /* check the menus if we can find that url/the urls */
    for(m=0; m<menu.length; m++) {
        for(n=0; n<menu[m].length; n++) {
            if( l && menu[m][n].url.indexOf(l)!=-1 ) 
                items[items.length] = {'m':m, 'n':n};
            if( m==0 && p && menu[m][n].url.indexOf(p)!=-1 ) 
                items[items.length] = {'m':m, 'n':n};
        }
    }
    /* default to first if not found */
    if( items.length==0 ) {
        items[0] = {'m':0, 'n':0};
    }
    /* And any other locations */
    for(i=0; i<items.length; i++ ) {
        if( (o=objById(id(items[i].m,items[i].n)))!=null ) {
            act_m(o, items[i].m, items[i].n, (i==0));
        }
    }
}

function just_url(url) {
    var pipe;
    if( (pipe=url.indexOf("|"))!=-1 ) {
        url = url.substr(0, pipe);
    }
    return url;
}
function url_args(url) {
    var pipe;
    if( (pipe=url.indexOf("|"))!=-1 ) {
        url = url.substr(pipe+1);
    }
    return url;
}

