tom.widget = {};
tom.space = {};
tom.space.config = {};
/**
 * class Component
 */
tom.widget.Component = tom.Class.create();
tom.extend(tom.widget.Component.prototype, {
  initialize: function(id, params) {
  	params = params ? params : {};
    //All "Container"'s properties here.
    var options = {
      visible: true,
      id: id,
      element: null,
      styleClass: "",
      zIndex: 0,
      fnCreateElement: null
    };
    tom.extend(this, options, params);
    //If function "createElement" exists, call it for creating element.
    if (this.fnCreateElement) {
      this.element = this.fnCreateElement();
    } else {
      this.element = typeof(id) == "string" ? $(id) : id;
    }
    this._lastDisplay = tom.Element.getStyle(this.element, "display");
  },
  getId: function() {
  	return this.id;
  },
  getElement: function() {
  	return this.element;
  },
  isVisible: function() {
  	return this.visible;
  },
  setVisible: function(v) {
  	this.visible = v;
  	this.element.style.display = v ? "block" : "none";
  },
  setWidth: function(w) {
  	this.getElement().style.width = w + "px";
  },
  setHeight: function(h) {
  	this.getElement().style.height = h + "px";
  },
  setLeft: function(l) {
  	this.getElement().style.left = l + "px";
  },
  setTop: function(t) {
  	this.getElement().style.top = t + "px";
  },
  getWidth: function() {
  	return this.getElement().offsetWidth;
  },
  getHeight: function() {
  	return this.getElement().offsetHeight;
  },
  setStyleClass: function(c) {
  	this.styleClass = c;
  	this.element.className = c;
  },
  setZIndex: function(zIndex) {
  	this.zIndex = zIndex;
  	this.getElement().style.zIndex = zIndex;
  },
  getZIndex: function() {
  	return this.zIndex;
  }
});

/**
 * class Window
 * extends tom.widget.Component
 */
tom.widget.Window = tom.Class.create();
tom.extend(tom.widget.Window.prototype, tom.widget.Component.prototype, {
  initialize: function(id, param) {
    tom.widget.Component.prototype.initialize.apply(this, [id, param]);
    //All "Window"'s properties here.
    var options = {
      handle: id,
      title: null,
      btnClose: null,
      zIndex: 1,
      isModal: false,
      canDrag: true,
      maskOpacity: 0.3
    };
    tom.extend(this, options, param);
    
    this.setZIndex(this.zIndex);
    this.title = $(this.title);
    
    var _w = this;
    //Add window close event.
    if (this.btnClose) {
      this.btnClose = $(this.btnClose);
      this.btnClose.onmousedown = function(e) {
        tom.Event.stop(e || window.event);
      }
      this.btnClose.onclick = function(e) {
        e = e || window.event;
        if (_w.onClose(e)) {
          _w.hide();
        }
        tom.Event.stop(e);
      }
    }
    if (this.canDrag) {
      this.handle = $(this.handle);
      new tom.dnd.Draggable(this.getId(), this);
    }
    
    //模态遮罩
    this.maskWindow = new tom.widget.MaskWindow(this.id + "_moduleframe", 
      {fnCreateElement: function() {
        var elem = document.createElement("DIV");
        elem.id = _w.id + "_moduleframe";
        tom.extend(elem.style, {
      	  display: "none",
		  position: "absolute",
		  backgroundColor: "#000",
		  zIndex: _w.zIndex - 1,
		  opacity: _w.maskOpacity,
		  filter: "Alpha(Opacity=" + _w.maskOpacity*100 + ")"
        });
        document.body.appendChild(elem);
        return elem;
      }}
    );
  },
  setTitle: function(t) {
    this.title.innerHTML = t;
  },
  getTitle: function() {
  	return this.title.innerHTML;
  },
  show: function() {
  	if (this.isModal) {
  	  this.maskWindow.setVisible(true);
      this.maskWindow.show();
  	}
  	this.setVisible(true);
  },
  hide: function() {
  	if (this.isModal) {
  	  this.maskWindow.setVisible(false);
  	}
  	this.setVisible(false);
  },
  isShow: function() {
  	return this.getVisible();
  },
  setCenter: function() {
    this.setLeft(tom.Position.getWindowScrollLeft() + (tom.Position.getWindowClientWidth() - this.getWidth()) / 2);
    this.setTop(tom.Position.getWindowScrollTop() + (tom.Position.getWindowClientHeight() - this.getHeight()) / 2);
  },
  onClose: tom.fnTrue
});

