var timer;

function showMenu(menuId) {
	window.clearInterval(timer);
	hideAll(menuId);

	var child = document.getElementById("child" + menuId);
	if (child && child.style.display == "none") {
		child.style.display = "block";
	}
}

function hideMenu(menuId) {
	var child = document.getElementById("child" + menuId);
	if (child) {
		timer = window.setInterval("document.getElementById('child" + menuId + "').style.display='none';", 200);
	}
}

function hideAll(exceptId) {
	var table = document.getElementsByTagName('table');
	for (i = 0; i < table.length; i++) {
		if (table[i].id.match('child') && table[i].id != "child" + exceptId) {
			table[i].style.display = "none";
		}
	}
}

function subMenu(thisId, thisData) {
	var fromLeft = navigator.userAgent.match("IE") && !navigator.userAgent.match("Opera") ? -81 : 0;
	var result = '';
	var total = thisData.length;
	if (total) {
		result = '<div><table cellpadding="0" cellspacing="0" id="child' + thisId + '" style="margin-left: ' + (fromLeft + 1) + 'px; display: none"><tr><td><img src="/images/submenu_top.gif" width="165" height="5" alt=""><div>';

		for (i = 0; i < total; i++) {
			result += '<a href="' + thisData[i][0] + '">' + thisData[i][1] + '</a>';
			if (i < total - 1)
				result += '<div align="left"><div></div></div>';
		}

		result += '</div><img src="/images/submenu_bottom.gif" width="165" height="13" alt="" /></td></tr></table></div>';
	}
	return result;
}

function showPopup(pageId) {
	var width  = 550;
	var height = 400;
	var left = screen.width / 2 - width / 2;
	var top  = screen.height / 2 - height / 2;
	window.open('/popup' + pageId + '.htm', '_blank', 'resizable=1,scrollbars=1,modal=1,height=' + height + ',width=' + width + ',left=' + left + ',top=' + top);
	return;
}

function popupImg(photo) {
	var width  = 550;
	var height = 400;
	var left = screen.width / 2 - width / 2;
	var top  = screen.height / 2 - height / 2;
	window.open('/popup.php?photo=' + photo, '_blank', 'resizable=1,scrollbars=1,modal=1,height=' + height + ',width=' + width + ',left=' + left + ',top=' + top);
	return;
}

function search() {
	return document.getElementById('searchForm').submit();
}

function findNews() {
	var form       = document.getElementById('newsForm');
	var from_month = document.getElementById('from_month');
	var from_year  = document.getElementById('from_year');
	var to_month   = document.getElementById('to_month');
	var to_year    = document.getElementById('to_year');

	if (from_month.value) {
		to_month.value = from_month.value;
	} else {
		from_month.options[0].value = 1;
		to_month.value = 12;
	}
	if (from_year.value) {
		to_year.value  = from_year.value;
	} else {
		from_year.options[0].value = 1970;
	}

	return form.submit();
}

function zeroFill() {
	return this >= 10 ? this : "0" + this;
}

Number.prototype.zeroFill = zeroFill;

function writeDate(where) {
	var format = '', date = new Date();
	//date.setTime((date.getTime() + date.getTimezoneOffset() * 60000) + 3 * 3600000);

	format += date.getHours().zeroFill() + ":" + date.getMinutes().zeroFill() + ":" + date.getSeconds().zeroFill();
	format += " &nbsp; | &nbsp; ";
	format += date.getDate().zeroFill() + " " + months[date.getMonth()] + " " + date.getFullYear();

	try { document.getElementById(where).innerHTML = format; } catch (e) { }
}

window.setInterval("writeDate('date');", 1000);

var sub = 0;
window.onload = function() {

	// Preload menu images
	var i_submenu_top    = new Image(); i_submenu_top.src    = "/images/submenu_top.gif";
	var i_submenu_bg     = new Image(); i_submenu_bg.src     = "/images/submenu_bg.gif";
	var i_submenu_bottom = new Image(); i_submenu_bottom.src = "/images/submenu_bottom.gif";

	// E-mail hiding
	var domain = document.domain;
	var link   = document.getElementsByTagName('a');
	for (i = 0; i < link.length; i++) {
		if (link[i].href.match('mail:')) {
			link[i].innerHTML = link[i].href.replace('mail:', '')      + '@' + domain;
			link[i].title     = link[i].href.replace('mail', 'mailto') + '@' + domain;
			link[i].href      = link[i].href.replace('mail', 'mailto') + '@' + domain;
		}
	}

	// Site menu
	try {
		var td  = (document.getElementById('mainMenu')).getElementsByTagName('td');

		for (j = 0; j < td.length; j++) {
			if (td[j].parentNode.id == "mainMenu") {

				if (sub == 1) {
					td[j].onmouseover = function() { showMenu(1); }
					td[j].onmouseout  = function() { hideMenu(1); }
				}
				if (sub == 2) {
					td[j].onmouseover = function() { showMenu(2); }
					td[j].onmouseout  = function() { hideMenu(2); }
				}
				if (sub == 3) {
					td[j].onmouseover = function() { showMenu(3); }
					td[j].onmouseout  = function() { hideMenu(3); }
				}
				// 4-? ???? ?????????
				if (sub == 5) {
					td[j].onmouseover = function() { showMenu(5); }
					td[j].onmouseout  = function() { hideMenu(5); }
				}

				td[j].innerHTML += subMenu(sub, menu[sub]);
				sub++;

			}

		}
	} catch (e) { }
};