/**
 * @Ertong
 */
function $(id) {return document.getElementById(id);}

function investigate(target)
{
	var s="<table>";
	var i;
	for (i in target)
	{
		try	{ s+='<tr><td>'+i+'</td><td>'+target[i]+'</td></tr>\n';	}
		catch(e)
		{ s+='<tr><td>'+i+'</td><td>not available</td></tr>\n';	}
	}
	s+='</table>'
	document.write(s);
	//alert(s);
};

function addEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, false);
		return true;
	}
	else if (obj.attachEvent)
		return obj.attachEvent("on"+evType, fn);
	else
		return false;
}

/*
	Incapsulates some operations with HTML elements
	Author: Ertong(ertongi@gmail.com)
	Web: ert.org.ua
*/
var ElemUtils={
	isVisible: function(elem)
	{
		if (!elem) return false;
		while (elem)
		{
			if (elem.style && (elem.style.visibility=='hidden' || elem.style.visibility=='none' || elem.style.display=='none')) return false;
			elem=elem.parentNode;
		}
		return true;
	},

	switchVisible: function(enable, tag)
	{
		var objs=document.getElementsByTagName(tag);
		for (i=0;i<objs.length; ++i)
		{
			if (!objs[i].style) continue;

			if (enable)
			{
				if (objs[i]._disabled_display)
				{
					--objs[i]._disabled_display;
					if (!objs[i]._disabled_display)
						objs[i].style.visibility='visible';
				}
			}
			else
			{
				if (objs[i]._disabled_display)
					++objs[i]._disabled_display;
				else if (this.isVisible(objs[i]) && !objs[i].disabled)
				{
					objs[i]._disabled_display=1;
					objs[i].style.visibility='hidden';
				}
			}
		}
	},

	switchInput: function(enable, tag)
	{
		objs=document.getElementsByTagName(tag);
		for (i=0;i<objs.length; ++i)
		{
			if (!objs[i].style) continue;

			if (enable)
			{
				if (objs[i]._disabled_display)
				{
					--objs[i]._disabled_display;
					if (!objs[i]._disabled_display)
						{
							objs[i].disabled=false;
							if (objs[i].switch_href) objs[i].href=objs[i].switch_href;
						}
				}
			}
			else
			{
				if (objs[i]._disabled_display)
					++objs[i]._disabled_display;
				else if (!objs[i].disabled && objs[i].type!='hidden' && ElemUtils.isVisible(objs[i]))
				{
					objs[i]._disabled_display=1;
					objs[i].disabled=true;
					if (objs[i].href) {objs[i].switch_href=objs[i].href; objs[i].href='javascript:;';}
				}
			}
		}
	},

	switchActiveElements: function(enable)
	{
		this.switchInput(enable,'input');
		this.switchInput(enable,'textarea');
		this.switchInput(enable,'button');
//		this.switchInput(enable,'a');
		this.switchVisible(enable,'select');
	},

	getZIndex: function (elem)
	{
		if (typeof(elem)!='object' || !elem.style) return 0;

		var cStyle = null;
		if (elem.currentStyle) cStyle = elem.currentStyle;
		else if (document.defaultView && document.defaultView.getComputedStyle)
			cStyle = document.defaultView.getComputedStyle(elem,"");

		var res = Number (cStyle ? cStyle.zIndex : elem.style.zIndex);
		return res;
	},

	getMaxZIndex: function (parent)
	{
		parent=parent || document.body;
		if (!parent.childNodes) return 0;
		var res = 0;
		var all = parent.childNodes;
		for(var i=0; i<all.length; i++)
		{
			var ind=this.getZIndex(all[i]);
			if (isNaN(ind))	ind=0;//this.getMaxZIndex(all[i]);

			res = Math.max(res, ind);
		}
		return res;
	},

	getDimensions: function (obj)
	{
		var display = obj.style.display;
		if (display != 'none' && display != null) // Safari bug
			return {width: obj.offsetWidth, height: obj.offsetHeight};

		// All *Width and *Height properties give 0 on objs with display none,
		// so enable the obj temporarily
		var els = obj.style;
		var oriVisibility = els.visibility;
		var oriPosition = els.position;
		var oriDisplay = els.display;
		els.visibility = 'hidden';
		els.position = 'absolute';
		els.display = 'block';
		var oriWidth = obj.clientWidth;
		var oriHeight = obj.clientHeight;
		els.display = oriDisplay;
		els.position = oriPosition;
		els.visibility = oriVisibility;

		return {width: oriWidth, height: oriHeight};
	},
	getHeight: function (obj)
	{
		return this.getDimensions(obj).height;
	},
	getWidth: function (obj)
	{
		return this.getDimensions(obj).width;
	},
	getPosition: function (obj)
	{
		var left = top = 0;
		if (obj.offsetParent) {
			left = obj.offsetLeft
			top = obj.offsetTop
			while (obj = obj.offsetParent) {
				left += obj.offsetLeft
				top += obj.offsetTop
			}
		}
		return {x:left, y:top};
	},
	getInnerText: function (obj)
	{
		if (obj.textContent) return obj.textContent;
		else if (obj.innerText)	return obj.innerText;
		else return '';
	},
	resetFocus: function()
	{ //sometimes, when the focused element become hidden, focus remains on it
	 // then use this function
	 	if (window.opera) document.createElement('input').focus();
	}
};

