﻿ if (typeof(dw) == "undefined") {
  dw = {};
}

if (typeof(dw.app) == "undefined") {
  dw.app = {};
}

if (typeof(dw.app.config) == "undefined") {
  dw.app.config = {};
}
dw.config.alert._fnCallBack = function(msg, ico, fn, autoHide) {
  if (!dw.alert.singleton) {
  	dw.alert.singleton = new dw.app.AlertWindow("", {isModal: true});
  }
  var aw = dw.alert.singleton
  switch (true) {
  	case typeof(fn) == "string":
  	  aw.fnCallback = new Function("e", fn);
  	  break;
  	case fn instanceof Function:
  	  aw.fnCallback = fn;
  	  break;
  	default:
  	  aw.fnCallback = dw.fnTrue;
  	  break;
  }
  aw.setIcon(ico || "OK");
  aw.setMessage(msg);
  aw.setAutoHide(typeof(autoHide) == "undefined" ? -1 : autoHide);
  aw.show();
  aw.setCenter();
};
       
dw.config.confirm._fnCallBack = function(msg, fnOk, fnCancel, title) {
  if (!dw.confirm.singleton) {
  	dw.confirm.singleton = new dw.app.ConfirmWindow();
  }
  var cw = dw.confirm.singleton;
  cw.fnOk = fnOk ? (fnOk instanceof Function) ? fnOk : (typeof(fnOk) == "string" ? new Function("e", fnOk) : dw.fnTrue) : dw.fnTrue;
  cw.fnCancel = fnCancel ? (fnCancel instanceof Function) ? fnCancel : (typeof(fnCancel) == "string" ? new Function("e", fnCancel) : dw.fnTrue) : dw.fnTrue;;
  cw.setTitle(title || "确认");
  cw.setMessage(msg);
  cw.show();
  cw.setCenter();
}

dw.config.information._fnCallBack = function(msg, autoHide, fn) {
  if (!dw.information.singleton) {
  	dw.information.singleton = new dw.app.InformationWindow();
  }
  var iw = dw.information.singleton;
  switch (true) {
  	case typeof(fn) == "string":
  	  iw.fnCallback = new Function("e", fn);
  	  break;
  	case fn instanceof Function:
  	  iw.fnCallback = fn;
  	  break;
  	default:
  	  iw.fnCallback = dw.fnTrue;
  	  break;
  }
  iw.setMessage(msg);
  iw.setAutoHide(autoHide);
  iw.show();
  iw.setCenter();
}


/*
 * class dw.app.AlertWindow
 */
dw.app.AlertWindow = dw.Class.create();
dw.extend(dw.app.AlertWindow.prototype, dw.widget.Window.prototype, {
  initialize: function(id, params) {
  	var fields = {
  	  autoHide: 0,
  	  timeout: null,
  	  zIndex: 10000
  	};
  	dw.extend(fields, dw.app.config.Alert, params || {});
    dw.widget.Window.prototype.initialize.apply(this, [fields.id, fields]);
    
    var _w = this;
    this.btnOk = $(this.btnOk);
    this.btnOk.onclick = function(e) {
      _w.hide();
      _w.fnCallback(e || window.event);
  	  if (_w.timeout) {
  	    clearTimeout(_w.timeout);
  	    _w.timeout = null;
  	  }
    }
    this.content = $(this.content);
    this.icon = $(this.icon);
  },
  setMessage: function(msg) {
  	this.content.innerHTML = msg.replace(/\r\n/i, "<br />");
  },
  setIcon: function(ico) {
  	switch (ico) {
  	  case "INFO":
  	    this.icon.src = "/images/info.gif";
  	    break;
  	  case "ERROR":
  	    this.icon.src = "/images/error.gif";
  	    break;
  	  case "OK":
  	    this.icon.src = "/images/ok.gif";
  	    break;
  	  default:
  	    break;
  	}
  },
  setAutoHide: function(ah) {
  	this.autoHide = ah;
  },
  onClose: function(e) {
  	this.maskWindow.setVisible(false);
  	this.fnCallback(e || window.event);
  	if (this.timeout) {
  	  clearTimeout(this.timeout);
  	  this.timeout = null;
  	}
  	return true;
  },
  fnCallback: dw.fnEmpty,
  show: function() {
  	dw.widget.Window.prototype.show.apply(this);
  	var _w = this;
    if (this.autoHide != -1) {
      this.timeout = setTimeout(
        function() {
	      _w.hide();
	      _w.maskWindow.setVisible(false);
	      _w.fnCallback();
	      _w.timeout = null;
        }, 
        this.autoHide
      );
    }
  }
});

