var curZIndex = 1000;
var curFormId = "";
var formList = {};
var homePage = null;

(function($){
  //拖拽插件,参数:id或object
  $.Move = function(_this){
      if(typeof(_this)=='object'){
          _this=_this;
      }else{
          _this=$("#"+_this);
      }
      if(!_this){return false;}

      _this.css({'position':'absolute'}).hover(function(){$(this).css("cursor","move");},function(){$(this).css("cursor","default");})
      _this.mousedown(function(e){//e鼠标事件
          var offset = $(this).offset();
          var x = e.pageX - offset.left;
          var y = e.pageY - offset.top;
          _this.css({'opacity':'0.3'});
          $(document).bind("mousemove",function(ev){//绑定鼠标的移动事件，因为光标在DIV元素外面也要有效果，所以要用doucment的事件，而不用DIV元素的事件
              _this.bind('selectstart',function(){return false;});
              var _x = ev.pageX - x;//获得X轴方向移动的值
              var _y = ev.pageY - y;//获得Y轴方向移动的值
              _this.css({'left':_x+"px",'top':_y+"px"});
          });
      });

      $(document).mouseup(function(){
          $(this).unbind("mousemove");
          _this.css({'opacity':''});
      })
  };
})(jQuery);


function openForm(title,url,icon,width,height, srcElement, opener, args, cb) {
  if(!title) {
	  alert("请设置窗口标题！");
	  return;
	}
	if(!url) {
	  alert("请设置窗口URL路径！");
	  return;
	}
  if(url.indexOf("/")!=0 && url.indexOf("../")!=0 && url.indexOf("http")!=0) 
    url = "/"+url;
  url += url.indexOf("?")<0 ? "?" : "&";
  url += "_isDebug=true";
  var form = $("#divFormTemplate").clone();
	form.attr("id", "form"+curZIndex);
	curFormId = "form"+curZIndex;
	form.css("zIndex", curZIndex);
	curZIndex++;
	if(width) form.css("width", width);
	if(height) form.css("height", height);
	var top = (document.documentElement.clientHeight-60-getPixel(form.css("height")))/2;
	if(top>0) form.css("top", parseInt(top)+"px");
	var left = (document.documentElement.clientWidth-getPixel(form.css("width")))/2;
	if(left>0) form.css("left", parseInt(left)+"px");
	if(icon) form.find("div.divFormIcon").css("backgroundImage", "url("+icon+")");
	form.find("div.divFormTitle").text(title);
	form.find("iframe.frameForm").attr("src", url);
	$.Move(form);
	form.appendTo("#bodyConsole");
  form.srcElement = $(srcElement);
  form.opener = opener;
  if(args) form.args = args;
  if(cb) form.cb = cb;
  formList[curFormId] = form;
  $("#divMask").show();
	form.show();
}

function openDialog(title, url, width, height, srcElement, opener, args, cb) {
  openForm(title, url, null, width, height, srcElement, opener, args, cb);
}

function closeSelfForm(formCloseBtn) {
  var form = formCloseBtn.parent().parent();
  closeForm(form);
}

function closeCurForm() {
  closeForm($("#"+curFormId));
}

function closeCurDialog() {
  closeCurForm();
}

function closeForm(form) {
  var formId = form.attr("id");
  delete formList[formId];
  form.remove();
  if(formId==curFormId) {
    curFormId = "";
    for(formId in formList) {
      curFormId = formId;
      return;
    }
  }
  $("#divMask").hide();
}

function closeDialog(id) {
  closeForm(formList[id]);
}

function getCurForm() {
  return formList[curFormId];
}

function getDialogArgs() {
  return getCurForm().args;
}

function getCurDialog() {
  return getCurForm();
}

function getCurDialogId() {
  return curFormId;
}

function onDialogOK(ret) {
  let curDialog = getCurDialog();
  if(curDialog && curDialog.cb) curDialog.cb(ret);
}

function openTabPage(title, url) {
  gotoPage(url);
}

function gotoPage(url) {
  if(url.indexOf("/")!=0 && url.indexOf("../")!=0 && url.indexOf("http")!=0) 
    url = "/"+url;
  url += url.indexOf("?")<0 ? "?" : "&";
  url += "_isDebug=true";
  $("#ifm").attr("src", url);
}
  
function changeFrameHeight(){
  var ifm= document.getElementById("ifm"); 
  ifm.height=document.documentElement.clientHeight;
}

window.onresize=function(){  
  changeFrameHeight();
} 

/**
 * 该方法由TaskBuilder在主进程中调用，以便传输当前用户的身份信息
 * @return {[type]} [description]
 */
function openTFP() {
  let url = getUrlArg("url");
  if(!url) return;
  gotoPage(url);
}