/*
	Incapsulates some operations with HTML elements
	Author: Ertong(dev@ert.org.ua)
	Web: ert.org.ua
*/
arr_preloadImages = new Array();
bool_onloadDone = false;

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function DoPreloadImages(imgArray) { //v3.0
  var d=document; 
  if(d.images)
  { 
  	if(!d.MM_p) 
  		d.MM_p=new Array();
    var i,j=d.MM_p.length,
    a=imgArray;
    for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}
  }
}

function ConditionalPreloadImages(imgArray)
{
	var i;
	for(i=0;i<imgArray.length; ++i)
	{
		var newindex = arr_preloadImages.length;
		arr_preloadImages[newindex] = imgArray[i];
	}
	if (bool_onloadDone)
	{
		DoPreloadImages(arr_preloadImages);
		arr_preloadImages = new Array();
	}
}

function BodyOnLoad()
{
	bool_onloadDone = true;
	ConditionalPreloadImages(BodyOnLoad.arguments);
}

function PreloadImages()
{
	ConditionalPreloadImages(PreloadImages.arguments);
}


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;
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

  
function to_brief_index()
{
	try
	{
		document.getElementById('usr').value = 'brief';
		document.getElementById('search_form_main').submit();
	}
	catch(ex)
	{
    	location.href='/?usr=brief';
	}
}


function to_vacancy_index()
{
	try
	{
		document.getElementById('usr').value = 'vacancy';
		document.getElementById('search_form_main').submit();
	}
	catch(ex)
	{
    	location.href='/?usr=vacancy';
	}
}
  
  
function dgebi(id)
{
	return document.getElementById(id);
}


function openSendAdWindow(type,id)
{
	var link='send_doc.php?usr='+type+'&id='+id;
	var params = 'width=600,height=640,status=0,location=0,resizable=1,scrollbars=0,left=100,top=50';
	window.open(link,'popup_sendad',params,0); 
	return false;
}

function openSendMailWindow(type,id)
{
	var link='send_mail.php?doc_type='+type+'&id_doc='+id;
	var params = 'width=400,height=540,status=0,location=0,resizable=1,scrollbars=0,left=100,top=50';
	window.open(link,'popup_sendmail',params,0); 
	return false;
}

function openHelpWindow(theme)
{
	var link='help.php?';
	if (theme) 
		if (theme=='support')
			link+='support';
		else
			link+='&theme='+theme;
	var params = 'width=672,height=562,status=0,location=0,resizable=1,scrollbars=0,left=100,top=50';
	window.open(link,'popup_help',params,0); 
	return false;
}

function openExampleBriefWindow()
{
	var link='info.php?example';
	var params = 'width=500,height=640,status=0,location=0,resizable=1,scrollbars=0,left=100,top=50';
	window.open(link,'popup_example',params,0); 
	return false;
}

function openUploadPhotoWindow()
{
	var link='upload_photo.php';
	var params = 'innerWidth=500,innerHeight=130,status=0,location=0,menubar=0,titlebar=0,resizable=1,scrollbars=0,left=100,top=50, directories=0';
	window.open(link,'upload_window',params,0); 
	return false;
}
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_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 (ElemUtils.isVisible(objs[i]) && !objs[i].disabled && objs[i].type!='hidden')
				{
					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;
	},

	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();
	},

	/**
	*	type==0 - enable in time seconds
	*	type==1 - disable in time seconds
	*/
	delayButton: function(btn, time, type)
	{
		if (!btn.delay)
		{
			btn.delay = {};
			btn.delay.caption = btn.value;
		}

		clearTimeout(btn.delay.timeoutId);

		btn.delay.time = time + 1;
		btn.delay.type = type;
		btn.disabled = type==0;


		btn.delay.func = function()
		{
			if (--btn.delay.time == 0)
			{
				btn.value = btn.delay.caption;
				btn.disabled = false;
				if (btn.delay.type==1) btn.click();
				btn.delay = null;
			}
			else
			{
				btn.value = btn.delay.caption+"(" + btn.delay.time + ")";
				btn.delay.timeoutId = setTimeout(btn.delay.func, 1000);
			}
		}
		btn.delay.func();
	}
};

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

	onLoad:function()
	{
		if (WindowUtils.loaded) return;
		WindowUtils.loaded=true;

		var hs=WindowUtils.onLoadHandlers;
		for (var i = 0; i < hs.length; ++i)
			if (typeof(hs[i])=='function') hs[i]()
			else if (typeof(hs[i])=='string') eval(hs[i]);
	},

	createBlockDiv:function ()
	{
		var div=document.createElement('div');
		var st=div.style;
		st.position='absolute';
		st.backgroundColor='#CCCCCC';
		st.filter='alpha(opacity=80)';
		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%';
		st.overflow='auto';

		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)
		{
			if (!obj.wnd.opened) return;	
		
			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);

//Other utils
//<div id=MessageBoxSample>
//	<div id=MessageWndSample_ID class=message_box_div style="display:none;">
//		<div id=MessageWndSample_ID_text></div>
//		<br>
//		<center><input id=MessageWndSample_ID_close type="button" value="Закрыть" onclick="$('MessageWndSample_ID').wnd.close();"></center>
//	</div>
//</div>
WindowUtils.showMessage = function (text, sample, onClose)
{
	if (!this.loaded)
	{
		this.onLoadHandlers.push(function(){WindowUtils.showMessage(text, sample, onClose)});
		return;
	}

	sample_box = sample || 'MessageBoxSample';
	sample_id = 'MessageWndSample_ID'

	var id=0;
	while ($('MessageBox'+id)) ++id;
	id='MessageBox'+id;

	var container = document.createElement('div');

	container.innerHTML = ($(sample_box).innerHTML).replace(new RegExp(sample_id, "g"), id);

	var elems = container.getElementsByTagName('div');

	var message_wnd = null;

	for (i in elems)
		if (elems[i].id==id)
			message_wnd = elems[i];

	if (!message_wnd) return;

	message_wnd.wnd = {
		onShow: function()
		{
			if ($(id+"_text")) $(id+"_text").innerHTML = text;
			if ($(id+"_close")) $(id+"_close").focus();
		},
		onClose:function(exitCode)
		{
			message_wnd.parentNode.removeChild(message_wnd);

			if (typeof(onClose)=='function') onClose(exitCode);
			if (typeof(onClose)=='string') eval(onClose);
		}
	};
	WindowUtils.showModal(message_wnd);
	return message_wnd;
}