dw.app.config.Alert = {
  id: "app_Alert",
  className: "win",
  handle: "app_Alert_handle",
  btnClose: "app_Alert_close",
  btnOk: "app_Alert_ok",
  content: "app_Alert_content",
  icon: "app_Alert_icon",
  zIndex: 10000
};
dw.app.config.Alert.fnCreateElement = function(e) {
  var cfg = dw.app.config.Alert;
  var elem = document.createElement("DIV");
  elem.id = cfg.id;
  elem.className = cfg.className;
  elem.style.position = "absolute";
	
  elem.innerHTML = '\
	<div id="' + dw.app.config.Alert.handle + '" class="wintitle">\
		<strong id="' + dw.app.config.Alert.title + '">提示</strong>\
		<span><a href="#"><img id="' + dw.app.config.Alert.btnClose + '" src="../images/winclose.gif"  border="0" /></a></span>\
	</div>\
	<div class="wincnt">\
		<dl class="winpic">\
			<dt><img id="' + dw.app.config.Alert.icon + '" src="/images/ok.gif" alt="" /></dt>\
			<dd id="' + dw.app.config.Alert.content + '">这是正确信息……</dd>\
		</dl>\
		<div class="winsingle button"><input id="' + dw.app.config.Alert.btnOk + '" class="winbtn" type="button" value="确 定" /></div>\
	</div>';
	
  document.body.appendChild(elem);
}

dw.app.ConfirmWindow = dw.Class.create();
dw.extend(dw.app.ConfirmWindow.prototype, dw.widget.Window.prototype, {
  initialize: function(id, params) {
  	var fields = {
  	  isModal: true,
  	  zIndex: 10000
  	};
  	dw.extend(fields, dw.app.config.Confirm, params || {});
    //Call super.
    dw.widget.Window.prototype.initialize.apply(this, [fields.id, fields]);
    
    var _w = this;
    this.btnOk = $(this.btnOk);
    this.btnOk.onclick = function(e) {
      _w.hide();
      _w.fnOk(e || window.event);
    }
    this.btnCancel = $(this.btnCancel);
    this.btnCancel.onclick = function(e) {
      _w.hide();
      _w.fnCancel(e || window.event);
    }
    
    this.title = $(this.title);
    this.content = $(this.content);
    this.icon = $(this.icon);
  },
  setTitle: function(title) {
  	this.title.innerHTML = title;
  },
  setMessage: function(msg) {
  	this.content.innerHTML = msg;
  },
  onClose: function(e) {
  	this.fnCancel(e || window.event);
  	return true;
  },
  fnOk: function(e) {
  	return;
  },
  fnCancel: function(e) {
  	return;
  }
});


dw.app.config.Confirm = {
  id: "ir_Confirm",
  className: "win",
  handle: "ir_Confirm_handle",
  btnClose: "ir_Confirm_close",
  btnOk: "ir_Confirm_ok",
  btnCancel: "ir_Confirm_cancel",
  title: "ir_Confirm_title",
  content: "ir_Confirm_content",
  icon: "ir_Confirm_icon",
  zIndex: 10000
};
dw.app.config.Confirm.fnCreateElement = function(e) {
  var cfg = dw.app.config.Confirm;
  var elem = document.createElement("DIV");
  elem.id = cfg.id;
  elem.className = cfg.className;
  elem.style.position = "absolute";
  
  elem.innerHTML = '\
	<div id="' + cfg.handle + '" class="wintitle">\
		<strong id="' + cfg.title + '">正确信息</strong>\
		<span>\
			<a href="#"><img id="' + cfg.btnClose + '" src="../images/winclose.gif" border="0" /></a>\
		</span>\
	</div>\
	<div class="wincnt">\
		<dl class="winpic">\
			<dt><img src="/images/question.gif" alt="" /></dt>\
			<dd id="' + cfg.content + '">这是正确信息……</dd>\
		</dl>\
		<div class="winsingle button">\
			<input id="' + cfg.btnOk + '" class="winbtn" type="button" value="确 定" /> \
			<input id="' + cfg.btnCancel + '" class="winbtn" type="button" value="取 消" />\
		</div>\
	</div>';
  
  document.body.appendChild(elem);
}

