// ----------------------------------------------
// Author: (c) Andreas 'decoman' Fay
// ----------------------------------------------
// Version:    1.4.6
// ----------------------------------------------
// Functionindex
// ----------------------------------------------
// v0.2 - checkBoxPosition
// v0.2 - close
// v0.1 - getPointerPosition
// v0.5 - open
// ----------------------------------------------


function infobox(strInfobox)
{
    // Attributes -------------------------------
    var intHeight                                =  0; // Used as approximation
    var strInfoboxId                             =  strInfobox;
    var blnOpen                                  =  false;
    var intLineLengthMax                         =  75;
    var intWidth                                 =  0; // Used as fixed value


    // ------------------------------------------
    // Method (privileged)
    // ------------------------------------------
    // Name:        checkBoxPosition
    // Description: Places box to pointer
    // Input:       MouseMovement-Event
    // Output:
    // ------------------------------------------
    // Version:     0.2
    // ------------------------------------------
    // ChangeLog
    // ------------------------------------------
    // v0.1 (06.01.2007)
    // v0.2 (18.07.2008)
    //      - added relative positioning to secure correct display in corners
    // ------------------------------------------
    this.checkBoxPosition                        =  function(mousemove)
    {
        if (blnOpen)
        {
            if (!mousemove)
                mousemove                        =  window.event;

            arrPointer                           =  getPointerPosition(mousemove);
            
            arrDimension                         =  getInnerDimensions();
            arrOffset                            =  page_scroll_offset_get();
            
            // Position-offset
            arrPosOffset                         =  new Array();
            /* arrPosOffset['x']                    =  (arrDimension['x'] + arrOffset['x'] - arrPointer['x'] - 20 < intWidth)
                                                      ? intWidth - (arrDimension['x'] + arrOffset['x'] - arrPointer['x'] - 20)
                                                      : 0; */
            arrPosOffset['y']                    =  (arrDimension['y'] + arrOffset['y'] - arrPointer['y'] - 40 < intHeight)
                                                      ? intHeight - (arrDimension['y'] + arrOffset['y'] - arrPointer['y'] - 40)
                                                      : 0;
            
            // Debug
            //alert("Dimension: "+arrDimension['x']+" x "+arrDimension['y']+"\nScrolloffset: "+arrOffset['x']+" x "+arrOffset['y']+"\nPointer: "+arrPointer['x']+" x "+arrPointer['y']);

            objMenu                              =  document.getElementById(strInfoboxId);
            
            // Determine relative position of box (considers distance between borders and pointer)
            if (arrDimension['x'] + arrOffset['x'] - arrPointer['x'] - 50 < intWidth) // 20: margin
                objMenu.style.left               =  arrPointer['x'] - intWidth - 15 + "px";
            else
                objMenu.style.left               =  arrPointer["x"] + 15 + "px";
                
            // if (arrDimension['y'] + arrOffset['y'] - arrPointer['y'] - 30 < intHeight) // 20: margin
                // objMenu.style.top                =  arrPointer['y'] - intHeight - 5 + "px";
            // else
                // objMenu.style.top                =  arrPointer["y"] + 5 + "px";
            objMenu.style.top                    =  arrPointer['y'] + 5 - arrPosOffset['y'] + "px";
        }
    }


    // ------------------------------------------
    // Method (privileged)
    // ------------------------------------------
    // Name:        close
    // Description:
    // Input:
    // Output:
    // ------------------------------------------
    // Version:     0.2
    // ------------------------------------------
    // ChangeLog
    // ------------------------------------------
    // v0.1 (06.01.2007)
    // v0.2 (19.05.2007)
    // ------------------------------------------
    this.close                                   =  function()
    {
        if (blnOpen)
        {
            objMenu                              =  document.getElementById(strInfoboxId);
            objMenu.style.visibility             =  "hidden";
            objMenu.style.zIndex                 =  -1;

            blnOpen                              =  false;
        }
    }


    // ------------------------------------------
    // Method (private)
    // ------------------------------------------
    // Name:        getPointerPosition
    // Description:
    // Input:       MouseMovement-Event
    // Output:      Coordinates of pointer
    // ------------------------------------------
    // Version:     0.1
    // ------------------------------------------
    // ChangeLog
    // ------------------------------------------
    // v0.1 (06.01.2007)
    // ------------------------------------------
    getPointerPosition                           =  function(mousemove)
    {
        if (!mousemove)
            mousemove                            =  window.event;

        arrPointer                               =  new Array();

        if (self.pageYOffset) // all except Explorer
        {
            x                                    =  self.pageXOffset;
            y                                    =  self.pageYOffset;
        }
        else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
        {
            x                                    =  document.documentElement.scrollLeft;
            y                                    =  document.documentElement.scrollTop;
        }
        else if (document.body) // all other Explorers
        {
            x                                    =  document.body.scrollLeft;
            y                                    =  document.body.scrollTop;
        }

        arrPointer["x"]                          =  mousemove.clientX+x;
        arrPointer["y"]                          =  mousemove.clientY+y;

        return arrPointer;
    }


    // ------------------------------------------
    // Method (privileged)
    // ------------------------------------------
    // Name:        open
    // Description: Opens infobox & estimates dimensions
    // Input:       (str) Text
    // Output:
    // ------------------------------------------
    // Version:     0.5
    // ------------------------------------------
    // ChangeLog
    // ------------------------------------------
    // v0.1 (06.01.2007)
    // v0.2 (19.05.2007)
    // v0.3 (12.02.2008)
    //      - added latex-support
    // v0.4 (13.02.2008)
    //      - object now gets positioned once here
    // v0.5 (18.07.2008)
    //      - estimation of dimensions
    // ------------------------------------------
    this.open                                    =  function(text)
    {
        if (text.length > 0)
        {
            // Prepare text (latex-support)
            text                                 =  text.replace(/-bs-/g, "\\");

            blnOpen                              =  true;
            
            
            // Estimate dimensions
            // Divide text into lines
            if (text.search(/<br>/) == -1)
            {
                arrText                          =  new Array();
                arrText[0]                       =  text;
            }
            else
                arrText                          =   text.split("<br>");
            intLengthMax                         =  0;
            intLines                             =  0;
            // Run through getting longest line
            for (var line in arrText)
            {
                if (arrText[line].length > intLengthMax)
                    intLengthMax                 =  arrText[line].length;
            }
            // Run through getting amount of lines (considering natural linebreaks)
            for (var line in arrText)
            {
                intLines                         += 1 + Math.floor(arrText[line].length / (intLineLengthMax < intLengthMax ? intLineLengthMax : intLengthMax));
            }
            
            intHeight                            =  intLines * 14;
            intWidth                             =  (intLineLengthMax < intLengthMax ? intLineLengthMax : intLengthMax) * 5;
            
            // Debug
            //alert("Height: "+intHeight+"\nWidth: "+intWidth+"\nLines: "+intLines+"\nmaxLength: "+intLengthMax);
            
            
            objMenu                              =  document.getElementById(strInfoboxId);
            objMenu.innerHTML                    =  text;
            objMenu.style.width                  =  intWidth + "px";

            // Position element if IE
            if (window.event)
                this.checkBoxPosition(window.event);

            objMenu.style.zIndex                 =  2;
            objMenu.style.visibility             =  "visible";
        }
    }
}