/*
	Implements ability to use something like modal windows
	Author: Ertong(ertongi@gmail.com)
	Web: ert.org.ua
*/
var WindowUtils={
	loaded:false,
	onLoadHandlers:[],

	onLoad:function()
	{
		var hs=WindowUtils.onLoadHandlers;
		WindowUtils.loaded=true;
		for (i in hs)
			if (typeof(hs[i])=='function') hs[i]()
			else if (typeof(hs[i])=='string') eval(hs[i]);
	},

	getSize:function (wnd)
	{
		var wnd = wnd ? wnd : window;
		var w,h;
		if (wnd.innerHeight) // all except Explorer
		{
			w = wnd.innerWidth;
			h = wnd.innerHeight;
		}
		else if (wnd.document.documentElement && wnd.document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			w = wnd.document.documentElement.clientWidth;
			h = wnd.document.documentElement.clientHeight;
		}
		else if (wnd.document.body) // other Explorers
		{
			w = wnd.document.body.clientWidth;
			h = wnd.document.body.clientHeight;
		}

		return {width:w, height:h};
	},
	getScroll:function (wnd)
	{
		var wnd = wnd ? wnd : window;
		var x,y;
		if (wnd.pageYOffset) // all except Explorer
		{
			x = wnd.pageXOffset;
			y = wnd.pageYOffset;
		}
		else if (wnd.document.documentElement && wnd.document.documentElement.scrollTop)
			// Explorer 6 Strict
		{
			x = wnd.document.documentElement.scrollLeft;
			y = wnd.document.documentElement.scrollTop;
		}
		else if (wnd.document.body) // all other Explorers
		{
			x = wnd.document.body.scrollLeft;
			y = wnd.document.body.scrollTop;
		}
		return {x:x, y:y};
	},

	createBlockDiv:function ()
	{
		var div=document.createElement('div');
		var st=div.style;
		st.position='absolute';
		st.backgroundColor='#CCCCCC';
		st.filter='alpha(opacity=50)';
		st.opacity='.5';
		st.width = st.height = "100%";
		st.top = st.left = '0px';
//			st.width=Math.max(document.body.scrollWidth, document.body.offsetWidth)+"px";
//			st.height=Math.max(document.body.scrollHeight, document.body.offsetHeight)+"px";
		st.zIndex=ElemUtils.getMaxZIndex()+1;
		document.body.appendChild(div);
		return div;
	},

	showModal:function (obj, onClose)
	{
		if (!this.loaded)
		{
			this.onLoadHandlers.push(function(){WindowUtils.showModal(obj, onClose)});
			return;
		}

		if (obj.wnd && obj.wnd.opened) return;
		ElemUtils.switchActiveElements(false);
		obj.wnd = obj.wnd ? obj.wnd : {};
		obj.wnd.blockDiv = this.createBlockDiv();

		if (!onClose && obj.wnd.onClose) onClose=obj.wnd.onClose;

		//window will be placed in the table, that is in the div
		var tbl_div = document.createElement('div');
		var table = document.createElement('table'); tbl_div.appendChild(table);
		var tbody = document.createElement('tbody'); table.appendChild(tbody);
		var tr = document.createElement('tr'); tbody.appendChild(tr);
		var td = document.createElement('td'); tr.appendChild(td);
		td.appendChild(obj.parentNode.removeChild(obj));
		obj.wnd.container = tbl_div;

		var st = tbl_div.style;
		st.zIndex = ElemUtils.getMaxZIndex()+1;
		st.position = 'absolute';
		st.top = st.left = '0px';
		/*st.width =*/ st.height = '100%';


		table.style.width = table.style.height = '100%';
		table.border = '0';
		td.vAlign = 'middle';
		td.align = 'center';

		document.body.appendChild(tbl_div);

		obj.style.display='block';
		obj.focus();
		obj.wnd.opened=true;

		if (typeof(obj.wnd.onShow)=='function') obj.wnd.onShow();
		if (typeof(obj.wnd.onShow)=='string') eval(obj.wnd.onShow);

		obj.wnd.close=function(exitCode)
		{
			obj.style.display='none';
			document.body.appendChild(obj.parentNode.removeChild(obj));

			document.body.removeChild(obj.wnd.container); obj.wnd.container=null;
			document.body.removeChild(obj.wnd.blockDiv); obj.wnd.blockDiv=null;

			ElemUtils.switchActiveElements(true);

			obj.wnd.opened=false;

			ElemUtils.resetFocus();

			if (typeof(onClose)=='function') onClose(exitCode);
			if (typeof(onClose)=='string') eval(onClose);
		}
	}
};
addEvent(window, 'load', WindowUtils.onLoad);