/*
 *	Классы выпадающего меню
 *	Версия: 2.0
 *
 *	*-<|:o)
 */

// ----------------------------------------------------------------------------
var agtX = 0;
var agtY = 0;
var agt = navigator.userAgent.toLowerCase();
var agtIE = (agt.indexOf('msie') != -1) ? true : false;

if(agtIE) agtY = 1;

function TPopMenu(AName)
{
	if(!AName) return;
	
	this.ClassHover		= '';
	this.ClassNormal	= '';
	this.HideTimeMain	= 220;
	this.HideTimePop	= 320;
	this.IdMain			= AName
	this.ShowPath		= true;
	this.BorderCorrect	= false;
	this.TablePrefix	= AName + '-t';
	this.FramePrefix	= AName + '-f';
	this.ShowFrame		= true;
	this.BottomParentWidth	= false;
	
	var LastId			= '';
	var Name			= AName;
	var PanelList		= new TPopMenu_PanelList(this);

	this.GetName = function() {
		return Name;
	}
	
	this.ShowChild = function(Source, IDChild, Position)
	{
		if(!Source.id || !IDChild) return;
		var item = PanelList.Add(this.TablePrefix + IDChild, true);
		if(!item) return;

		var item_top = 0;
		var item_left = 0;
		
		if(this.ShowFrame) {
			item.FrameName = this.FramePrefix + IDChild;
		}
		item.Show(item_top, item_left);
	}
	
	this.Show = function(Source, Level, ClassNormal, ClassHover)
	{
		if(!Source.id) return;
//		var inner = this.AsInner(Source, true);
//		if(inner && LastId == Source.id) return;

		PanelList.ClearLevel(Level - 1);
		
		var item = PanelList.Add(Source.id, false);
		if(!item) return;
		item.ClassHover = ClassHover; 
		item.ClassNormal = ClassNormal;
		if(Level == 1) item.HideTime = this.HideTimeMain;
		item.SetHover();
		
		LastId = Source.id;
	}
	
	this.ShowDefault = function(Source, Level)
	{
		return this.Show(Source, Level, this.ClassNormal, this.ClassHover)
	}

	this.Hide = function(Source)
	{
		if(!Source.id) return;
		var inner = this.AsInner(Source, false);
		if(inner && LastId == Source.id) return;
		var item = PanelList.GetItem(Source.id);
		if(item) item.Hide();
	}

	this.HideItem = function(Name)
	{
		var item = PanelList.GetItem(Name);
		if(!item) return;
		if(Name == LastId) {
			PanelList.Clear(0);
			LastId = '';
		}
	}
	
	this.AsInner = function(Source, IsShow)
	{
		// только для IE
		if(!agtIE) return false;
		
		var x = window.event ? window.event.x : 0;
  		var y = window.event ? window.event.y : 0;

		item_top = Source.offsetTop;
		item_left = Source.offsetLeft;
		var parent = PanelList.GetItemPrev(Source.id);
		if(parent) {
			var parent_obj = parent.GetObject();
			item_top += parent_obj.offsetTop;
			item_left += parent_obj.offsetLeft;
		} else {
			var main_position = this.getMainPosition();
			item_top += main_position[0];
			item_left += main_position[1];
		}
		item_right = item_left + Source.offsetWidth;
		item_bottom = item_top + Source.offsetHeight;
		item_left += (IsShow) ? 0 : 1;
		item_top += (IsShow) ? 0 : 1;
		
		return (
			item_left < x && x < item_right &&
			item_top < y && y < item_bottom
		) ? true : false;
	}
	
	this.getMainPosition = function()
	{
		var node = document.getElementById(this.IdMain);
		var curleft = 0;
		var curtop = 0;
		if (node) {
			while (node.offsetParent) {
				curleft += node.offsetLeft;
				curtop += node.offsetTop; 
				node = node.offsetParent;
			}
		} else if (node.x) {
			curleft += node.x;
			curtop += node.y;
		}
		return new Array(curtop, curleft);
	}
	
}

