/**
*   cTooltip v. 2.0
*   Class showing / hiding a tooltip at certain 
*   point on the map. Tooltip can contain any 
*   information.
**/
function cTooltip(oMap, sDiv, iTargetWidth, iTargetHeight, iOffsetX, iOffsetY) {
    this._oMap = oMap;                      // GMap2 object
    this._eDiv = $(sDiv);                   // Tooltip element
    this._iTargetWidth = iTargetWidth || 0;      // Width in pixels of target element
    this._iTargetHeight = iTargetHeight || 0;    // Heigth in pixels of target element
    this._iOffsetX = iOffsetX || 0;     // Offset X in pixels - Tweaking x position
    this._iOffsetY = iOffsetY || 0;     // Offset Y in pixels - Tweaking y position
    this._bCenter = false;
    
    
    /** 
    *   Determine if tooltip should center itself or align to the right/left
    **/
    this.checkAlign = function(sType){
        switch ( sType ) {
            case "POINT":
                this._bCenter = false;
                break;
            case "LINESTRING":
                this._bCenter = true;
                break;
            case "POLYGON":
                this._bCenter = true;
                break;
        }
    }
        
    
    
    this.findPos = function(obj) {
        var curleft = 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 used by getPosition. Results in X and Y difference
    this.subGPoints = function (a,b) {
        return new GPoint(a.x - b.x, a.y - b.y);
    }   
   
   
    // Get position for the tooltip. Checks of right side is out of bounds. If so, display left.
    // return value: object with x and y ( object.x, object.y )
    this.getPixelPosition = function(point){ 
                
        //var mapOffsetTop        = this._oMap.getContainer().offsetTop;
        //var mapOffsetLeft       = this._oMap.getContainer().offsetLeft;

        //var oMapPos             = this.findPos(this._oMap.getContainer());
        
        // Bug: Bij een map met een offset aan de linker kant IE telt te offset op bij het totaal hetgeen resulteerd in een positie die te ver naar rechts is.
        
        var mapOffsetTop        = this._oMap.getContainer().offsetTop;
        var mapOffsetLeft       = this._oMap.getContainer().offsetLeft;
        
        var TlcLatLng           = this._oMap.fromContainerPixelToLatLng(new GPoint(0,0),true);
        var TlcDivPixel         = this._oMap.fromLatLngToDivPixel(TlcLatLng);
        var pointDivPixel       = this._oMap.fromLatLngToDivPixel(point);
        var pointContainerPixel = this.subGPoints(pointDivPixel, TlcDivPixel);     

        // Get bounds information for the map
        var bounds          = this._oMap.getBounds();
        var boundsSpan      = bounds.toSpan();
        var longSpan        = boundsSpan.lng();
        var latSpan        = boundsSpan.lat();
        var mapWidth        = this._oMap.getSize().width;
        var mapHeight        = this._oMap.getSize().height;
        
        // Convert pixel width to degrees ( map width )
        var tooltipWidthInDeg = ( this._eDiv.offsetWidth ) / mapWidth * longSpan;
        var tooltipHeightInDeg = ( this._eDiv.offsetHeight ) / mapHeight * latSpan;

        // Offset tooltip to align to center of point. Used for polygones etc.
        if ( this._bCenter ) { 
            iCenterOffsetY = 0;
            iCenterOffsetX = ( this._eDiv.offsetWidth / 2 ) + (this._iTargetWidth/2);            
        } else { 
            iCenterOffsetX = 0; 
            iCenterOffsetY = 0; 
        }
        
        
        // Check for X position.        
        if ( ( point.lng() + tooltipWidthInDeg ) > bounds.getNorthEast().lng() ) {      
            pointContainerPixel.x = pointContainerPixel.x - this._eDiv.offsetWidth - (Math.round(this._iTargetWidth/2)) + mapOffsetLeft + iCenterOffsetX;
        } else {                                                                        
            pointContainerPixel.x = pointContainerPixel.x + (Math.round(this._iTargetWidth/2)) + this._iOffsetX + mapOffsetLeft - iCenterOffsetX;
        }       
        
        // Check for Y position
        if ( ( point.lat() - tooltipHeightInDeg ) < bounds.getSouthWest().lat() ) {
            pointContainerPixel.y = pointContainerPixel.y - this._eDiv.offsetHeight + ( this._iTargetHeight / 2 ) + this._iOffsetY + mapOffsetTop;
        } else {
            pointContainerPixel.y = pointContainerPixel.y - (this._iTargetWidth/2) + this._iOffsetY + mapOffsetTop;
        }
        
        //GLog.write(pointContainerPixel.x + 'x' + pointContainerPixel.y + ' - pointpixelcontainer');
        
        return pointContainerPixel;
    }   
    
    
    // check if tooltip is onscreen
    this.markerOnScreen = function (point) {
        var bounds = this._oMap.getBounds();
        // check for onscreen.
        if ( bounds.contains(point) ) {
            return true;
        } else {
            return false;    
        }
    }


    // Show tooltip on object
    this.show = function(point){
        this._bIsHidden = false;
        var oCoordinates = this.getPixelPosition(point);
        this._eDiv.style.top = oCoordinates.y + 'px';
        this._eDiv.style.left = oCoordinates.x + 'px';                                                
    }
    this.showPixels = function(oPixels){
        var point = this._oMap.fromContainerPixelToLatLng(oPixels);
        this.show(point);
    }
    
    
    this.setText = function(sText){
        this._eDiv.innerHTML = sText;
    }

    // Hides the tooltip div.
    this.hide = function () {
        this._bIsHidden = true;
        this._eDiv.style.left = '-500px';
        this._eDiv.style.top = '0px';
    }    
    



    // Show the tooltip div at coordinates with sText inside.
    // DECIPATED: old style, to keep old way
    this.showTooltip = function ( oCoordinates, sText ) {
        this._eDiv.innerHTML = sText;
        this._eDiv.style.top = oCoordinates.y + 'px';
        this._eDiv.style.left = oCoordinates.x + 'px';                                                
    }

    // Show tooltip by calling this function with eg. an anchor.
    this.showTooltipExternal  = function ( marker ) {
        if ( markerOnScreen(marker.getPoint()) ) {
            oCoordinates = getPosition(marker.getPoint());        
            showTooltip(oCoordinates, marker.tooltip);
        }
    }

    // Hides the tooltip div.
    this.hideTooltip  = function () {
        this._eDiv.style.left = '-500px';
        this._eDiv.style.top = '0px';
    }
}
//]]>// JavaScript Document
