/*
 * Submenu #pageMenu script
 *
 * Developer Viktor Ryabov (www.e-gloryon.com)
 * Date: 2009/04/16
 *
 * Required libraries: jquery.js
 */
var t = 200; // Submenu collapse delay, ms
var t1 = 150; // Submenu animation time, ms
var w = false;
$(document).ready(function () {
	$('#pageMenu div.submenu').each(function (index) {
		$(this).hover(
			function () { $(this).attr('mouse', '1'); },
			function () { $(this).attr('mouse', '0'); setTimeout("check(" + index + ")", t); }
		).hide().css('position', 'absolute').attr('ind', index);
		
	});
	w = $('#pageMenu div.submenu:first').css('width');
	w = (w == 'auto' || w == '0px') ? $('#pageMenu a:first').outerWidth() : false;
	$('#pageMenu a.menu').each(function () {
		if ($(this).next().is('div.submenu'))
			$(this).hover(
				function () { $(this).attr('mouse', '1'); show($(this).next()); },
				function () { $(this).attr('mouse', '0'); setTimeout("check(" + $(this).next().attr('ind') + ")", t); }
			).addClass('expandible collapsed');
		else
			$(this).hover(
				function () { $(this).attr('mouse', '1') },
				function () { $(this).attr('mouse', '0') }
			);
	});
});
function show(submenu) {
	sm_prev = submenu.prev();
	sm_prev.removeClass('collapsed').addClass('expanded');
	m1 = sm_prev.outerHeight();
	m2 = parse_number(sm_prev.css('margin-bottom'));
	m3 = parse_number(submenu.children(':first').css('margin-top'));
	m4 = parse_number(submenu.css('padding-top'));
	if (w !== false) submenu.width(w);
	submenu.show(t1).css('left', submenu.parent('div').outerWidth() + 'px').css('margin-left', 0).css('margin-top', '-' + (m1 + m2 + m3 + m4) + 'px');
}
function hide(submenu) {
	submenu.prev().removeClass('expanded').addClass('collapsed');
	submenu.hide(t1);
}
function parse_number(str) {
	num = str.match(/^\-?\d+\.?\d*/);
	return num ? parseFloat(num) : 0;
}
function check(i) {
	var subm = $('#pageMenu .submenu').eq(i);
	var flag = false;
	if (subm.attr('mouse') == '1' || subm.prev().attr('mouse') == '1') flag = true;
	if (!flag) {
		subm.children('.submenu').each(function () {
			flag = flag || $(this).attr('mouse') == '1';
		});
	}
	if (!flag) {
		subm.children('a').each(function () {
			flag = flag || $(this).attr('mouse') == '1';
		});
	}
	if (!flag) hide(subm);
}
