﻿function navigatieMenu_srcId(oElm) {
    var bFound = false;
    do {
        oElm = oElm.parentNode;
        if (oElm.nodeName == 'UL') {
            if ((oElm.id == '') == false) {
                bFound = true;
            }
        }
    } while (bFound == false)
    return oElm.id;
}

function navigatieMenu_srcVal(sId, sVal) {
    var hidProperties = document.getElementById(sId + '_properties');
    var value = hidProperties.value;
    var values = value.split(';');
    for (var i = 0; i < values.length; i++) {
        var subValues = values[i].split('=');
        if (subValues.length == 2) {
            if (subValues[0] == sVal) {
                return subValues[1];
            }
        }
    }
}

function navigatieMenu_getDirection(oElm) {
    return navigatieMenu_srcVal(navigatieMenu_srcId(oElm), 'direction');
}

function navigatieMenu_getEffect(oElm) {
    return Number(navigatieMenu_srcVal(navigatieMenu_srcId(oElm), 'effect'));
    /* Effecten
     No Effect
    1 Fade Effect
    2 Scroll
    3 Scroll with TextScroll */ 
}

function navigatieMenu_getIaddStep(oElm) { // deze zou moeten verdwijnen
    return Number(navigatieMenu_srcVal(navigatieMenu_srcId(oElm), 'iaddstep'));
}

function navigatieMenu_getIaddMs(oElm) {
    return Number(navigatieMenu_srcVal(navigatieMenu_srcId(oElm), 'iaddms'));
}

function navigatieMenu_getBorderWidth(oElm) {
    return Number(navigatieMenu_srcVal(navigatieMenu_srcId(oElm), 'borderwidth'));
}

function navigatieMenu_getArrowWidth(oElm) {
    return Number(navigatieMenu_srcVal(navigatieMenu_srcId(oElm), 'arrowwidth'));
}

function navigatieMenu_createMenu(menuElement) {
    var subMain = menuElement.id;
    var elmULs = menuElement.parentNode.getElementsByTagName('UL');    
    for ( var x = 0; x < elmULs.length; x++ ) {        
        if ((elmULs[x].id == subMain) == false) {
            var elmLI = navigatieMenu_srcLI(elmULs[x]);
            if (elmLI.parentNode.id == subMain) {
                elmLI.className += ' submain';                  
                if (navigatieMenu_getDirection(elmLI) == 'horizontal') {
                    navigatieMenu_srcSub(elmULs[x]).style.top = elmLI.offsetHeight + 'px';
                } else {
                    navigatieMenu_srcSub(elmULs[x]).style.left = elmLI.parentNode.offsetWidth + 'px';
                }                
            } else {
                elmLI.className += ' sub';
                navigatieMenu_createArrowWidth(elmLI);                
                navigatieMenu_srcSub(elmULs[x]).style.left = elmLI.parentNode.offsetWidth - navigatieMenu_getBorderWidth(elmLI) * 2 + 'px';
            }          
        }              
        for ( var y = 0; y < elmULs[x].childNodes.length; y++ ) {
            if (elmULs[x].childNodes[y].nodeName == 'LI') {
                elmULs[x].childNodes[y].onmouseover = function () {navigatieMenu_action(this, 'over')};
                elmULs[x].childNodes[y].onmouseout = function () {navigatieMenu_action(this, 'out')};         
            }        
        }    
    }
}

function navigatieMenu_createArrowWidth(elmLI) {
    if (elmLI.style.width == '') {        
        var iWidth = elmLI.offsetWidth + navigatieMenu_getArrowWidth(elmLI);
        for ( var x = 0; x < elmLI.parentNode.childNodes.length; x++ ) {
            if (elmLI.parentNode.childNodes[x].nodeName == 'LI') {
                elmLI.parentNode.childNodes[x].style.width = iWidth + 'px';
            }            
        }    
    }
}

function navigatieMenu_srcLI(elmUL) {
    var returnVal = elmUL;
    while ((returnVal.nodeName == 'LI') == false) {
        returnVal = returnVal.parentNode;
    }
    return returnVal;    
}

function navigatieMenu_srcSub(elmUL) {
    var returnVal = elmUL;
    while ((returnVal.parentNode.nodeName == 'LI') == false) {
        returnVal = returnVal.parentNode;
    }
    return returnVal;
}

