// Version 0.2      the .copy() parameters were wrong
// version 1.0      added .show() .hide() .setContents() .setPoint() .setOpacity() .overlap
// version 1.1      Works with GMarkerManager in v2.67, v2.68, v2.69, v2.70 and v2.71
// version 1.3      add .isHidden()
// version 1.4      permit .hide and .show to be used before addOverlay()
// version 1.5      fix positioning bug while label is hidden
// version 1.6      added .supportsHide()
// version 1.7      fix .supportsHide()


      function ELabel(point, html, classname, pixelOffset, percentOpacity, fontColor, fontBackgroundColor, overlap, corner) {
        // Mandatory parameters
        this.point = point;
        this.html = html;
        
        // Optional parameters
        this.classname = classname||"";
        this.pixelOffset = pixelOffset||new GSize(0,0);
        if (percentOpacity) {
          if(percentOpacity<0){percentOpacity=0;}
          if(percentOpacity>100){percentOpacity=100;}
        }        
        this.percentOpacity = percentOpacity;
        this.overlap=overlap||false;
        this.fontColor=fontColor;
        this.fontBackgroundColor=fontBackgroundColor;
        //this.zoomable=bZoomable||false;
        this.hidden = false;
        this.lastZoom = 0;
        if (corner)
            this.corner=corner;
        else
            this.corner=1;
      } 
      
      ELabel.prototype = new GOverlay();

      ELabel.prototype.initialize = function(map) {
        var div = document.createElement("div");
        
        div = this.createDivInnerHTML(div);
        
        map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div); //G_MAP_FLOAT_SHADOW_PANE
        this.map_ = map;
        this.div_ = div;
        if (this.classname != "")
        {
            if (this.percentOpacity) {        
              if(typeof(div.style.filter)=='string'){div.style.filter='alpha(opacity:'+this.percentOpacity+')';}
              if(typeof(div.style.KHTMLOpacity)=='string'){div.style.KHTMLOpacity=this.percentOpacity/100;}
              if(typeof(div.style.MozOpacity)=='string'){div.style.MozOpacity=this.percentOpacity/100;}
              if(typeof(div.style.opacity)=='string'){div.style.opacity=this.percentOpacity/100;}
            }
            if (this.overlap) {
              var z = GOverlay.getZIndex(this.point.lat());
              this.div_.style.zIndex = z;
            }
        }
        if (this.hidden) {
          this.hide();
        }
        
