﻿//Предварительная загрузка картинок
(new Image(3,21)).src = '/javamenu/menu_line.gif';
(new Image(1,21)).src = '/javamenu/menu_bg.gif';
(new Image(1,22)).src = '/javamenu/menu_bg_first.gif';
(new Image(1,22)).src = '/javamenu/menu_bg_first_hover.gif';
(new Image(204,23)).src = '/javamenu/blue_head.gif';
(new Image(204,23)).src = '/javamenu/red_head.gif';
(new Image(3,5)).src = '/javamenu/more_icon.gif';

//Скрипт выпадающего меню
var Pulldown = {

	menuRootElementId:'menu',
	isHidden:{},
	left:null,
	top:null,
	topLevelHeight:21,
	cellHeight:23,
	
	isIE:		(navigator.userAgent.indexOf("MSIE ")>0 ? true : false),
	isOpera:	(!!(window.opera && opera.buildNumber)),
	isWebKit:	(/WebKit/.test(navigator.userAgent)),	
	
	init:	function(nodes, groups, nodesInGroups, nodesGroups){
		this.nodes = nodes;
		this.groups = groups;
		this.nodesInGroups = nodesInGroups;
		this.nodesGroups = nodesGroups;
		/*	позиция меню на странице	*/
		var pos = this.getAbsolutePos(document.getElementById(this.menuRootElementId));
		this.left = pos.x;
		this.top = pos.y;
		/*	устанавливаем флаги 'группа скрыта'	*/
		/*	позиционируем группы				*/
		for (i=0; i < this.groups.length; i++) {
			var groupElement	=	document.getElementById(this.groups[i]);
			var groupPosition	=	this.evalGroupPosition(this.groups[i]);
			groupElement.style.left	= groupPosition.x+'px';
			groupElement.style.top	= groupPosition.y+'px';
			this.isHidden[this.groups[i]] = true;
		}
	},

	nodeOver: function(obj)	{
		var nodeId = obj.id;
		var nodeGroupId = this.getNodeGroup(nodeId);
		if (nodeGroupId != null) {
			this.showGroup(nodeGroupId);
		}
	},

	nodeOut: function(obj)	{
		var nodeId = obj.id;
		var nodeGroupId = this.getNodeGroup(nodeId);
		if(nodeGroupId!=null)	{
			this.hideGroup(nodeGroupId);
		}
	},
	
	groupOver: function(obj)	{
		var currentGroupId = obj.id;
		this.showGroup(currentGroupId);
	},
	
	groupOut: function(obj)	{
		var currentGroupId	=	obj.id;
		this.hideBranch(currentGroupId);
	},

	showGroup:		function(groupId)	{
		if (this.isHidden[groupId]) {
			this.showElement(groupId);
			this.isHidden[groupId] = false;
		}
		//this.drawGroup(groupId);
		var parentNode		= this.getGroupNode(groupId);	
		if (parentNode != null) {
			var parentNodeOwner = this.getNodeOwner(parentNode);
			if (parentNodeOwner != null) {
				this.showGroup(parentNodeOwner);
			}
		}
	},

	hideBranch:		function(groupId)	{
		this.hideGroup(groupId);
		this.hideParentGroup(groupId);
	},

	hideParentGroup:	function(groupId)	{
		this.hideElement(groupId);
		this.isHidden[groupId] = true;
		var parentGroup = this.getParentGroup(groupId);
		if(parentGroup!=null)	{
			this.hideParentGroup(parentGroup);
		}
	},

	hideGroup:	function(groupId)	{
		if (!this.isHidden[groupId]) {
			this.hideElement(groupId);
			this.isHidden[groupId] = true;
		}
	},
	
	getNodeOwner:	function(nodeId)	{
		var result = null;
		if (this.nodesInGroups[nodeId]!=undefined) {
			result = this.nodesInGroups[nodeId];
		}
		return result;
	},	
	
	getNodeGroup:	function(nodeId)	{
		var result = null;
		if (this.nodesGroups[nodeId]!=undefined) {
			result = this.nodesGroups[nodeId];
		}
		return result;
	},

	getGroupNode:	function(groupId)	{
		var result = null;
		for (var nodeId in this.nodesGroups) {
			if (this.nodesGroups[nodeId] == groupId) {
				result = nodeId;
				break;
			}
		}
		return result;
	},
	
	getParentGroup:	function(groupId)	{
		var result = null;
		for (var nodeId in this.nodesGroups) {
			if (this.nodesGroups[nodeId] == groupId) {
				result = this.nodesInGroups[nodeId];
				break;
			}
		}
		return result;
	},	

	getChildGroups:	function(groupId)	{
		var result = null;
		var nodesInGroupCounter = 0;
		for (var nodeId in this.nodesInGroups) {
			if (this.nodesInGroups[nodeId] == groupId) {
				result[nodesInGroupCounter] = this.nodesGroups[nodeId];
				nodesInGroupCounter++;
			}
		}
		return result;
	},

	/*	позиционирование	*/
	getNodePosition:	function(nodeId)	{
		var result		=	{};
		var nodeOwner	=	this.getNodeOwner(nodeId);
		var nodeElement	=	document.getElementById(nodeId);
		if (nodeOwner != null) {
			var ownerPosition = this.evalGroupPosition(nodeOwner);
			result.x = ownerPosition.x;
			result.y = ownerPosition.y + nodeElement.offsetTop;
		}	else	{
			var absNodeElementPos = this.getAbsolutePos(nodeElement);
			var nodeElementX = absNodeElementPos.x - this.left;
			var nodeElementY = absNodeElementPos.y - this.top;
			
			/*	коррекция	*/
			if (this.isIE)		{ nodeElementX -=1;	}
			if (this.isOpera)	{ nodeElementX -=1; }
			if (this.isWebKit)	{ nodeElementX -=1; }
			
			result.x = nodeElementX;
			result.y = nodeElementY;
		}
		
		return result;
	},
	
	getNodeSize:	function(nodeId)	{
		result = this.getElementSize(document.getElementById(nodeId));
		return result;
	},	
	
	evalGroupPosition:	function(groupId)	{
		var groupPosition	= {};
		var nodeId			= this.getGroupNode(groupId)
		var nodePosition	= this.getNodePosition(nodeId);
		var nodeSize		= this.getNodeSize(nodeId);
		
		if (nodePosition.y == 0)	{
			groupPosition.x = nodePosition.x - 1;
			groupPosition.y = this.topLevelHeight + nodePosition.y;
		}	else	{
			/*	коррекция	*/
			if (this.isIE) {
				groupPosition.x = nodePosition.x + nodeSize.w + 1;
				groupPosition.y = nodePosition.y - 1;
			}	else if(this.isOpera)	{
				groupPosition.x = nodePosition.x + nodeSize.w + 1;
				groupPosition.y = nodePosition.y - 1;
			}	else if(this.isWebKit)	{
				groupPosition.x = nodePosition.x + nodeSize.w + 1;
				groupPosition.y = nodePosition.y - 1;
			}	else	{
				groupPosition.x = nodePosition.x + nodeSize.w + 1;
				groupPosition.y = nodePosition.y - 1;
			}
		}
		return groupPosition;
	},

	/*	общие функции	*/
	showElement:	function(id)	{
		var element = document.getElementById(id);
		if (element.style.visibility != 'visible') {
			element.style.visibility = 'visible';
		}
	},
	
	hideElement:	function(id)	{
		var element = document.getElementById(id);
		if (element.style.visibility != 'hidden') {
			element.style.visibility = 'hidden';
		}
	},

	getAbsolutePos:	function(el)
	{
		
		var r = { x: el.offsetLeft, y: el.offsetTop };
		if (el.offsetParent)
		{
			var tmp = this.getAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}
		return r;
	},
	
	getElementSize:	function(el)
	{
		var r = { w: el.offsetWidth, h: el.offsetHeight };
		return r;
}	}