function navigatieMenu_action(elmLI, action) {
    // action == 'over' || action == 'out'
    if (action == 'over') {
        elmLI.className += ' cmsMenuItemOver';
    } else {
        elmLI.className = elmLI.className.replace(' cmsMenuItemOver', '');
    }
    var elmDiv = elmLI.getElementsByTagName('DIV');
    if(elmDiv.length >0){
        if (action == 'over') {
            elmDiv[0].className += ' cmsMenuItemOver';   
        } else {
            elmDiv[0].className = elmDiv[0].className.replace(' cmsMenuItemOver', '');        
        }
    }
    var elmLU = elmLI.getElementsByTagName('UL');    
    if (elmLU.length > 0) {
        var elmSub = elmLU[0];
        while ((elmSub.parentNode.nodeName == 'LI') == false) {
            elmSub = elmSub.parentNode;
        }
        if (action == 'over') {
            elmSub.className += ' cmsMenuItemOver';            
        } else {
            elmSub.className = elmSub.className.replace(' cmsMenuItemOver', '');
        }
        switch(navigatieMenu_getEffect(elmLI)) {
            case 1:
                var oEffect = new navigatieMenu_doOppacityEffect;
                oEffect.setObj(elmSub);
                if ( action == 'over' ) {
                    oEffect.fadeIn();
                    timer_add('navigatieMenu_start', 'navigatieMenu_executeEffect(\'navigatieMenu_start\')', navigatieMenu_getIaddMs(elmSub), oEffect);
                } else {
                    oEffect.fadeOut();
                    timer_add('navigatieMenu_stop', 'navigatieMenu_executeEffect(\'navigatieMenu_stop\')', navigatieMenu_getIaddMs(elmSub), oEffect);
                }                    
                break;
            case 2:
            case 3:
                var oEffect = new navigatieMenu_doScrollEffect;
                oEffect.setObj(elmSub, (navigatieMenu_getEffect(elmLI) == 3));    
                if ( action == 'over' ) {
                    oEffect.scrollIn();                    
                    timer_add('navigatieMenu_start', 'navigatieMenu_executeEffect(\'navigatieMenu_start\')', navigatieMenu_getIaddMs(elmSub), oEffect);
                } else {
                    oEffect.scrollOut();
                }                                          
                break;
            default: break; //zonder effect                                      
        }                
    }
}

function navigatieMenu_executeEffect(sEffect) {   
    var aArr
    switch (sEffect) {
        case 'navigatieMenu_start': aArr = timer_item('navigatieMenu_start'); break;
        case 'navigatieMenu_stop': aArr = timer_item('navigatieMenu_stop'); break;
    }
     
    var oEffect = aArr[5];
    
    var iTimBgn = aArr[2];
    var iTimEnd = aArr[3];
    var iTimTot = iTimEnd - iTimBgn;
    var iTimNow = new Date().getTime() - iTimBgn;
    var iProc = (iTimNow / iTimTot);
    
    oEffect.setProc(iProc);
    
    switch (sEffect) {
        case 'navigatieMenu_start': oEffect.start(); break;
        case 'navigatieMenu_stop': oEffect.stop(); break;
    }
}

