/**
    Auto stick MENU
*/

var _defIndexPage = "index.php";
var _menuUrl;


function stickMenu() {

	var containerId = "sousmenuleft";
    var url = location.href;
    var container = document.getElementById(containerId);

    if (container) {
        if (url.lastIndexOf("/") == (url.length-1)) {
            url = url+_defIndexPage;
        }
        if (url.lastIndexOf("?") >= 0) {
            url = url.substring(0, url.lastIndexOf("?"));
        }
        if (url.lastIndexOf("#") >= 0) {
            url = url.substring(0, url.lastIndexOf("#"));
        }
        _menuUrl = url;
        as_parseChildsElement(container);
    } // if
} // function


function as_parseChildsElement(element) {
    var props  = 0;

    // walk down
    for (var i=0; i<element.childNodes.length; i++) {
        var child = element.childNodes[i];
        props |= as_parseChildsElement(child); // aggregate bits
    }

    // on the way back now
    switch (element.tagName) {
    case "TR":
        props |= 1;
    break;

    case "TD":
        if (props & 4) {
            if (!(props & 2)) {
                //element.className = "active";
            }
            props |= 2;
            props &= 1 | 2; // reset bit 4
        }
    break;

    case "A":
    if (props & 2) {
        break;
    } // if
    var href = element.getAttribute("href");
    if (menu_isSameUrl(_menuUrl, href)) {
        element.className = 'active';
        props |= 4;
    }
    break;
    } // switch

    return props;
}


function menu_isSameUrl(url, href) {
    var a = url.split(/[?\/]/i);
    var b = href.split(/[?\/]/i);
    var i = a.length - 1;
    var j = b.length - 1;
    while ((i >= 0) && (j >= 0)) {
        if (b[j] == "..") { j-=2; continue; }
        if (a[i] == "..") { i-=2; continue; }
        if ((b[j] == ".") || (b[j] == "")) { j--; continue; }
        if ((a[i] == ".") || (a[i] == "")) { i--; continue; }
        if (! (a[i] == b[j])) return false;
        i--;
        j--;
    }
    return true;
}

if (window.attachEvent) {
	window.attachEvent("onload", stickMenu);
} else {
	window.addEventListener("DOMContentLoaded", stickMenu, false);
}
