﻿// JScript 檔
if (typeof FWUI=="undefined") {var FWUI={};}
FWUI.sys = {}; 
var ua = navigator.userAgent.toLowerCase(); 
var s; 
(s = ua.match(/msie ([\d.]+)/)) ? FWUI.sys.ie = s[1] : 
(s = ua.match(/firefox\/([\d.]+)/)) ? FWUI.sys.firefox = s[1] : 
(s = ua.match(/chrome\/([\d.]+)/)) ? FWUI.sys.chrome = s[1] : 
(s = ua.match(/opera.([\d.]+)/)) ? FWUI.sys.opera = s[1] : 
(s = ua.match(/version\/([\d.]+).*safari/)) ? FWUI.sys.safari = s[1] : 0; 

/*
//Testing
if (FWUI.sys.ie) document.write('IE: ' + FWUI.sys.ie); 
if (FWUI.sys.firefox) document.write('Firefox: ' + FWUI.sys.firefox); 
if (FWUI.sys.chrome) document.write('Chrome: ' + FWUI.sys.chrome); 
if (FWUI.sys.opera) document.write('Opera: ' + FWUI.sys.opera); 
if (FWUI.sys.safari) document.write('Safari: ' + FWUI.sys.safari); 
*/

//FWUI.map={};
if (!FWUI.map) {FWUI.map={};}

FWUI.map.HOUSE_PHOTO_IN_ORIGINAL_PATH = "http://av.cthouse.com.tw/production/上傳位置";
FWUI.map.HOUSE_PHOTO_PATH = "http://av.cthouse.com.tw/production/上傳位置/house_thumbnail";

FWUI.map.HomePageWindowTemplate =
  '<table width="260" border="0" align="center" cellpadding="0" cellspacing="0">'+
  '  <tr>'+
  '    <td width="130"><table width="130" height="100" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#999999">'+
  '      <tr>'+
  //'        <td align="center" bgcolor="#009F94"><a href="#{WebRoot}/Showroom/HouseDetail.aspx?houseId=#{HouseId}"><img src="#{Photo1}" width="120" height="90" border="0" /></a></td>'+
  '        <td align="center" bgcolor="#009F94"><a href="javascript:popupHouseDetail(\'#{WebRoot}/Showroom/HouseDetail.aspx?houseId=#{HouseId}\');"><img src="#{Photo1}" width="120" height="90" border="0" /></a></td>'+
  '      </tr>'+
  '    </table></td>'+
  '    <td width="130"><table width="125" border="0" align="center" cellpadding="0" cellspacing="0">'+
  //'      <tr><td><a href="#{WebRoot}/Showroom/HouseDetail.aspx?houseId=#{HouseId}" class="Over12_redB">#{CaseName}</a></td>'+
  '      <tr><td><a href="javascript:popupHouseDetail(\'#{WebRoot}/Showroom/HouseDetail.aspx?houseId=#{HouseId}\');" class="Over12_redB">#{CaseName}</a></td>'+
  '      </tr>'+
  '      <tr><td align="left" style="font-size: 12px">'+
  //'        <br />#{Comment}'+
  '        #{Address}<br />'+
  '        #{HouseArea}坪 #{HtaName}<br />'+
  '        #{SellPrice}萬 #{LayoutString}<br />'+
  '      </td></tr>'+
  '      <tr>'+
  '        <td height="20"><a href="javascript:popupHouseDetail(\'#{WebRoot}/Showroom/HouseDetail.aspx?houseId=#{HouseId}\');"><img src="#{WebRoot}/images/icon_more.jpg" width="30" height="9" border="0" /></a></td>'+
  '      </tr>'+
  '    </table></td>'+
  '  </tr>'+
  '</table>';

FWUI.map.HouseDetailWindowTemplate =
  '<table width="130" border="0" align="center" cellpadding="0" cellspacing="0">'+
  '  <tr><td background="#{WebRoot}/images/search_map_bg.jpg">'+
  '    <table width="130" height="100" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#999999">'+
  //'      <tr><td align="center" bgcolor="#009F94"><a href="#{WebRoot}/Showroom/HouseDetail.aspx?houseId=#{HouseId}"><img src="#{Photo1}" width="120" height="90" border="0" /></a></td>'+
  '      <tr><td align="center" bgcolor="#009F94"><a href="javascript:popupHouseDetail(\'#{WebRoot}/Showroom/HouseDetail.aspx?houseId=#{HouseId}\');"><img src="#{Photo1}" width="120" height="90" border="0" /></a></td>'+
  '      </tr></table>'+
  '    <table width="100" border="0" cellspacing="0" cellpadding="0">'+
  '        <tr><td height="10"></td></tr>'+
  '    </table>'+
  '    <table width="130" border="0" align="center" cellpadding="0" cellspacing="0">'+
  //'      <tr><td><a href="#{WebRoot}/Showroom/HouseDetail.aspx?houseId=#{HouseId}" class="Over12_redB">#{CaseName}</a></td>'+
  '      <tr><td><a href="javascript:popupHouseDetail(\'#{WebRoot}/Showroom/HouseDetail.aspx?houseId=#{HouseId}\');" class="Over12_redB">#{CaseName}</a></td>'+
  '      </tr><tr>'+
  '        <td align="left" style="font-size: 12px">'+
  //'        #{Comment}<br />'+
  '        #{Address}<br />'+
  '        #{HouseArea}坪 #{HtaName}<br />'+
  '        #{SellPrice}萬 #{LayoutString}<br />'+
  '      </td></tr>'+
  '      <tr><td height="20" align="right"><a href="javascript:popupHouseDetail(\'#{WebRoot}/Showroom/HouseDetail.aspx?houseId=#{HouseId}\');"><img src="#{WebRoot}/images/icon_more.jpg" width="30" height="9" border="0" /></a></td>'+
  '      </tr></table></td>'+
  '  </tr>'+
  '</table>';