/**
 * class MaskWindow
 */
tom.widget.MaskWindow = tom.Class.create();
tom.extend(tom.widget.MaskWindow.prototype, tom.widget.Component.prototype, {
  initialize: function(id, params) {
  	tom.widget.Component.prototype.initialize.apply(this, [id, params]);
  },
  show: function() {
  	this.setVisible(true);
    this.setLeft(0);
    this.setTop(0);
    this.setWidth(tom.Position.getWindowScrollWidth());
    this.setHeight(tom.Position.getWindowScrollHeight());
  },
  hide: function() {
  	this.setVisible(false);
  }
});

/**
 * class Menu
 * extends tom.widget.Component
 */
tom.widget.Menu = tom.Class.create();
tom.extend(tom.widget.Menu.prototype, tom.widget.Component.prototype, {
  initialize: function(id, params) {
    //fields
    var fields = {
      items: new tom.util.Set(),
      container: null,
      fnCreateElement: function() {
        var el = document.createElement("DIV");
        el.style.width = "96px";
        el.style.border = "1px solid #999";
        el.style.backgroundColor = "#fff";
        el.style.display = "none";
        el.style.position = "absolute";
        el.style.padding = "1px";
        document.body.appendChild(el);
        return el;
      }
    };
    tom.extend(fields, params);
    //call super()
    tom.widget.Component.prototype.initialize.apply(this, [id, fields]);
    this.container = this.container ? (typeof(this.container) == "string" ? $(this.container) : this.container) : this.element;
    this.setVisible(false);
    //
    var _this = this;
    
    this._cbDocClick = function(e) {
  	    if (_this.isVisible()) _this.hide(false);
  	    tom.Event.stop(e||window.event);
  	    setTimeout(function() {
  	        tom.Event.removeEvent(document, "click", _this._cbDocClick);
  	      }, 10);
  	    return false;
      }
  },
  addItem: function(mi) {
  	this.items.add(mi);
  	mi.menu = this;
  	this.container.appendChild(mi.element);
  	return mi;
  },
  removeItem: function(mi) {
  	this.items.removeObject(mi);
  	mi.menu = null;
  	this.container.removeChild(mi.element);
  	return mi;
  },
  show: function(x, y) {
    if (!this.isVisible()) {
      this.setVisible(true);
      var _this = this;
      tom.Event.addEvent(document, "click", this._cbDocClick, false);
    }
  },
  hide: function() { this.setVisible(false); },
  showOnPointer: function(e) {
  	if (!this.isVisible()) {
  	  var p = tom.Event.pointer(e);
  	  this.setLeft(p.x);
  	  this.setTop(p.y);
  	  this.show();
  	}
  }

});
/**
 * class MenuItem
 */
tom.widget.MenuItem = tom.Class.create();
tom.extend(tom.widget.MenuItem.prototype, tom.widget.Component.prototype, {
  initialize: function(id, params) {
    //fields
	var fields = {
	  text: "menuitem",
	  onMouseOver: function() { this.element.style.backgroundColor = "#ddd"; },
	  onMouseOut: function() { this.element.style.backgroundColor = ""; },
	  onClick: tom.fnEmpty,
	  menu: null,
	  fnCreateElement: function() {
	    var el = document.createElement("DIV");
	    el.style.cursor = "default";
	    el.style.padding = "1px 4px";
	    el.innerHTML = this.text;
	    return el;
	  }
	};
	tom.extend(fields, params);
  	//Call super.
    tom.widget.Component.prototype.initialize.apply(this, [id, fields]);

    var _this = this;
    this.element.onmousemove = function(e) {
      e = e || window.event;
      _this.onMouseOver(e);
      tom.Event.stop(e);
      return false;
    };
    this.element.onmouseout = function(e) {
      e = e || window.event;
      _this.onMouseOut(e);
      tom.Event.stop(e);
      return false;
    };
    this.element.onclick = function(e) {
      e = e || window.event;
      _this.onClick(e);
      return false;
    };
  }
});



//package tom.space
tom.config.alert._fnCallBack = function(msg, ico, fn, autoHide) {
  if (!tom.alert.singleton) {
  	tom.alert.singleton = new tom.space.AlertWindow("", {isModal: true});
  }
  var aw = tom.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 = tom.fnTrue;
  	  break;
  }
  aw.setIcon(ico || "OK");
  aw.setMessage(msg);
  aw.setAutoHide(typeof(autoHide) == "undefined" ? -1 : autoHide);
  aw.show();
  aw.setCenter();
}

