﻿// JScript 檔
/*
 * Note. Reference to UrMapUtiljs, fundamental functions are also in reference JavaScript file.
 * 2009/05/06
 */
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; 

/*
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={}; //Reference to UrMapUtiljs
if (!FWUI.map) {FWUI.map={};}

FWUI.map.MARKER_PHOTO_PATH = "/blog/userfiles/";

FWUI.map.HomePageMyMarkerWindowTemplate =
  '<table width="270" 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"><img src="#{MainImage}" width="120" height="90" border="0" /></td>'+
  '      </tr>'+
  '    </table></td>'+
  '    <td width="140"><table width="130" border="0" align="center" cellpadding="0" cellspacing="0">'+
  '      <tr><td><span class="Over12_redB">#{MarkerName}</span></td>'+
  '      </tr>'+
  '      <tr><td align="left" style="font-size: 12px">'+
  '        #{Description}'+
  //'        地址:#{Address}'+
  '      </td></tr>'+
  '      <tr>'+
  '        <td height="20">#{EditAction} #{DelAction}'+
  //'        <a href="javascript:delMyMarker(#{MarkerId}, \'#{MarkerName}\');"><img src="#{WebRoot}/blog/images/icon_delete.gif" width="40" height="12" border="0" alt="刪除定點標註"/></a>'+
  '        </td>'+
  '      </tr>'+
  '    </table></td>'+
  '  </tr>'+
  '</table>';

FWUI.map.DeatilPageMyMarkerWindowTemplate =
  '<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="#{MainImage}" 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><span class="Over12_redB">#{MarkerName}</span></td>'+
  '      </tr><tr>'+
  '        <td align="left" style="font-size: 12px">'+
  '        #{Description}'+
  //'        地址:#{Address}'+
  '      </td></tr>'+
  '      <tr>'+
  '        <td height="20">#{EditAction} #{DelAction}'+
  //'        <a href="javascript:delMyMarker(#{MarkerId}, \'#{MarkerName}\');"><img src="#{WebRoot}/blog/images/icon_delete.gif" width="40" height="12" border="0" alt="刪除定點標註"/></a>'+
  '        </td>'+
  '      </tr></table></td>'+
  '  </tr>'+
  '</table>';

FWUI.map.MarkerData = {MarkerId: "", FranchiseId: "", MarkerName: "", Description: "", Latitude: "", Longitude: "", Address: "", MainImage: "", MarkerType: "",
                       IsLogin: false, IsOwner: false, EditAction: "", DelAction: "", WebRoot: ""};

// 函式：用來產生一個使用自訂圖示的房屋物件地標
FWUI.map.MyMarker = function (markerJson, id, name, lat, lng, addrs, uIcon, desc){
    this.init(markerJson, id, name, lat, lng, addrs, uIcon, desc);
};
FWUI.map.MyMarker.prototype = {
	id: null,
	franchiseId: null,
	markerName: null,
	desc: null,
  longitude: null,
  latitude: null,
  uIcon: null,
  uLatLng: null,
  address: null,
  mainImage: null,
  markerType: null,
  isLogin: false,
  isOwner: false,
  editAction: '',
  delAction: '',
  uMarker: null,
  windowTemplate: "",
  markerJsonObj: FWUI.map.MarkerData,
  webRoot: "",
	
	init: function(markerJson, id, name, lat, lng, addrs, uIcon, desc)
  {
    if(markerJson)
    {
      this.id = markerJson.MarkerId;
      this.franchiseId = markerJson.FranchiseId;
      this.markerName = markerJson.MarkerName;
      this.address = markerJson.Address;
      this.latitude = markerJson.Latitude;
      this.longitude = markerJson.Longitude;
      this.markerJsonObj = markerJson;
      if (markerJson.MainImage == "" || markerJson.MainImage == "NULL" || markerJson.MainImage == null)
        this.markerJsonObj.MainImage = this.markerJsonObj.WebRoot+"/images/no_200x150.jpg";
      else
        this.markerJsonObj.MainImage = this.markerJsonObj.WebRoot + FWUI.map.MARKER_PHOTO_PATH + this.markerJsonObj.MainImage;
      this.desc = markerJson.Description;
      this.webRoot = this.markerJsonObj.WebRoot;

      //precess login & owner data modification links
      this.isLogin = markerJson.IsLogin;
      this.isOwner = markerJson.IsOwner;
      if (this.isLogin && this.isOwner)
      {
        markerJson.EditAction =
        "<a href=\"javascript:EditMyMarker("+this.id+", \'"+this.markerName+"\');\"><img src=\""+this.webRoot+"/blog/images/icon_edit.gif\" width=\"40\" height=\"12\" border=\"0\" alt=\"編輯定點標註\"/></a>";
        markerJson.DelAction =
        "<a href=\"javascript:delMyMarker("+this.id+", \'"+this.markerName+"\');\"><img src=\""+this.webRoot+"/blog/images/icon_delete.gif\" width=\"40\" height=\"12\" border=\"0\" alt=\"刪除定點標註\"/></a>";
        this.editAction = markerJson.EditAction;
        this.delAction = markerJson.DelAction;
      }
    } else {
      this.id = id;
      this.markerName = name;
      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_blog01.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);
		marker.id = self.id;
	  marker.addListener('click', function() {
		  marker.openInfoWindow(self.desc);
	  });
	  return marker;
	},
  showWindow: function()
  {
    var template = new Template(this.windowTemplate);
    this.uMarker.openInfoWindow(template.evaluate(this.markerJsonObj), 160, 210);
    /*
    var template = $.template(this.windowTemplate); //jQuery template
    var temDiv = document.createElement("div");
    $(temDiv).append(template, this.markerJsonObj);
    var content = temDiv.innerHTML;
    this.uMarker.openInfoWindow(temDiv.innerHTML, 160, 210);
    //新增一個UInfoWindowOptions物件
    var infowindowOptions = new UInfoWindowOptions();
    infowindowOptions.width = 230;//開啟時的寬度
    infowindowOptions.height = 160;//開啟時的高度
    infowindowOptions.maxWidth = 300;//開啟最大時的寬度
    infowindowOptions.maxHeight = 200;//開啟最大時的高度
    infowindowOptions.selectedTab = 0;//開啟infoWindow時的指定分頁(從0開始)
    infowindowOptions.infoWindowTabs = [
        new UInfoWindowTab(this.markerName, content)//,
        //new UInfoWindowTab('分頁二', document.getElementById('testTabContent2')),
        //new UInfoWindowTab('分頁名稱可以很長', '這是第三個分頁的內容')
        ];
    //當infoWindow被打開時執行的function
    //infowindowOptions.onOpenFn = function(){alert('open');};
    //當infoWindow被關閉時執行的function
    //infowindowOptions.onCloseFn = function(){alert('Close');};
    //this.uMarker.openInfoWindow(infowindowOptions);
    */
  }
};

