const iframeID = "#SysJustIFRAME";
var gMenuObj = [
  {
    MainTitle: "產品總覽",
    SubMenu: [
      {
        SubTitle: "基金",
        SubLink: "javascript:goUrl('/w/html/select.djhtm',true)",
        Outside: "/products.asp",
        Child: [
          // 進階查詢
          "/w/html/select.djhtm",
          // 境內基金頁面
          "/w/wr/wr01.djhtm",
          "/w/wr/wr02.djhtm",
          "/w/wr/wr03.djhtm",
          "/w/wr/wr04.djhtm",
          "/w/wr/wr05.djhtm",
          "/w/wr/wr06.djhtm",
          "/w/wr/wr10.djhtm",
          "/w/wr/wr11.djhtm",
          "/w/wr/wr12.djhtm",
          "/w/wr/wr902.djhtm",
          // 境外基金頁面
          "/w/wb/wb01.djhtm",
          "/w/wb/wb02.djhtm",
          "/w/wb/wb03.djhtm",
          "/w/wb/wb04.djhtm",
          "/w/wb/wb05.djhtm",
          "/w/wb/wb06.djhtm",
          "/w/wb/wb07.djhtm",
          "/w/wb/wb08.djhtm",
          "/w/wb/wb902.djhtm",
        ],
        Name: [
          "基金資訊總覽",
          // 境內基金頁面
          "國內基金-基本資料",
          "國內基金-淨值表",
          "國內基金-績效比較",
          "國內基金-持股資料",
          "國內基金-相關新聞",
          "國內基金-行事曆",
          "國內基金-配息狀況",
          "國內基金-規模變動表",
          "國內基金-風險報酬",
          "國內基金-報告書",
          // 境外基金頁面
          "境外基金-基本資料",
          "境外基金-淨值表",
          "境外基金-績效比較",
          "境外基金-持股狀況",
          "境外基金-配息狀況",
          "境外基金-行事曆",
          "境外基金-績效評比",
          "境外基金-規模變動表",
          "境外基金-報告書",
        ]
      },
      {
        SubTitle: "ETF",
        SubLink: "javascript:goUrl('/ETFWeb/html/et081001.djhtm#CID=ALL',true)",
        Outside: "/products.asp",
        Child: [
          "/ETFWeb/html/et081001.djhtm#CID=ALL",
          "/ETFWeb/HTML/et305001.djhtm",
          "/ETFWEB/HTML/ET011001.DJHTM",
          "/ETFWeb/HTML/ET010001.DJHTM",
          "/ETFWeb/HTML/ETNEW0003.DJHTM",
          "/ETFWeb/HTML/BASIC0002.DJHTM",
          "/ETFWeb/HTML/ET012001.DJHTM",
          "/ETFWeb/HTML/ET013001.DJHTM",
          "/ETFWeb/HTML/ET050000.DJHTM",
          "/ETFWeb/HTML/ETFREPORT.DJHTM",
          "/ETFWeb/HTML/BASIC0011.DJHTM",
          "/ETFWeb/HTML/BASIC0015.DJHTM",
        ],
        Name: [
          "ETF資訊總覽",
          "ETF進階搜尋",
          "ETF-基本資料",
          "ETF-淨值表",
          "ETF-收盤行情",
          "ETF-技術分析",
          "ETF-績效表現",
          "ETF-持股變動",
          "ETF-配息狀況",
          "ETF-報告書",
          "ETF-風險報酬",
          "ETF-規模變動",
        ]
      },
      {
        SubTitle: "美股",
        SubLink: "javascript:goUrl('/w/html/NextbankStockList.djhtm',true)",
        Outside: "/products.asp",
        Child: ["/w/html/NextbankStockList.djhtm","/b2brwdCommon/proxy/nextbank/"],
        Name: ["美股資訊總覽","美股資訊"]
      },
      {
        SubTitle: "基金配息查詢",
        SubLink: "javascript:goUrl('/w/yp/yp700/yp7000001.djhtm',true)",
        Outside: "/products.asp",
        Child: ["/w/yp/yp700/yp7000001.djhtm"],
        Name: ["基金配息查詢"]
      },
    ],
  },
  {
    MainTitle: "投資趨勢",
    SubMenu: [
      {
        SubTitle: "投資觀點",
        SubLink: "",
        Outside: "/products_draw.asp",
        Child: ['/w/html/viewpointList.djhtm','/w/html/Article/viewpointArticle.djhtm'],
        ClassName:'noBackground',
        Painting: {
          number:1,
          content: { title:'將來投資觀點', subTitle:'靠資訊力提升投資決策力', text:'將專業的市場訊息化為簡單易懂的理財小品，不論新手老手都能一起掌握投資機會。'}
        },
      },
      {
        SubTitle: "投資主題",
        SubLink: "",
        Outside: "/products_draw.asp",
        Child: ['/w/html/topicsList.djhtm'],
        ClassName:'noBackground',
        Painting: {
          number:1,
          content:{title:'將來投資主題',subTitle:'用關鍵字快速找相關商品',text:'最熱門的投資主題都在這，用耳熟能詳的關鍵字，輕鬆聯結相關的投資標的。'}
        },
      },
      {
        SubTitle: "投資主題",
        SubLink: "",
        Outside: "/products_info.asp",
        Child: ['/w/html/topicsArticle.djhtm'],
        ClassName:'noBackground',
      },
      {
        SubTitle: "投資懶人包",
        SubLink: "",
        Outside: "/products_draw.asp",
        Child: ['/w/html/lazybagList.djhtm'],//['/CustomFile/investingTopics.html?a=2'],
        ClassName:'noBackground',
        Painting: {
          number:1,
          content:{title:'將來投資懶人包',subTitle:'做好資產配置投資快速上路',text:'依照風險屬性與目標選擇適合的投資組合，一鍵即可全部申購並共同管理。'}
        },
      },
      {
        SubTitle: "投資懶人包",
        SubLink: "",
        Outside: "/products_info.asp",
        Child: ['/w/html/lazybagArticle.djhtm'],
        ClassName:'noBackground',
      },
    ],
  },
  {
    MainTitle: "熱門產品",
    SubMenu: [
      {
        SubTitle: "全站人氣排行",
        SubLink: "",
        Outside: "/products_draw.asp",
        Child: ['/w/yp/yp081010.djhtm'],
        Painting: {
          number:2,
          content:{title:'全站人氣排行',subTitle:'最近大家都在看的投資產品',text:'以嘉實資訊相關網站之瀏覽點擊數為篩選參考<sup>註9</sup>，申購時仍須考量自身風險承受度並了解相關商品風險。'}
        },
      },
      {
        SubTitle: "專家嚴選商品",
        SubLink: "",
        Outside: "/products_draw.asp",        
        Child: ['/w/html/recommendGoods.djhtm'],
        Painting: {
          number:2,
          content:{title:'專家嚴選標的',subTitle:'外部機構精選投資標的',text:'外部專家推薦的投資商品僅供參考，投資人申購時仍須考量本身風險承受度。'}
        },
      },
    ],
  },
  {
    MainTitle: "財經前線",
    SubMenu: [
      {
        SubTitle: "全球市場總覽",
        SubLink: "javascript:goUrl('/w/wh/wh09.djhtm',true)",
        Outside: "/products_draw.asp",
        Child: ["/w/wh/wh09.djhtm"],
        Painting: {
          number:3,
          content:{title:'全球市場總覽',subTitle:'最新金融市場表現速報',text:'投資從看市場開始，一張圖帶你快速取得全球最新的金融市場表現！'}
        },
      },
      {
        SubTitle: "機構趨勢報告",
        SubLink: "javascript:goUrl('/w/wh/wh01b.djhtm?a=na&b=0',true)",
        Outside: "/products_draw.asp",
        Child: ["/w/wh/wh01b.djhtm?a=na&b=0"],
        Painting: {
          number:3,
          content:{title:'機構趨勢報告',subTitle:'專家團隊市場與趨勢分析',text:'兼具深度與廣度，以宏觀的角度回顧市場、展望未來。'}
        },
      },
      {
        SubTitle: "國際財經新聞",
        SubLink:
          "javascript:goUrl('/ETFWeb/NEWS/NEXTBANKNEWLIST.DJHTM#TYPE=2&PAGE=1',true)",
        Outside: "/products_draw.asp",
        Child: ["/ETFWeb/NEWS/NEXTBANKNEWLIST.DJHTM#TYPE=2&PAGE=1"],
        ClassName:'iframeShadow',
        Painting: {
          number:3,
          content:{title:'國際財經新聞',subTitle:'擴大視野找尋投資契機',text:'時時閱讀國際新聞，掌握日新月異的市場脈動。'},
        },
      },
      {
        SubTitle: "國際財經新聞",
        SubLink:
          "javascript:goUrl('/ETFWeb/NEWS/NEXTBANKNEWLIST.DJHTM#TYPE=2&PAGE=1',true)",
        Outside: "/products.asp",
        Child: ["/ETFDATA/DJHTM/ETKMDJNEWSCONTENTNEXTBANK.DJHTM"],
        ClassName:'iframeShadow',
      },
      {
        SubTitle: "各國經濟指標",
        SubLink: "javascript:goUrl('/w/RB/RBB_AX000020.djhtm',true)",
        Outside: "/products_draw.asp",
        Child: ["/w/RB/RBB_AX000020.djhtm"],
        Painting: {
          number:3,
          content:{title:'各國經濟指標',subTitle:'讀指標讓數字說話',text:'看懂經濟指標，洞悉市場趨勢變化，就能提高投資勝率！'}
        },
      },
    ],
  },
  {
    MainTitle: "公告事項",
    SubMenu: [
      { 
        SubTitle: "投資公告", 
        SubLink: "",
        Outside: "/products_draw.asp",
        Child: ['/ETFWEB/NEWS/NEXTBANKAnnouncementLIST.DJHTM#TYPE=2&PAGE=1','/w/html/AnnouncementPage.djhtm'], 
        Painting: {
          number:4,
          content:{title:'投資訊息公告',subTitle:'重要的投資小事',text:'來自將來銀行、基金公司、主管機關等重要公告訊息。'}
        },
      },
      { 
        SubTitle: "通路報酬揭露", 
        SubLink: "",
        Outside: "/products_draw.asp",
        Child: ['/w/html/fundReward.djhtm?cid='], 
        Painting: {
          number:4,
          content:{title:'通路報酬揭露',subTitle:'關於費用說清楚講明白',text:'揭露將來銀行銷售基金時，投資人所支付的費用，與本行收取基金公司之費用。'}
        },
      },
      { 
        SubTitle: "警語與投資人權益", 
        SubLink: "",
        Outside: "/products_draw.asp",
        Child: ['/w/html/Warning.djhtm'],
        Painting: {
          number:4,
          content:{title:'警語與投資人權益',subTitle:'投資前應詳閱的細節',text:'將來銀行與你同等在乎你的權益，建議申購前應詳閱以下說明。'}
        },
      },
      { 
        SubTitle: "信託業務紛爭受理方式", 
        SubLink: "",
        Outside: "/products_draw.asp", 
        Child: ['/w/html/Dispute.djhtm'],
        Painting: {
          number:4,
          content:{title:'信託業務紛爭',subTitle:'本行處理程序之說明',text:'假如遇到信託業務糾紛，將來銀行提供有效的申訴與溝通管道供使用。'}
        },
      },
    ],
  },
];