tom.config.confirm._fnCallBack = function(msg, fnOk, fnCancel, title) {
  if (!tom.confirm.singleton) {
  	tom.confirm.singleton = new tom.space.ConfirmWindow();
  }
  var cw = tom.confirm.singleton;
  cw.fnOk = fnOk ? (fnOk instanceof Function) ? fnOk : (typeof(fnOk) == "string" ? new Function("e", fnOk) : tom.fnTrue) : tom.fnTrue;
  cw.fnCancel = fnCancel ? (fnCancel instanceof Function) ? fnCancel : (typeof(fnCancel) == "string" ? new Function("e", fnCancel) : tom.fnTrue) : tom.fnTrue;;
  cw.setTitle(title || "确认");
  cw.setMessage(msg);
  cw.show();
  cw.setCenter();
}
tom.config.information._fnCallBack = function(msg, autoHide, fn) {
  if (!tom.information.singleton) {
  	tom.information.singleton = new tom.space.InformationWindow();
  }
  var iw = tom.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 = tom.fnTrue;
  	  break;
  }
  iw.setMessage(msg);
  iw.setAutoHide(autoHide);
  iw.show();
  iw.setCenter();
}

tom.config.login._fnCallBack = function(backurl) {
  if (!tom.login.singleton) {
  	tom.login.singleton = new tom.space.LoginWindow();
  }
  var lw = tom.login.singleton;
  lw.setBackurl(backurl);
  lw.show();
  lw.setCenter();
}

/*
 * class tom.space.AlertWindow
 */
tom.space.AlertWindow = tom.Class.create();
tom.extend(tom.space.AlertWindow.prototype, tom.widget.Window.prototype, {
  initialize: function(id, params) {
  	var fields = {
  	  autoHide: 0,
  	  timeout: null,
  	  zIndex: 10000
  	};
  	tom.extend(fields, tom.space.config.Alert, params || {});
    tom.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);
    
    /*
    tom.extend(this.getElement().style, {
      position: "absolute",
      width: "240px",
      backgroundColor: "#fff",
      zIndex: _w.zIndex
    });
    */
  },
  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: tom.fnEmpty,
  show: function() {
  	tom.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
      );
    }
  }
});


tom.space.config.Alert = {
  id: "index_Alert",
  className: "win",
  handle: "index_Alert_handle",
  btnClose: "index_Alert_close",
  btnOk: "index_Alert_ok",
  content: "index_Alert_content",
  icon: "index_Alert_icon",
  zIndex: 10000
};
tom.space.config.Alert.fnCreateElement = function(e) {
  var cfg = tom.space.config.Alert;
  var elem = document.createElement("DIV");
  elem.id = cfg.id;
  elem.className = cfg.className;
  elem.style.position = "absolute";
	
  elem.innerHTML = '\
	<div id="' + tom.space.config.Alert.handle + '" class="wintitle">\
		<strong id="' + tom.space.config.Alert.title + '">提示</strong>\
		<span><a href="#"><img id="' + tom.space.config.Alert.btnClose + '" src="/images/winclose.gif" /></a></span>\
	</div>\
	<div class="wincnt">\
		<dl class="winpic">\
			<dt><img id="' + tom.space.config.Alert.icon + '" src="/images/ok.gif" alt="" /></dt>\
			<dd id="' + tom.space.config.Alert.content + '">这是正确信息……</dd>\
		</dl>\
		<div class="winsingle button"><input id="' + tom.space.config.Alert.btnOk + '" class="winbtn" type="button" value="确 定" /></div>\
	</div>';
	
  document.body.appendChild(elem);
  return elem;
}


tom.space.ConfirmWindow = tom.Class.create();
tom.extend(tom.space.ConfirmWindow.prototype, tom.widget.Window.prototype, {
  initialize: function(id, params) {
  	var fields = {
  	  isModal: true,
  	  zIndex: 10000
  	};
  	tom.extend(fields, tom.space.config.Confirm, params || {});
    //Call super.
    tom.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;
  }
});


tom.space.config.Confirm = {
  id: "tom_space_Confirm",
  className: "win",
  handle: "tom_space_Confirm_handle",
  btnClose: "tom_space_Confirm_close",
  btnOk: "tom_space_Confirm_ok",
  btnCancel: "tom_space_Confirm_cancel",
  title: "tom_space_Confirm_title",
  content: "tom_space_Confirm_content",
  icon: "tom_space_Confirm_icon",
  zIndex: 10000
};
tom.space.config.Confirm.fnCreateElement = function(e) {
  var cfg = tom.space.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" /></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);
  return elem;
}