FWUI.map.HouseDetailNoLinkWindowTemplate =
  '<table width="130" border="0" align="center" cellpadding="0" cellspacing="0">'+
  '  <tr><td background="#{WebRoot}/images/search_map_bg.jpg">'+
  '    <table width="130" height="100" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#999999">'+
  '      <tr><td align="center" bgcolor="#009F94"><img src="#{Photo1}" width="120" height="90" border="0" /></td>'+
  '      </tr></table>'+
  '    <table width="100" border="0" cellspacing="0" cellpadding="0">'+
  '        <tr><td height="10"></td></tr>'+
  '    </table>'+
  '    <table width="130" border="0" align="center" cellpadding="0" cellspacing="0">'+
  '      <tr><td>#{CaseName}</td>'+
  '      </tr><tr>'+
  '        <td align="left">'+
  '        #{Address}<br />'+
  '       #{HouseArea}坪 #{HtaName}<br />#{UnitPriceStr}'+
  //'        #{HtcName}<br />'+
  '        #{SellPrice}萬 #{LayoutString}<br />'+
  '      </td></tr>'+
  '      <tr><td>&nbsp;</td></tr></table></td>'+
  '  </tr>'+
  '</table>';

FWUI.map.RentingHouseDetailWindowTemplate =
  '<table width="130" border="0" align="center" cellpadding="0" cellspacing="0">'+
  '  <tr><td background="#{WebRoot}/images/search_map_bg.jpg">'+
  '    <table width="130" height="100" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#999999">'+
  '      <tr><td align="center" bgcolor="#009F94"><a href="#{WebRoot}/Showroom/RentingHouseDetail.aspx?houseId=#{HouseId}"><img src="#{Photo1}" width="120" height="90" border="0" /></a></td>'+
  '      </tr></table>'+
  '    <table width="100" border="0" cellspacing="0" cellpadding="0">'+
  '        <tr><td height="10"></td></tr>'+
  '    </table>'+
  '    <table width="130" border="0" align="center" cellpadding="0" cellspacing="0">'+
  '      <tr><td><a href="#{WebRoot}/Showroom/RentingHouseDetail.aspx?houseId=#{HouseId}" class="Over12_redB">#{CaseName}</a></td>'+
  '      </tr><tr>'+
  '        <td align="left">'+
  //'        #{Comment}<br />'+
  '        #{Address}<br />'+
  '        #{HouseArea}坪 #{HtaName}<br />'+
  '        #{LeaseFee}萬 #{LayoutString}<br />'+
  '      </td></tr>'+
  '      <tr><td height="20" align="right"><a href="#{WebRoot}/Showroom/RentingHouseDetail.aspx?houseId=#{HouseId}"><img src="#{WebRoot}/images/icon_more.jpg" width="30" height="9" border="0" /></a></td>'+
  '      </tr></table></td>'+
  '  </tr>'+
  '</table>';

FWUI.map.FranchiseDetailWindowTemplate =
  '<table width="360" border="0" align="center" cellpadding="0" cellspacing="0">'+
  '  <tr>'+
  '    <td width="130"><table width="130" height="100" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#999999">'+
  '      <tr>'+
  '        <td align="center" bgcolor="#009F94"><a href="#{WebRoot}/AboutCompany/FranchiseDetail.aspx?franchiseId=#{FranchiseId}"><img src="#{Photo1}" width="120" height="90" border="0" /></a></td>'+
  '      </tr>'+
  '    </table></td>'+
  '    <td width="220"><table width="200" border="0" align="center" cellpadding="0" cellspacing="0">'+
  '      <tr><td><a href="#{WebRoot}/AboutCompany/FranchiseDetail.aspx?franchiseId=#{FranchiseId}" class="Over12_redB">#{FranchiseName}</a></td>'+
  '      </tr>'+
  '      <tr><td align="left">'+
  //'        團隊特質:#{TeamNature}<br />'+
  '        地址:#{Address}<br />'+
  '        電話:#{FranchiseTel}<br />'+
  '        傳真:#{FranchiseFax}<br />'+
  '        店長:#{CaptainName}<br />'+
  '        Email:<a href="mailto:#{Email}">#{Email}</a><br />'+
  '      </td></tr>'+
  '      <tr>'+
  '        <td height="20"><a href="#{WebRoot}/AboutCompany/FranchiseDetail.aspx?franchiseId=#{FranchiseId}"><img src="#{WebRoot}/images/icon_more.jpg" width="30" height="9" border="0" /></a></td>'+
  '      </tr>'+
  '    </table></td>'+
  '  </tr>'+
  '</table>';


FWUI.map.House = {HouseId: "", CaseName: "", Comment: "", Address: "", HouseArea: "",
                  HtaName: "", SellPrice: "", LeaseFee: "", LayoutString: "", Photo1: "",
                  Latitude: "", Longitude: "", WebRoot: ""};
	
FWUI.map.Franchise = {FranchiseId: "", FranchiseName: "", CaptainName: "", FranchiseTel: "", FranchiseFax: "",
                  Address: "", TeamNature: "", Email: "", CaptainMobile: "", Photo1: "",
                  Latitude: "", Longitude: "", WebRoot: ""};

