var Window = {
	list : [],
	callbacks : [],
	callbackscancel : [],
	count : 0,
	hooked : false,
	reverse : false,
	Gray : function(id)
	{
		var r = Resolution();
		var w = r.Width, h = r.Height;
		
		var g = document.createElement("div");
		g.style.backgroundColor = "white";
		g.id = id+"_gray";
		S_Opacity(g,"80");
		g.style.zIndex = (11000+(Window.list.length*2)-1);
		g.style.position = "absolute";
		g.style.left = "0px";
		g.style.top = "0px";
		
		var offset = ie?22:18;
		g.style.width = (w-offset)+"px";
		g.style.height = document.body.scrollHeight+"px";
		document.body.appendChild(g);
	},
	Open : function(width,title,content,button,callback,image,callbackcancel)
	{
		var a = document.body.getElementsByTagName("select");
		for(i=0;i<a.length;i++) a[i].style.visibility = "hidden";
		if(!Window.hooked)
		{
			Hook.Into("resize",Window.Resize);
			Hook.Into("scroll",Window.Resize);
			Window.hooked = true;
		}
		var r = Resolution();
		var w = r.Width, h = r.Height;
		
		Window.Gray("window"+Window.count);
	
		var d = document.createElement("div");
		d.id = "window"+Window.count;
		d.style.visibility = "hidden";
		d.style.zIndex = (11000+(Window.list.length*2));
		d.style.width = width+"px";
		d.className = "window_large";
		d.style.position = "absolute";		
		var html = '<form onsubmit="Window.OK(\'window'+Window.count+'\');return false;"><div style="background-image:url(images/windowBack.gif);height:36px;padding:0px 10px;"><img src="images/x.gif" align="right" style="cursor:pointer;margin-top:7px" onclick="Window.Close(\'window'+Window.count+'\');"><div class="window_header">'+title+'</div></div>'+
			'<div style="padding:10px"><div style="background-color:white;padding:2px;font:10pt tahoma;color:black">'+content+'</div>'+
			'<div align="center" style="margin-top:3px">';
		if(button && button!="forceok") html += '<input type="button" value="'+button+'" onmousedown="Window.OK(\'window'+Window.count+'\');">';
		html += '<input type="button" value="'+(button&&button=="forceok"?"Ok":"Cancel")+'" onmousedown="Window.Cancel(\'window'+Window.count+'\');"></div></div></form>';
		d.innerHTML = html;
		d.style.left = ((w-width)/2)+document.body.scrollLeft+(20*Window.list.length)-14+"px";
		document.body.appendChild(d);
		d.style.top = ((h-d.offsetHeight)/2)+document.body.scrollTop+(20*Window.list.length)+"px";
		d.style.visibility = "";
		
		var a = document.getElementById(d.id).getElementsByTagName("input");
		//var a = $(d).$$("input");
		if(a.length > 0) a[0].focus();
		
		Window.list.push(d);
		Window.callbacks["window"+Window.count] = callback;
		Window.callbackscancel["window"+Window.count] = callbackcancel;
		Window.count++;
		Select.Add();			
	},
	Display : function(text,mod)
	{	
		if(!Window.hooked)
		{
			Hook.Into("resize",Window.Resize);
			Hook.Into("scroll",Window.Resize);
			Window.hooked = true;
		}
		var r = Resolution();
		var w = r.Width, h = r.Height;
		
		Window.Gray("window"+Window.count);
		
		var d = document.createElement("div");
		d.id = "window"+Window.count;
		d.style.visibility = "hidden";
		d.style.zIndex = (11000+(Window.list.length*2));
		d.className = "window_display";	
		var img = "loading";
		if(mod) img = mod;
		d.innerHTML = '<table border=0><tr><td><img src="images/'+img+'.gif"></td><td class="window_display_text">'+text+'</td></tr></table>';
		document.body.appendChild(d);
		d.style.left = ((w-d.offsetWidth)/2)+document.body.scrollLeft+"px";
		d.style.top = ((h-d.offsetHeight)/2)+document.body.scrollTop+"px";
		d.style.visibility = "";
		d.align = "center";		
		Window.list.push(d);
		Window.count++;
	},
	Timed : function(text,mod)
	{
		Window.Close();
		Window.Display(text,mod);
		setTimeout("Window.Close();",1500);
	},
	Cancel : function(o)
	{
		if(Window.reverse) Select.Remove();
		else Select.Add();
		if(Window.callbackscancel[o]) Window.callbackscancel[o]();
		Window.Close(o);
	},
	Close : function(id)
	{
		var a = document.body.getElementsByTagName("select");
		for(i=0;i<a.length;i++) a[i].style.visibility = "";
		Window.hooked = false;
		for(i=0;i<Window.list.length;i++)
		{
			if((typeof(id) == "string" && Window.list[i].id == id) || (typeof(id) == "number" && i == id) || id == null) 
			{
				Remove(Window.list[i]);
				Remove(document.getElementById(Window.list[i].id+"_gray"));
				if(id != null)
				{
					Window.list.splice(i,1);
					break;
				}
			}
		}
		if(id == null) Window.list = [];
	},
	Pop : function()
	{
		if(Window.list.length > 0)
		{
			Remove(Window.list[Window.list.length-1]);
			Remove(document.getElementById(Window.list[Window.list.length-1].id+"_gray"));
			Window.list.splice(Window.list.length-1,1);
		}
	},
	OK : function(id)
	{
		var a = document.body.getElementsByTagName("select");
		for(i=0;i<a.length;i++) a[i].style.visibility = "";
		if(Window.callbacks[id]())
		{
			for(i=0;i<Window.list.length;i++)
			{
				if(Window.list[i].id == id) 
				{
					Remove(Window.list[i]);
					Remove(document.getElementById(Window.list[i].id+"_gray"));
					Window.list.splice(i,1);
					break;
				}
			}
		}
	},
	Resize : function()
	{
		for(i=0;i<Window.list.length;i++)
		{
			var r = Resolution();
			var w = r.Width, h = r.Height;
			
			var gray = document.getElementById(Window.list[i].id+"_gray");
			gray.style.width = (w)+"px";
			gray.style.height = document.body.scrollHeight+"px";

			Window.list[i].style.left = ((w-Window.list[i].offsetWidth)/2)+document.body.scrollLeft+(20*i)+"px";
			Window.list[i].style.top = ((h-Window.list[i].offsetHeight)/2)+document.body.scrollTop+(20*i)+"px";
		}
	}
}
function Error(string)
{
	//Window.Open("400","Notification Window",'<center><br>'+string+'<br><br></center>',null,null,"red");
}