function navigatieMenu_doScrollEffect() {
    var _o;
    var _textScroll = new Boolean()
    var cnt;
    var proc;
    var len;
    var direction;                        
    var parentLength = 0;
    ////var stylTop;
    var stylRight; 
    var stylBottom; 
    ////var stylLeft;
    var doOK = new Boolean();
    
    this.setObj = function (o, textScroll) {
        _o = o;
        if ((textScroll == null) == false) {
            _textScroll = textScroll;
        }
    }      
    
    this.setProc = function(iProc) {
        proc = iProc;
        cnt = len * proc;
    }      
    
    this.scrollIn = function() {                
        var stylClip = _o.style.clip;    
        if (stylClip.replace(' ', '') == '') {
            stylClip = 'rect(0px, 0px, 0px, 0px)';
        }
        stylClip = stylClip.replace('rect(', '').replace(')', '').replace(/,/g, '').replace(/px/g, ',').replace(/auto/g, 'auto,').replace (/ /g, '');
        stylClip = stylClip.substring(0, stylClip.length - 1);
        var arrayStylClip = stylClip.split(',');
        ////stylTop = arrayStylClip[0]
        stylRight = arrayStylClip[1]
        stylBottom = arrayStylClip[2]
        ////stylLeft = arrayStylClip[3]                                
        if (_o.parentNode.className.indexOf('submain') == -1 ||navigatieMenu_getDirection(_o) == 'vertical') {                
            direction = 'horizontal'; 
            len = _o.offsetWidth;
            cnt = Number(stylRight);  
            parentLength = Number('0' + _o.style.left.replace('px', '')) - 1;                                                 
        } else {
            direction = 'vertical';
            len = _o.offsetHeight;
            cnt = Number(stylBottom);
            parentLength = Number('0' + _o.style.top.replace('px', '')) - navigatieMenu_getBorderWidth(_o) * 2;
        }                
        if ((_o.className.indexOf('start') == -1) && isNaN(cnt) == false) {
            _o.className += ' start';
            doOK = true;
            doScrollIn();
        } else {
            doOK = false;
        }                                                                                                                     
    }
    
    this.scrollOut = function() { 
        setTimeout(doScrollOut, 1);                                                                                   
    }
    
    this.start = function () {
        doScrollIn();
    }
    
    this.stop = function () {
        doScrollOut();
    }
    
    function doScrollIn() {
        if (doOK) {
            if (isNaN(cnt)) {
                timer_remove('navigatieMenu_start');
                _o.className = _o.className.replace('start', '');
            } else {                            
                if (_textScroll == true) { 
                    switch (direction) {
                        case 'vertical': _o.style.clip = 'rect(' + Number(len - cnt) + 'px auto auto auto)';
                                _o.style.top = cnt - len + parentLength + 'px';
                                break;
                        case 'horizontal': _o.style.clip = 'rect(auto auto auto ' + Number(len - cnt) + 'px)'; 
                                _o.style.left = cnt - len + parentLength + 'px';
                                break;                                   
                    }                               
                } else {                                
                    switch (direction) {
                        case 'vertical': _o.style.clip = 'rect(auto auto ' + cnt + 'px auto)'; break;
                        case 'horizontal': _o.style.clip = 'rect(auto ' + cnt + 'px auto auto)'; break;                                   
                    }                                                    
                }
                if ((cnt >= len || _o.className.indexOf('cmsMenuItemOver') == -1)) {
                    _o.className = _o.className.replace('start', '');                   
                    _o.style.clip = 'rect(auto auto auto auto)';               
                    switch (direction) {
                        case 'vertical': _o.style.top = parentLength + navigatieMenu_getBorderWidth(_o) * 2 + 'px'; break;
                        case 'horizontal': _o.style.left = parentLength + 1 + 'px'; break;
                    }
                }
            }             
        }
    }
    
    function doScrollOut() {
        if (_o.className.indexOf('cmsMenuItemOver') == -1) {
            _o.style.clip = 'rect(0px 0px 0px 0px)';
        } else {
            setTimeout(doScrollOut, 1);  
        }               
    }                                                       
}                

function navigatieMenu_doOppacityEffect() {            
    var _o;           
    var cnt;
    var proc;
    var doOK = new Boolean();
                          
    this.setObj = function (o) {
        _o = o;                            
    }
    
    this.setProc = function(iProc) {
        proc = iProc;
        cnt = proc * 100;
    }  
    
    this.fadeIn = function() {                
        cnt = 0;
        if ((_o.className.indexOf('start') == -1)) {
            _o.className += ' start';
            _o.style.visibility = 'visible';
            doOK = true;
            doFadeIn(); 
        } else {
            doOK = false;
        }
    }
    
    this.fadeOut = function() {
        doOK = false;
        setTimeout(checkFadeOut, 1);
    }
    
    this.start = function () {
        doFadeIn();
    }
    
    this.stop = function () {
        doFadeOut();
    }    
    
    function checkFadeOut() {
        cnt = 100;                                
        if (_o.className.indexOf('cmsMenuItemOver') == -1) {
            doOK = true;                       
        } else {
            doOK = false;
            setTimeout(checkFadeOut, 1);
        }  
    }
    
    function doFadeIn() {
        if (doOK) {
            _o.style.opacity = cnt / 100;
            _o.style.MozOpacity = cnt / 100;
            _o.style.KhtmlOpacity = cnt / 100;
            _o.style.filter = 'alpha(opacity=' + cnt + ')';                
            if (cnt >= 100) {
                _o.style.filter = '';                     
            }        
        }
    }
    
    function doFadeOut() {
        if (doOK) {
            cnt = 100 - cnt;
            _o.style.opacity = cnt / 100;
            _o.style.MozOpacity = cnt / 100;
            _o.style.KhtmlOpacity = cnt / 100;
            _o.style.filter = 'alpha(opacity=' + cnt + ')';                
            if (cnt <= 0) {
                _o.style.visibility = 'hidden';
                _o.style.filter = '';
                _o.className = _o.className.replace('start', '');
            }             
        }
    }                                                       
}