var all_hidden=0;

function hideall() {
	var i=0;
	while (i<allaids.length) {
		document.getElementById(allaids[i]).style.visibility=all_hidden?'visible':'hidden';
		document.getElementById(allaids[i]).style.display=all_hidden?'block':'none';
		document.getElementById(allaids[i]+'2').style.visibility=all_hidden?'hidden':'visible';
		document.getElementById(allaids[i]+'2').style.display=all_hidden?'none':'block';
//		if (all_hidden==0) {
//			allaids[i+1]=document.getElementById('td'+allaids[i]).style.clientWidth;
//			alert(allaids[i+1]);
//		}
		i++;
	}
	all_hidden=1-all_hidden;
}

function set_select(list,pid) {
	var i=0;
	var j=1;
	while (list.options[i] && j) {
		if (list.options[i].value==pid) {
			list.selectedIndex=i;
			j=0;
		}
		i++;
	}
}


function set_select(list,pid) {
	var i=0;
	var j=1;
	while (list.options[i] && j) {
		if (list.options[i].value==pid) {
			list.selectedIndex=i;
			j=0;
		}
		i++;
	}
}


function pos_x(e)
{
	var i=e.pageX;
	var clientPC = navigator.userAgent.toLowerCase();
	var is_ie = ( (clientPC.indexOf("msie") != -1) && ( clientPC.indexOf("opera") == -1 ) );
	if (typeof(i)=="undefined") i=e.x;
	if (typeof(document.body.scrollLeft) != 'undefined') {
		if (!is_ie) return i;
		return document.body.scrollLeft+i;
	} else {
		return i;
	}
}

function pos_y(e)
{
	var i=e.pageY;
	var clientPC = navigator.userAgent.toLowerCase();
	var is_ie = ( (clientPC.indexOf("msie") != -1) && ( clientPC.indexOf("opera") == -1 ) );
	if (typeof(i)=="undefined") i=e.y;
	if (typeof(document.body.scrollTop) != 'undefined') {
		if (!is_ie) return i;
		return document.body.scrollTop+i;
	} else {
		return i;
	}
}

var popup_prefix;
var popup_id;
function popup_show(prefix,id,e)
{
	if (popup_prefix) {
		popup_close(popup_prefix);
	}
	popup_prefix=prefix;
	popup_id=id;
	var ost=document.getElementById(prefix).style;
	ost.visibility='visible';
	var newx=pos_x(e)-15;
	var newy=pos_y(e)-3;
//	alert("newx="+newx+", newy="+newy);
	if (e) {
		ost.left=ost.pixelLeft=newx;
		ost.top=ost.pixelTop=newy;
	}
	return true;
}

function popup_close(prefix)
{
	document.getElementById(prefix).style.visibility='hidden';
}

function popup_go(prefix,baseref,mode,aid)
{
	var url;
	url=baseref+popup_id+'&mode_'+aid+'='+mode;
	if (document.all['popup_nw_'+prefix].checked) {
		window.open(url);
		popup_close(prefix);
	} else {
		document.location=url;
	}
}

function adm_place_find_table(td)
{
	while (td!=null && td.tagName!='TABLE') {
		td=td.parentNode;
	}
	return td;
}

var adm_table;
var adm_td;
var adm_tab_x,adm_tab_y;
var adm_tab_xorig,adm_tab_yorig;
var adm_tab_parent;

function adm_place_down(td,e)
{
	if (adm_table) return false;
	adm_table=adm_place_find_table(td);
	if (adm_table==null) return false;
	adm_td=td;
	adm_td.className='adminwbodyA';

	adm_tab_parent=adm_table.parentNode;
	adm_tab_x=pos_x(e)+2;
	adm_tab_y=pos_y(e)+2;

	adm_tab_x=get_abs_x(adm_table)-pos_x(e);
	adm_tab_y=get_abs_y(adm_table)-pos_y(e);
	
	adm_table.style.position='absolute';
	adm_table.style.left=adm_table.style.pixelLeft=pos_x(e)+adm_tab_x;
	adm_table.style.top=adm_table.style.pixelTop=pos_y(e)+adm_tab_y;
	show_nords(1);
	adminshader(1);
	return false;
}

