

var REUVER = {
    description : 'This is the object that stores stuff',
    version : '0.1',
    cookie: {},
    cookieName: 'a',
    main_li_items : [],
    main_li_active : 0,
    main_li_images: [],
    sub_ul_items : [],
    sub_ul_active: 0
};

function showMenuImage(a) {
    var newimage = a.replace(/.*\//,"") + '.jpg';
    document.getElementById('submenu').style.backgroundImage = 'url(/uploads/images/menuafbeeldingen/doelmap/' + newimage + ')';
} // showmenuimage

function setClass( classes_str, classname_str, state_b){
    if (state_b) {
        if ( classes_str.indexOf(classname_str) < 0 ) {
            return classes_str += ' ' + classname_str;
        }main_li_images
    }
    else {
        return classes_str.replace(classname_str,'');
    }
    return classes_str;
    
}
function showSub ( subul ) {
    // check if this sub is already viewable, if so, just return;
    // check for the sibbling of this, it should be the ul sub
    //if (subul.style.display == 'none') {
        for (var i in REUVER.sub_ul_items) {
            ullist = REUVER.sub_ul_items[i];
            if (ullist == subul) {
                subul.style.display = 'block';
                var submenupadding = 20;
                var submenuel = document.getElementById('submenu');
                var size = YAHOO.util.Dom.getRegion(submenuel);
                var subheight = size.bottom - size.top - submenupadding; // height of the submenu excuding the padding/margin
                size = YAHOO.util.Dom.getRegion(subul);
                var ulheight = size.bottom - size.top; // height of the ul including the padding/margin
                if (subheight < ulheight) {
                    submenuel.style.height = ulheight + 'px'; // the padding will be added automaticly
                }
                showMenuImage(REUVER.main_li_images[i]);
                ullist.parentNode.className = setClass(ullist.parentNode.className, 'menuparent', true);
                REUVER.cookie.menuactive = i;
                writeCookie();
            }
            else {
                ullist.style.display = 'none';
                ullist.parentNode.className = setClass(ullist.parentNode.className, 'menuparent', false)
            }
        }
    // }
} // showSub
function subMenuTrigger() { // this is the li parent of the sub ul
    var target = this.getElementsByTagName('ul')[0];
    if (typeof(target)=='object' && target.tagName=='UL') { showSub(target); }
    return false;
}

function imageTrigger() {
    showMenuImage(this.href);
}

function addMenuFunctionality() {
    

    // start by hiding all the submenu's OK

    // loop all the main items 
        // loose the hrefs  OK
        // add listeners to show submenus / rollovers 

    // loop all the submenu items
        // add listeners to take care of the rollovers

    var assmenu = YAHOO.util.Dom.getElementsByClassName('primary-nav')[0]; // primary-nav == ul list
    // console.log(assmenu);
    var templist = YAHOO.util.Dom.getChildren(assmenu); // the first level li elements
    var item;
    for (var i in templist) { 
        item = templist[i];
        if (item.tagName == 'LI' && item.parentNode == assmenu) {
            REUVER.main_li_items.push(item);
            var itemchildren = YAHOO.util.Dom.getChildren(item);
            for (var j in itemchildren) {
                var itemchild = itemchildren[j];
                if (itemchild.tagName == 'A') {
                    // use the href before we delete it...
                    REUVER.main_li_images.push(itemchild.href);
                    itemchild.removeAttribute("href");
                    YAHOO.util.Event.addListener(itemchild.parentNode, "click", subMenuTrigger);
                }
                if (itemchild.tagName == 'UL') {
                    itemchild.style.display = 'none';
                    REUVER.sub_ul_items.push(itemchild);
                    var links = itemchild.getElementsByTagName('a');
                    for (var k in links) {
                        YAHOO.util.Event.addListener(links[k], "mouseover", imageTrigger);
                    }
                }
            }                
            // console.log(t);
        }
        else {
            // console.log('other li: ' + item);            
        }
    }
} // addMenuFunctionality

function readCookie() {
    var currentValue = YAHOO.util.Cookie.get(REUVER.cookieName);
    try { 
        var contentobj = YAHOO.lang.JSON.parse(currentValue);
    } 
    catch (x) { 
        var contentobj = {};
    } 
    if (typeof(contentobj)=='object') { REUVER.cookie = contentobj; }
}
function writeCookie() {
    var value = YAHOO.lang.JSON.stringify(REUVER.cookie);
    YAHOO.util.Cookie.set(REUVER.cookieName, value);
}
function getStarted () {
    readCookie();
    addMenuFunctionality();
    var active = (REUVER.cookie.menuactive) ? REUVER.cookie.menuactive : 0;
    var activeParent = REUVER.main_li_items[active];
    showSub(activeParent.getElementsByTagName('ul')[0]);
    showMenuImage(REUVER.main_li_images[active]);
    // if (REUVER.cookie.menuactive) { REUVER.main_li_items[REUVER.cookie.menuactive] }
}

YAHOO.util.Event.onDOMReady( getStarted );