/*
 * class tom.space.InformationWindow
 */
tom.space.InformationWindow = tom.Class.create();
tom.extend(tom.space.InformationWindow.prototype, tom.widget.Window.prototype, {
  initialize: function(id, params) {
  	var fields = {
  	  autoHide: 1000,
  	  canDrag: false,
  	  isModal: true,
  	  timeout: null
  	};
  	tom.extend(fields, tom.space.config.Information, params || {});
    //Cal super.
    tom.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() {
  	tom.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
      );
    }
  }
});

tom.space.config.Information = {
  id: "tom_space_info",
  className: "win",
  content: "tom_space_info_content",
  icon: "tom_space_info_icon",
  zIndex: 10000
};
tom.space.config.Information.fnCreateElement = function(e) {
  var cfg = tom.space.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);
  return elem;
}

tom.space.LoginWindow = tom.Class.create();
tom.extend(tom.space.LoginWindow.prototype, tom.widget.Window.prototype, {
  initialize: function(id, params) {
  	var fields = {
  	  canDrag: false,
  	  isModal: true
  	};
  	tom.extend(fields, tom.space.config.LoginWindow, params || {});
    //Cal super.
    tom.widget.Window.prototype.initialize.apply(this, [fields.id, fields]);
    
    var _this = this;
    this.userName = $(this.userName);
    this.backurl = $(this.backurl);
    this.elBtnCancel = $(this.btnCancel);
    this.elBtnCancel.onclick = function(e) {
      _this.hide();
    }
  },
  setBackurl: function(backurl) {
    backurl = backurl ? backurl : document.location;
    $("backurl_lw").value = backurl;
  }
});

tom.space.config.LoginWindow = {
  id: "tom_space_login",
  className: "win",
  handle: "tom_space_login_title",
  btnClose: "tom_space_login_close",
  btnCancel: "tom_space_login_cancel",
  zIndex: 100
};
tom.space.config.LoginWindow.fnCreateElement = function(e) {
  var cfg = tom.space.config.LoginWindow;
  var elem = document.createElement("DIV");
  elem.id = cfg.id;
  elem.className = cfg.className;
  elem.style.position = "absolute";
  
  url = document.location;
  if(/\%25u/.test(url)){
		url = encodeURIComponent(url);  
  }
  elem.innerHTML =
    '<div id="' + cfg.handle + '" class="wintitle">\
      <strong><img src="/images/winlogo.gif" /></strong>\
      <span><a href="#"><img id="' + cfg.btnClose + '" src="/images/winclose.gif" /></a></span>\
     </div>\
     <div class="wincnt logcnt">\
      <h3>登录</h3>\
	  <form id="loginFormLw" action="http://pass.tom.com/dologin.php" method="post" onsubmit="return tom.login.onSubmit();">\
    	<input type="hidden" name="backurl" id="backurl_lw" value="' + url + '" />\
        <input type="hidden" name="svcid" value="1" />\
        <input type="hidden" name="issave" value="1" />\
        <input type="hidden" name="doAction" value="1" />\
      <dl class="winlogin">\
       <dt><label for="tomidlogin">用户名：</label></dt>\
       <dd><input type="text" name="tomid" id="tomidlogin" class="input" onblur="tom.login.autoCompleteUserId();" /></dd>\
       <dt><label for="tompwdlogin">密&nbsp;&nbsp;码：</label></dt>\
       <dd><input type="password" name="tompwd" id="tompwdlogin" class="input" /></dd>\
       <dt>&nbsp;</dt>\
       <dd><input type="checkbox" name="issave" value="1" checked="checked" />记住密码</dd>\
       <dt>&nbsp;</dt>\
       <dd><button type="submit" class="winbtn">登 录</button><button id="'+this.btnCancel+'" type="reset" class="winbtn">取 消</button></dd>\
       <dt>&nbsp;</dt>\
       <dd><a href="http://pass.tom.com/recover.php">忘记密码</a>&nbsp;&nbsp;<a href="http://pass.tom.com/register.php?svcid=1">注册用户</a>&nbsp;&nbsp;<a href="http://pass.tom.com/help.html" target="_blank">帮助</a></dd>\
      </dl>\
      </form>\
     </div>';
  document.body.appendChild(elem);
  return elem;
}
    