// 帮助页面关闭
var closeRightHelpPage = function () {
  if (document.readyState == 'complete') {
    window.parent['closeRightHelpPage']();
  }
}

jQuery(function () {
  var height = jQuery(".ui-layout-east").height();
  jQuery("#helper").css("height", height - 25);
  //jQuery("#helper").css("overflow","auto");
});

/**点击右侧help 的一项时 跳转到详细页面**/
function showHelpContentHtml (title, url) {
  // 需要iframe初始化完成之后才执行这个方法
  /*底部页签切换*/
  jQuery("#toThisPage_Btn").removeClass("topic_select");
  jQuery("#mainIndex_Btn").addClass("topic_select");


  // jQuery("#thisTitle").html(title + "帮助:");
  window.top.document.getElementsByTagName("iframe")[0].contentWindow.document.getElementById("thisTitle").innerHTML = title + "帮助";
  jQuery("#mainIndex").css("display", "none");
  jQuery("#helpFrame").css("display", "");
  jQuery("#thisPageContent").css("display", "");
  if (jQuery("#thisPageContent").css("height") == "auto") {
    jQuery("#thisPageContent").css("height", jQuery(document).height() - 65);
  } else {
    jQuery("#thisPageContent").css("height", jQuery("#helper").height() - jQuery("#helpcontenttitle").height() - 20);
  }
  window.top.document
    .getElementsByTagName("iframe")[0]
    .contentWindow.document.getElementById("helpFrame")
    .setAttribute("src", "/designer/help/back/toc/" + url);

  // window.top.rightHelpFrame.contentWindow.document.title = title + '&' + url
  // jQuery("#helpFrame").attr("src", "/designer/help/back/toc/" + url);
}
/**点击右侧help 的一项时 跳转到详细页面**/
function showHelpContentJSP (title, url) {
  // 需要iframe初始化完成之后才执行这个方法
  /*底部页签切换*/
  jQuery("#toThisPage_Btn").removeClass("topic_select");
  jQuery("#mainIndex_Btn").addClass("topic_select");


  // jQuery("#thisTitle").html(title + "帮助:");
  window.top.document.getElementsByTagName("iframe")[0].contentWindow.document.getElementById("thisTitle").innerHTML = title + "帮助";
  jQuery("#mainIndex").css("display", "none");
  jQuery("#helpFrame").css("display", "");
  jQuery("#thisPageContent").css("display", "");
  if (jQuery("#thisPageContent").css("height") == "auto") {
    jQuery("#thisPageContent").css("height", jQuery(document).height() - 65);
  } else {
    jQuery("#thisPageContent").css("height", jQuery("#helper").height() - jQuery("#helpcontenttitle").height() - 20);
  }

  window.top.rightHelpFrame.contentWindow.document.title = title + '&' + url
  // jQuery("#helpFrame").attr("src", "/designer/help/back/toc/" + url);
}

var thisHelpPageUrl = ""; //存放当前主题页面的url
var title = "";
/*当前帮助主题*/
function toThisHelpPage (helpid) {
  if (helpid != "") {
    // console.log(helpid);
    //thisHelpPageUrl为空时说明页面第一访问
    jQuery.get("/designer/api/designtime/applications/modules/forms/help?id=" + helpid, function (data) {
      if (data.errcode === 0) {
        thisHelpPageUrl = data.data.href;
        title = data.data.label;

        // 第一次如果下方没有window.top.rightHelpFrame先存起数据传给vue
        window.top.rightHelpFrameObj = {
          title: title,
          url: thisHelpPageUrl
        }


        if (window.top.rightHelpFrame) {
          window.top.rightHelpFrameObj = {
            title: title,
            url: thisHelpPageUrl
          }
          // window.top.rightHelpFrame.contentWindow.document.title = title + '&' + thisHelpPageUrl
          // console.log(window.top.rightHelpFrame)
          showHelpContentHtml(title, thisHelpPageUrl);
        }
      } else {
        jQuery("#thisTitle").html("当前页Not帮助信息...");
      }
    });
  } else {
    showHelpContentHtml(title, thisHelpPageUrl);
  }
}


/*点击返回右侧帮助主索引*/
function toHelpMainIndex () {
  jQuery("#thisPageContent").css("display", "none");
  jQuery("#mainIndex").css("display", "");
  jQuery("#mainIndex").css("height", jQuery("#helper").height() - jQuery("#helpcontenttitle").height() - 15);
  jQuery("#helpcontent").css("height", jQuery("#mainIndex").height() - 2);
  jQuery("#helpcontent").css("overflow", "auto");
  initHelpIndexTree("");
}

/*初始化帮助主索引*/
function initHelpIndexTree (parem) {
  jQuery("#mainIndex_Btn").removeClass("topic_select");
  jQuery("#toThisPage_Btn").addClass("topic_select");
  /* HelpHelper.doHelpTreeIndex(parem, function(html) {
    jQuery("#thisTitle").html("MyApps帮助目录");
    if (html) {
      jQuery("#helpcontent").html(html);
    } else {
      jQuery("#helpcontent").html("<br>当前页Not帮助信息...");
    }
  }); */
  jQuery.get("/designer/api/designtime/applications/modules/getHelpTreeIndex", function (res) {
    if (res.errcode === 0) {
      jQuery("#thisTitle").html("MyApps帮助目录");

      if (res.data) {
        jQuery("#helpcontent").html(res.data);
      } else {
        jQuery("#helpcontent").html("<br>当前页Not帮助信息...");
      }
    }
  })

}

/* 点击关联主题 */
jQuery('#mainIndex_Btn').click(function () {
  var thisPageArr = window.document.title.split('&')
  showHelpContentHtml(thisPageArr[0], thisPageArr[1])
})

/* 软件列表帮助的内容 */
/*if help menus sub menus click to open or close sub menus*/
function isShowSubHelpMenus (obj) {
  var subobj = obj.next("img").next("a").next("br").next("ul");
  if (subobj.css("display") == "none") {
    subobj.css("display", "");
    obj.attr("src", "../images/minus.gif");
    obj.next("img").attr("src", "../images/toc_open.gif");
  } else {
    subobj.css("display", "none");
    obj.attr("src", "../images/plus.gif");
    obj.next("img").attr("src", "../images/toc_closed.gif");
  }
}

window.top.toThisHelpPage = toThisHelpPage;
