var isMSIE = /*@cc_on!@*/false;
var timerBlindOne = null, timerBlindTwo = null, timerBlindThree = null;

/**
 *
 */
function init() {
    var nodes = cssQuery("#navigation a"), node;
    for (var c = 0; c < nodes.length; c++) {
        node = nodes[c];
        if (node.getAttribute("id")) {
            var which = node.getAttribute("id").replace("nav-maincat-", "");
            if ((which == "home") || (which == "products") || (categories[which * 3 + 2].length - 2)) {
                if (isMSIE) {
                    node.attachEvent("onmouseover", expandBlindOne);
                    node.attachEvent("onmouseout", fadeBlindOne);
                } else if (typeof(node.addEventListener) != 'undefined') {
                    node.addEventListener("mouseover", expandBlindOne, false);
                    node.addEventListener("mouseout", fadeBlindOne, false);
                }
            } else {
                node.className = "no-children";
            }
        }
    }

    var nodes = cssQuery("#menu-one .menu-one-list dd"), node;
    for (var c = 0; c < nodes.length; c++) {
        node = nodes[c];
        if (isMSIE) {
            node.attachEvent("onmouseover", expandBlindTwo);
            node.attachEvent("onmouseout", fadeBlindTwo);
        } else if (typeof(node.addEventListener) != 'undefined') {
            node.addEventListener("mouseover", expandBlindTwo, false);
            node.addEventListener("mouseout", fadeBlindTwo, false);
        }
    }

    var nodes = cssQuery("#menu-two .menu-one-list dd"), node;
    for (var c = 0; c < nodes.length; c++) {
        node = nodes[c];
        if (isMSIE) {
            node.attachEvent("onmouseover", expandBlindThree);
            node.attachEvent("onmouseout", fadeBlindThree);
        } else if (typeof(node.addEventListener) != 'undefined') {
            node.addEventListener("mouseover", expandBlindThree, false);
            node.addEventListener("mouseout", fadeBlindThree, false);
        }
    }

    var menuBlinds = cssQuery("#blind-one")[0];
    if (isMSIE) {
        menuBlinds.style.width = "199px";
    } else {
        menuBlinds.style.width = "200px";
    }

}

/**
 *
 */
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

/**
 *
 */
function expandBlindOne(evt) {
    cancelFadeOne();
    collapseBlindTwo();

    var context = (typeof(evt.srcElement) != 'undefined') ? evt.srcElement : evt.currentTarget;
    var which = context.getAttribute("id").replace("nav-maincat-", "");
    if (which == "products") {
        var maxItems = (products.length / 3) - 1,
            source = products.concat(),
            url = "/produkte.php?gruppe=",
            steps = 3;
    } else {
        collapseBlindTwo();
        if (which == "home") {
            var maxItems = categories.length / 3 - 1 + 1,
                source = categories.concat(),
                url = "/content.php?cid=",
                steps = 3;

            // WORKAROUND-Sprache: "lang" Variable wird in template.inc gesetzt (Philipp H.)

      	    source.unshift("0", txtMenuProducts, ["", ""]);
        } else {
            var maxItems = categories[which * 3 + 2].length / 2 - 1,
                source = categories[which * 3 + 2].concat(),
                url = "/content.php?cid=",
                steps = 2;
        }
    }

    var nodes = cssQuery("#menu-one .menu-one-list dd"), c= 0;
    while (c < maxItems) {
        var node = nodes[c];
        node.style.display = "block";
        var anchor = node.firstChild;
        if ((which == "home") && (c == 0)) {
            anchor.href = "/produkte.php";
            
            anchor.firstChild.nodeValue = txtMenuProducts;
        } else {
            anchor.href = url + source[c * steps];
            anchor.firstChild.nodeValue = source[c * steps + 1];
        }

        if ((which == "products") && (source[c * 3 + 2].length)) {
            anchor.className = "has-children";
        } else {
            anchor.className = "no-children";
        }
        c++;
    }
    for (; c < nodes.length; c++) {
        node = nodes[c];
        node.style.display = "none";
    }

    pos = findPos(context);

    if (which != "home") {
        var menuBlinds = cssQuery("#blind-one")[0];
        menuBlinds.style.display = "block";
        menuBlinds.style.left = (parseInt(pos[0]) + 202) + "px";
    }

    var menuElement = cssQuery("#menu-one dl")[0];
    menuElement.style.display = "block";
    if (isMSIE) {
        menuElement.style.left = (parseInt(pos[0]) + 201) + "px";
        menuElement.style.top = (pos[1] + 1) + "px";
    } else {
        menuElement.style.left = (parseInt(pos[0]) + 202) + "px";
        menuElement.style.top = (pos[1] - 1) + "px";
    }

    var menuBackground = cssQuery("#backdrop-one")[0];
    menuBackground.style.display = "block";
    if (isMSIE) {
        menuBackground.style.left = (parseInt(pos[0]) + 203) + "px";
        menuBackground.style.top = (pos[1] + 1) + "px";
        menuBackground.style.width = parseInt(menuElement.offsetWidth) - 2;
        menuBackground.style.height = menuElement.offsetHeight;
    } else {
        menuBackground.style.left = (parseInt(pos[0]) + 202) + "px";
        menuBackground.style.top = (pos[1] - 1) + "px";
        menuBackground.style.width = document.defaultView.getComputedStyle(menuElement, "").getPropertyValue("width");
        menuBackground.style.height = document.defaultView.getComputedStyle(menuElement, "").getPropertyValue("height");
    }
}



