/**
 * staticka trida 
 */ 
var SWindow = function() {
	var pr = {
		w: 200, h: 0, opened: false, mo: new SJEL.Morph(),
		wh: null, whIn: null, whInIn: null, cross: 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.7,
		
		Init: function () {
			pr.wh = SJEL.CE("div");
			pr.whIn = SJEL.CE("div");
			pr.whInIn = SJEL.CE("div");
			pr.cross = SJEL.CE("div");
			//pr.bg = SJEL.CE("div");
			
			pr.wh.className = "s_win";
			pr.whIn.className = "s_win_in";
			pr.whInIn.className = "s_win_in_in";
			pr.cross.className = "s_win_cross";
			pr.wh.appendChild(pr.cross);
			pr.wh.appendChild(pr.whIn);
			pr.whIn.appendChild(pr.whInIn);

			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)		
		 */		 		
		Open: function (_w, _h, _t, _cl) {
			if (pr.opened)
				return false;
				
			pu.Init();
			SJEL.AddEvent(document, "keyup", pu.OnKeyUp);
		
			pr.w = _w;
			pr.h = _h;
			
			pu.SetText(_t);

			if (pr.w <= 0)
				pr.w = 200;
			
			if (_cl != undefined)
				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: ((wwh[0] - pr.w) / 2) + "px", top: (((wwh[1] - pr.h) / 2) + sy) + "px"});
				document.body.appendChild(pr.wh);
			} else {
				document.body.appendChild(pr.wh);
				SJEL.SStyle(pr.wh, {left: ((wwh[0] - pr.w) / 2) + "px", top: (((wwh[1] - pr.wh.offsetHeight) / 2) + sy) + "px"});
			}

			if (!SJEL.ie) {
				SJEL.SStyle(pr.wh, {opacity: 0.0});
				pr.mo.Init(pr.wh, {opacity: 1.0}, 250);
				pr.mo.Morph();
				pr.mo.OnMorphFinished(function(){});
			}

			pr.opened = true;

			return true;
		},
		
		SetText: function(_t) {
			if (_t == undefined)
				return;

			if (typeof(_t) == "string")
				pr.whInIn.innerHTML = _t;
			else if (typeof(_t) == "object") {
				//alert('iiieeeeeeeeeee');
				pr.whInIn.innerHTML = "";
				pr.whInIn.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);

			if (!SJEL.ie) {
				pr.mo.Init(pr.wh, {opacity: 0.0}, 250);
				pr.mo.Morph();
				pr.mo.OnMorphFinished(function(){document.body.removeChild(pr.wh);});
			//document.body.removeChild(pr.bg);
			} else
				document.body.removeChild(pr.wh);

			pr.opened = false;
		}
	};

	return pu;
}();

//SWindow.Init();