function TPopMenu_PanelList(APopMenu)
{
	if(!APopMenu) return;

	this.Items	= new Array();
	var PopMenu	= APopMenu;

	this.Add = function(Name, IsPanel)
	{
		if(this.GetItem(Name)) return false;
		var item = new TPopMenu_PanelItem(PopMenu, Name, IsPanel);
		this.Items = this.Items.concat(item);
		return item;
	}
	
	this.ClearLevel = function(Index)
	{
		if(Index == 0) {
			this.Clear(0);
			return;
		}
		var index = 0;
		var count = this.Items.length;
		for(var i = 0; i < count; i++) {
			var item = this.Items[i];
			if(item.IsPanel()) {
				index++;
				if(index == Index) {
					this.Clear(i + 1);
					return;
				}
			}
		}
	}

	this.Clear = function(Index)
	{
		var count = this.Items.length;
		for(var i = Index; i < count; i++) {
			var item = this.Items[i];
			item.SetHide();
		}
		this.Items = this.Items.slice(0, Index);
	}

	this.GetItem = function(Name)
	{
		var count = this.Items.length;
		for(var i = 0; i < count; i++) {
			var item = this.Items[i];
			if(item.GetName() == Name) return item;
		}
		return false;
	}
	
	this.GetItemPrev = function(Name)
	{
		var count = this.Items.length;
		for(var i = 0; i < count; i++) {
			var item = this.Items[i];
			if(item.GetName() == Name) {
				return (i) ? this.Items[i - 1] : false;
			}
		}
		return false;
	}
}

function TPopMenu_PanelItem(APopMenu, AName, APanel)
{
	if(!AName) return;
	if(!APopMenu) return;
	
	this.ClassHover		= '';
	this.ClassNormal	= '';
	this.HideTime		= APopMenu.HideTimePop;
	this.FrameName		= '';
	
	var Timer;
	var Name			= AName;
	var Object			= document.getElementById(AName);
	var Panel			= APanel;
	var PopMenu			= APopMenu;

	this.GetName = function() {
		return Name;
	}
	
	this.Show = function(Top, Left)
	{
		this.SetShow();
	}
	
	this.Hide = function()
	{
		if(!PopMenu.ShowPath && this.ClassNormal) Object.className = this.ClassNormal;
		Timer = setTimeout(PopMenu.GetName() + ".HideItem('" + Name + "');", this.HideTime);
	}
	
	this.SetHover = function()
	{
		if(this.ClassHover == '') return;
		if(Object.className != this.ClassHover) Object.className = this.ClassHover;	
	}
	
	this.SetNormal = function()
	{
		if(this.ClassNormal == '') return;
		if(Object.className != this.ClassNormal) Object.className = this.ClassNormal;
	}
	
	this.SetHide = function()
	{
		clearTimeout(Timer);
		if(Panel) {
			Object.style.display = 'none';
			if(this.FrameName) {
				var obj_frame = document.getElementById(this.FrameName);
				obj_frame.style.display = 'none';
			}
		} else {
			this.SetNormal();
		}
	}

	this.SetShow = function()
	{
		clearTimeout(Timer);
		if(Panel) {
			Object.style.display = '';
			if(this.FrameName) {
				var obj_frame = document.getElementById(this.FrameName);
				obj_frame.style.left	= Object.offsetLeft + 'px';
				obj_frame.style.top		= Object.offsetTop + 'px';
				obj_frame.style.height	= Object.offsetHeight + 'px';
				obj_frame.style.width	= Object.offsetWidth + 'px';
				obj_frame.style.display = 'block';
			}
		} else {
			this.SetHover();
		}
	}
	
	this.GetObject = function()
	{
		return Object;
	}
	this.IsPanel = function()
	{
		return (Panel) ? true : false;
	}

}
// ----------------------------------------------------------------------------