/*
 * extent MyMarker for DeatilPagePage
 */
FWUI.map.DeatilPageMyMarker = function (markerJson, id, name, lat, lng, addrs, uIcon, desc){
  //customize properties
  this.windowTemplate = FWUI.map.DeatilPageMyMarkerWindowTemplate;
  this.init(markerJson, id, name, lat, lng, addrs, uIcon, desc);  
};
FWUI.map.DeatilPageMyMarker.prototype = new FWUI.map.MyMarker;

// override windowConfig
FWUI.map.DeatilPageMyMarker.prototype.createMarker = function(self) {
  var marker = new UMarker(this.uLatLng, this.uIcon);
  marker.id = self.id;
  marker.addListener('click', function() {
    var template = new Template(self.windowTemplate);
    var content = template.evaluate(self.markerJsonObj);
    marker.openInfoWindow(content, 160, 220);
    /*
    var template = $.template(self.windowTemplate); //jQuery template
    var temDiv = document.createElement("div");
    $(temDiv).append(template, self.markerJsonObj);
    var content = temDiv.innerHTML;
   
    marker.openInfoWindow(content, 160, 220);
    //新增一個UInfoWindowOptions物件
    var infowindowOptions = new UInfoWindowOptions();
    infowindowOptions.width = 160;//開啟時的寬度
    infowindowOptions.height = 220;//開啟時的高度
    infowindowOptions.maxWidth = 170;//開啟最大時的寬度
    infowindowOptions.maxHeight = 250;//開啟最大時的高度
    infowindowOptions.selectedTab = 0;//開啟infoWindow時的指定分頁(從0開始)
    infowindowOptions.infoWindowTabs = [new UInfoWindowTab(self.markerJsonObj.MarkerName, content)];
    marker.openInfoWindow(infowindowOptions);*/
  });
  return marker;
};
FWUI.map.DeatilPageMyMarker.prototype.showWindow = function()
{
  var template = new Template(this.windowTemplate);
  this.uMarker.openInfoWindow(template.evaluate(this.markerJsonObj), 160, 210);
  /*
  var template = $.template(this.windowTemplate); //jQuery template
  var temDiv = document.createElement("div");
  $(temDiv).append(template, this.markerJsonObj);
  var content = temDiv.innerHTML;
  this.uMarker.openInfoWindow(temDiv.innerHTML, 160, 210);
  
  //新增一個UInfoWindowOptions物件
  var infowindowOptions = new UInfoWindowOptions();
  infowindowOptions.width = 160;//開啟時的寬度
  infowindowOptions.height = 220;//開啟時的高度
  infowindowOptions.maxWidth = 170;//開啟最大時的寬度
  infowindowOptions.maxHeight = 250;//開啟最大時的高度
  infowindowOptions.selectedTab = 0;//開啟infoWindow時的指定分頁(從0開始)
  infowindowOptions.infoWindowTabs = [new UInfoWindowTab(this.markerName, template.evaluate(this.markerJsonObj))];
  //this.uMarker.openInfoWindow(infowindowOptions);
  */
};

