/* by redrauscher. */

/* 
 * For the sub menue by redrauscher. 
 * Expects a div tag id string
 */
var ie = navigator.userAgent.indexOf('MSIE ') != -1;
function SubMenue(id, id2)
{
	this.firstrun = false;
    var timeout = null;
    var ie6 = false;
    
    if(!window.navigator.userAgent.match(/iPhone/) && !window.navigator.userAgent.match(/iPad/)) // Not applicapble for Apple's iPhone and iPad devices
    {
        if (window.addEventListener) {
            window.addEventListener("load", registerPopupMenue, true);
            //window.addEventListener("load", showPopupMenuAtStartAndHideAgain, true);
        }
        else {
            window.attachEvent("onload", registerPopupMenue);
            //window.attachEvent("onload", showPopupMenuAtStartAndHideAgain);
        }
    }
    
    /* Register needed event listener for the popupmenue */
    function registerPopupMenue()
    {
		if(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) {
			ie6 = true;
		}
		
        var idTag = document.getElementById(id);
        if(!idTag)
            return;

		
		var id2Tag = document.getElementById(id2);
        if(id2Tag.getElementsByTagName('li').length <= 1)
        {
            id2Tag.style.display = "none";
        }
        else
        {
            id2Tag.style.display = "block";
        }
        
        id2Tag.style.width = (idTag.getElementsByTagName("label")[0].offsetWidth - 7) + "px"; // TODO Adapt when changing CSS style
        
        if(window.addEventListener)
        {
            window.addEventListener("resize", changeMenuePosition, true);
            idTag.addEventListener("click", toggleMenueVisibility, true);
            //idTag.addEventListener("mouseover", clearToggleInterval, true);
            //id2Tag.addEventListener("mouseover", clearToggleInterval, true);
        }
        else
        {
            window.attachEvent("onresize", changeMenuePosition);
			if(ie6) {
				idTag.attachEvent("onclick", toggleMenueVisibilityIE);
			} else {
				idTag.attachEvent("onclick", toggleMenueVisibility);
			}
            //idTag.attachEvent("onmouseover", clearToggleInterval);
            //id2Tag.attachEvent("onmouseover", clearToggleInterval);
        }

		if(!ie6) {
			showPopupMenuAtStartAndHideAgain();
		} else {
			id2Tag.style.display = "none";
		}
    }

    /* Don't hide the popup menu again */
    function clearToggleInterval()
    {
        clearTimeout(timeout);
    }

    /* Pops the sub menu up at start */
    function showPopupMenuAtStartAndHideAgain()
    {
        if (!ie6) 
        {
			this.firstrun = true;
            toggleMenueVisibility();
			toggleMenueVisibility();
			this.firstrun = false;
            //timeout = window.setTimeout(toggleMenueVisibility, 2000);
        }
        else
        {
            toggleMenueVisibilityIE();
			toggleMenueVisibilityIE();
            //timeout = window.setTimeout(toggleMenueVisibilityIE, 2000);
        }
    }

    /* Show and or hide the popup menue for IE6 only*/  
    function toggleMenueVisibilityIE(){
        var ulTag = document.getElementById(id2);
        if(!ulTag)
            return;
        
        if(ulTag.getElementsByTagName("li").length <= 1)
            return;

        var oe = document.getElementById('openclose');
		if (!ulTag.style || ulTag.style.display == "none") {
			changeMenuePosition();
			oe.innerHTML = document.getElementById('closer').innerHTML;
			oe.parentNode.className = "open";
			ulTag.style.display = "block";
        }
        else 
        {
            ulTag.style.display = "none";
			oe.innerHTML = document.getElementById('opener').innerHTML;
			oe.parentNode.className = "closed";
        }
    }
    
	this.toggleMenueVisibility = function() {
		this.firstrun = false;
		toggleMenueVisibility();
	}

    /* Show and or hide the popup menue */  
    function toggleMenueVisibility()
    {
        /* 
         * Deactivate popup menu if no list items are present
         * And hide the popup label
         * Deprecated?
         */
        /*var id2Tag = document.getElementById(id2);
        if(!id2Tag)
            return;
        var liTags = id2Tag.getElementsByTagName("li");
        if (liTags.length <= 1) 
        {
            var idTag = document.getElementById(id);
            idTag.style.display = "none"; // Hide  
            return; // Deactivate
        }*/
        // <--
    
        var ulTag = document.getElementById(id2);
        var ulTags = ulTag.getElementsByTagName("li");
        if(!ulTag)
            return;

        var va = "sub";
        var max = 51; // See the css style @ #popupmenu
        //var min = (ulTags.length * -(12 + 6 + 10) + max); // See the css style @ #popupmenu li
		var height = (ulTag.offsetHeight * -1) + max + ulTags[0].offsetHeight;
		var min = height;
		
        var dif = ulTags.length;
		var oe = document.getElementById('openclose');
		
		if(ulTag.getAttribute("invis") == null || ulTag.getAttribute("invis") == 1) 
        {
            changeMenuePosition();
            ulTag.setAttribute("invis", 0);
            ulTag.style.bottom = min + 'px';
            va = "add";
			
			oe.innerHTML = document.getElementById('closer').innerHTML;
			oe.parentNode.className = "open";
        } else {
			oe.innerHTML = document.getElementById('opener').innerHTML;
			oe.parentNode.className = "closed";
		}
		
		if(!firstrun) {
			var interId = window.setInterval(function() {
	            var clearme = false;
	            if(ulTag && ulTag.style) {
	                var ergo = 0;
	                if(va == "add" && replacepx(ulTag.style.bottom) < max) {
	                    ergo = (replacepx(ulTag.style.bottom) + dif);
	                    if(ergo > max) ergo = max;
	                } else if(va == "sub" && replacepx(ulTag.style.bottom) > min) {
	                    ergo = (replacepx(ulTag.style.bottom) - dif);
	                    if(ergo < min) ergo = min;
	                } else {
	                    clearme = true;
	                }

	                ulTag.style.bottom = ergo+"px";
	            } else {
	                clearme = true;
	            }

	            if(clearme) {
	                var endpos = va == "add" ? max : min;
	                ulTag.style.bottom = endpos+"px";

	                if(va == "sub") ulTag.setAttribute("invis", "1");

	                clearInterval(interId);
	            }
	        }, 1);
		} else {
			var endpos = va == "add" ? max : min;
            ulTag.style.bottom = endpos+"px";
			ulTag.setAttribute("invis", va == "add" ? "0" : "1");
		}
    }
    
    /* Change the x-position of the popup menue while resizing the web browser window */
    function changeMenuePosition()
    {
        var idTag = document.getElementById(id);
        if(!idTag)
            return;
        var id2Tag = document.getElementById(id2);
        id2Tag.style.left = getAbsoluteOffset(idTag.getElementsByTagName("label")[0]).x + "px";
    }
    
    /*
     * Returns the absolute position offset (x;y) as JSON object
     */
    function getAbsoluteOffset(elm) {
        var pos={y:0,x:0};
        if(elm) {
            var elem=elm;
        
            while(elem && elem.tagName.toUpperCase() != 'BODY') {
                pos.y += elem.offsetTop;
                pos.x += elem.offsetLeft;
                if(!elem.offsetParent)
                    break;
                elem = elem.offsetParent;
            }
        }
    
        bodyT = elem.offsetTop;
        bodyL = elem.offsetLeft;
        if(bodyT != 0) pos.y += bodyT;
        if(bodyL != 0) pos.x += bodyL;
        
        return pos;
    }
    
    function replacepx(string) {
        if(string.length > 0) return parseInt(string.replace("px", ""));
        return string;
    }
}

/* VOID */ 
function fvoid()
{}


sm = new SubMenue("popup", "popupmenu");

function togglemenu() {
	sm.firstrun = false;
	sm.toggleMenueVisibility();
	
	return false;
}

//Just for X-Mas
/*if(ie) {
    window.attachEvent("onload", muetze);
} else {
    window.addEventListener("load", muetze, false);
}*/
// --

intervId = 0;
function muetze() {
    //if(location.href == "http://www.redrauscher.de/?tt=1") {
        var head = document.getElementsByTagName("head")[0];
        script = document.createElement('script');
        script.id = 'jQuery';
        script.type = 'text/javascript';
        script.src = "/js/jquery.js";
        script.onload = "alert('test');";
        head.appendChild(script);
        
        intervId = window.setInterval(function() {
            if($(".logo")) {
                clearInterval(intervId);
                $(".logo").prepend("<div style='position:absolute;margin-top:-35px;margin-left:-18px;'><a href='/'><img id='meutze' src='/upload/muetze.png' /></a></div>"); 
            }
        }, 100);
    //}
}

