﻿
function Rectangle(bounds ,CompanyName , CompanyID , point ,imageUrl, html ,ShowImages,PlaceWidth,PlaceHeight)
{
    this.bounds_ = bounds;
    this.imageWidth = PlaceWidth;
    this.imageHeight = PlaceHeight;
    this.CompanyID_ = CompanyID;
    this.point_ = point;
    this.html_ = html;
    this.imageName = imageUrl.replace(/\\/g, '/');
    this.CompanyName_ = CompanyName;
    this.title_ = CompanyName;
    this.ShowImages_ = ShowImages;
    if( this.ShowImages_ == 0 )
    {
        if(this.CompanyName_ != null && this.CompanyName_.length > 20)
            this.CompanyName_ = this.CompanyName_.substring(0,20) + '...';
    }
}
Rectangle.prototype = new GOverlay();

Rectangle.prototype.initialize = function (map) {
    var div = document.createElement("div");
    var span = document.createElement("div");
    div.title = this.title_;
    span.title = this.title_;
    div.style.position = "absolute";
    div.style.color = '#000';
    div.style.fontSize = '9pt';
    div.style.border = "1px solid #888";
    div.style.backgroundColor = '#FFF';
    div.style.overflow = 'hidden';
    div.style.width = this.imageWidth+ 'px';
    div.style.height = this.imageHeight+ 'px';

    map.getPane(G_MAP_MAP_PANE).appendChild(div);

    div.appendChild(span);

    this.map_ = map;
    this.div_ = div;
    this.span_ = span;
    this.div_.style.cursor = 'hand';
    this.span_.style.cursor = 'hand';
    if (this.ShowImages_ == 0) {
        this.span_.style.paddingTop = "30%";
    }
    GEvent.bindDom(this.div_, "mousedown", this, this.onMouseDown);
}

Rectangle.prototype.remove = function()
{
    this.div_.parentNode.removeChild( this.div_ );
}

Rectangle.prototype.redraw = function(force)
{
    if (!force) return;
    var c1 = this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());
    var c2 = this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());

    //得到图片大小    
    var curZoom = map.getZoom();
        
    if(curZoom == 17)
    {
        imageWidth = this.imageWidth;
        imageHeight = this.imageHeight;
        this.span_.style.fontSize = "9pt";
    }
    else if(curZoom == 16)
    {
        imageWidth = this.imageWidth/2 ;
        imageHeight = this.imageHeight/2;
        this.span_.style.fontSize = "4pt";
    }
    else if(curZoom == 15)
    {
        imageWidth = this.imageWidth/4;
        imageHeight = this.imageHeight/4;
        this.span_.style.fontSize = "2pt";
    }
    else
    {
         imageWidth = this.imageWidth/8;
         imageHeight = this.imageHeight/8;
         this.span_.style.fontSize = "1pt";
    }

//    this.div_.style.width = imageWidth;
//    this.div_.style.height = imageHeight;
    this.div_.style.width = imageWidth + 'px';
    this.div_.style.height = imageHeight + 'px';
    
    this.div_.style.left = Math.min(c2.x, c1.x)  - imageWidth/2 - 1 + "px";
    this.div_.style.top = Math.min(c2.y, c1.y)  - imageHeight/2 - 1 + "px";

    this.span_.style.textAlign = "center";
    this.span_.style.width = this.div_.style.width;
       
    this.span_.title = this.title_;  
     
    if( this.ShowImages_ == 0 )
    {
        this.span_.innerHTML = this.CompanyName_;
    }
    else
    {
        this.span_.innerHTML = '<img src=' + this.imageName + ' style="width:' + imageWidth + ';height:' + imageHeight + ';">';
    }
}

Rectangle.prototype.onMouseDown = function()
{
    var c3 = this.map_.fromLatLngToDivPixel(this.point_);
    var clientX = c3.x;
    var clientY ;
    
    var curZoom = map.getZoom();
    if(curZoom == 17)
    {
       clientY = c3.y -30 ;
    }
    else if(curZoom == 16)
    {
       clientY = c3.y + 15 ;
    }
    else if(curZoom == 15)
    {
       clientY = c3.y + 25 ;
    }
    else
    {
        clientY = c3.y +30 ; 
    }
    
    var gPoint = new GPoint(clientX,clientY);
    newPoint = this.map_.fromDivPixelToLatLng(gPoint);
    map.openInfoWindowHtml(newPoint,this.html_);
}
