﻿// JScript 文件

var map;
var points;
var arrayMarkers = new Array();


$(function () {
creatMap();if(map.isLoaded()==true&&isloadall==true) getMarker2(undefined);
 });

 function creatMap() {
    map = new GMap2(document.getElementById("map_div"));
    //map.addControl(new GLargeMapControl3D());//3d缩放标签
    //map.addControl(new GSmallMapControl());//缩放标签
    //map.addControl(new GMapTypeControl());//切换标签
    map.enableScrollWheelZoom();
    map.enableContinuousZoom();
    var customUI = map.getDefaultUI();
    customUI.maptypes.hybrid = true;
    customUI.maptypes.physical = false;
    map.setUI(customUI);
    //map.addControl(new GScaleControl());地图标尺
    //map.addControl(new GOverviewMapControl());

    map.setMapType(G_NORMAL_MAP);
    if (project[0].GoogleAPIX != '' && project[0].GoogleAPIY != '') {
        var obj = new keyForMarker();
        obj.name=project[0].ProjectName;
        obj.summary=project[0].Summary;
        obj.imgarry=project[1];
        obj.markerid=project[0].ProjectID;
        obj.x = project[0].GoogleAPIX;
        obj.y = project[0].GoogleAPIY;
        obj.lpurl = new Date(project[0].CreatedOn).getFullYear().toString();
        if((new Date(project[0].CreatedOn).getMonth()+1).toString().length=1) obj.lpurl+="0"+(new Date(project[0].CreatedOn).getMonth()+1).toString();
        else obj.lpurl+=(new Date(project[0].CreatedOn).getMonth()+1).toString();
        obj.lpurl += "/"+project[0].ProjectID+".html";
        obj.jiage = project[0].AveragePrice;
        obj.address = project[0].Address;
        obj.phone = project[0].SalePhone;
        obj.islp = true;
        obj.pricetypetagname = project[0].PriceTypeTagName;
        obj.opendate = new Date(project[0].OpenDate).getFullYear().toString()+"-"+(new Date(project[0].OpenDate).getMonth()+1).toString()+"-"+new Date(project[0].OpenDate).getDate().toString();
        var latLng = new GLatLng(obj.x,obj.y);
        obj.marker = createMarker(obj.islp, 13, latLng, "/static/PubImg/m2.png", obj.name, obj.jiage, obj.imgarry, obj.lpurl, obj.markerid, obj.phone, obj.opendate, obj.address, obj.pricetypetagname);
        map.addOverlay(obj.marker);
        arrayMarkers.push(obj);
        map.setCenter(latLng, project[0].Zoomratio);
        createMObj();
     }
     else
     {
         getAddress();
     }

}

function createMarker(isLp,_zoom,point,imgurl,m_name,m_info,m_arr,lpurl,mid,phone,opendate,address,pricetypetagname)
{
  var myIcon= new GIcon(G_DEFAULT_ICON);
  myIcon.image = imgurl;                 
  myIcon.iconSize = new GSize(5, 22);   //图标大小        
  myIcon.shadowSize = new GSize(0, 0); //阴影大小   
 opts = {       
    "icon": myIcon,        
    "clickable": false,         
    "draggable": false,        
    "labelText": m_name,       
    "labelOffset": new GSize(16,-16),
    "url": lpurl, 
    "id":mid,
    "islp":isLp,
    "zoom":_zoom,
    "img":m_arr,
    "jiage":m_info,
    "phone":phone,
    "opendate":opendate,
    "address": address,
    "pricetypetagname":pricetypetagname
}; 
  var newMarker = new LabeledMarker(point, opts);
  return newMarker;
}