$(document).ready(function () {

  var iframeUrl = decodeURIComponent(DJDeCode(decodeURI(getUrlParam().surl)));
  goUrl(iframeUrl, false);

});


function goUrl(url, external) {
  // 至頂
  $("html, body").animate({ scrollTop: 0 }, 500);

  if (!external) external = false;

  switch (document.location.pathname) {
    case "/":
    case "/index.html":
    case "/products.asp":
    case "/products_info.asp":
    case "/products_draw.asp":
      //external
      if (external) {
        //refresh
        document.location.href = findParentHTML(url) + "?sUrl=" + DJEnCode(url);
      } else {
        var iframeEl = $(iframeID);
        var subMenuObj = findSubMenu(url);

        if (!!subMenuObj && subMenuObj.ClassName!==undefined){
          iframeEl.addClass(subMenuObj.ClassName);
        }

        iframeEl.attr("src", url);
      }
      break;
    default:
      break;
  }

  return void 0;
}

function findSubMenu(sUrl){
  let match_flag = false;
  let tmpObj = undefined;
  for (let j = 0; j < gMenuObj.length; j++) {
    let firstLeayer = gMenuObj[j];
    // SubTitle 迴圈
    for (let i = 0; i < firstLeayer.SubMenu.length; i++) {
      let secondLayer = firstLeayer.SubMenu[i];

      for (var k = 0; k < secondLayer.Child.length; k++) {
        if (
          sUrl.toUpperCase().indexOf(secondLayer.Child[k].toUpperCase()) > -1
        ) {
          tmpObj = secondLayer;
          match_flag = true;
          break;
        }
      }
      if (match_flag) break;
    }
  }
  return tmpObj;
}