//        this.div_.onclick=function(){
//            window.event.cancelBubble = true;  
//            window.alert('test');
//        }
      }

      ELabel.prototype.createDivInnerHTML=function(div){
        if (this.classname == "")
        {
            var nXAbsolute = 0;
            var nYAbsolute = 0;
            var sXPos1, sXPos2;
            var sYPos1, sYPos2;
            div.style.position = "absolute";
            div.style.top = "0px";
            div.style.left = "0px";
            switch (this.corner){
            case 3:
                sXPos1 = "right:0px";
                sYPos1 = "top:1px";
                sXPos2 = "right:1px";
                sYPos2 = "top:0px";
                break;
            case 5:
                sXPos1 = "right:0px";
                sYPos1 = "bottom:0px";
                sXPos2 = "right:1px";
                sYPos2 = "bottom:1px";
                break;
            case 7:
                sXPos1 = "left:1px";
                sYPos1 = "bottom:0px";
                sXPos2 = "left:0px";
                sYPos2 = "bottom:1px";
                break;
            case 1:
            default:
                sXPos1 = "left:1px";
                sYPos1 = "top:1px";
                sXPos2 = "left:0px";
                sYPos2 = "top:0px";
                break;
           }
            if (this.fontBackgroundColor == "#000000")
            {
                div.innerHTML = '<div style="word-wrap:normal;position:absolute;'+sYPos1+';'+sXPos1+';font-size:smaller;color:' + this.fontBackgroundColor + ';">' + this.html.replace(/ /g,"&nbsp;") + '</div><div style="word-wrap:normal;position:absolute;'+sYPos2+';'+sXPos2+';color:' + this.fontColor + ';font-size:smaller" id="PushpinLabelText" >' + this.html.replace(/ /g,"&nbsp;") + '</div>' ; //<div class="' + this.classname + '"> </div>
            }
            else
            {
                div.innerHTML = '<div style="word-wrap:normal;position:absolute;'+sYPos2+';'+sXPos2+';color:' + this.fontBackgroundColor + ';font-size:smaller" id="PushpinLabelText" >' + this.html.replace(/ /g,"&nbsp;") + '</div><div style="word-wrap:normal;position:absolute;'+sYPos1+';'+sXPos1+';font-size:smaller;color:' + this.fontColor + ';">' + this.html.replace(/ /g,"&nbsp;") + '</div>' ; //<div class="' + this.classname + '"> </div>
            }
        }
        else
        {
            div.innerHTML = this.html;
            div.className = this.classname; 
        }
        return div;
      }

      ELabel.prototype.remove = function() {
        this.div_.parentNode.removeChild(this.div_);
      }

      ELabel.prototype.copy = function() {
        return new ELabel(this.point, this.html, this.classname, this.pixelOffset, this.percentOpacity, this.overlap, this.corner);
      }

      ELabel.prototype.redraw = function(force) {
        if (this.classname == ""){
            //if (this.map_.getZoom() != this.lastZoom){
                this.lastZoom = this.map_.getZoom();
                if (this.lastZoom > 10)
                    this.div_.style.fontSize=((this.map_.getZoom()-8)*2).toString()+"pt";
                else
                    this.div_.style.fontSize="4pt";
        }
        var p = this.map_.fromLatLngToDivPixel(this.point);
        var h = parseInt(this.div_.clientHeight);
        this.pixelOffset.width = - this.div_.clientWidth /2;
        this.div_.style.left = (p.x + this.pixelOffset.width) + "px";
        this.div_.style.top = (p.y +this.pixelOffset.height - h) + "px";
      }

      ELabel.prototype.show = function() {
        if (this.div_) {
            this.div_.style.display="";
          this.redraw();
        }
        this.hidden = false;
      }
      
      ELabel.prototype.hide = function() {
        if (this.div_) {
            this.div_.style.display="none";
        }
        this.hidden = true;
      }
      
      ELabel.prototype.isHidden = function() {
        return this.hidden;
      }
      
      ELabel.prototype.supportsHide = function() {
        return true;
      }

      ELabel.prototype.setContents = function(html) {
        this.html = html;
        //this.div_.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;
        //this.div_.innerHTML=this.html;
        if (this.div_)
            this.createDivInnerHTML(this.div_);
        if (this.map_)
            this.redraw(true);
      }
      
      ELabel.prototype.setPoint = function(point) {
        this.point = point;
        if (this.overlap) {
          var z = GOverlay.getZIndex(this.point.lat());
          this.div_.style.zIndex = z;
        }
        this.redraw(true);
      }
      
      ELabel.prototype.setOpacity = function(percentOpacity) {
        if (percentOpacity) {
          if(percentOpacity<0){percentOpacity=0;}
          if(percentOpacity>100){percentOpacity=100;}
        }        
        this.percentOpacity = percentOpacity;
        if (this.percentOpacity) {        
          if(typeof(this.div_.style.filter)=='string'){this.div_.style.filter='alpha(opacity:'+this.percentOpacity+')';}
          if(typeof(this.div_.style.KHTMLOpacity)=='string'){this.div_.style.KHTMLOpacity=this.percentOpacity/100;}
          if(typeof(this.div_.style.MozOpacity)=='string'){this.div_.style.MozOpacity=this.percentOpacity/100;}
          if(typeof(this.div_.style.opacity)=='string'){this.div_.style.opacity=this.percentOpacity/100;}
        }
      }
      
      ELabel.prototype.hitTest=function(oPoint) {
        var bResult=false;
        if (oPoint){
            var p = this.map_.fromLatLngToDivPixel(oPoint);
            var l = parseInt(this.div_.offsetLeft);
            var t = parseInt(this.div_.offsetTop);
            var w = parseInt(this.div_.offsetWidth);
            var h = parseInt(this.div_.offsetHeight);
            if((p.x > l) && (p.x < l+w) && (p.y > t) && (p.y < t+h))
                bResult=true;
        }
        return bResult;
      }

      ELabel.prototype.getPoint = function() {
        return this.point;
      }
      ELabel.prototype.U = function() {
        return this.point;
      }
      ELabel.prototype.V = function() {
        return this.point;
      }
      ELabel.prototype.W = function() {
        return this.point;
      }
      ELabel.prototype.X = function() {
        return this.point;
      }
      ELabel.prototype.Y = function() {
        return this.point;
      }
      ELabel.prototype.Z = function() {
        return this.point;
      }

        