/*
Выпадающее меню, расчитано только на одно меню на странице.
Пример использования:
<div class="sim_menu" id="sim_menu">
	<span class="sim_item" onmouseover="SIM.itemover(this);">Item 1</span>
	<div class="sim_menu" orientation="bottom">
		<div class="sim_item" onmouseover="SIM.itemover(this);">Subitem 1.1</div>
		<div class="sim_item" onmouseover="SIM.itemover(this);">Subitem 1.2</div>
		<div class="sim_item" onmouseover="SIM.itemover(this);">Subitem 1.3</div>
	</div>
	<span class="sim_item" onmouseover="SIM.itemover(this);">Item 2</span>
	<div class="sim_menu" orientation="bottom">
		<div class="sim_item" onmouseover="SIM.itemover(this);">Subitem 2.1</div>
		<div class="sim_item" onmouseover="SIM.itemover(this);">Subitem 2.2</div>
	</div>
</div>
<script type="text/javascript">SIM.init('sim_menu');</script>
*/
window.SIM = 
{
	opened_nodes: [],
	close_timer: [],
	next_node_id: 0,
	relations: {},

	get_left: function(obj)
	{
		var l = 0;
		while (obj)
		{
			if (obj.style.position && obj.style.position != 'static') { l += obj.offsetLeft; break; }
			l += obj.offsetLeft; obj = obj.offsetParent;
		}
		return l;
	},

	get_top: function(obj)
	{
		var l = 0;
		while (obj)
		{
			if (obj.style.position && obj.style.position != 'static') { l += obj.offsetTop; break; }
			l += obj.offsetTop; obj = obj.offsetParent;
		}
		return l;
	},

	hide_show_selects: function(obj, h)
	{
		var agent = navigator.userAgent.toLowerCase();
		if (!window.opera && document.all && agent.indexOf("msie ") > -1 && parseFloat(agent.substr(agent.indexOf("msie ") + 5)) < 7)
		{
			for (var step = 0; step <= 1; step++)
			{
				var s = null;
				if (step == 0) { s = document.getElementsByTagName("SELECT"); }
				if (step == 1) { s = document.getElementsByTagName("IFRAME"); }
				for (var i = 0; i < s.length; i++)
				{
					var p = new Array(this.get_left(s[i]), this.get_top(s[i]), s[i].offsetWidth, s[i].offsetHeight, this.get_left(obj), this.get_top(obj), obj.offsetWidth, obj.offsetHeight);
					if (p[0] + p[2] > p[4] && p[0] < p[4] + p[6] && p[1] + p[3] > p[5] && p[1] < p[5] + p[7]) { s[i].style.visibility = h ? 'hidden' : 'visible'; }
				}
			}
		}
	},

	init: function(id)
	{
		var root = document.getElementById(id);
		this.attach_event(root, 'mouseover', this.menuover);
		this.attach_event(root, 'mouseout', this.menuout);

		this.setup_relations(root, true);
		for (i in this.relations)
		{
			var menu_item = this.relations[i];
			if (menu_item['submenu'])
			{
				var d = document.body;
				menu_item['submenu'].parentNode.removeChild(menu_item['submenu']);
				if (d.childNodes.length) { d.insertBefore(menu_item['submenu'], d.childNodes[0]); }
				else { d.appendChild(menu_item['submenu']); }
				this.attach_event(menu_item['submenu'], 'mouseover', this.menuover);
				this.attach_event(menu_item['submenu'], 'mouseout', this.menuout);
			}
		}
	},

	attach_event: function(o, evt, func)
	{
		if (o.attachEvent) { o.attachEvent('on' + evt, func); }
		else { o.addEventListener(evt, func, false); }
	},

	setup_relations: function(obj)
	{
		var childs = obj.childNodes;
		var previous_item_id = null;
		for (var i = 0, c = childs.length; i < c; i++)
		{
			var child = childs[i];
			if (child.className && child.className.match(/\bsim_item\b/))
			{
				if (!child.sim_id) { child.sim_id = (this.next_node_id++); }
				this.relations[child.sim_id] = {'parent': obj};
				previous_item_id = child.sim_id;
			}
			else if (child.className && child.className.match(/\bsim_menu\b/))
			{
				if (!child.sim_id) { child.sim_id = (this.next_node_id++); }
				this.relations[previous_item_id]['submenu'] = child;
				child.style.position = 'absolute';
				child.style.display = 'none';
				this.setup_relations(child);
			}
			else { this.setup_relations(child); }
		}
	},

	close_all_nodes: function()
	{
		while (this.opened_nodes.length)
		{
			var obj = this.opened_nodes.pop();
			this.hide_show_selects(obj, false);
			obj.style.display = 'none';
		}
	},

	menuover: function() { if (SIM.close_timer) { clearTimeout(SIM.close_timer); SIM.close_timer = null; } },
	menuout: function() { if (!SIM.close_timer) SIM.close_timer = setTimeout("SIM.close_all_nodes()", 800); },
	
	itemover: function(item_obj)
	{
		var sub_obj = this.relations[item_obj.sim_id]['submenu'];
		var parent_obj = this.relations[item_obj.sim_id]['parent'];
		
		while (this.opened_nodes.length)
		{
			if (parent_obj && parent_obj.sim_id == this.opened_nodes[this.opened_nodes.length-1].sim_id) { break; }
			this.hide_show_selects(this.opened_nodes[this.opened_nodes.length-1], false);
			this.opened_nodes[this.opened_nodes.length-1].style.display = 'none';
			this.opened_nodes.pop();
		}
		
		this.hide_show_selects(parent_obj, true);
		
		if (sub_obj)
		{
			var orientation = sub_obj.getAttribute('orientation');
			var new_left = 0; var new_top = 0;
			if (orientation == 'bottom') { new_left = this.get_left(item_obj); new_top = (this.get_top(parent_obj) + parent_obj.offsetHeight); }
			else { new_left = (this.get_left(parent_obj) + parent_obj.offsetWidth); new_top = this.get_top(item_obj); }
			sub_obj.style.left = new_left + 'px';
			sub_obj.style.top = new_top + 'px';
			this.hide_show_selects(sub_obj, true);
			this.opened_nodes.push(sub_obj);
			sub_obj.style.display = 'block';
			if (sub_obj.offsetWidth < parent_obj.offsetWidth) { sub_obj.style.width = (parent_obj.offsetWidth) + 'px'; }
			else { sub_obj.style.width = (sub_obj.offsetWidth) + 'px'; }
		}
	}
}