function findParentHTML(sUrl) {
  let match_flag = false;
  let tempPath = "/products.asp";
  for (let j = 0; j < gMenuObj.length; j++) {
    let firstLeayer = gMenuObj[j];
    // SubTitle 迴圈
    for (let i = 0; i < firstLeayer.SubMenu.length; i++) {
      let secondLayer = firstLeayer.SubMenu[i];

      for (var k = 0; k < secondLayer.Child.length; k++) {
        if (
          sUrl.toUpperCase().indexOf(secondLayer.Child[k].toUpperCase()) > -1
        ) {
          tempPath = secondLayer.Outside;
          match_flag = true;
          break;
        }
      }
      if (match_flag) break;
    }
  }
  return tempPath;
}

//產生麵包屑
function genBreadcrumbAndPainting(bgType) {
  var match_flag = false;
  var paintingNum = 0;
  var contentObj = {title:'',subTitle:'',text:''};
  var getQuerysUrl = decodeURIComponent(DJDeCode(decodeURI(getUrlParam().surl)));
  console.log("getQuerysUrl: " + getQuerysUrl);
  //特例麵包屑
  if (bgType===1) document.write('<div class="col-12">');

  // MainTitle迴圈
  for (let j = 0; j < gMenuObj.length; j++) {
    let firstLeayer = gMenuObj[j];
    // SubTitle 迴圈
    for (let i = 0; i < firstLeayer.SubMenu.length; i++) {
      let secondLayer = firstLeayer.SubMenu[i];
      for (var k = 0; k < secondLayer.Child.length; k++) {
        if (
          getQuerysUrl
            .toUpperCase()
            .indexOf(secondLayer.Child[k].toUpperCase()) > -1
        ) {
          document.write('<div class="breadcrumb">');
          document.write('  <a class="breadcrumb-item prefix-icon" href="/index.html">');
          document.write('  <img src="CustomFile/images/icon_home.svg" alt="">');
          document.write("  </a>");
          document.write('  <span class="breadcrumb-item disable">' + firstLeayer.MainTitle + "</span>");
          document.write('  <a class="breadcrumb-item " href="' + secondLayer.SubLink + '">' + secondLayer.SubTitle +"</a>" );
          document.write("</div>");
          if (secondLayer.Name !== undefined) {
            document.write('<h1>' + secondLayer.Name[k] + "</h1>");
          }
          if(secondLayer.Painting!==undefined){  
            paintingNum = secondLayer.Painting.number;
            contentObj = secondLayer.Painting.content;
          }

          match_flag = true;
          break;
        }
      }
      if (match_flag) break;
    }
    if (match_flag) break;
  }

  if (bgType===1) document.write('</div>');

  genPaintingScript(paintingNum,contentObj);
}