function popupHouseDetail(url)
{
  //window.open(url, 'width=800,height=360');
  var houseDeatilWindow = window.open(url, 'houseDeatilWindow', '');
  houseDeatilWindow.focus();
}

function setMapType(mapObj, type)
{
  //電子地圖(U_MAP_TYPE)、衛星影像(U_SATELLITE_TYPE)及衛星地圖(U_HYBRID_TYPE)
  if (type == 1)
    mapObj.setMapType(U_MAP_TYPE)
  if (type == 2)
    mapObj.setMapType(U_SATELLITE_TYPE)
  if (type == 3)
    mapObj.setMapType(U_HYBRID_TYPE)
}

function createUMarker(fwuiHouseMarker)
{
	var marker = new UMarker(fwuiHouseMarker.uLatLng, fwuiHouseMarker.uIcon);
	if (isOpenOverlayManager)
	  marker = new UMarker(fwuiHouseMarker.uLatLng, fwuiHouseMarker.uIcon, fwuiHouseMarker.title);

	//marker.id = fwuiHouseMarker.id;
  marker.addListener('mouseover', function() {
	  marker.openInfoWindow(fwuiHouseMarker.desc);
  });
  return marker;
}

/** markers must be a collection of FWUI.map.HouseMarker **/
function addMarks(mapObj, markers, skipMarkers)
{
  if (skipMarkers) {
	  for (var i = 0; i < markers.length; i++) {
  	  if (isContainMarker(markers[i].id, skipMarkers))
  	      continue;
  	  else
		    mapObj.addOverlay(markers[i].uMarker);
	  }
  } else {
	  for (var i = 0; i < markers.length; i++) {
		  mapObj.addOverlay(markers[i].uMarker);
	  }
	}
}