/*
 * extent HomePageMyMarker for BlogHomePage
 */
FWUI.map.HomePageMyMarker = function (markerJson, id, name, lat, lng, addrs, uIcon, desc){
  //customize properties
  this.windowTemplate = FWUI.map.HomePageMyMarkerWindowTemplate;
  this.init(markerJson, id, name, lat, lng, addrs, uIcon, desc);
};
FWUI.map.HomePageMyMarker.prototype = new FWUI.map.MyMarker;

// override windowConfig
FWUI.map.HomePageMyMarker.prototype.createMarker = function(self) {
  var marker = new UMarker(self.uLatLng, self.uIcon);
  marker.id = self.id;
  marker.addListener('click', function() {
    var template = new Template(self.windowTemplate);
    var content = template.evaluate(self.markerJsonObj);
    marker.openInfoWindow(content, 300, 120);
    /*
    var template = $.template(self.windowTemplate); //jQuery template
    var temDiv = document.createElement("div");
    $(temDiv).append(template, self.markerJsonObj);
    var content = temDiv.innerHTML;
    marker.openInfoWindow(content, 300, 120);
    
    //新增一個UInfoWindowOptions物件
    var infowindowOptions = new UInfoWindowOptions();
    infowindowOptions.width = 310;//開啟時的寬度
    infowindowOptions.height = 120;//開啟時的高度
    infowindowOptions.maxWidth = 3500;//開啟最大時的寬度
    infowindowOptions.maxHeight = 150;//開啟最大時的高度
    infowindowOptions.selectedTab = 0;//開啟infoWindow時的指定分頁(從0開始)
    infowindowOptions.infoWindowTabs = [new UInfoWindowTab(self.markerJsonObj.MarkerName, content)];
    //marker.openInfoWindow(infowindowOptions);
    */
  });
  return marker;
};
FWUI.map.HomePageMyMarker.prototype.showWindow = function()
{
  var template = new Template(this.windowTemplate);
  this.uMarker.openInfoWindow(template.evaluate(this.markerJsonObj), 310, 110);
  /*
  var template = $.template(this.windowTemplate); //jQuery template
  alert("FWUI.map.HomePageMyMarker.prototype.showWindow: "+template);
  var temDiv = document.createElement("div");
  $(temDiv).append(template, this.markerJsonObj);
  var content = temDiv.innerHTML;
  this.uMarker.openInfoWindow(temDiv.innerHTML, 310, 110);  
  
  //新增一個UInfoWindowOptions物件
  var infowindowOptions = new UInfoWindowOptions();
  infowindowOptions.width = 310;//開啟時的寬度
  infowindowOptions.height = 120;//開啟時的高度
  infowindowOptions.maxWidth = 3500;//開啟最大時的寬度
  infowindowOptions.maxHeight = 150;//開啟最大時的高度
  infowindowOptions.selectedTab = 0;//開啟infoWindow時的指定分頁(從0開始)
  infowindowOptions.infoWindowTabs = [new UInfoWindowTab(this.markerName, template.evaluate(this.markerJsonObj))];
  //this.uMarker.openInfoWindow(infowindowOptions);
  */
};