function createInfoWindow(isLp, m_name, m_info, m_arr, lpurl, m_addr, m_open, m_phone, m_pricedes) {

   // 提示窗口创建动态 DOM 对象， DIV 标签
  var div = document.createElement('div');
  div.className = 'mapfont';  
  if(isLp)//判断是否为项目楼盘标注
  {
      div.style.width = '260px';
      var div_Name = document.createElement('div');
      if (m_info == '0') m_info = '--';
      div_Name.innerHTML = '<div style="float:right;"><img onerror="this.src=\'/lp/images/lp_list_nopic.gif\';" src="http://fang0.com/' + m_arr + '" width="100" height="82" /></div><div><ul><li><strong>' + m_name + '</strong>&nbsp;&nbsp;<a href="/lp/' + lpurl + '" style="color: #03F;">[详细页]</a></li><li><strong>均价:</strong>&nbsp;&nbsp;<strong class="red">' + m_info + '</strong>' + m_pricedes + '</li><li><strong>开盘日期:</strong>&nbsp;&nbsp;' + m_open + '</li><li><strong>销售电话:</strong>&nbsp;&nbsp;<strong class="red">' + m_phone + '</strong></li><li><strong>地址:</strong>&nbsp;&nbsp;' + m_addr + '</li></ul></div>';
      div.appendChild(div_Name); 
  }
  else
  {
      div.style.width = '100px';
      var div_Name = document.createElement('div');
      var iurl;
      if(m_arr==undefined)
      {
        iurl = '/lp/images/lp_list_nopic.gif';
      }else iurl= m_arr.SavedUrl;
      div_Name.innerHTML = '<strong>' + m_name + '</strong><div style="float:right;"><img src="http://fang0.com/' + iurl + '" onerror="this.src=\'/lp/images/lp_list_nopic.gif\';"/><div>';
      div.appendChild(div_Name);
  }
  return div;
}

function getAddress()
{
    var latlng;
    if (google.loader.ClientLocation) {
    latlng = new GLatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
    } 
    map.setCenter(latlng,13);
}

function getMarker(obj)
{
    if(obj.checked)
    {
          for(var j=0;j<arrayMarkers.length;j++)
          {
               if(arrayMarkers[j].icoid!=undefined)
               {
                     if(obj.value == arrayMarkers[j].icoid.toString())
                     {
                         map.addOverlay(arrayMarkers[j].marker);
                         map.panTo(arrayMarkers[j].marker.getPoint());
                     }

               }
          }
     }
     else
     {
          for(var j=0;j<arrayMarkers.length;j++)
          {
              if(arrayMarkers[j].icoid!=undefined)
               {
                 if(obj.value == arrayMarkers[j].icoid.toString())
                 {
                    map.removeOverlay(arrayMarkers[j].marker);
                  }
              }
          }
     }
}
function getMarker2(obj)
{
    if(obj!=undefined){
       map.clearOverlays();
        var temp;
        var temp_id = obj.id.replace("li_","");
        for(var j=0;j<arrayMarkers.length;j++)
        {
             if(arrayMarkers[j].icoid!=undefined)
             {
                  temp = arrayMarkers[j].marker;
                  if(temp_id == arrayMarkers[j].icoid.toString())
                  {
                    map.addOverlay(temp);
                  }
                  else
                  {
                     map.removeOverlay(temp);
                  }
             }
        }
        map.addOverlay(arrayMarkers[0].marker);
    }
    else{
            map.clearOverlays();
            for(var j=0;j<arrayMarkers.length;j++)
            {
                 temp = arrayMarkers[j].marker;
                 if(arrayMarkers[j].icoid!=undefined)
                 {
                        map.addOverlay(temp);
                 }
            }
            map.addOverlay(arrayMarkers[0].marker);
    }
}
function keyForMarker()
{
  this.markerid;
  this.marker;
  this.name;
  this.summary;
  this.imgarry;
  this.x;
  this.y;
  this.icoid;
  this.date;
  this.lpurl;
  this.jiage;
  this.address;
  this.phone;
  this.opendate;
  this.islp;
  this.address;
  this.phone;
  this.opendate;
  this.pricetypetagname;
}