/**
 *
 */
function expandBlindTwo(evt) {
    cancelFadeOne();
    cancelFadeTwo();
    collapseBlindThree();

    var context = (typeof(evt.srcElement) != 'undefined') ? evt.srcElement : evt.currentTarget;
    if (context.nodeName.toLowerCase() == "dd") context = context.firstChild;

    if (context.className != "has-children") {
        evt.returnValue = false;
        evt.cancelBubble = true;
        return;
    } else {
        var which = parseInt(context.getAttribute("id").replace("menu-one-", ""));
        var idx = which * 3 + 2;

        var nodes = cssQuery("#menu-two .menu-one-list dd"), c= 0;
        while (c < products[idx].length / 2 - 1) {
            var node = nodes[c];
            node.style.display = "block";

            var anchor = node.firstChild;
            anchor.href = "/produkte.php?gruppe=" + products[idx][c * 2];
            anchor.firstChild.nodeValue = products[idx][c * 2 + 1];
            anchor.className = "has-children";
            c++;
        }
        for (; c < nodes.length; c++) {
            var node = nodes[c];
            node.style.display = "none";
        }

        pos = findPos(context);

        var menuBlinds = cssQuery("#blind-two")[0];
        menuBlinds.style.display = "block";
        menuBlinds.style.left = (parseInt(pos[0]) + 201) + "px";

        var menuElement = cssQuery("#menu-two dl")[0];
        menuElement.style.display = "block";
        menuElement.style.left = (parseInt(pos[0]) + 201) + "px";
        menuElement.style.top = (pos[1]) + "px";

        var menuBackground = cssQuery("#backdrop-two")[0];
        menuBackground.style.display = "block";
        menuBackground.style.left = (parseInt(pos[0]) + 201) + "px";
        menuBackground.style.top = (pos[1]) + "px";
        if (isMSIE) {
            menuBackground.style.width = parseInt(menuElement.offsetWidth) - 2;
            menuBackground.style.height = menuElement.offsetHeight;
        } else {
            menuBackground.style.width = document.defaultView.getComputedStyle(menuElement, "").getPropertyValue("width");
            menuBackground.style.height = document.defaultView.getComputedStyle(menuElement, "").getPropertyValue("height");
        }
    }
    evt.returnValue = false;
    evt.cancelBubble = true;
}

/**
 *
 */
