/**
 * Smava Main Functions
 */
function SmavaBehavior() {
    this.menuTimeout = null;
    this.isCSS = (document.body && document.body.style) ? true : false;
    this.isW3C = (this.isCSS && document.getElementById) ? true : false;
    this.isOpera = (navigator.userAgent.indexOf("Opera") >= 0) ? true : false;
    this.isIE4 = (this.isCSS && document.all && !this.isOpera) ? true : false;
    this.isNN4 = (document.layers) ? true : false;
    this.isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0 && !this.isOpera) ? true : false;

    this.init = function() {
        this.menu = new Array();
        var menus = document.getElementsBySelector("#primarynav li.menu");        
        for (var i = 0; i < menus.length; i++) {
            var menuId = menus[i].getAttribute("id");
            if (menuId != null) {
                var menu_wrapper = document.getElementsBySelector("#primarynav li#" + menuId + " .menu-wrapper")[0];
                if (menu_wrapper != null) {
                    this.menu[menuId] = menu_wrapper;
                    this.addEvent(
                        menus[i],
                        "mouseout",
                        function(evt) {
                            var id;
                            if (this == window && event) { // IE/Win does not understand "this", if event is attached other than "a.onclick = '...'"
                                var elem = event.srcElement;
                                // Source element might be a nested node inside the desired link node
                                while (elem.nodeName.toLowerCase() != 'li') {
                                    elem = elem.parentNode;
                                }
                                id = elem.id;
                            } else {
                                id = this.getAttribute("id");
                            }
                            behavior.menuTimeout = window.setTimeout("behavior.mouseoutMenu('" + id + "')", 10);
                        }
                    )
                    this.addEvent(
                        menus[i],
                        "mouseover",
                        function(evt) {
                            if (behavior.menuTimeout != null) {
                                window.clearTimeout(behavior.menuTimeout)
                            }
                            var id;
                            if (this == window && event) { // IE/Win does not understand "this", if event is attached other than "a.onclick = '...'"
                                elem = event.srcElement;
                                // Source element might be a nested node inside the desired link node
                                while (elem.nodeName.toLowerCase() != 'li') {
                                    elem = elem.parentNode;
                                }
                                id = elem.id;
                            } else {
                                id = this.getAttribute("id");
                            }
                            behavior.mouseoverMenu(id, evt);
                        }
                    )
                    this.addEvent(
                        menu_wrapper,
                        "mouseover",
                        function(evt) {
                            if (behavior.menuTimeout != null) {
                                window.clearTimeout(behavior.menuTimeout)
                            }
                            var elem;
                            if (this == window && event) { // IE/Win does not understand "this", if event is attached other than "a.onclick = '...'"
                                elem = event.srcElement;
                                // Source element might be a nested node inside the desired link node
                                while (elem.nodeName.toLowerCase() != 'div' || elem.className.toLowerCase() != "menu-wrapper") {
                                    elem = elem.parentNode;
                                }
                            } else {
                                elem = this;
                            }
                            elem.style.display = "block";
                        }
                    )
                }
            }
        }
    }
	this.mouseoverMenu = function(id, evt) {
        var elem = this.menu[id];
        if (elem != null) {
            currentMenuId = id;
            elem.style.display = "block";
        }
    }
	this.mouseoutMenu = function(id, evt) {
        var elem = this.menu[id];
        if (elem != null) {
            elem.style.display = "none";
        }
    }
    this.addEvent = function(target, eventType, listenerFunction, useCapture) {
        if (target.addEventListener) {
            // W3C DOM approach
            useCapture = (typeof useCapture == 'boolean') ? useCapture : false;
            target.addEventListener(eventType, listenerFunction, useCapture);
            return true;
        } else if (target.attachEvent) {
            // IE/Win DOM approach
            var r = target.attachEvent('on' + eventType, listenerFunction);
            return r;
        } else {
            // fallback approach (IE/Mac and anything else that gets this far)
            var onEv = 'on' + eventType;
            // if there's an existing event handler function
            if(typeof target[onEv] == 'function') {
                // store it
                var existing = target[onEv];
                // attach new onload handler
                target[onEv] = function() {
                    // call existing function
                    existing();
                    // call given function
                    listenerFunction();
                };
            } else {
                target[onEv] = listenerFunction;
            }
            return true;
        }
    }

    /**
     * Convert object name string or object reference
     * into a valid element object reference
     */
    this.getRawObj = function(obj) {
        var theObj;
        if (typeof obj == "string") {
            if (this.isW3C) {
                theObj = document.getElementById(obj);
            } else if (this.isIE4) {
                theObj = document.all(obj);
            } else if (this.isNN4) {
                theObj = seekLayer(document, obj);
            }
        } else {
            // pass through object reference
            theObj = obj;
        }
        return theObj;
    }

    /**
     * Retrieve the x coordinate of a non-positionable object
     */
    this.getOffsetLeft = function(obj) {
        var elem = this.getRawObj(obj);
        var result = 0;
        while (elem) {
            result += elem.offsetLeft;
            elem = elem.offsetParent;
        }
        return result;
    }

    /**
     * Retrieve the y coordinate of a non-positionable object
     */
    this.getOffsetTop = function(obj) {
        var elem = this.getRawObj(obj);
        var result = 0;
        while (elem) {
            result += elem.offsetTop;
            elem = elem.offsetParent;
        }
        return result;
    }

    /**
     * Behavior for IE-Browser.
     * The Problem:
     * Browser send on click the name and value as param. Normal Browser get the
     * first submitbutton(name,value), in case the user send the form via Enter-Key.
     * NOT IE!
     *
     * Solution:
     * add a hidden input with name and value from main submitbutton in the dom before submit form.
     */
    this.sendWithoutClickSubmit = function(form, buttonName) {
        var submitHiddenField = document.createElement("input");
        submitHiddenField.setAttribute("type", "hidden");
        submitHiddenField.setAttribute("name", buttonName);
        submitHiddenField.setAttribute("value", "");
        form.appendChild(submitHiddenField);
        return true;
    }
    this.init();
}
var behavior = new SmavaBehavior();