/** markers must be a collection of FWUI.map.HouseMarker **/
function removeMarkers(mapObj, markers)
{
	for (var i = 0; i < markers.length; i++) {
		mapObj.removeOverlay(markers[i].uMarker);
	}
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
function removeFWUIMarker(markerId, targetMarkers)
{
  for (var i = 0; i < targetMarkers.length; i++)
   if (markerId == targetMarkers[i].id)
   {
     targetMarkers.remove(i);
     return;
   }
}
function refreshMarkers(mapObj, markers)
{
  removeMarkers(mapObj, markers);
  addMarks(mapObj, markers);
}
function isContainMarker(markerId, skipMarkers)
{
  for (var i = 0; i < skipMarkers.length; i++)
   if (markerId == skipMarkers[i].id)
     return true;
  return false;
}
function createUIcon(imgSrc, iconWidth, iconHeight)
{
  var uIcon = new UIcon();
  uIcon.image = imgSrc;
  uIcon.iconWidth = iconWidth;
  uIcon.iconHeight = iconHeight;
  uIcon.iconAnchor = new UPoint(Math.round(iconWidth/2), iconHeight);
  uIcon.infoWindowAnchor = new UPoint(Math.round(iconWidth/2), 0);
  return uIcon;
}
// 函式：用來產生一個使用自訂圖示的房屋物件地標
FWUI.map.HouseMarker = function (houseJson, id, lat, lng, addrs, uIcon, desc){
    this.init(houseJson, id, lat, lng, addrs, uIcon, desc);
};
FWUI.map.HouseMarker.prototype = {
	id: null,
	title: null,
  address: null,
  longitude: null,
  latitude: null,
  desc: null,
  uIcon: null,
  uLatLng: null,
  uMarker: null,
  windowTemplate: "",
  unitPrice: 0,
  unitPriceStr: "",
  houseJsonObj: FWUI.map.House,
  webRoot: "",
	
	init: function(houseJson, id, lat, lng, addrs, uIcon, desc)
  {
    if(houseJson)
    {
      this.id = houseJson.HouseId;
      this.title = houseJson.CaseName;
      this.address = houseJson.Address;
      this.latitude = houseJson.Latitude;
      this.longitude = houseJson.Longitude;
      this.houseJsonObj = houseJson;
      if (houseJson.Photo1 == "")
        this.houseJsonObj.Photo1 = this.houseJsonObj.WebRoot+"/images/no_200x150.jpg";
      else
        this.houseJsonObj.Photo1 = FWUI.map.HOUSE_PHOTO_PATH+this.houseJsonObj.Photo1;
      this.desc = "<b>"+houseJson.CaseName+"<b><br/>\n"+houseJson.Address+"<br/>\n"+houseJson.Comment;
      this.webRoot = this.houseJsonObj.WebRoot;
      //add for houseSelldeal
      if (houseJson.UnitPrice)
      {
        this.unitPrice = houseJson.UnitPrice;
        this.unitPriceStr = "單價: "+this.unitPrice+"萬<br/>\n";
        this.houseJsonObj.UnitPriceStr = this.unitPriceStr;
      } else {
        this.houseJsonObj.UnitPriceStr = "";
      }
    } else {
      this.id = id;
      this.address = addrs;
      this.latitude = lat;
      this.longitude = lng;
      this.desc = desc;
    }
    //UIcon
    if (uIcon)
    {
      this.uIcon = uIcon
    } else {
      this.uIcon = new UIcon();
	    this.uIcon.image = this.webRoot+'/images/icon_red03.gif';
	    this.uIcon.iconWidth = 25;
	    this.uIcon.iconHeight = 23;
	    this.uIcon.iconAnchor = new UPoint(17, 23);
	    this.uIcon.infoWindowAnchor = new UPoint(13, 0);
	  }
    //ULatLng
    this.uLatLng = new ULatLng(this.latitude, this.longitude);
    //UMarker
		this.uMarker = this.createMarker(this);
  },
  setUicon: function(newUicon)
  {
    if (newUicon)
    {
      this.uIcon = newUicon;
      this.uMarker = this.createMarker(this);
    }
  },
  createMarker: function(self)
  {
    var marker = new UMarker(self.uLatLng, self.uIcon);
    if (isOpenOverlayManager)
      marker = new UMarker(self.uLatLng, self.uIcon, self.title);
		
		marker.id = self.id;
	  marker.addListener('mouseover', function() {
		  marker.openInfoWindow(self.desc);
	  });
	  return marker;
	},
  showWindow: function()
  {
    var template = new Template(this.windowTemplate);
    this.uMarker.openInfoWindow(template.evaluate(this.houseJsonObj), 160, 220);
  }
};

/*
 * extent HouseMarker for HouseDetailPage
 */
FWUI.map.HouseDetailMarker = function (houseJson, id, lat, lng, addrs, uIcon, desc){
  //customize properties
  this.windowTemplate = FWUI.map.HouseDetailWindowTemplate;
  this.init(houseJson, id, lat, lng, addrs, uIcon, desc);  
};
FWUI.map.HouseDetailMarker.prototype = new FWUI.map.HouseMarker;

// override windowConfig
FWUI.map.HouseDetailMarker.prototype.createMarker = function(self) {
  var marker = new UMarker(self.uLatLng, self.uIcon);
  if (isOpenOverlayManager)
    marker = new UMarker(self.uLatLng, self.uIcon, self.title);
  
  marker.id = self.id;
  marker.addListener('mouseover', function() {
    var template = new Template(self.windowTemplate);
    //alert(self.houseJsonObj.HouseId);
    var content = template.evaluate(self.houseJsonObj);
    marker.openInfoWindow(content, 160, 220);
  });
  return marker;
};
FWUI.map.HouseDetailMarker.prototype.showWindow = function()
{
  var template = new Template(this.windowTemplate);
  this.uMarker.openInfoWindow(template.evaluate(this.houseJsonObj), 160, 220);
};

/*
 * extent HouseDetailMarker for HouseDetailNoLinkMarker
 */
FWUI.map.HouseDetailNoLinkMarker = function (houseJson, id, lat, lng, addrs, uIcon, desc){
  //customize properties
  this.windowTemplate = FWUI.map.HouseDetailNoLinkWindowTemplate;
  this.init(houseJson, id, lat, lng, addrs, uIcon, desc);  
};
FWUI.map.HouseDetailNoLinkMarker.prototype = new FWUI.map.HouseDetailMarker;
FWUI.map.HouseDetailMarker.prototype.createMarker = function(self) {
  var marker = new UMarker(self.uLatLng, self.uIcon);
  if (isOpenOverlayManager)
    marker = new UMarker(self.uLatLng, self.uIcon, self.title);
  
  marker.id = self.id;
  marker.addListener('mouseover', function() {
    var template = new Template(self.windowTemplate);
    var content = template.evaluate(self.houseJsonObj);
    marker.openInfoWindow(content, 160, 220);
  });
  return marker;
};

/*
 * extent HouseDetailMarker for RentingHouseDetailMarker
 */
FWUI.map.RentingHouseDetailMarker = function (houseJson, id, lat, lng, addrs, uIcon, desc){
  //customize properties
  this.windowTemplate = FWUI.map.RentingHouseDetailWindowTemplate;
  this.init(houseJson, id, lat, lng, addrs, uIcon, desc);  
};
FWUI.map.RentingHouseDetailMarker.prototype = new FWUI.map.HouseDetailMarker;

/*
 * extent HouseMarker for HomePage
 */
FWUI.map.HomePageMarker = function (houseJson, id, lat, lng, addrs, uIcon, desc){
  //customize properties
  this.windowTemplate = FWUI.map.HomePageWindowTemplate;
  this.init(houseJson, id, lat, lng, addrs, uIcon, desc);
};
FWUI.map.HomePageMarker.prototype = new FWUI.map.HouseMarker;

// override windowConfig
FWUI.map.HomePageMarker.prototype.createMarker = function(self) {
  var marker = new UMarker(self.uLatLng, self.uIcon);
  if (isOpenOverlayManager)
    marker = new UMarker(self.uLatLng, self.uIcon, self.title);
  marker.id = self.id;
  marker.addListener('mouseover', function() {
    var template = new Template(self.windowTemplate);
    var content = template.evaluate(self.houseJsonObj);
    marker.openInfoWindow(content, 300, 110);
  });
  return marker;
};
FWUI.map.HomePageMarker.prototype.showWindow = function()
{
  var template = new Template(this.windowTemplate);
  this.uMarker.openInfoWindow(template.evaluate(this.houseJsonObj), 300, 110);
};

/*
 * extent HouseDetailMarker for Franchise
 */
FWUI.map.FranchiseDetailMarker = function (franchiseJson, id, lat, lng, addrs, uIcon, desc){
  //customize properties
  this.windowTemplate = FWUI.map.FranchiseDetailWindowTemplate;
  this.init(franchiseJson, id, lat, lng, addrs, uIcon, desc);
};
FWUI.map.FranchiseDetailMarker.prototype = new FWUI.map.HouseDetailMarker;

// override windowConfig
FWUI.map.FranchiseDetailMarker.prototype.init = function(franchiseJson, id, lat, lng, addrs, uIcon, desc)
{
  if(franchiseJson)
  {
    this.id = franchiseJson.HouseId;
    this.address = franchiseJson.Address;
    this.latitude = franchiseJson.Latitude;
    this.longitude = franchiseJson.Longitude;
    this.houseJsonObj = franchiseJson;
    if (franchiseJson.Photo1 == "")
      this.houseJsonObj.Photo1 = this.houseJsonObj.WebRoot+"/images/no_200x150.jpg";
    else
      this.houseJsonObj.Photo1 = FWUI.map.HOUSE_PHOTO_IN_ORIGINAL_PATH+this.houseJsonObj.Photo1;
    this.desc = "<b>"+franchiseJson.FranchiseName+"<b><br/>\n"+franchiseJson.Address+"<br/>\n";
    this.webRoot = this.houseJsonObj.WebRoot;
  } else {
    this.id = id;
    this.address = addrs;
    this.latitude = lat;
    this.longitude = lng;
    this.desc = desc;
  }
  //UIcon
  if (uIcon)
  {
    this.uIcon = uIcon
  } else {
    this.uIcon = new UIcon();
    this.uIcon.image = this.webRoot+'/images/icon_green.gif';
	    this.uIcon.iconWidth = 25;
	    this.uIcon.iconHeight = 23;
    this.uIcon.iconAnchor = new UPoint(13, 30);
    this.uIcon.infoWindowAnchor = new UPoint(13, 0);
  }
  this.uLatLng = new ULatLng(this.latitude, this.longitude);
	this.uMarker = this.createMarker(this);
};
FWUI.map.FranchiseDetailMarker.prototype.createMarker = function(self) {
  var marker = new UMarker(self.uLatLng, self.uIcon);
  if (isOpenOverlayManager)
    marker = new UMarker(self.uLatLng, self.uIcon, self.title);
  
  marker.id = self.id;
  marker.addListener('click', function() {
    var template = new Template(self.windowTemplate);
    var content = template.evaluate(self.houseJsonObj);
    marker.openInfoWindow(content, 370, 125);
  });
  return marker;
};
FWUI.map.FranchiseDetailMarker.prototype.showWindow = function()
{
  var template = new Template(this.windowTemplate);
  this.uMarker.openInfoWindow(template.evaluate(this.houseJsonObj), 370, 125);
};


FWUI.map.Asppoi = {DataId:"", AId:"", Label:"", Address:"", Link:"", Tel:"", City:"", Town:"", Lng:"", Lat:"", AtId:"", AZipCode:"", AtName:"", WebRoot:"/"};
/*
 * 函式：用來產生一個使用自訂圖示的基礎物件地標
 */
FWUI.map.AsppoiMarker = function (asppoiJson, dataId, asppoiId, addrs, lng, lat, label, link, tel, city, town, uIcon){
    this.init(asppoiJson, dataId, asppoiId, addrs, lng, lat, label, link, tel, city, town, uIcon);
};
FWUI.map.AsppoiMarker.prototype = {
	dataId: null,
  uIcon: null,
  uLatLng: null,
  uMarker: null,
  longitude: null,
  latitude: null,
  //Asppoi object fields
  asppoiId: null,
  label: null,
  address: null,
  link: '',
  tel: '',
  city: '',
  town: '',
  atId: '',
  aZipCode: '',
  atName: null,
  webRoot: '/',
  windowWidth: 116,
  windowHigh: 48,  
  infoWindowDesc: '',	
	init: function(asppoiJson, dataId, asppoiId, addrs, lng, lat, label, link, tel, city, town, uIcon)
  {
    if (asppoiJson)
    {
      this.dataId = asppoiJson.DataId;
      this.asppoiId = asppoiJson.AId;
      this.label = asppoiJson.Label;
      this.address = asppoiJson.Address;
      this.link = asppoiJson.Link;
      this.tel = asppoiJson.Tel;
      this.city = asppoiJson.City;
      this.town = asppoiJson.Town;
      this.longitude = asppoiJson.Lng;
      this.latitude = asppoiJson.Lat;
      this.atId = asppoiJson.AtId;
      this.aZipCode = asppoiJson.AZipCode;
      this.atName = asppoiJson.AtName;
      this.webRoot = asppoiJson.WebRoot;
      
      if (this.atId == '416') //416	各級學校
      {
        this.windowWidth = 136;
        this.windowHigh = 48;
      }
      else if (this.atId == '518') //518	公園
      {
        this.windowWidth = 100;
        this.windowHigh = 35;
      }
    } else {
      this.dataId = dataId;
      this.address = addrs;
      this.longitude = lng;
      this.latitude = lat;

      this.asppoiId = asppoiId;
      this.label = label;
      if (link)
        this.link = link;
      if (tel)
        this.tel = tel;
      if (city)
        this.city = city;
      if (town)
        this.town = town;
    }  
 	  this.infoWindowDesc = '<span style="font-size:9px"><b>' + this.label + '</b>';
	  if (this.link != '')
	    this.infoWindowDesc += '<br/><a href="'+this.link+'">'+ this.address + '</a>';
	  else
	    this.infoWindowDesc += '<br/>'+this.address;
	  if (this.tel != '')
	      this.infoWindowDesc += '<br/>Tel: '+this.tel;
	  //if (this.city != '')
	  //    this.infoWindowDesc += '<br/>'+this.city+this.town;
    this.infoWindowDesc +='</span>';
    //UIcon
    if (uIcon)
    {
      this.uIcon = uIcon
    } else {
      this.uIcon = new UIcon();
      this.uIcon.image = this.webRoot+'/images/icon_green.gif';
	    this.uIcon.iconWidth = 25;
	    this.uIcon.iconHeight = 23;
	    this.uIcon.iconAnchor = new UPoint(13, 30);
	    this.uIcon.infoWindowAnchor = new UPoint(13, 0);
	  }
    //ULatLng
    this.uLatLng = new ULatLng(this.latitude, this.longitude)
    //UMarker
	  this.uMarker = this.createMarker(this);	  
  },
  setUicon: function(newUicon)
  {
    if (newUicon)
    {
      this.uIcon = newUicon;
      this.uMarker = this.createMarker(this);
    }
  },
  createMarker: function(self)
  {
    var marker = new UMarker(self.uLatLng, self.uIcon);
    if (isOpenOverlayManager)
      marker = new UMarker(self.uLatLng, self.uIcon, self.title);
    marker.id = self.asppoiId;
	  marker.addListener('mouseover', function() {
		  marker.openInfoWindow(self.infoWindowDesc, self.windowWidth, self.windowHigh);
	  });
	  return marker;
  },
  showWindow: function()
  {
    this.uMarker.openInfoWindow(this.infoWindowDesc, this.windowWidth, this.windowHigh);
  }
};


function getNorthEast(wd1, jd1, length)
{
  //var jla = (111.133 - 0.559 * Math.cos(2 * wd1)) * 1000;
  //var wla = (111.413 * Math.cos(wd1) + 0.094 * Math.cos(3 * wd1)) * 1000;
  var jl = 111133; //緯度一度約111.133 km  
  var wl = 111413; //經度一度約111.413 km
  //window.status="[jl: "+jl+", wl: "+wl +"] , [jla: "+jla+", wla: "+wla+"]" + "{jl-jla: "+(jl-jla)+", wl-wla: "+(wl-wla)+"}, wlb: "+wlb;

  var wd2 = wd1 + (length / jl);
  var jd2 = jd1 + (length / wl);
  //alert(wd1+", "+ jd1+", length= "+length+"\n"+wd2+", "+jd2+", n= "+n);
  return new ULatLng(wd2, jd2);
}
function getSouthWest(wd1, jd1, length)
{
  var jl = 111133; //緯度一度約111.133 km
  var wl = 111413; //經度一度約111.413 km
  var wd2 = wd1 - (length / jl);
  var jd2 = jd1 - (length / wl);
  return new ULatLng(wd2, jd2);
}

// 函式：用來產生一個正方形
FWUI.map.SquareBounds = function (centerLatLng, length){
    this.init(centerLatLng, length);
};

FWUI.map.SquareBounds.prototype = {
  southWest: null,
  northEast: null,
  bounds: null,
  swLat: null,
  swLng: null,
  neLat: null,
  neLng: null,
  southEast: null,
  northWest: null,
  latLngs: null,  
  msg: null,
  
	init: function(centerLatLng, length){
	  this.southWest = getSouthWest(centerLatLng.lat(), centerLatLng.lng(), length);
    this.northEast = getNorthEast(centerLatLng.lat(), centerLatLng.lng(), length);
    this.bounds = new UBounds(this.southWest, this.northEast);

    this.swLat = this.southWest.lat();
    this.swLng = this.southWest.lng();
    this.neLat = this.northEast.lat();
    this.neLng = this.northEast.lng();

    this.southEast = new ULatLng(this.southWest.lat() + this.bounds.getWidth(), this.southWest.lng());
    this.northWest = new ULatLng(this.northEast.lat() - this.bounds.getWidth(), this.northEast.lng());

    this.latLngs = [this.southWest, this.southEast, this.northEast, this.northWest];
    this.msg = 
      "bounds.getSouthWest(): "+this.bounds.getSouthWest().lat()+", "+this.bounds.getSouthWest().lng()+"\n"+
      "bounds.getNorthEast(): "+this.bounds.getNorthEast().lat()+", "+this.bounds.getNorthEast().lng()+"\n"+
      "bounds.getWidth(): "+this.bounds.getWidth()+"\n"+
      "bounds.getHeight(): "+this.bounds.getHeight()+"\n"+
      "bounds.getCenter(): "+this.bounds.getCenter().lat()+", "+this.bounds.getCenter().lng()+"<br/>";
    /*
    alert("latLngs.length: "+latLngs.length+"\n"+
      "southEast: "+southEast.lat()+", "+southEast.lng()+"\n"+
      "northWest: "+northWest.lat()+", "+northWest.lng()
    );
    */
 
    if (document.getElementById('msg'))
		  document.getElementById('msg').innerHTML = msg;  
  },
  draw: function(mapObj, boderCollor, boderWidth)
  {
    var bc = (boderCollor)? boderCollor: 'blue';
    var bw = (boderWidth)? boderWidth: 1;
    //UPolygon(latlngs, borderColor?, borderWeight?, borderOpacity?, fillColor?, fillOpacity?);
    var polygon = new UPolygon(this.latLngs, bc, bw);
    if (mapObj)
      mapObj.addOverlay(polygon);
    else if (map)
      map.addOverlay(polygon);    
  }
}


// 函式：用來產生一個成交行情房屋標示物件地標
FWUI.map.StatisticHouseMarker = function (statisticHouseJson){
    this.init(statisticHouseJson);
};
FWUI.map.StatisticHouseMarker.prototype = {
	id: null,
  address: null,
  unitPrice: 0,
  grades: null,
  section: null,
  longitude: null,
  latitude: null,
  houseJsonObj: null,
  desc: null,
  uIcon: null,
  uLatLng: null,
  uMarker: null,
  windowTemplate: "",
  webRoot: "",
	
	init: function(houseJson)
  {
    if(houseJson)
    {
      this.id = houseJson.HouseId;
      this.unitPrice = houseJson.UnitPrice;
      this.address = houseJson.Address;
      this.grades = houseJson.Grades;
      this.section = houseJson.Section;
      this.latitude = houseJson.Latitude;
      this.longitude = houseJson.Longitude;
      this.houseJsonObj = houseJson;
      this.desc = "單價: "+houseJson.UnitPrice+"萬<br/>"+houseJson.Address;
      this.webRoot = this.houseJsonObj.WebRoot;
      this.uIcon = new UIcon();
	    this.uIcon.image = this.webRoot+'/images/icon_green.gif';
	    this.uIcon.iconWidth = 25;
	    this.uIcon.iconHeight = 23;
	    this.uIcon.iconAnchor = new UPoint(13, 30);
	    this.uIcon.infoWindowAnchor = new UPoint(13, 0);
	  }
    //ULatLng
    this.uLatLng = new ULatLng(this.latitude, this.longitude);
    //UMarker
		this.uMarker = this.createMarker(this);
  },
  setUicon: function(newUicon)
  {
    if (newUicon)
    {
      this.uIcon = newUicon;
      this.uMarker = this.createMarker(this);
    }
  },
  createMarker: function(self)
  {
    var marker = new UMarker(self.uLatLng, self.uIcon);
    if (isOpenOverlayManager)
      marker = new UMarker(self.uLatLng, self.uIcon, self.title);
		marker.id = self.id;
	  marker.addListener('click', function() {
		  marker.openInfoWindow(self.desc, 100, 50);
	  });
	  return marker;
	},
  showWindow: function()
  {
    //var template = new Template(this.windowTemplate);
    //this.uMarker.openInfoWindow(template.evaluate(this.houseJsonObj), 100, 50);
    this.uMarker.openInfoWindow(this.desc, 100, 50);
  }
};

/*None module functions*/
//FWUI.map.Map = {};
var map;
var shiftDownLat = 20 * 0.000009259; //中心點向下移動20m
function initMap(houseMarkers, mapDivName, dontShowFirst, umapZoomControl){
  if (mapDivName)
    map = new UMap(document.getElementById(mapDivName));
  else
    map = new UMap(document.getElementById('map'));

	//map.setControlType(U_FULL_CONTROL);
  if (umapZoomControl)
  	map.addControl(umapZoomControl);
  else
  	map.addControl(U_FULLZOOM_CONTROL);

	//map.addControl(U_MINIZOOM_CONTROL);
	//map.addControl(U_TYPE_CONTROL);
	map.addControl(U_SCALE_CONTROL);
	
  //alert("initMap: "+map);
 	if (houseMarkers && houseMarkers.length > 0) {
  	//alert("houseMarkers.length: "+houseMarkers.length);
  	// 加入所有的地標
  	addMarks(map, houseMarkers);
  	//alert("after addMarks(map, houseMarkers);");
  	var _level = 9;
  	//_level = fitMarkersToMapZoomLevel(map, houseMarkers);
		//alert("_level: "+ _level);
		//var tmpLatLng = new ULatLng(houseMarkers[0].uLatLng.lat() + shiftDownLat, houseMarkers[0].uLatLng.lng());
		var tmpLatLng = new ULatLng(houseMarkers[0].uLatLng.lat(), houseMarkers[0].uLatLng.lng());
    map.centerAndZoom(tmpLatLng, _level);
    //OpenInfoWindow at first time
    if (!dontShowFirst)
      houseMarkers[0].showWindow();    
	} else {
    map.centerAndZoom(new ULatLng(25.04756754722678, 121.51727647120187), 8); //台北市
	}
}

function fitMarkersToMapZoomLevel(uMap, markers)
{
  var bounds = uMap.getBounds();
  var _level = 8;
  for (var i = 0; i < markers.length; i++) {
 	  bounds.extend(markers[i].uLatLng);
 	}
 	//使用 getBoundsZoomLevel(bounds) 取得地圖的顯示比例
	_level = uMap.getBoundsZoomLevel(bounds);
  return _level;
}


FWUI.map.IsSmallMap = true;
var small = FWUI.map.IsSmallMap;
function switchMapSize(tblMap, tdMapArea, tdHouseArea, mapDiv, imgArrow, houseMarkers, WebRoot)
{
  var tblMap = document.getElementById(tblMap);
  var tdMapArea = document.getElementById(tdMapArea);
  var tdHouseArea = document.getElementById(tdHouseArea);
  var mapDiv = document.getElementById(mapDiv);
  var imgArrow = document.getElementById(imgArrow);
  small = !small;
  if (small)
  {
    tblMap.style.width = "280px";
    tdMapArea.style.width = "310px";
    //tdMapArea.background= WebRoot+"/images/search_map_bg1.gif";
    tdHouseArea.style.width = "440px";
    tdHouseArea.style.display = "block";
    mapDiv.style.width="280px";
    imgArrow.style.src= "url("+WebRoot+"/images/search_map3.jpg)";
    imgArrow.onmouseout="MM_swapImgRestore()";
    imgArrow.onmouseover="MM_swapImage('imgArrow','','"+WebRoot+"/images/search_map3o.jpg',8)"
    imgArrow.alt="展開";
  } else {
    tblMap.style.width = "720px";
    tdMapArea.style.width = "750px";
    //tdMapArea.background= WebRoot+"/images/search_map770.gif";
    tdHouseArea.style.width = "0px";
    tdHouseArea.style.display = "none";
    mapDiv.style.width="720px";
    imgArrow.style.src= "url("+WebRoot+"/images/search_map5.jpg)";
    imgArrow.onmouseout="MM_swapImgRestore()";
    imgArrow.onmouseover="MM_swapImage('imgArrow','','"+WebRoot+"/images/search_map5o.jpg',9)"
    imgArrow.alt="縮小";
    //alert("imgArrow: "+imgArrow+", imgArrow.src: "+imgArrow.src);
  }
  //alert("imgArrow.src: "+imgArrow.src);
 	if (houseMarkers.length > 0) {
   	map.centerAndZoom(houseMarkers[0].uLatLng, 9);
    houseMarkers[0].showWindow();
    houseMarkers[0].showWindow();
	} else {
    map.centerAndZoom(new ULatLng(25.04756754722678, 121.51727647120187), 8); //台北市
	}
  map.checkResize();  
}

function switchMapSize2(tblMap, tdMapArea, tdHouseArea, mapDiv, imgArrow, houseMarkers, WebRoot)
{
  var tblMap = document.getElementById(tblMap);
  var tdMapArea = document.getElementById(tdMapArea);
  var tdHouseArea = document.getElementById(tdHouseArea);
  var mapDiv = document.getElementById(mapDiv);
  var imgArrow = document.getElementById(imgArrow);
  small = !small;
  if (small)
  {
    tblMap.style.width = "280px";
    tdMapArea.style.width = "290px";
    //tdMapArea.background= WebRoot+"/images/search_map_bg1.gif";
    tdHouseArea.style.width = "380px";
    tdHouseArea.style.display = "block";
    mapDiv.style.width="260px";
    imgArrow.style.src= "url("+WebRoot+"/images/search_map3.jpg)";
    imgArrow.onmouseout="MM_swapImgRestore()";
    imgArrow.onmouseover="MM_swapImage('imgArrow','','"+WebRoot+"/images/search_map3o.jpg',8)"
    imgArrow.alt="展開";
  } else {
    tblMap.style.width = "640px";
    tdMapArea.style.width = "670px";
    //tdMapArea.background= WebRoot+"/images/search_map770.gif";
    tdHouseArea.style.width = "0px";
    tdHouseArea.style.display = "none";
    mapDiv.style.width="640px";
    imgArrow.style.src= "url("+WebRoot+"/images/search_map5.jpg)";
    imgArrow.onmouseout="MM_swapImgRestore()";
    imgArrow.onmouseover="MM_swapImage('imgArrow','','"+WebRoot+"/images/search_map5o.jpg',9)"
    imgArrow.alt="縮小";
    //alert("imgArrow: "+imgArrow+", imgArrow.src: "+imgArrow.src);
  }
  //alert("imgArrow.src: "+imgArrow.src);
 	if (houseMarkers.length > 0) {
   	map.centerAndZoom(houseMarkers[0].uLatLng, 9);
    houseMarkers[0].showWindow();
    houseMarkers[0].showWindow();
	} else {
    map.centerAndZoom(new ULatLng(25.04756754722678, 121.51727647120187), 8); //台北市
	}
  map.checkResize();  
}

function showHouseInfoWindow(houseId)
{
  if (houseMarkers && houseMarkers.length > 0)
    for (i = 0; i < houseMarkers.length; i++)
    {
      if (houseMarkers[i].id == houseId)
      {
        houseMarkers[i].showWindow();
        //map.checkResize();  
        houseMarkers[i].showWindow();
        break;
      }
    }
}

//for 2.0 new function,  add by Andy Chen, 2009/10/13
//isOpenOverlayManager is referenced for open OverlayManager functions
var isOpenOverlayManager = false;
function buildOverlayManager(fwuiHouseMarkers)
{
  if (!isOpenOverlayManager) return;
  //alert("buildOverlayManager(fwuiHouseMarkers), WebRoot: "+WebRoot);
  var overlayManager;
  var uHouseMarkers = new Array();
  var myMergeIcon = createUIcon(_WEBROOT+'/images/icon_merge.gif', 40, 41);
  var overlayOpts = {minLevel:2, maxLevel:10, autoMergeMarker:true, mergeIcon: myMergeIcon, callback:function(){alert('reloaded!');}};
  //new 一個UOverlayManager物件，並設定最小顯示層級為6，最大顯示層級為10，當重新載入地標時alert('reloaded')
  var overlayManager = new UOverlayManager(map, overlayOpts);
  for (i = 0; i< fwuiHouseMarkers.length; i++)
  {
    //var myHouseMarker = fwuiHouseMarkers[i];
    //uHouseMarkers.push(createUMarker(myHouseMarker));
    uHouseMarkers.push(fwuiHouseMarkers[i].uMarker);
  }
  if (fwuiHouseMarkers && fwuiHouseMarkers.length > 0)
  {
    //alert("uHouseMarkers.length: "+uHouseMarkers.length);
    //將marker陣列增加至manager裡
    overlayManager.add(uHouseMarkers);
    //必須呼叫refresh才會將overlay放到地圖上
    overlayManager.refresh();
  }
}