function expandBlindThree(evt) {
    cancelFades();

    var context = (typeof(evt.srcElement) != 'undefined') ? evt.srcElement : evt.currentTarget;
    if (context.nodeName.toLowerCase() == "dd") context = context.firstChild;

    if (context.className != "has-children") {
        evt.returnValue = false;
        evt.cancelBubble = true;
        return;
    } else {
        pos = findPos(context);

        var menuBlinds = cssQuery("#blind-three")[0];
        menuBlinds.style.display = "block";
        menuBlinds.style.left = (parseInt(pos[0]) + 201) + "px";

        var teaser = cssQuery("iframe#teaser");
        if (!teaser.length) {
            var iframe = document.createElement("iframe");
            iframe.style.display = "none";
            iframe.style.position = "absolute";
            iframe.style.zIndex = 999;
            iframe.style.width = "380px";
            iframe.style.height = "418px";
            iframe.style.top = "10px";
            iframe.style.border = 0;
            iframe.style.margin = 0;
            iframe.style.padding = 0;
            
            iframe.id = "teaser";
            iframe.frameBorder = "0";
            iframe.border = 0;
            iframe.setAttribute("frameborder", "0");
            iframe.setAttribute("border", "0");
            iframe.width = 380;
            iframe.height = 418;
            document.body.appendChild(iframe);
        }

        var teaserContent = cssQuery("iframe#teaser")[0];
        teaserContent.style.display = "block";
        teaserContent.style.left = (parseInt(pos[0]) + 207) + "px";
        var urlP = context.href.indexOf("=");
        var i = context.href.substr(urlP + 1, context.href.length);
        teaserContent.src = "/teaser.php?id=" + i;

        if (isMSIE) {
            teaserContent.attachEvent("onmouseover", cancelFades);
            teaserContent.attachEvent("onmouseout", fadeAll);
        } else if (typeof(teaserContent.addEventListener) != 'undefined') {
            teaserContent.addEventListener("mouseover", cancelFades, false);
            teaserContent.addEventListener("mouseout", fadeAll, false);
        }

    }
    evt.returnValue = false;
    evt.cancelBubble = true;
}

/**
 *
 */
function fadeBlindOne() {
    timerBlindOne = setTimeout("collapseBlindOne()", 567);
}

/**
 *
 */
function fadeBlindTwo() {
    fadeBlindOne();
    timerBlindTwo = setTimeout("collapseBlindTwo()", 456);
}

/**
 *
 */
function fadeBlindThree() {
    fadeBlindOne();
    fadeBlindTwo();
    timerBlindThree = setTimeout("collapseBlindThree()", 345);
}

/**
 *
 */
function fadeAll() {
    fadeBlindOne();
    fadeBlindTwo();
    fadeBlindThree();
}

/**
 *
 */
function cancelFadeOne() {
    if (timerBlindOne != null) clearTimeout(timerBlindOne);
    timerBlindOne = null;
}

/**
 *
 */
function cancelFadeTwo() {
    if (timerBlindTwo != null) clearTimeout(timerBlindTwo);
    timerBlindTwo = null;
}

/**
 *
 */
function cancelFadeThree() {
    if (timerBlindThree != null) clearTimeout(timerBlindThree);
    timerBlindThree = null;
}

/**
 *
 */
function cancelFades() {
    cancelFadeOne();
    cancelFadeTwo();
    cancelFadeThree();
}

/**
 *
 */
function collapseBlindOne() {
    collapseBlindTwo();

    if (timerBlindOne != null) {
        var el = cssQuery("#menu-one dl");
        el[0].style.display = "none";

        var el = cssQuery("#backdrop-one");
        el[0].style.display = "none";

        var el = cssQuery("#blind-one");
        el[0].style.display = "none";

        timerBlindOne = null;
    }
}

/**
 *
 */
function collapseBlindTwo() {
    collapseBlindThree();

    if (timerBlindTwo != null) {
        var el = cssQuery("#menu-two dl");
        el[0].style.display = "none";

        var el = cssQuery("#backdrop-two");
        el[0].style.display = "none";

        var el = cssQuery("#blind-two");
        el[0].style.display = "none";

        timerBlindTwo = null;
    }
}

/**
 *
 */
function collapseBlindThree() {
    if (timerBlindThree != null) {
        var menuBlinds = cssQuery("#blind-three")[0];
        menuBlinds.style.display = "none";

        var teaserContent = cssQuery("iframe#teaser")[0];
        teaserContent.style.display = "none";

        timerBlindThree = null;
    }
}


