/**
 * staticka trida
 */
var SWindow = function() {
	var pr = {
		w: 505, h: 0, opened: false,
		wh: null, whIn: null, cross: null, bg: null,
		sel: null, closeFunc: null,

		// toto cele kvuli IE6 bitch
		HideSelects: function() {
			if (pr.sel == null)
				pr.sel = SJEL.$T(document.body, "select");
			for (var i = 0; i < pr.sel.length; i++)
				SJEL.SStyle(pr.sel[i], {visibility: "hidden"});
		},

		ShowSelects: function() {
			for (var i = 0; i < pr.sel.length; i++)
				SJEL.SStyle(pr.sel[i], {visibility: "visible"});
		}
	};

	var pu = {
		bgCol: "#000", opac: 0.8,

		Init: function ( _close ) {
			pr.wh = SJEL.CE("div");
			pr.whIn = SJEL.CE("div");
			pr.whTop = SJEL.CE("div");
			pr.whBottom = SJEL.CE("div");
			pr.cross = SJEL.CE("div");
			pr.title = SJEL.CE("div");
			pr.bg = SJEL.CE("div");

			pr.wh.className = "s_win";
			pr.whIn.className = "s_win_in";
			pr.whTop.className = "s_win_top";
			pr.whBottom.className = "s_win_bottom";
			pr.cross.className = "s_win_cross";
			var ttt = _close + " " || "close";
			
			pr.cross.appendChild( SJEL.CTN( ttt ) );
			pr.title.className = "s_win_title";
			pr.wh.appendChild(pr.cross);
			pr.wh.appendChild(pr.title);
			pr.wh.appendChild(pr.whTop);
			pr.wh.appendChild(pr.whIn);
			pr.wh.appendChild(pr.whBottom);

			SJEL.AddEvent(pr.cross, "click", function() {
				pu.Close();
			});

			SJEL.SStyle(pr.bg, {position: "fixed", zIndex: 5999, top: "0px", left: "0px", width: "100%", height: "100%", backgroundColor: pu.bgCol, opacity: pu.opac});

			if (SJEL.ie == 6) {
				SJEL.SStyle(pr.wh, {position: "absolute"});
				SJEL.SStyle(pr.bg, {position: "absolute"});
			}

			SJEL.AddEvent(pr.bg, "click", function() {
				pu.Close();
			});
		},

		/**
		 * _w - width, _h - height, _t - string or element, _cl - class name (added to div with s_win)
		 * _title - window title
		 */
		Open: function (_w, _h, _t, _cl, _title, _close ) {
			if (pr.opened)
				return false;

			pu.Init( _close || '' );
			SJEL.AddEvent(document, "keyup", pu.OnKeyUp);

			pr.w = _w;
			pr.h = _h;

			pu.SetText(_t);

			if (pr.w <= 0)
				pr.w = 200;

			if (_title) {
				if (typeof _title == "string")
					pr.title.innerHTML = _title;
				else
					pr.title.appendChild(_title);
			}

			if (_cl)
				SJEL.AddClass(pr.wh, _cl);

			SJEL.SStyle(pr.wh, {width: pr.w + "px"});

			if (pr.h > 0)
				SJEL.SStyle(pr.wh, {height: pr.h + "px"});

			if (SJEL.ie == 6) {
				SJEL.SStyle(pr.bg, {height: document.body.offsetHeight + "px"});
				pr.HideSelects();
			}

			var wwh = SJEL.GWindowWH(), sy = 0;

			if (SJEL.ie == 6)
				sy = SJEL.GScrollXY()[1];

			document.body.appendChild(pr.bg);
			if (pr.h > 0) {
				SJEL.SStyle(pr.wh, {left: Math.floor((wwh[0] - pr.w) / 2) + "px", top: Math.floor(((wwh[1] - pr.h) / 2) + sy) + "px"});
				document.body.appendChild(pr.wh);
			} else {
				document.body.appendChild(pr.wh);
				SJEL.SStyle(pr.wh, {left: Math.floor((wwh[0] - pr.w) / 2) + "px", top: Math.floor(((wwh[1] - pr.wh.offsetHeight) / 2) + sy) + "px"});
			}


			pr.opened = true;

			return true;
		},

		SetText: function(_t) {
			if (_t == undefined)
				return;

			if (typeof(_t) == "string")
				pr.whIn.innerHTML = _t;
			else if (typeof(_t) == "object") {
				//alert('iiieeeeeeeeeee');
				pr.whIn.innerHTML = "";
				pr.whIn.appendChild(_t);
			}
		},

		OnKeyUp: function(_e) {
			if (!_e)
				_e = event;

			switch (_e.keyCode) {
				case 27: pu.Close(); break;  // esc
			}
		},

		OnCloseFunc: function (_f) {
			pr.closeFunc = _f;
		},

		Close: function () {
			if (pr.closeFunc != null)
				pr.closeFunc();

			if (SJEL.ie == 6)
				pr.ShowSelects();

			SJEL.RemoveEvent(document, "keyup", pu.OnKeyUp);
			document.body.removeChild(pr.bg);
			document.body.removeChild(pr.wh);

			pr.opened = false;
		}
	};

	return pu;
}();

//SWindow.Init();
