var tooltext_table;
var fantatool;
var arrowleft;
var arrowright;
var arrowtop;
var leftcheeseimg;
var rightcheeseimg;
var topcheeseimg;
    
var scrollposition = 0;
var minDiffBeforeSwitchToAnotherPosition = 5;

function ShowFantaTool(text,source,posid, eventSource)
{
	
    if (Initialize() && text!="" && (posid.toUpperCase()!="PROFILETOOLTIP" || (posid.toUpperCase()=="PROFILETOOLTIP" && eventSource.toUpperCase()=="ONFOCUS")))
        {
            var arrow_pos_left;
            var fantatool_pos_left;
            
            var posrightcorrection = 10;
            
            // relationaler Abstand cheeseimg und tooltipp nach oben
            var tooltippDistanceTop=-11;
            
            if(document.all)
            {
                tooltippDistanceTop += 5;
            }
            
            // Anstand links zwischen tooltipp und auslösendes Element
            var distanceLeft=2; 
            
            var selected_arrow;

            tooltext_table.innerHTML = text;

            var sourcepos = FindPosition(source);
            var source_pos_left = sourcepos[0];
            var source_pos_top = sourcepos[1];
            
            var arrow_pos_top = source_pos_top-100;
            var fantatool_pos_top = source_pos_top-100;
                
            var sourcedim = GetDimensions(source);
            var source_height = sourcedim[0];
            var source_width = sourcedim[1];
            fantatool.style.display="block";

            // INPUT BOXEN TOOLTIPP=========================================================================================================
            if(posid.toUpperCase()=="PROFILETOOLTIP")
                {
                    
                    if(document.all)
                    {
                        arrow_pos_top += -4;
                    }
                    
                    selected_arrow = arrowleft;
                    selected_arrow.style.display="block";
                    
                    if((source_pos_left + source_width + posrightcorrection)<600)
                    {
                        arrow_pos_left=600 + distanceLeft;
                        fantatool_pos_left = 600 + leftcheeseimg.offsetWidth + distanceLeft;
                    }
                    else
                    {
                        arrow_pos_left= source_pos_left + source_width  + distanceLeft;
                        fantatool_pos_left = source_pos_left + source_width + leftcheeseimg.offsetWidth + distanceLeft;
                    }
                    
                    arrow_pos_top += (source_height - leftcheeseimg.offsetHeight) / 2;
                    
                    // wenn tooltipp mittig werden sollte die nächste raus die übernächste rein
                    fantatool_pos_top = arrow_pos_top + tooltippDistanceTop;
                    //fantatool_pos_top += (source_height - fantatool.offsetHeight) / 2;
                    
                    
                }
            // INPUT BOXEN TOOLTIPP ENDE=========================================================================================================
            else
                {
                
                var notdone=true;
                
                //Sonderfall IE 6 div über selectbox
                if(source.name=="savecontact")
                {
                    if(document.all)
                    {
                        var navvers=navigator.userAgent.indexOf("MSIE")
                        if(navigator.userAgent.substring(navvers+5,navvers+7).indexOf("7.")==-1)
                        {
                            posrightcorrection = posrightcorrection + 1;
                            selected_arrow = arrowleft;
                            selected_arrow.style.display="block";
                
                            arrow_pos_left=source_pos_left + (source_width) + posrightcorrection  + 50;
                            arrow_pos_top += (source_height - leftcheeseimg.offsetHeight) / 2;
                
                            fantatool_pos_left = source_pos_left + (source_width) + 6 + posrightcorrection + 50;
                            fantatool_pos_top += (source_height - fantatool.offsetHeight) / 2;
                            notdone=false;
                        }
                    }
                }
                
                if(notdone)
                {
                    if (posid.toUpperCase()=="MARKER")
                        {
                            posrightcorrection = 4;
                        }
            
                    var places = GetPlaceRightAndBottom(source,source_pos_top,source_pos_left,source_width, source_height);
                    
                    var source_place_right = places[0];
                    var source_place_bottom = places[1];
                
                    var paddingToSource = 0;
                    // zeige TT oberhalb des mouseevents --> zu wenig platz unterhalb?
                    
                    var switchValue = 20;
                    
                    if(!document.all)
                    {
                            switchValue += 20;
                    }
                    
                    if(source_pos_top + (fantatool.offsetHeight/2) + switchValue > Fensterhoehe())
                    {
                        selected_arrow = arrowtop;
                        selected_arrow.style.display="block";
                
                        arrow_pos_left=source_pos_left + (source_width/2) -(topcheeseimg.offsetWidth/2); // -13 wegen Bildbreite (mehr "Fleisch rechts)
                        arrow_pos_top += -6;
                        if(document.all)
                        {
                            arrow_pos_top += -8;
                        }
                
                        fantatool_pos_left=source_pos_left + (source_width/2) -(fantatool.offsetWidth/2)-1;
                        fantatool_pos_top += -fantatool.offsetHeight - 6; 
                    }
                    // zeige TT links des mouseevents --> zu wenig platz rechts?
                    else if(source_place_right <= (fantatool.offsetWidth) + minDiffBeforeSwitchToAnotherPosition)
                    {
                        arrow_pos_top += -5;
                        if(document.all)
                        {
                            arrow_pos_top += -4;
                        }
                        
                        selected_arrow = arrowright;
                        selected_arrow.style.display="block";

                        arrow_pos_left= source_pos_left - 3*distanceLeft;
                        arrow_pos_top += (source_height - leftcheeseimg.offsetHeight) / 2 ;
                
                        fantatool_pos_left= source_pos_left  - (fantatool.offsetWidth) - 3*distanceLeft;
                        
                        // wenn tooltipp mittig werden sollte die nächste raus die übernächste rein
                        fantatool_pos_top = arrow_pos_top + tooltippDistanceTop;
                        //fantatool_pos_top += (source_height - fantatool.offsetHeight) / 2;
                    }
                    else // zeige tooltipp rechts vom mouseevent
                    {
                        if(document.all)
                        {
                            arrow_pos_top += -5;
                        }
                        
                        selected_arrow = arrowleft;
                        selected_arrow.style.display="block";
            
                        arrow_pos_left=source_pos_left + (source_width) + distanceLeft;
                        arrow_pos_top += (source_height - leftcheeseimg.offsetHeight) / 2 ;
            
                        fantatool_pos_left = source_pos_left + (source_width) + leftcheeseimg.offsetWidth + distanceLeft;
                        
                        // wenn tooltipp mittig werden sollte die nächste raus die übernächste rein
                        fantatool_pos_top = arrow_pos_top + tooltippDistanceTop;
                        //fantatool_pos_top += (source_height - fantatool.offsetHeight) / 2;
                    }
                }
            }
                
            fantatool_pos_top -= scrollposition;
            arrow_pos_top -= scrollposition;

            fantatool.style.left = fantatool_pos_left + 'px';
            fantatool.style.top = fantatool_pos_top + 'px'; 
            
            selected_arrow.style.left = arrow_pos_left + 'px';
            selected_arrow.style.top = arrow_pos_top + 'px';    
        }
}
function HideFantaTool(source)
{
    
    if(Initialize())
        {
            if (isIE7()) {
                fantatool.style.top = -fantatool.offsetHeight-260;
                arrowleft.style.top = -arrowleft.offsetHeight-260;
                arrowtop.style.top = -arrowtop.offsetHeight-260;
                arrowright.style.top = -arrowright.offsetHeight-260;
            } else {
                fantatool.style.display = "none";
                arrowleft.style.display = "none";
                arrowright.style.display = "none";
                arrowtop.style.display = "none";
            }
        }
}

