window.onresize = windowBatch;
window.onload = function() {
	setTimeout(windowBatch,200);
}
function DropDown(thisId) {
	this.checkSubs = function() {
		var windowWidth=$(window).width();
		var subs=document.getElementById('menu').getElementsByTagName('ul')[0].getElementsByTagName('ul');
		for (i=0;i<subs.length;i++) {
			var thisX=$('#'+subs[i].parentNode.id).offset().left;
			if (thisX==0) {
				thisX=subs[i].parentNode.style.marginLeft.replace("px","");
				if (thisX==0) {
					thisX=subs[i].parentNode.parentNode.getElementsByTagName('li')[0].style.marginLeft.replace("px","");
				}
			}
			
			if (thisX==''|| !thisX) thisX=0;
			subs[i].getElementsByTagName('li')[0].style.marginLeft=thisX+"px";
			subs[i].thisX=thisX;
			
			var myLinks=subs[i].getElementsByTagName('a');
			for (a=0;a<myLinks.length;a++) {
				var widthRequired=$(myLinks[a]).offset().left+$(myLinks[a]).width()+10;
				if (widthRequired>windowWidth) {
					subs[i].getElementsByTagName('li')[0].style.marginLeft=((windowWidth-(widthRequired+30))+subs[i].thisX)+"px";
				}
			}
			subs[i].onmouseover = function() {
				this.parentNode.getElementsByTagName('a')[0].className+=' active';
			}
			subs[i].onmouseout = function() {
				if (this.parentNode.getElementsByTagName('a')[0].className=='active active') {
					this.parentNode.getElementsByTagName('a')[0].className='active';
				} else {
					this.parentNode.getElementsByTagName('a')[0].className='';
				}
			}
		}
	}
}
var myMenu=new DropDown('myMenu');
function windowBatch() {
	if (document.getElementsByTagName('head')[0].closeStyle) {
		document.getElementsByTagName('head')[0].removeChild(document.getElementsByTagName('head')[0].closeStyle);
	}
	document.getElementsByTagName('head')[0].openStyle=document.createElement('link');
	document.getElementsByTagName('head')[0].openStyle.setAttribute("rel", "stylesheet");
	document.getElementsByTagName('head')[0].openStyle.setAttribute("type", "text/css");
	document.getElementsByTagName('head')[0].openStyle.setAttribute("href","/css/opensubmenu.css");
	document.getElementsByTagName('head')[0].appendChild(document.getElementsByTagName('head')[0].openStyle);
	myMenu.checkSubs();
	if (document.body.className!='frontpage editor') {
		document.getElementsByTagName('head')[0].removeChild(document.getElementsByTagName('head')[0].openStyle);
	}
}