function get_abs_x(obj)
{
	var v=0;
	obj=obj.offsetParent;
	while (obj) {
		v+=obj.offsetLeft;
		obj=obj.offsetParent;
	}
	return v;
}

function get_abs_y(obj)
{
	var v=0;
	obj=obj.offsetParent;
	while (obj) {
		v+=obj.offsetTop;
		obj=obj.offsetParent;
	}
	return v;
}

function adm_place_up(td,e)
{
	if (adm_table==null) return;
	adm_td.className='adminwbody';
	adm_table.style.left=adm_table.style.pixelLeft=0;
	adm_table.style.top=adm_table.style.pixelTop=0;
	adm_table.style.position='relative';
/*	adm_table.style.width='100%';
/*	adm_table.parentNode=adm_tab_parent;*/
	adm_table=null;
	show_nords(0);
	adminshader(0);
	return false;
}

function adm_place_move(td,e)
{
	if (adm_table==null) return;
	adm_table.style.left=adm_table.style.pixelLeft=pos_x(e)+adm_tab_x;
	adm_table.style.top=adm_table.style.pixelTop=pos_y(e)+adm_tab_y;
	return false;
}

function show_nords(flag,aid)
{
	var i=0;
	var a="nord_"+aid;
	while (i<nordids.length) {
		if (nordids[i]!=a) {
			document.getElementById(nordids[i]).className='nord';
			document.getElementById(nordids[i]).style.visibility=flag?'visible':'hidden';
			document.getElementById(nordids[i]).style.display=flag?'block':'none';
		}
		i++;
	}
}

function adminshader(flag)
{
	var st=document.getElementById('admin-shader').style;
	st.visibility=flag?'visible':'hidden';
	st.display=flag?'block':'none';
	st.height=st.pixelHeight=page_y();
	
}

function win_x()
{
	var v;
	v=window.innerWidth;
	if (!v) v=document.body.clientWidth;
	return v;
}

function stopPropogation(e)
{
	e.cancelBubble=true;
	if (e.stopPropagation) e.stopPropagation();
}

function win_y()
{
	var v;
	v=window.innerHeight;
	if (!v) v=document.body.clientHeight;
	return v;
}

function page_y()
{
	var v;
	var x,y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;
	if (test1 > test2) { // all but Explorer Mac
		v=document.body.scrollHeight;
	} else { // Explorer Mac, would also work in Explorer 6 Strict, Mozilla and Safari
		v=document.body.offsetHeight;
	}
	return v;
}


function admin_wdel(flag,aid,wname,atype)
{
	adminshader(flag);
	var st=document.getElementById('admin-wdel').style;
	if (flag==1) {
		st.left=st.pixelLeft=(win_x()-350)/2;
		st.top=st.pixelTop=(win_y()-220)/2+document.body.scrollTop;
	}
	st.visibility=flag?'visible':'hidden';
	st.display=flag?'block':'none';
	document.forms.frm_wdel.assigned_id_16.value=aid;
	if (flag==1) {
		document.getElementById('wdelname').innerHTML=wname;
		if (atype==0) document.getElementById('wdeltype').innerHTML='На этой странице';
		if (atype==1) document.getElementById('wdeltype').innerHTML='На всех страницах этой директории';
		if (atype==2) document.getElementById('wdeltype').innerHTML='РЕКУРСИВНО!!!';
	}
}

var admin_waddprev;

function admin_wadd(flag,place_id,placename,page_id)
{
	adminshader(flag);
	var st=document.getElementById('admin-wadd').style;
	if (flag==1) {
		st.left=st.pixelLeft=(win_x()-750)/2;
		st.top=st.pixelTop=(win_y()-500)/2+document.body.scrollTop;
	}
	document.forms.frm_wadd.place_id_4.value=place_id;
	document.forms.frm_wadd.widget_id_4.value="";
/*	document.forms.frm_wadd.submiter.disabled="yes";*/
	if (flag==1) {
		document.getElementById('waddname').innerHTML=placename;
		document.getElementById('waddlist').src='/admin/single/wadd.page?page_id_5853='+page_id+'&place_id_5853='+place_id;
		document.getElementById('waddinfo').src='/shared/vcity.ru.html';
	}
	st.visibility=flag?'visible':'hidden';
	st.display=flag?'block':'none';
	admin_waddprev=null;
}