function isIE7() {
    return navigator.appVersion.indexOf("MSIE") > -1 && parseFloat(navigator.appVersion.split("MSIE")[1]) > 6;
}

function Initialize()
{
    if( (tooltext_table = document.getElementById('tooltext')) && (fantatool = document.getElementById('fantatool')) 
        &&    (arrowleft = document.getElementById('arrowleft')) && (arrowright = document.getElementById('arrowright')) 
        &&    (arrowtop = document.getElementById('arrowtop')) )
        {
            rightcheeseimg = document.getElementById('arrow_right_img');
            leftcheeseimg = document.getElementById('arrow_left_img');
            topcheeseimg = document.getElementById('arrow_top_img');
            return true;
        }
    else {return false;}
}
function GetPlaceRightAndBottom(source,s_top,s_left,s_width,s_height)
{
    var placeRight=0;
    var placeBottom=0;
    placeRight = Fensterbreite() - s_width - s_left;
    placeBottom = Fensterhoehe() + scrollposition - (s_top) - s_height;
    
    return [ placeRight, placeBottom]
        }
function GetDimensions(source) 
{
    var withHeight = 0;
    var withWidth  = 0;
    
    withHeight += source.offsetHeight;
    withWidth += source.offsetWidth;
    
    return [ withHeight, withWidth]
        }
function FindPosition(source) 
{
    if( source.offsetParent ) 
        {
            for( var posX = 0, posY = 0; source.offsetParent; source = source.offsetParent )
                {
                    posX += source.offsetLeft;
                    posY += source.offsetTop;
                }
            return [ posX, posY ];
        } 
    else 
        {
            return [ source.x, source.y ];
        }
}

function Fensterbreite () {
    if (window.innerWidth) {
        return window.innerWidth;
    } else if (document.body && document.body.offsetWidth) {
        return document.body.offsetWidth;
    } else {
        return 0;
    }
}
function Fensterhoehe () {
    if (window.innerHeight) {
        return window.innerHeight;
    } else if (document.body && document.body.offsetHeight) {
        return document.body.offsetHeight;
    } else {
        return 0;
    }
}
    