/*Blog franchise marker map display methods*/
var franchiseMarkers = new Array();
var franchiseMarkerListJson = null;
function buildFranchiseMarkers(franchiseMarkerListJson, type)
{
  //map.closeInfoWindow();
  //map.clearOverlays();
  removeMarkers(map, franchiseMarkers);
  if (franchiseMarkerListJson && franchiseMarkerListJson.length > 0)
  {
    franchiseMarkers = new Array();
    for (i = 0; i< franchiseMarkerListJson.length; i++)
    {
      var myFranchiseMarker = null;
      if (type == 1)
        myFranchiseMarker = new FWUI.map.HomePageMyMarker(franchiseMarkerListJson[i]);
      else
        myFranchiseMarker = new FWUI.map.DeatilPageMyMarker(franchiseMarkerListJson[i]);
      
      franchiseMarkers.push(myFranchiseMarker);
    }
    addMarks(map, franchiseMarkers);
    //map.centerAndZoom(franchiseMarkers[0].uLatLng, 6)
    //setTimeout('franchiseMarkers[0].showWindow()', 1200);
  }
}
function getHomePageFranchiseMarkerListCallback(res)
{
  if (res)
  {
    eval("franchiseMarkerListJson = "+res.value+";");
    if (map)
      buildFranchiseMarkers(franchiseMarkerListJson, 1);
    else
      setTimeout("buildFranchiseMarkers(franchiseMarkerListJson, 1)", 1500);    
  }
}
function getDetailPageFranchiseMarkerListCallback(res)
{
  if (res)
  {
    eval("franchiseMarkerListJson = "+res.value+";");
    if (map)
      buildFranchiseMarkers(franchiseMarkerListJson, 2);
    else
      setTimeout("buildFranchiseMarkers(franchiseMarkerListJson, 2)", 1500);    
  }
}

function showFranchiseMarkerInfoWindow(markerId)
{
  if (franchiseMarkers.length > 0)
    for (i = 0; i < franchiseMarkers.length; i++)
    {
      if (franchiseMarkers[i].id == markerId)
      {
        franchiseMarkers[i].showWindow();
        break;
      }
    }
}

function getFranchiseMarker(markerId)
{
  if (franchiseMarkers.length > 0)
    for (i = 0; i < franchiseMarkers.length; i++)
    {
      if (franchiseMarkers[i].id == markerId)
      {
        return franchiseMarkers[i];
      }
    }
}

//Fullerton.Cthouse.Web.Blog.BlogAjaxHelper.getFranchiseMarkerJson(double swLat, double swLng, double neLat, double neLng);
function getHomePageFranchiseMarkerJson(swLat, swLng, neLat, neLng)
{
  //alert("getHomePageFranchiseMarkerJson :"+swLat+", "+swLng+", "+neLat+", "+neLng);
  Fullerton.Cthouse.Web.Blog.BlogAjaxHelper.getOpenFranchiseMarkerJson(swLat, swLng, neLat, neLng, getHomePageFranchiseMarkerListCallback);
}
function getDetailPageFranchiseMarkerJson(swLat, swLng, neLat, neLng)
{
  //alert("getDetailPageFranchiseMarkerJson");
  Fullerton.Cthouse.Web.Blog.BlogAjaxHelper.getOpenFranchiseMarkerJson(swLat, swLng, neLat, neLng, getDetailPageFranchiseMarkerListCallback);
}

