﻿var SWT_Window = Class.create();

SWT_Window.prototype = {
	initialize: function (w, h, title, _id) {
		this._id = _id ? _id : "swt_win_" + swt.rndn(6);
		this.title = title || "";
		this.win = document.createElement("div");
		this.win.id = this._id;
		this.toolBar = document.createElement("div");
		this.toolBar.id = this._id + "_toolBar";
		this.body = document.createElement("div");
		this.body.id = this._id + "_body";
		this.win.className = "swt_win";
		this.toolBar.className = "swt_win_toolBar";
		this.body.className = "swt_win_body";
		this.win.appendChild(this.toolBar);
		this.win.appendChild(this.body);
		this.win.style.width = w + "px";
		if (h) this.body.style.height = h + "px";
		this.body.style.width = (w - 94) + "px";
		
		var _o = document.createElement("img"), _t = this;
		_o.src = "/swt/img/close.gif";
		_o.alt = "close";
		_o.className = "close";
		_o.onclick = function () {_t.close();};
		this.toolBar.appendChild(_o);
		
		_o = document.createElement("span");
		_o.className = "swt_loading";
		_o.appendChild(document.createTextNode("loading..."));
		this.body.appendChild(_o);
		
		this.updateTitle(title);
	},
	close: function () {
		this.win.style.display = "none";
		swt.overLay(0, swt.gradualOverlay);
	},
	show: function () {
		this.win.style.display = "block";
		swt.appendChild(this.win, arguments[0]);
		this.width = this.win.offsetWidth;
		this.height = this.win.offsetHeight;
		var _w = document.documentElement.clientWidth,
			_h = document.documentElement.clientHeight,
			_t = document.documentElement.scrollTop;
		var _l = (_w - this.width) / 2, _t2 = (_h - this.height) / 2 + _t;
		this.win.style.left = ((_l >= 0) ? _l : 0) + "px";
		this.win.style.top = ((_t2 >= 0) ? _t2 : 0) + "px";
	},
	updateTitle: function (title) {
		swt.removeChildrenByTag(this.toolBar, "span");
		var o = document.createElement("span");
		o.appendChild(document.createTextNode(title));
		o.className = "title";
		this.toolBar.appendChild(o);
	}
};