function createMObj()
{ 
    if(markers.length>0)
    {
      for(var i=0;i<markers[0].length;i++)
      {
        var obj = new keyForMarker();
        obj.name=markers[0][i].Name;
        obj.summary=markers[0][i].Summary;
        obj.imgarry=markers[1][i][0];
        obj.markerid="zb"+markers[0][i].SupportingFacilitiesInfoID;
        obj.x = markers[0][i].GoogleAPIX;
        obj.y = markers[0][i].GoogleAPIY;
        obj.icoid = markers[2][i];
        obj.islp = false;
        var latLng = new GLatLng(obj.x,obj.y);
        obj.marker = createMarker(obj.islp, 14, latLng, "/static/PubImg/m3.png", obj.name, obj.jiage, obj.imgarry, obj.lpurl, obj.markerid, obj.phone, obj.opendate, obj.address, obj.pricetypetagname);
        arrayMarkers.push(obj);
      }          
    }
}
//-----------------------------------------------------------------------

function LabeledMarker(latlng, options){   
    this.latlng = latlng; 
    this.labelText = options.labelText || ""; 
    this.labelClass = options.labelClass || "markerLabel"; 
    this.labelOffset = options.labelOffset || new GSize(0, 0);
    this.lpurl = options.url||"#";
    this.did = "d_"+options.id || "d_0";
    this.islp = options.islp || false;
    this.jiage = options.jiage;
    this.img = options.img;
    this.zoom = options._zoom;
    this.phone = options.phone;
    this.opendate = options.opendate;
    this.address = options.address;
    this.pricetypetagname = options.pricetypetagname;
    this.map = map; 
    GMarker.apply(this, arguments); 
    
} 
 
LabeledMarker.prototype =new GMarker(new GLatLng(0, 0));

LabeledMarker.prototype.initialize = function(map) { 
    GMarker.prototype.initialize.call(this, map); 
    var div = document.createElement("div"); 
    div.className = this.labelClass; 
    div.innerHTML = this.labelText; 
    div.style.position = "absolute"; 
    div.id= this.did;
    if(this.id.toString().indexOf("zb")!=-1) div.style.background="#999999";
    map.getPane(G_MAP_MARKER_PANE).appendChild(div);  
    this.div = div;
    GEvent.bindDom(this.div,"click",this,this.showWindow);
    GEvent.bindDom(this.div,"mouseover",this,this.mouseover);
    GEvent.bindDom(this.div,"mouseout",this,this.mouseout); 
}; 

LabeledMarker.prototype.redraw = function(force) { 
    GMarker.prototype.redraw.call(this, map); 
    if (!force) return;    
    var p = this.map.fromLatLngToDivPixel(this.latlng); 
    var z = GOverlay.getZIndex(this.latlng.lat()); 
    this.div.style.left = (p.x + this.labelOffset.width-20) + "px"; 
    this.div.style.top = (p.y + this.labelOffset.height-18) + "px"; 
    this.div.style.zIndex = z + 1; // in front of the marker   
}; 

LabeledMarker.prototype.remove = function() { 
    this.div.parentNode.removeChild(this.div); 
    this.div = null; 
    GMarker.prototype.remove.call(this); 
};
LabeledMarker.prototype.showWindow = function()
{
    this.openInfoWindowHtml(createInfoWindow(this.islp, this.labelText, this.jiage, this.img, this.lpurl, this.address, this.opendate, this.phone, this.pricetypetagname));
   //GEvent.trigger(this,"click",this);
};
LabeledMarker.prototype.mouseover = function()
{
   this.div.style.zIndex = this.div.style.zIndex+100000;
   this.setImage( '/static/PubImg/m1.png'); this.div.style.background="rgb(0, 153, 51)";
};
LabeledMarker.prototype.mouseout = function()
{
   this.div.style.zIndex = this.div.style.zIndex-100000;
   if(this.islp) {this.div.style.background="#FF9900";this.setImage( '/static/PubImg/m2.png');}else {this.div.style.background="#999999";this.setImage( '/static/PubImg/m3.png');};
};