/*========================================================================================================================================*/
/*delete functions*/
var tmpDeleteMarkerId = -1; //for display process
function delFranchiseMarkerCallback(res)
{
  if (res && res.value == "OK") {
    map.closeInfoWindow();  
    alert("刪除成功!");
    if (tmpDeleteMarkerId != -1)
    {
      removeMarkers(map, franchiseMarkers);
      removeFWUIMarker(tmpDeleteMarkerId, franchiseMarkers);
      addMarks(map, franchiseMarkers);
      tmpDeleteMarkerId = -1;
    }
  } else if (res && res.value == "NOT_LOGIN") {
    alert("請先登入為管理者!");  
  } else if (res && res.value == "NOT_OWNER") {
    alert("您不是標註管理者,無法刪除!");
  } else if (res && res.value == "FAILE") {
    alert("刪除失敗!");
  }
}

function delMyMarker(markerId, markerName)
{
  //alert(markerId);
  var cfmMsg = "確定要刪除 ["+markerName+"]?";//\r\n您必須是該標註的加盟店人員才能刪除!";
  if (confirm(cfmMsg))
  {
    Fullerton.Cthouse.Web.Blog.BlogAjaxHelper.deleteFranchiseMarker(markerId, delFranchiseMarkerCallback);
    tmpDeleteMarkerId = markerId;
  }
}

/*========================================================================================================================================*/
/*update functions*/
var tmpEditMarkerId;
function editFranchiseMarkerCallback(res)
{
  if (res && res.value == "OK") {
    var url = window.location + "/FranchiseQuery/UpdateQueryByMapInCircle.aspx?mapSearchType=21&markerId="+tmpEditMarkerId+"&lastPage="+encodeURIComponent(window.location);
    //alert(url);
    window.location.href = url;
  } else if (res && res.value == "NOT_LOGIN") {
    alert("請先登入為管理者!");
    window.location.refresh();
  } else if (res && res.value == "NOT_OWNER") {
    alert("您不是標註管理者,無法編輯!");
  } else if (res && res.value == "FAILE") {
    alert("系統錯誤,請稍候再試!");
  }
  tmpEditMarkerId = "";
}
function EditMyMarker(markerId, markerName)
{
  //alert(markerId);
  //var myMarker = getFranchiseMarker(markerId);
  tmpEditMarkerId = markerId;
  Fullerton.Cthouse.Web.Blog.BlogAjaxHelper.checkMarkerFrachiseLogin(markerId, editFranchiseMarkerCallback);
}

function buildFranchiseMarkersForUpdate(res)
{
  eval("franchiseMarkerListJson = "+res.value+";");
  /*
   * this function is reference to UrMapCircleUtil.js
	 * franchiseMarker, marked, lat, lng are defined in UrMapCircleUtil.js for placeMyMarker()
   */
  map.clearOverlays();
  if (franchiseMarkerListJson && franchiseMarkerListJson.length > 0)
  {
    var myMarker = new FWUI.map.DeatilPageMyMarker(franchiseMarkerListJson[0]);
    var myIcon = new UIcon();
    myIcon.image = myMarker.webRoot + '/images/定位.png';
    myIcon.iconWidth = 86;
    myIcon.iconHeight = 47;
    myIcon.iconAnchor = new UPoint(40, 36);
    //alert("myMarker.webRoot: "+myMarker.webRoot);    
    franchiseMarker = new UMarker(myMarker.uLatLng, myIcon);
    map.addOverlay(franchiseMarker);
    marked = true;
    lat = myMarker.uLatLng.lat();
    lng = myMarker.uLatLng.lng();
    //alert("marked: "+marked);
    map.centerAndZoom(myMarker.uLatLng, 9)
  }
}

function initUpdateMyMarker(markerId)
{
  Fullerton.Cthouse.Web.Blog.BlogAjaxHelper.getOpenFranchiseMarkerJsonById(markerId, buildFranchiseMarkersForUpdate);
}

/*========================================================================================================================================*/


