function closeMenus(menuId) {
	var allDivs = document.getElementsByTagName('div');
	var menuId = ''+menuId;
	for (var i=0;i<allDivs.length;i++) {
		if ((allDivs[i].id.substring(0,4)=='menu')&&(menuId.indexOf(allDivs[i].id.substring(4))==-1)&&(allDivs[i].id!='menuContainer') &&(allDivs[i].id!='menuDiv') &&(allDivs[i].id!='menuTd')) {
			// hide unused menu parts
			allDivs[i].style.display = "none";
		}
	}
}

function openMenu(menuId) {
	closeMenus(menuId);
	if (document.getElementById('menu'+menuId)) {
		document.getElementById('menu'+menuId).style.position = 'absolute';
		document.getElementById('menu'+menuId).style.left = (findPosX(document.getElementById('button'+menuId))+0)+"px";
		document.getElementById('menu'+menuId).style.top =  (findPosY(document.getElementById('button'+menuId))+20)+"px";
		document.getElementById('menu'+menuId).style.display = "";
	}
}


function openSubMenu(menuId) {
	closeMenus(menuId);
	if (document.getElementById('menu'+menuId)) {
		document.getElementById('menu'+menuId).style.position = 'absolute';
		document.getElementById('menu'+menuId).style.left = (findPosX(document.getElementById('img'+menuId))+10)+"px";
		document.getElementById('menu'+menuId).style.top =  (findPosY(document.getElementById('img'+menuId))+5)+"px";
		document.getElementById('menu'+menuId).style.display = "";
	}
}


function clearDefault(el) {
	if (el.defaultValue==el.value) el.value = "";
}

function disableElement(el,text) {
	el.disabled=true;
	el.value=text;
}

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 MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


/*
	Litebox 
*/

var imgNext;
var imgPrev;

function showBox(theTarget) {
	var theBody = document.getElementsByTagName('body')[0];
	var pageCoords = getPageCoords();

	var theShadow = document.createElement('div');
	theShadow.id = 'shadow';
	theShadow.style.height = (pageCoords[1] + 'px');
	theShadow.className = 'on';
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
	theBody.insertBefore(theShadow, theBody.firstChild);

	var theLoading = document.createElement('div');
	theLoading.id = 'loading';
	theLoading.style.top = parseInt(pageCoords[2] + (pageCoords[0] - 55) / 2) + 'px';
	theLoading.onclick = function() { closeBox(); }
	theShadow.appendChild(theLoading);

	var imgPreload = new Image();
	imgPreload.onload = function() {
		var theBox = document.createElement('div');
		theBox.id = 'litebox';
		theBox.style.width = imgPreload.width + 10 + 'px';
		theBox.style.marginTop = parseInt(pageCoords[2] + (pageCoords[0] - imgPreload.height - 50) / 2) + 'px';

		var theImage = document.createElement('img');
		theImage.src = theTarget.href;
		theImage.alt = theTarget.title;
		theImage.width = imgPreload.width;
		theImage.onclick = function() { closeBox(); }
		theImage.title = "Нажмите на картинку или ESC для закрытия окна";

		var theCaption = document.createElement('p');
		theCaption.innerHTML = (theImage.alt) ? theImage.alt : '';
		

		var allThumbs = new Array();
		var allLinks = document.getElementsByTagName('a');
		var linkLen = allLinks.length;
		for (i=0,j=0; i<linkLen; i++) {
			if (allLinks[i].getAttribute('rel') == 'lightbox') {
				allThumbs[j++] = allLinks[i];
			}
		}
		linkLen = allThumbs.length;
		for (i=0; i<linkLen; i++) {
			if (allThumbs[i].href == theTarget.href) {
				if (allThumbs[i-1]) {
					var thePrevLink = document.createElement('a');
					thePrevLink.className = 'prev';
					thePrevLink.title = allThumbs[i-1].title;
					thePrevLink.href = allThumbs[i-1].href;
					thePrevLink.onclick = function() { closeBox(); showBox(this); return false; }
					theCaption.insertBefore(thePrevLink, theCaption.firstChild);
					imgPrev = allThumbs[i-1];
				}
				if (allThumbs[i+1]) {
					var theNextLink = document.createElement('a');
					theNextLink.className = 'next';
					theNextLink.title = allThumbs[i+1].title;
					theNextLink.href = allThumbs[i+1].href;
					theNextLink.onclick = function() { closeBox(); showBox(this); return false; }
					theCaption.insertBefore(theNextLink, theCaption.firstChild);
					imgNext = allThumbs[i+1];
				}
			}
		}

		theShadow.removeChild(theLoading);
		theBox.appendChild(theImage);
		theBox.appendChild(theCaption);
		theShadow.appendChild(theBox);

		document.onkeypress = getKey;
		return false;
	}
	imgPreload.src = theTarget.href;
}

function getPageCoords() {
	var coords = [0, 0, 0]; // height of window, document, scroll pos
	// all except IE
	if (window.innerHeight) {
		coords[0] = window.innerHeight;
		coords[2] = window.pageYOffset;
	}
	// IE 6 Strict
	else if (document.documentElement && document.documentElement.clientHeight !=0) {
		coords[0] = document.documentElement.clientHeight;
		coords[2] = document.documentElement.scrollTop;
	}
	else if (document.body) {
		coords[0] = document.body.clientHeight;
		coords[2] = document.body.scrollTop;
	}

	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;
	if (test1 > test2) {
		coords[1] = document.body.scrollHeight;
	} else {
		coords[1] = document.body.offsetHeight;
	}
	if (coords[1] < coords[0]) coords[1] = coords[0];

	return coords;
}

function closeBox() {
	var theBody = document.getElementsByTagName('body')[0];
	var theBox = document.getElementById('litebox');
	if (theBox) theBox.style.display = 'none';
	var theShadow = document.getElementById('shadow');
	if (theShadow) theShadow.style.display = 'none';
	theBody.removeChild(theShadow);

	selects = document.getElementsByTagName('select');
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = 'visible';
	}
	document.onkeypress = '';
	imgPrev = imgNext = '';
	return false;
}

function getKey(e) {
	var arrowImg;

	if (!e) var e = window.event;
	var keycode = e.keyCode ? e.keyCode : e.which;

	switch (keycode) {
  	case 27: // esc
		case 32: // spacebar
			closeBox();
			break;
		case 37: // <-
			arrowImg = imgPrev ? imgPrev : '';
			break;
		case 39: // ->
			arrowImg = imgNext ? imgNext : '';
	}
	if (arrowImg) { closeBox(); showBox(arrowImg); }
	return false;
}

function initLitebox() {
	if (!document.getElementsByTagName) { return; }
	var anchors = document.getElementsByTagName('a');

	for (i=0,len=anchors.length; i<len; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == 'lightbox')) {
			anchor.onclick = function() { showBox(this); return false; }
		}
	}
	anchor = null;
}

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

addLoadEvent(initLitebox);