/*
 * class dw.app.InformationWindow
 */
dw.app.InformationWindow = dw.Class.create();
dw.extend(dw.app.InformationWindow.prototype, dw.widget.Window.prototype, {
  initialize: function(id, params) {
  	var fields = {
  	  autoHide: 1000,
  	  canDrag: false,
  	  isModal: true,
  	  timeout: null
  	};
  	dw.extend(fields,dw.app.config.Information, params || {});
    //Cal super.
    dw.widget.Window.prototype.initialize.apply(this, [fields.id, fields]);
    
    this.content = $(this.content);
    this.icon = $(this.icon);
  },
  setMessage: function(msg) {
  	this.content.innerHTML = msg.replace(/\r\n/i, "<br />");
  },
  setAutoHide: function(ah) {
  	this.autoHide = ah;
  },
  onClose: function(e) {
  	this.fnCallback(e || window.event);
  	if (this.timeout) {
  	  clearTimeout(this.timeout);
  	  this.timeout = null;
  	}
  	return true;
  },
  fnCallback: function(e) {
  	return;
  },
  show: function() {
  	dw.widget.Window.prototype.show.apply(this);
  	var _w = this;
    if (this.autoHide != -1) {
      this.timeout = setTimeout(
        function() {
	      _w.hide();
	      _w.fnCallback();
	      _w.timeout = null;
        }, 
        this.autoHide
      );
    }
  }
});

dw.app.config.Information = {
  id: "ir_info",
  className: "win",
  content: "ir_info_content",
  icon: "ir_info_icon",
  zIndex: 10000
};
dw.app.config.Information.fnCreateElement = function(e) {
  var cfg = dw.app.config.Information;
  var elem = document.createElement("DIV");
  elem.id = cfg.id;
  elem.className = cfg.className;
  elem.style.position = "absolute";
  elem.innerHTML = 
	'\
		<div class="wincnt">                              \
			<dl class="winpic">                             \
				<dt><img src="/images/ok.gif" /></dt>\
				<dd id="' + cfg.content + '">这是提示信息……</dd>                     \
			</dl>                                           \
		</div>                                            \
	</div>';

  document.body.appendChild(elem);
}

dw.app.FrameWindow = dw.Class.create();
dw.extend(dw.app.FrameWindow.prototype, dw.widget.Window.prototype, {
    initialize: function(id, params) {
  	var fields = {
  	  autoHide: 0,
  	  id: "mod_popframe",
  	  btnClose: "fw_frame_close",
  	  title:"title_frame",
  	  width:"",
  	  height:"",
  	  frmUrl :'',
  	  isModal: true,
  	  canDrag: true,
  	  timeout: null,
  	  zIndex: 20000,
  	  fnCreateElement: function() {
	    var elem = document.createElement("DIV");
	    elem.id = fields.id;
	    elem.className = "frame";
	    elem.style.display = "none";
	    elem.style.position = "absolute";
	    elem.style.width = this.width;
	    elem.innerHTML = '<div>\
	<div id="title_handle" class="frametitle"  >\
		<strong id="title_frame">正确信息</strong>\
		<span>\
			<a href="#"><img id="fw_frame_close" src="../images/winclose.gif" border="0" title="关闭" /></a>\
		</span>\
	</div>\
	<div class="framecnt">\
		<iframe id="frame_content" src="'+this.frmUrl+'" scrolling="auto" frameborder="0" width="'+this.width+'" marginwidth="0" height="'+this.height+'" marginheight="0" ></iframe>\
	</div></div>';
	    document.body.appendChild(elem);
	    return elem;
	  }
  	};
  	dw.extend(fields, params);
    dw.widget.Window.prototype.initialize.apply(this, [fields.id, fields]);
    this.title = $(this.title);
    },
   show: function() {
  	dw.widget.Window.prototype.show.apply(this);
  	//this.maskWindow.setVisible(true);
  	this.setVisible(true);
  },
  setTitle: function(title) {
  	this.title.innerHTML = title;
  },
  onClose: function(e) {
  	this.maskWindow.setVisible(false);
  	this.setVisible(false);
  	if (this.timeout) {
  	  clearTimeout(this.timeout);
  	  this.timeout = null;
 }

  	document.body.removeChild($(this.id));
  	return true;
  }
});