function genPaintingScript(num,content){
  //var content={title:'',subTitle:'',text:''};
  switch (num){
    case 1:
      document.write('<script type="text/javascript" src="CustomFile/js/index-animate/product.js"></script>');
      document.write('<script type="text/javascript" src="CustomFile/js/index-animate/product_sm.js"></script>');
      //content={title:'精選投資資訊',subTitle:'提供最新市場動態',text:'經典配置，投資常勝！股債配置比重更均衡，全方位分散各種市場風險，成功提高勝率，更適合長期持有！'}
      genPainting(content);
      break;
    case 2:
      document.write('<script type="text/javascript" src="CustomFile/js/index-animate/recommend.js"></script>');
      document.write('<script type="text/javascript" src="CustomFile/js/index-animate/recommend_sm.js"></script>');
      //content={title:'嚴選投資推薦',subTitle:'提供多樣化產品推薦',text:'經典配置，投資常勝！股債配置比重更均衡，全方位分散各種市場風險，成功提高勝率，更適合長期持有！'}
      genPainting(content);
      break;
    case 3:
      document.write('<script type="text/javascript" src="CustomFile/js/index-animate/article.js"></script>');
      document.write('<script type="text/javascript" src="CustomFile/js/index-animate/article_sm.js"></script>');
      //content={title:'投資組合推薦',subTitle:'滿足更多投資人需求',text:'經典配置，投資常勝！股債配置比重更均衡，全方位分散各種市場風險，成功提高勝率，更適合長期持有！'}
      genPainting(content);
      break;
    case 4:
      document.write('<script type="text/javascript" src="CustomFile/js/index-animate/announcement.js"></script>');
      document.write('<script type="text/javascript" src="CustomFile/js/index-animate/announcement_sm.js"></script>');
      //content={title:'投資相關公告',subTitle:'提供最新投資公告',text:'經典配置，投資常勝！股債配置比重更均衡，全方位分散各種市場風險，成功提高勝率，更適合長期持有！'}
      genPainting(content);
      break;
    case 5:

    default:
      break;
   
  }
}
//產生插圖
function genPainting(obj) {
  
  document.write('<div class="col-12 col-md-6 col-lg-4 d-flex content-center">');
  document.write('  <div class="product-main-box">');
  document.write('    <h1 class="text-center text-md-left">'+obj.title+'</h1>');
  document.write('    <div class="line-text">'+obj.subTitle+'</div>');
  document.write('    <p class="mb-0">'+obj.text+'</p>');
  document.write('  </div>');
  document.write('</div>');

  document.write('<div class="col-12 col-md-6 col-lg-8 px-5 px-md-0">');
  document.write('  <div id="animate-043C1F93D699C943A78FA0A6882D7FDA" class="animate mx-auto d-none d-md-block">');
  document.write('    <canvas class="animate__canvas"></canvas>');
  document.write('    <div class="animate__overlay"></div>');
  document.write('   </div>');
  document.write('   <div id="animate-043C1F93D699C943A78FA0A6882D7FDA-sm" class="animate mx-auto d-block d-md-none">');
  document.write('    <canvas class="animate__canvas"></canvas>');
  document.write('    <div class="animate__overlay"></div>');
  document.write("  </div>");
  document.write("</div>");



}

