function ge(id){return document.getElementById(id);}
function cutpx(str){
	strout=str.replace('px','');
	strout=strout.replace('NaN','');
	return strout;
}
/* function absPosition(obj) выдает позицию html элемента
 *
*/
function absPosition(obj) {
	this.x = 0;
	this.y = 0;
    while(obj!=null) {
		this.x += (obj.offsetLeft)?obj.offsetLeft:0;
		this.y += (obj.offsetTop)?obj.offsetTop:0;
		obj = (obj.offsetParent)?obj.offsetParent:null;
	}
	return {x:this.x,y:this.y};
}

function div_width(obj){
	var childrens = null,chld = null,sumWidth=0,n1=0,i1=0,j1=0,k1=0;
	childrens= obj.childNodes;
	sumWidth=0;
	n1=childrens.length;
	for(i1=0;i1<n1;i1++){
		if(childrens[i1].nodeName.toUpperCase()=='UL'){
			chld=childrens[i1].childNodes;
			k1=chld.length;
			for(j1=0;j1<k1;j1++){
				if(chld[j1].nodeName.toUpperCase()=='LI'){
					sumWidth+=chld[j1].offsetWidth;
				}
			}
		}
	}
	return sumWidth;
}

function menu_init(){
	obj=ge('main_menu');
	//out=ge('debout');
	//out.innerHTML='';
	childrens= obj.childNodes;
	n=childrens.length;
	for(i=0;i<n;i++){
		if(childrens[i].nodeName.toUpperCase()=='UL'){
			chld=childrens[i].childNodes;
			k=chld.length;
			for(j=0;j<k;j++){
				if(chld[j].nodeName.toUpperCase()=='LI'){
					pos=absPosition(chld[j]);
					//out.innerHTML+='<br>Li info: offsetLeft:'+pos.x+' offsetWidth:'+chld[j].offsetWidth;
					
					ch=chld[j].childNodes;
					nn=ch.length;
					for(ii=0;ii<nn;ii++){
						//out.innerHTML+='<br> '+ch[ii].className;
						if(ch[ii].className=='hv'){
							width=div_width(ch[ii]);
							pos=absPosition(ch[ii]);
							mn=ge('main');
							mpos=absPosition(mn);
							//out.innerHTML+='<br> offsetLeft:'+pos.x+' offsetWidth:'+width+' main left:'+mpos.x+' width:'+mn.offsetWidth;
							off=(mn.offsetWidth+mpos.x)-(pos.x+width)-70;
							if(off<0){
							  //out.innerHTML+='<br> margin:'+(off+cutpx(ch[ii].style.marginLeft))+'px';
							  ch[ii].style.marginLeft=(off+cutpx(ch[ii].style.marginLeft))+'px';
							  ch[ii].style.width=(width+20)+'px';
							  /*cont=ch[ii].innerHTML;
							  ch[ii].innerHTML='';
							  ch[ii].innerHTML=cont;
							  newDiv = document.createElement("div");
							  newDiv.className='hv';
							  newDiv.style.marginLeft=ch[ii].style.marginLeft;
							  newDiv.innerHTML = "<ul><li><a href='/contacts/page19/'>подробнее</a></li><li>|</li><li><a href='/contacts/page36/'>jjggga super giga submenu</a></li><li>|</li><li><a href='/contacts/page37/'>Большой подраздел</a></li></ul>";//cont.substr(0,20)+'gg'+cont.substr(20);
							  chld[j].replaceChild(newDiv,ch[ii]);*/
							  /*
							  ch[ii].clientWidth=width;
							  ch[ii].offsetWidth=width;
							  ch[ii].clientHeight=46;
							  ch[ii].offsetHeight=46;
							  */
							}
							//out.innerHTML+='<br> marginLeft:'+ch[ii].style.marginLeft;
						}
					}/**/
				}
			}
		}
	}
	
}
