/********************************************/
/* Javascript textpopup with shaded corners */
/********************************************/

/* Declare some global variables, that will be persisted across the mouseover events */

var popupWindowObj; /* pointer to textPopupWindow object */
var textPopupActive = false;
var textPopupActiveOject = null;
var textPopupActiveValign;
var textPopupActivePosition;
var textPopupActiveSize;
var textPopupActiveFollow;

/*
    This function inserts the popup div at the end of the document and is then reused
*/

/* Get the scroll offset of the window */

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}


/* Find the position of a DOM Element */

function findPosition(obj) {
	var curleft = 0;
    var 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 insertTextPopupIntoDOM (){
    var textPopupHTML = new Array
    (
    '<div id="textPopupWindow" class="textPopup textPopupSizeSmall" onmouseover="showTextPopup(this,\'\',\'\',\'\');" onmouseout="hideTextPopup(this);">',
    '<div id="textPopupIframeBackground" style="background: transparent;"></div>',
    '   <div id="textPopupWindowContent" class="textPopupcontent">',
    '       empty',
    '   </div>',
    '   <div class="dropshadow">',
    '       <div class="top">',
    '           ',
    '       </div>',
    '       <div class="bottom">',
    '           ',
    '       </div>',
    '   </div>',
    '</div>'
    );

    for(var i=0; i<textPopupHTML.length; i++) {
        document.writeln(textPopupHTML[i]);
    }

    popupWindowObj = document.getElementById('textPopupWindow');

    var browser = navigator.userAgent;
    if (browser.indexOf("MSIE") >= 0 ){
        document.getElementById("textPopupIframeBackground").style.display = '';
    }
}

/*
    Mouseoverhandler for showing the textPopup
*/

function showTextPopup(theObj, valign, position, size, follow, textContent) {
    /* following parameters are possible: */
    /* valign = [top|bottom|], position = [inside|outside], size = [small|medium], follow = [true|false] */
    /* textContent = [String] for dynamic content */
    
    valign = valign.toUpperCase();
    position = position.toUpperCase();
    size = size.toUpperCase();

    //check if the popup-object exists
    if(!popupWindowObj)
        return(false);

    /*
    Execute this code only at the initial mouseover of a new button object that triggers the popup,
    textpopupActive will be set to true this time and will be set to false by the reallyHide function,
    when the popup is actually hidden
    */

    if(!textPopupActive) {   //Execute only at initial mouseover
        // Set the parameters of this mouseover to global variables that can be used by other event handlers
        textPopupActiveValign = valign;
        textPopupActivePosition = position;
        textPopupActiveSize = size;
        textPopupActiveFollow = follow;

        //Changes classname of the popupwindow according to the 'valign' and 'size' parameter
        //Check alignment
        if(valign == 'TOP') {
            popupWindowObj.className = 'textPopupTop';
        }
        else if(valign == 'BOTTOM') {
            popupWindowObj.className = 'textPopupBottom';
        }
        else  {
            popupWindowObj.className = 'textPopup';
        }

        //Check size
        if(size == 'SMALL') {
            popupWindowObj.className += ' textPopupSizeSmall';
        }
        else if(size == 'MEDIUM') {
            popupWindowObj.className += ' textPopupSizeMedium';
        }
        else {
            popupWindowObj.className += ' textPopupSizeMedium';
        }

        //Look for the HTML-content of the popup and copy that into the popupwindow
        if(textContent) {
           document.getElementById('textPopupWindowContent').innerHTML = textContent;
        }

        else {
            var goNext = true;
            var sibling = theObj.nextSibling;

            //First Look for child div with classname 'popupContent'
            for(var i=0; i < theObj.childNodes.length; i++) {

                if(theObj.childNodes[i].className) {
                    if(theObj.childNodes[i].className.indexOf('popupContent') != -1) {
                        document.getElementById('textPopupWindowContent').innerHTML = theObj.childNodes[i].innerHTML;
                        goNext = false;
                    }
                }
            }

            //If that fails:
            //Look for sibling div with classname 'popupContent'

            while(goNext && sibling) {
                if(sibling.className) {
                    if(sibling.className.indexOf('popupContent') != -1) {
                        document.getElementById('textPopupWindowContent').innerHTML = sibling.innerHTML;
                        goNext = false;
                    }
                }

                if(sibling.nextSibling) {
                    sibling = sibling.nextSibling;
                }

                else {
                    goNext = false;
                }
            }
        }
				
        //reset the popupwindow relative to the calling object
        coords = findPosition(theObj); //find position of popup-caller button
        popupWindowObj.style.left = coords[0] + (theObj.offsetWidth/2) + "px"; //horizontally center the popup over the caller-object
        popupWindowObj.style.top = coords[1] + "px"; //basic vertical realignment

        //Further adjust the vertical coordinate according to 'valign' and 'position' property
        if(valign == 'TOP' && position == 'INSIDE') {
            //popupWindowObj.style.top = ((coords[1] - popupWindowObj.offsetHeight) + (theObj.offsetHeight/1.5)) + "px";
            popupWindowObj.style.top = ((coords[1] - popupWindowObj.offsetHeight) + (theObj.offsetHeight)/2) + "px";
        }
        else if(valign == 'BOTTOM' && position == 'INSIDE') {
            //popupWindowObj.style.top = (coords[1] + (theObj.offsetHeight*2)) + "px";
            popupWindowObj.style.top = (coords[1] + (theObj.offsetHeight*0.5)) + "px";
        }
        else if(valign == 'TOP') {
            popupWindowObj.style.top = (coords[1] - popupWindowObj.offsetHeight) + "px";
        }
        else if(valign == 'BOTTOM') {
            popupWindowObj.style.top = (coords[1] + theObj.offsetHeight) + "px";
        }

        //Find the newly defined coords of the repositioned popupwindow
       coords = findPosition(popupWindowObj);

        //Check if it is positioned outside the TOP of the screen and reposition popup at bottom align
        if((findPosition(popupWindowObj)[1] < getScrollXY()[1]) && valign == 'TOP') {   // only trigger this event when valign == TOP!
            showTextPopup(theObj, 'bottom', position, size, follow); //recursively call this function again with 'valign = bottom'
            return;
        }

        //Check if the 'follow' parameter is set to true
        if(follow) {
            if(!theObj.onmousemove)
                theObj.onmousemove = followTextPopup;  //Set the mouseover eventhandler on the caller object

            if(window.event) //only needed for IE. directly readjust popup-position to the mouse pointer
               followTextPopup(window.event);  //used in IE only for smooth transition
        }

        //Important!
        textPopupActive = true; //this popup is now active and visible
        textPopupActiveOject = theObj; //the object that called the currently activated popupwindow

        //check if there is any valid content in the popup
        if(document.getElementById('textPopupWindowContent').innerHTML.length > 2) { //Content should at least contain more than 2 characters
            //Only show if there is any content
            document.getElementById("textPopupIframeBackground").style.width = (document.getElementById("textPopupWindowContent").offsetWidth - 0) + 'px';
            document.getElementById("textPopupIframeBackground").style.height = (document.getElementById("textPopupWindowContent").offsetHeight - 0) + 'px';

            popupWindowObj.style.visibility='visible'; //Finally display the window
        }
    }

    //Cancel the hide timeout if the popupwindow receives a mouseover again from the caller OR the popupwindow itself
    else if(popupWindowObj.getAttribute("timerId") && textPopupActiveFollow == false )  {  /* if the follow option is active, then don't do this */
        clearTimeout(popupWindowObj.getAttribute("timerId"));
        popupWindowObj.setAttribute ("timerId", null);
    }

    /*
    If a mouseover is generated by another caller, then immediately hide the current popupwindow without delay and
    and recursively call the showTextPop handler with the new caller
    */

    if(textPopupActiveOject != theObj && theObj != popupWindowObj) {
        reallyHideTextPopup(); //immmediately hide the current popup
        showTextPopup(theObj, valign, position, size, follow, textContent); //call a new popup window
    }

    return;
}

/*
    Mouseout handler for delayed hiding of the popup (exeption: follow parameter = true)
*/

function hideTextPopup(theObj) {
    /*
    Try to hide submenu in case of mouseout
    Can be cancelled by a mouseover
    */

    var timerId;
    var timeOutHandler = "reallyHideTextPopup()";
    var timeOut = 500; /* standard delay (milliseconds) of mouseout */

    //check if the popup-object exists
    if(!popupWindowObj)
        return(false);

    //If the follow property of the active popup is set to true, then immediately hide
    if(textPopupActiveFollow) {
        reallyHideTextPopup();
    }
    else {
        timerId = window.setTimeout(timeOutHandler, timeOut);
        popupWindowObj.setAttribute ("timerId", timerId);
    }

    return;
}

/*
    Actually hide the popup window
*/

function reallyHideTextPopup() {
    popupWindowObj.style.visibility='hidden';  //hide the window
    clearTimeout(popupWindowObj.getAttribute("timerId"));
    popupWindowObj.setAttribute ("timerId", null); //set the timer attribute of the popupwindow to null
    textPopupActive = false; //Important! set popupwindow inactive
}

/*
    Mousemove handler in case the 'follow(mouse)' option is selected
*/

function followTextPopup(e) {

    //check if the popup-object exists
    if(!popupWindowObj)
        return(false);

    //Do not execute when there is still a timer running !!!
    if(popupWindowObj.getAttribute("timerId"))  {
        return(false);
    }

    var posx = 0;
	var posy = 0;

    if (!e) var e = window.event;

    if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

    //adjust the horizontal position of the popupwindow
    popupWindowObj.style.left = posx + "px";

    //adjust the vertical position according to the valign property relative to mousepointer
    if(textPopupActiveValign == 'TOP') {
        popupWindowObj.style.top = posy - popupWindowObj.offsetHeight - 10 + "px";
    }
    else if(textPopupActiveValign == 'BOTTOM') {
        popupWindowObj.style.top = posy + 15 + "px";
    }

    //find the newly coords of the repositioned popupwindow
    coords = findPosition(popupWindowObj);

    //check if it is positioned outside the TOP of the screen and reposition popup at bottom align
    if(findPosition(popupWindowObj)[1] < getScrollXY()[1]) {
        reallyHideTextPopup();
        showTextPopup(textPopupActiveOject, 'bottom', textPopupActivePosition, textPopupActiveSize, true);
        return;
    }
}