/**
* Tooltipp
*/
function SmavaTooltipp() {
    this.winWidth = 0;
    this.winHeight = 0;
    this.currentObject = null;
    this.width = 0;
    this.height = 0;
    this.offsetX = 0;
    this.offsetY = 0;
    this.safetyOffset = 4;
    this.isTooltippVisible = false;
    this.isReady = false;
    this.mouseoutTimeout = null;
    this.clazz = "";
    this.iebody = ((document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body);
    this.isMoveable = true;

    this.tooltipNode = document.createElement("div"); this.tooltipNode.setAttribute("id", "tooltip");
    this.shadowNode = document.createElement("div"); this.shadowNode.setAttribute("id", "tooltip-shadow");
    this.shadowTextNode = document.createElement("div"); this.shadowTextNode.setAttribute("id", "tooltip-shadow-text");
    this.shadowPointerNode = document.createElement("div"); this.shadowPointerNode.setAttribute("id", "tooltip-shadow-pointer");
    this.displayNode = document.createElement("div"); this.displayNode.setAttribute("id", "tooltip-display");
    this.textNode = document.createElement("div"); this.textNode.setAttribute("id", "tooltip-text");
    this.pointerNode = document.createElement("div"); this.pointerNode.setAttribute("id", "tooltip-pointer");
    
    /**
     * Methode initialisiert die Sprechblase
     */
    this.init = function() {
        this.create();
        behavior.addEvent(
            document,
            "mousemove",
            function(evt) {
                var posx = 0;
                var posy = 0;
                if (!evt) { evt = window.event; }
                if (evt.pageX || evt.pageY) {
                    posx = evt.pageX;
                    posy = evt.pageY;
                } else if (evt.clientX || evt.clientY) {
                    posx = evt.clientX + document.body.scrollLeft;
                    posy = evt.clientY + document.body.scrollTop;
                }
                tooltip.moveTo(posx, posy);
            }
        )
        this.tooltips = document.getElementsBySelector("span.tooltip");
        for (var x = 0; x < this.tooltips.length; x++) {
        
            behavior.addEvent(
                this.tooltips[x],
                "mouseover",
                function(evt) {
                    var id;
                    if (this == window && event) { // IE/Win does not understand "this", if event is attached other than "a.onclick = '...'"
                        var elem = event.srcElement;
                        id = elem.id;
                        if (id == null || id == '') {
                        	id = elem.parentNode.id;
                        }
                    } else {
                        id = this.getAttribute("id");
                    }
                    tooltip.mouseover(id);
                }
            )
            behavior.addEvent(
                this.tooltips[x],
                "mouseout",
                function() {
                    var id;
                    if (this == window && event) { // IE/Win does not understand "this", if event is attached other than "a.onclick = '...'"
                        var elem = event.srcElement;
                        id = elem.id;
                    } else {
                        id = this.getAttribute("id");
                    }
                    tooltip.mouseout(id);
                }
            )
        }
    }
    this.mouseover = function(id) {
        if (this.isReady) {
            if (this.mouseoutTimeout != null) { clearTimeout(this.mouseoutTimeout); }
            var tooltipText = document.getElementById(id + "-t");
            this.setContent(tooltipText);
            this.show();
        }
    }
    this.mouseout = function(id) {
        if (this.isReady) {
            this.mouseoutTimeout = setTimeout("tooltip.hide('" + id + "')", 100);
        }
    }
    /**
    * Methode f�gt einen Node in den Contentbereich der Sprechblase
    */
    this.setContent = function(node) {
        if (node != null) {
            if (this.textNode.hasChildNodes()) {
                this.textNode.removeChild(this.textNode.firstChild);
            }
            if (this.shadowTextNode.hasChildNodes()) {
                this.shadowTextNode.removeChild(this.shadowTextNode.firstChild);
            }
            this.textNode.appendChild(node.cloneNode(true));
            this.shadowTextNode.appendChild(node.cloneNode(true));
        }
    }
    /**
     * Methode bekommt ein HTML-Objekt �bergeben, speichert es und 
     * fragt die Dimensionen ab.
     */
    this.setCurrentObject = function(htmlobj, event) {
        this.windowWidth = window.offsetWidth;
        this.windowHeight = window.offsetHeight;
        this.currentObject = htmlobj;

        // TODO: debug

        this.currentObjectWidth = this.currentObject.offsetWidth;
        this.currentObjectHeight = this.currentObject.offsetHeight;
        this.currentObjectX = (event.clientX ? event.clientX : (event.pageX ? event.pageX : 0));
        this.currentObjectY = (event.clientY ? event.clientY : (event.pageY ? event.pageY : 0));
    }
    /**
     * Methode zeigt die Sprechblase an
     */
    this.show = function() {
        this.tooltipNode.style.display = "block";
    }
    /**
     * Methode versteckt die Sprechblase
     */
    this.hide = function() {
        this.tooltipNode.style.display = "none";
    }
    /**
     * Methode positioniert die Sprechblase an den vorgegebenen Koordinaten. 
     * Die Koordinaten sich auf den Ursprung der Sprechblase, d.h. wo die 
     * Pfeilspitze positioniert sein soll.
     */
    this.moveTo = function(x, y) {
        if(this.isMoveable){
            this.x = x + ((behavior.isIE4 || behavior.isIE6css) ? this.iebody.scrollLeft : 0);
            this.y = y + ((behavior.isIE4 || behavior.isIE6css) ? this.iebody.scrollTop : 0);
            this.scrollTop = document.all ? this.iebody.scrollTop : pageYOffset;
            this.scrollLeft = document.all ? this.iebody.scrollLeft : pageXOffset;
            this.winWidth = (window.innerWidth ? window.innerWidth : document.getElementsByTagName("html")[0].offsetWidth) - 20;
            this.winHeight = (window.innerHeight ? window.innerHeight : document.getElementsByTagName("html")[0].offsetHeight) - 20;
            this.width = this.tooltipNode.offsetWidth;
            this.height = this.tooltipNode.offsetHeight;

/*
            if ((this.x + this.width) >= (this.winWidth + this.scrollLeft)) {
                this.offsetX = (this.width + this.safetyOffset) * -1;
            } else {
                this.offsetX = this.safetyOffset;
            }
            if ((this.y - this.height) <= this.scrollTop) {
                this.offsetY = this.safetyOffset;
            } else {
                this.offsetY = (this.height + this.safetyOffset) * -1;
            }
            this.update();
            this.tooltipNode.style.left = (this.x + this.offsetX) + "px";
            this.tooltipNode.style.top = (this.y + this.offsetY) + "px";
            alert("test", alert);
*/

            this.tooltipNode.style.left = (this.x + this.safetyOffset - 20) + "px";
            this.tooltipNode.style.top = (this.y - this.safetyOffset + (this.height * -1)) + "px";
        }
    }
    /**
     * �berpr�ft die offsetY und offsetX Werte und rendert die Toolbox neu. zb. positioniert den Pointer neu
     */
    this.update = function() {
        var type = "";
        var position = "";
        var left = "";
        if (this.offsetY == this.safetyOffset) {        // right
            if (this.offsetX == this.safetyOffset) {    // bottom
                type = "br";
                position = "absolute";
            } else {                                    // top
                type = "tr";
                position = "relative";
            }
            left = "20";
        } else {                                        // left
            if (this.offsetX == this.safetyOffset) {    // bottom
                type = "bl";
                position = "absolute";
            } else {                                    // top
                type = "tl";
                position = "relative";
            }
            left = this.width - "40";
        }
        if (type != this.type) {
            this.changePointer(type, position, left);
            this.type = type;
        }
    }
    this.changePointer = function() {

    }

    /**
    * Methode baut das notwendige DOM und f�gt es in den bestehenden Tree ein.
    */
    this.create = function() {
        var firstData = document.createElement("span");
        firstData.appendChild(document.createTextNode("Alles mal mit einem gr�sserem Text um mal zu �berpr�fen wie der tooltip sich verh�lt."));
        this.textNode.appendChild(firstData);
        this.displayNode.appendChild(this.textNode);
        this.displayNode.appendChild(this.pointerNode);

        this.shadowTextNode.appendChild(firstData.cloneNode(true));
        this.shadowNode.appendChild(this.shadowTextNode);
        this.shadowNode.appendChild(this.shadowPointerNode);

        this.tooltipNode.appendChild(this.shadowNode);
        this.tooltipNode.appendChild(this.displayNode);
        document.getElementsByTagName("body")[0].appendChild(this.tooltipNode);

        this.isReady = true;
    }	

    window.onload = function() {
        tooltip.init();
    }
}
var tooltip = new SmavaTooltipp();