function admin_wadd_set(widget_id,td)
{
	document.getElementById('waddinfo').src='/admin/single/wadd.page?mode_5853=show&widget_id_5853='+widget_id;
	document.forms.frm_wadd.widget_id_4.value=widget_id;
	if (admin_waddprev) admin_waddprev.className='adminbodyel';
	td.className='adminbodyelA';
	admin_waddprev=td;
}

function admin_wedit(flag,url,doReload,name)
{
	adminshader(flag);
	var st=document.getElementById('admin-wedit').style;
	if (flag==1) {
		st.left=st.pixelLeft=(win_x()-document.forms.weditsize.x.value)/2;
		st.top=st.pixelTop=(win_y()-document.forms.weditsize.y.value-30)/2+document.body.scrollTop;
		document.getElementById('wedit').src=url;
	}
	st.visibility=flag?'visible':'hidden';
	st.display=flag?'block':'none';
	if (doReload) {
		document.forms.reloadform.submit();
//		document.getElementById('wedit').src="/shared/blank.html";
//		window.location.reload();
	}
	document.getElementById('admin-wedit-head').innerHTML=name?name:'Редактирование';
}


function set_dots(aid,suffix)
{
	document.getElementById('aid'+aid+'t').className="admindots"+suffix;
	document.getElementById('aid'+aid+'b').className="admindots"+suffix;
	document.getElementById('aid'+aid+'l').className="admindots2"+suffix;
	document.getElementById('aid'+aid+'r').className="admindots2"+suffix;
}

var widget_selected;
function widget_select(aid)
{
	if (widget_selected) set_dots(widget_selected,'');
	if (widget_selected==aid) {
		widget_selected=null;
		show_nords(0);
		return;
	}
	widget_selected=aid;
	set_dots(widget_selected,'A');
	show_nords(1,aid);
	
}

function widget_reposition(plid,ordpos)
{
	var frm=document.forms.widgetmoveform;
	frm.assigned_id_16.value=widget_selected;
	frm.place_id_16.value=plid;
	frm.ordering_16.value=ordpos;
	frm.submit();
}

var urlToSet;
function SetUrlInput(i)
{
	urlToSet=i;
}

function SetUrl2(url)
{
	urlToSet.value=url;
}

function add_event(obj,event_name,func)
{
	if(obj.addEventListener){
		if(event_name=="mousewheel"){event_name="DOMMouseScroll";}
		obj.addEventListener(event_name,func,false);
	}else{
		if(obj.attachEvent){obj.attachEvent("on"+event_name,func);}
	}
}

var iopen;
function imageOpen(href)
{
	admin_wimg(1,href);
//	admin_wimg(1,'/cgi-bin/img.pl?img='+href);
	/*
	iopen=new Image();
	add_event(iopen,"load",function(ev){imageOpen2();});
	iopen.src=href;
	*/
}

function admin_wimg_set_size(st,st2,x,y)
{
	st2.width=x;
	st2.height=y;
	st.left=st.pixelLeft=(win_x()-x)/2;
	st.top=st.pixelTop=(win_y()-y-30)/2+document.body.scrollTop;
}

var wimgCopy=new Image();
wimgCopy.src='/shared/lightbox/loading.gif';

function admin_wimg(flag,url)
{
	adminshader(flag);
	var st=document.getElementById('admin-wimg').style;
	if (flag==1) {
		var st2=document.getElementById('admin-wimg2').style;
		admin_wimg_set_size(st,st2,840,640);
		document.getElementById('wimg').src='/shared/lightbox/loading.gif';
		document.getElementById('wimg').src=url;
	}
	st.visibility=flag?'visible':'hidden';
	st.display=flag?'block':'none';
}

function imageOpen2()
{
	var iopen=document.getElementById('wimg');
	if (iopen.src=='/shared/lightbox/loading.gif') return;
	var x=iopen.width+20;
	var y=iopen.height+70;
	var st=document.getElementById('admin-wimg').style;
	var st2=document.getElementById('admin-wimg2').style;
	admin_wimg_set_size(st,st2,x,y);
}