//取網址關參數
function request(sUrl, paras) {
  var url = sUrl;
  var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
  var paraObj = {};
  for (i = 0; (j = paraString[i]); i++) {
    paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(
      j.indexOf("=") + 1,
      j.length
    );
  }
  var returnValue = paraObj[paras.toLowerCase()];
  if (typeof returnValue == "undefined") {
    return "";
  } else {
    return returnValue;
  }
}

function getUrlParam() {
  var url = document.location.href.toLowerCase();
  var queryString = {};
  url.replace(
    new RegExp("([^?=&]+)(=([^&]*))?", "g"),
    function ($0, $1, $2, $3) {
      queryString[$1] = $3;
    }
  );
  return queryString;
}

//replace字串(同asp用法)
function replaceString(s, s1, s2) {
  if (s.length == 0 || s1.length == 0) return s;
  // process head
  if (s.indexOf(s1) == 0) s = s2 + s.substr(s1.length, s.length - s1.length);
  // process tail
  if (s.lastIndexOf(s1) == s.length - s2.length)
    s = s.substr(0, s.length - s1.length) + s2;
  // process middle
  var arrS = s.split(s1);
  if (arrS.length == 1) return s;
  var result = "";
  var nLen = arrS.length;
  for (i = 0; i < nLen; i++) {
    result += arrS[i];
    if (i != nLen - 1) result += s2;
  }
  return result;
}
function DJEnCode(sURL) {
  if (sURL == "") return sURL;
  sURL = sURL.toUpperCase();
  sURL = replaceString(replaceString(sURL, "{", "??"), "}", "==");
  sURL = replaceString(
    replaceString(
      replaceString(
        replaceString(
          replaceString(
            replaceString(
              replaceString(replaceString(sURL, "Z", "^"), ".", "]"),
              "_",
              "["
            ),
            "/",
            "$"
          ),
          "?",
          "{"
        ),
        "=",
        "}"
      ),
      "&",
      "!"
    ),
    "#",
    "?|"
  );
  return sURL;
}
function DJDeCode(sURL) {
  if (sURL == "") return sURL;
  sURL = sURL.toUpperCase();
  sURL = replaceString(
    replaceString(
      replaceString(
        replaceString(
          replaceString(
            replaceString(
              replaceString(replaceString(sURL, "^", "Z"), "]", "."),
              "[",
              "_"
            ),
            "$",
            "/"
          ),
          "{",
          "?"
        ),
        "}",
        "="
      ),
      "!",
      "&"
    ),
    "?|",
    "#"
  );
  sURL = replaceString(replaceString(sURL, "??", "{"), "==", "}");
  return sURL;
}
