var UadTree;
var UpoiTree;
var UpoiSource;
var UpoiType;
var Upois = new Array();
var UpoiThread = 0;
var UIcons = new Object();
var UdisplayedActiveAd = new Object();
var UactivePoi;
var curMarker;
var theHtml='';

function customInit() {
  //change default search address show
  //UexValues =  ['範例：台北市內湖區瑞光路188巷46號7樓', '範例：仁愛路二段', '範例：忠孝東路 基隆路', '範例：總統府'];
  
  /* initialize tab content */
  UpoiTree = new treelist(U.gi('poiDiv'), poiTypeClick, false, 'haleluya_Tree.xml');
  /* initialize tab pane */
  _Utabpane.addTab('poi', '分類查詢', 'poiDiv', 0);
  //_Utabpane.addTab('detail', '服務資料內容', 'detailDiv', 1);
  _Utabpane.changeTabLabel('list', '地圖搜尋結果');  
  _Utabpane.selectTab('poi');
  var banner = document.createElement('div');
  
  U.gi('poiDiv').appendChild(banner);
  /* initialize main function */
  
  /* initialize event */
  _Umap.addListener('zoom', mapChange);
  _Umap.addListener('moveend', mapChange);
  _Umap.addListener('click', mapClick);
  
  /* initialize icon */
  UadIcon = new UIcon();
  UadIcon.image = 'default.gif';
  UadIcon.iconWidth = 20;
  UadIcon.iconHeight = 20;
  UadIcon.iconAnchor = new UPoint(10, 10);
  UadIcon.infoWindowAnchor = new UPoint(10, 1);
  _Umap.centerAndZoom(new ULatLng(25.04, 121.55), 9);
    
  //測距
  if(U.gi('measureLink'))
    U.gi('measureLink').onclick = function() {_Umap.setMapAction(U_MEASURE_ACTION);};
  
  UIconImagePath = 'http://apam.urmap.com/apam/';
  //UIconImagePath = 'http://192.168.10.234/apam/';
  

}

function poiTypeClick(node) {
  if(node.isSelected()) {
    var data = node.data.split(';');
    getPoi(data[0], data[1]);
  } else {
    UpoiType = null;
    clearPoi();
  }
}

function mapChange() {
  clearTimeout(UpoiThread);
  if(_Umap.getZoomLevel()<7)
    clearPoi();
  else {
    UpoiThread = setTimeout(function() {getPoi(UpoiSource, UpoiType);}, 1000);
  }
}

function mapClick(overlay, latlng) {
  if(overlay) {
    if(!overlay.isPoi)
      return;
    curMarker = overlay;
    theHtml = '';
    var isData = false;
    //若有廣告資料, 顯示廣告窗與內容
    if( parseInt(overlay.poiType)%2 == 0 )  {
        SearchBean.getAdvert(overlay.contentId, showDetailContent);
        isData = true;
    }  else  {
        for(var i=0 ; i < Upois.length ; i++)  {
            if(Upois[i].poiType != overlay.poiType && Upois[i].label == overlay.label)  {
                SearchBean.getAdvert(Upois[i].contentId, showDetailContent);
                isData = true;
            } 
        }
    }
    
    if(!isData)  {
      overlay.openInfoWindow('<b>'+overlay.label+'</b><br>'+overlay.desc+'<p><a href="javascript:void(0);" id="setStartLink">設為起點</a> | <a href="javascript:void(0);" id="setEndLink">設為終點</a></p>');
      U.gi('setStartLink').onclick = function() {_setRoutingStartLocation(overlay.getPoint(), overlay.label);};
      U.gi('setEndLink').onclick = function() {_setRoutingEndLocation(overlay.getPoint(), overlay.label);};
    }
  }
}

//query backend
function getPoi(poiSource, poiType) {
  UpoiSource = poiSource;
  UpoiType = poiType;
  if(!UpoiType) {
    clearPoi();
    return;
  }
  if(_Umap.getZoomLevel()<7)
    return;
  var center = _Umap.getCenter();
  var bounds = _Umap.getBounds();
  if(UpoiSource=='asp')
    SearchBean.findAspPoi(center.x(), center.y(), bounds.getWidth(U_TM2_COORDSYS), bounds.getHeight(U_TM2_COORDSYS), UpoiType, false, getAspPoiCallback);
}

function getAspPoiCallback(result) {
  clearPoi();
  var da = result.split(';');
  for(i=0; i<da.length; i++) {
    var at = da[i].split(',');
    if(at.length<13)
      continue;
    var icon = new UIcon();
    icon.image = UIconImagePath+at[3];
    icon.iconWidth = parseInt(at[11]);
    icon.iconHeight = parseInt(at[12]);
    icon.iconAnchor = new UPoint(Math.round(icon.iconWidth/2), Math.round(icon.iconHeight/2));
    icon.infoWindowAnchor = new UPoint(Math.round(icon.iconWidth/2), 0);
    var poi = new UMarker(new ULatLng(parseInt(at[2]), parseInt(at[1]), U_TM2_COORDSYS), icon, at[4], at[5]);
    poi.pid = at[0];
    poi.label = at[4];
    poi.desc = at[5];
    poi.isPoi = true;
    poi.passiveAction = at[6];
    poi.extLink = at[7];
    poi.activeAction = at[8];
    poi.contentId = at[9];
    poi.poiType= at[10];
    Upois.push(poi);
    _Umap.addOverlay(poi);
    if(poi.activeAction!='0' && !UdisplayedActiveAd[poi.contentId]) {
      UactivePoi = poi;
      UdisplayedActiveAd[poi.contentId] = 'displayed';
      if(poi.contentId=='0')
        SearchBean.getAsppoiContent(poi.pid, showPopupContent);
      else
        SearchBean.getAdvert(poi.contentId, showPopupContent);
    }
  }
}

function clearPoi() {
  while(Upois.length>0)
    _Umap.removeOverlay(Upois.pop());
}

function showDetailContent(result) {
//alert(curMarker.poiType);    
    theHtml = '<b>'+curMarker.label+'</b><br>'+curMarker.desc;
    if(result && result != '')  {
    	theHtml += '<div id=\"advert\" style=\"width:210;height:120; \">';      
    	theHtml += result;
        theHtml += '</div>';        
    }
    theHtml += '<p><a href=\"javascript:void(0);\" id=\"setStartLink1\">設為起點</a> '
            + '| <a href=\"javascript:void(0);\" id=\"setEndLink1\">設為終點</a></p>';
            
    curMarker.openInfoWindow(theHtml);
    U.gi('setStartLink1').onclick = function() {_setRoutingStartLocation(curMarker.getPoint(), curMarker.label);};
    U.gi('setEndLink1').onclick = function() {_setRoutingEndLocation(curMarker.getPoint(), curMarker.label);};
    
}
