
// Новый скрипт

var img1 = new Image();
img1.src = 'img/submenuo.gif';
function skMenu (menu, name){
	this.aMenuItems = menu;
	this.sName = name;
	this.nItem = 0;
	this.ShowTimer = 0;
	this.HideTimer = 0;
	
	var ActiveBar = null;
	var ActiveItem = null;
	var ShowMustGoOn = false;
	var SubMenusID = new Array();
	
	this.create = function (){
		var items = '';
		var submenu = '';
		for (var i=0; i < this.aMenuItems.length; i++) {
			this.nItem++;
			items += this.getMenuL1Item(this.nItem, this.aMenuItems[i][0], this.aMenuItems[i][1]);
			if (this.aMenuItems[i][2] && typeof this.aMenuItems[i][2] == 'object') submenu += this.getMenuLn(this.aMenuItems[i][2]);
		}
		document.writeln(this.getMenuL1(items) + submenu);
	}
	
	this.getMenuL1 = function (items){
		return "<TABLE WIDTH=100% HEIGHT=20 BORDER=0 CELLSPACING=0 CELLPADDING=0 CLASS=menuBar>" +
				"<TR onMouseOver='" + this.sName + ".MenuBarMOver(event);' onMouseOut='" + this.sName + ".MenuBarMOut(event);'>"+ 
					items +
				"</TR>"+ 
			"</TABLE>";
	}
	
	this.getMenuL1Item = function (num, name, url){
		if (url) {
			var ieAdd = this.isIE ? " STYLE='width: 100%'" : '';
			return "<TD ID=m" + num + " NOWRAP><A ID=am" + num + " HREF='" + url + "'" + ieAdd + " onClick='" + this.sName + ".MenuBarClick(event);'>" + name + "</A></TD>";
		}
		return "<TD ID=m" + num + " NOWRAP>" + name + "</TD>";
	}
	
	this.getMenuLnItem = function (num, name, url, hasSubMenu){
		var add = hasSubMenu ? "<IMG ID=im" + num + " SRC=img/submenu.gif WIDTH=9 HEIGHT=13 BORDER=0 ALIGN=right>" : "";
		var ieAdd = this.isIE ? " STYLE='width: 100%'" : '';
		//return "<TR><TD ID=m" + num + " NOWRAP><A HREF='" + url + "'><DIV " + ieAdd + "><NOWRAP>" + add + name + "</NOWRAP></DIV></A></TD></TR>\n";
		return "<TR><TD ID=m" + num + " NOWRAP><A HREF='" + url + "'" + ieAdd + "><NOBR>" + add + name + "</NOBR></A></TD></TR>\n";
	}
	
	this.getMenuLn = function (aItems){
		var items = '';
		var submenu = '';
		var hasSubMenu = false;
		var parent = this.nItem;
		var info = {id: parent, hasSubMenu: false};
		for (var i=0; i < aItems.length; i++) {
			this.nItem++;
			hasSubMenu = typeof aItems[i][2] == 'object';
			if (hasSubMenu) info.hasSubMenu = true;
			items += this.getMenuLnItem(this.nItem, aItems[i][0], aItems[i][1], hasSubMenu);
			if (hasSubMenu) submenu += this.getMenuLn(aItems[i][2]);
		}
		SubMenusID[SubMenusID.length] = info;
		return "<DIV STYLE='visibility: hidden;' ID=sm" + parent + " CLASS=menu onMouseOver='" + this.sName + ".MenuMOver(event);'onMouseOut='" + this.sName + ".MenuMOut(event);'>" +
				"<TABLE ID=tsm" + parent + " BORDER=0 CELLSPACING=0 CELLPADDING=0>" + items + "</TABLE></DIV>" + submenu;
	}
	
	this.MenuBarMOver = function (event){
		if (SubMenusID) {
			var tab;
			for (var i=0; i < SubMenusID.length; i++){
				tab = document.getElementById('tsm' + SubMenusID[i].id);
				tab.style.width = (tab.offsetWidth + (SubMenusID[i].hasSubMenu ? (this.isOP ? -10 : 16) : 10)) + 'px';
			}
			SubMenusID = null;
		}
		var button = getContainer(window.event ? window.event.srcElement : event.target, 'TD');
		if (!button.id) return false;
		window.clearTimeout(this.HideTimer);
		button.className = 'active';
		button.menu = document.getElementById('s' + button.id) || null;
		var x = getPageOffsetLeft(button);
  		var y = getPageOffsetTop(button) + button.offsetHeight;
		if (ActiveBar != null && ActiveBar.id != button.id) MenuClose(ActiveBar);
		ActiveBar = button;
		if (button.menu) {
			button.menu.style.left = x + "px";
			button.menu.style.top  = y + "px";
			if (!ShowMustGoOn) {
				window.clearTimeout(this.ShowTimer);
				this.ShowTimer = window.setTimeout(this.sName + ".ShowMenu();", 400);
			}
			else {this.ShowMenu();}
		}
		
	}
	
	this.MenuBarMOut = function (event){
		var button = window.event ? window.event.srcElement : event.target;
		var toElem = window.event ? window.event.toElement : (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
		var toElemDIV = getContainer(toElem, 'DIV');
		var toElemTD = getContainer(toElem, 'TD');
		if (toElemDIV && toElemDIV.id.search(/^sm\d+$/)) {
			MenuClose(button);
		}
		else if (toElemDIV == null && !ShowMustGoOn) {this.HideMenu();}
		else{
			window.clearTimeout(this.HideTimer);
			this.HideTimer = window.setTimeout(this.sName + ".HideMenu();", 500);
		}
	}
	
	this.MenuMOver = function (event){
		var src = this.isIE ? window.event.srcElement : event.target;
		var item = getContainer(src, 'TD');
		if (!item || !item.id) return false;
		window.clearTimeout(this.HideTimer);
		
		var menu = getContainer(src, 'DIV');
		item.className = 'active';
		if (ActiveItem && ActiveItem.id == item.id) return;
		ActiveItem = item;
		if (menu.activeItem != null) {// Закрыть подменю
			SubMenuClose(menu);
		}
		menu.activeItem = item;
		var img = document.getElementById('i' + item.id);
		if (img){ // Есть подменю
			img.src = 'img/submenuo.gif';
			item.subMenu = document.getElementById('s' + item.id);
			var x = getPageOffsetLeft(item) + item.offsetWidth - 1;
  			var y = getPageOffsetTop(item) - 1;
			var maxX, maxY;
			if (this.isIE) {
				maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) +
						(document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
				maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) +
						(document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
			}
			if (this.isOP) {
				maxX = document.documentElement.scrollLeft + window.innerWidth;
				maxY = document.documentElement.scrollTop  + window.innerHeight;
			}
			if (this.isMZ) {
				maxX = window.scrollX + window.innerWidth;
				maxY = window.scrollY + window.innerHeight;
			}
			maxX -= item.subMenu.offsetWidth;
			maxY -= item.subMenu.offsetHeight;

			if (x > maxX)
				x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth
					+ (menu.offsetWidth - item.offsetWidth));
			y = Math.max(0, Math.min(y, maxY));
  
			item.subMenu.style.left = x + "px";
			item.subMenu.style.top  = y + "px";
			window.clearTimeout(this.ShowTimer);
			this.ShowTimer = window.setTimeout(this.sName + ".SubMenuOpen();", 300);
		}
	}
	
	this.MenuBarClick = function (event){
		evnt = this.isIE ? window.event : event;
		if (!ShowMustGoOn) {
			this.ShowMenu();
			if (this.isIE) evnt.returnValue = false;
			else evnt.preventDefault();
		}
	}
	
	this.MenuMOut = function (event){
		var src = window.event ? window.event.srcElement : event.target;
		var toElem = window.event ? window.event.toElement : (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
		var toElemDIV = getContainer(toElem, 'DIV');
		if (toElemDIV == null){
			window.clearTimeout(this.HideTimer);
			this.HideTimer = window.setTimeout(this.sName + ".HideMenu();", 500);
			return;
		}
		var item = getContainer(src, 'TD');
		if (item && item.id && toElemDIV.id != ('s' + item.id) && ActiveItem.id != item.id) {
			item.className = '';
			var img = document.getElementById('i' + item.id);
			if (img){
				img.src = 'img/submenu.gif';
			}
			return;
		}
	}
	
	function MenuClose(button){
		button.className = '';
		if (button.menu != null) {
			SubMenuClose(button.menu);
			button.menu.style.visibility = "hidden";
		}
		ActiveBar = null;
		ActiveItem = null;
	}
	
	this.ShowMenu = function (){
			ActiveBar.menu.style.visibility = "visible";
			window.clearTimeout(this.ShowTimer);
			//document.getElementById('a' + ActiveBar.id).onclick  = 'return false';
			ShowMustGoOn = true;

	}
	
	this.HideMenu = function (){
		MenuClose(ActiveBar);
		window.clearTimeout(this.ShowTimer);
		ShowMustGoOn = false;
	}
	
	this.SubMenuOpen = function (){
		if (!ActiveItem || !ActiveItem.subMenu) return;
		ActiveItem.subMenu.style.visibility = 'visible';
		window.clearTimeout(this.ShowTimer);
	}
	
	function SubMenuClose(menu){
		if (menu == null || menu.activeItem == null) return false;
		if (menu.activeItem.subMenu != null) {
			SubMenuClose(menu.activeItem.subMenu);
			menu.activeItem.subMenu.style.visibility = "hidden";
			menu.activeItem.subMenu = null;
		}
		menu.activeItem.className = '';
		var img = document.getElementById('i' + menu.activeItem.id);
		if (img)
			img.src = 'img/submenu.gif';
		menu.activeItem = null;
	}
	 
	function getPageOffsetLeft(el) {
		return el.offsetParent != null ? el.offsetLeft + getPageOffsetLeft(el.offsetParent) : el.offsetLeft;
	}

	function getPageOffsetTop(el) {
		return el.offsetParent != null ? el.offsetTop + getPageOffsetTop(el.offsetParent) : el.offsetTop;
	}
	
	function getContainer(node, tagName) {
		while (node != null) {
			if (node.tagName != null && node.tagName == tagName && node.id)
				return node;
			node = node.parentNode;
		}
 		return node;
	}
	

	ua = navigator.userAgent.toLowerCase();
	this.isOP = (ua.indexOf("opera") != -1);
	
	this.isIE = ((ua.indexOf("msie") != -1) && !this.isOP);
	this.isMZ = (navigator.product == "Gecko");
}

function ShowFoto(img){
	var foto1= new Image();
	foto1.src = img.src;
	if((foto1.width != 0)&&(foto1.height != 0) && (foto1.width > img.width || foto1.height != img.height)){
		width=foto1.width+20;
		height=foto1.height+20;
		size="width="+width+",height="+height;
		newwin=window.open(foto1.src,"",size);
	}
}

function CheckFoto(img){
	var foto1= new Image();
	foto1.src = img.src;
	if((foto1.width != 0)&&(foto1.height != 0) && (foto1.width > img.width || foto1.height != img.height)){
		img.style.cursor = 'pointer';
	}
}