var TOP_MENU_ITEM_PREFIX = "tm_item";
var TOP_MENU_SUBITEM_PREFIX = "tm_subitem";
var TOP_MENU_ITEMS = 7;

var selectedMenu = null;

//Определяем версию броузера
var isNS4 = (document.layers) ? true : false;
var isIE4 = (document.all && !document.getElementById) ? true : false;
var isIE5 = (document.all && document.getElementById) ? true : false;
var isNS6 = (!document.all && document.getElementById) ? true : false;
var isFF = (-1 != navigator.userAgent.indexOf('Firefox') ||
		-1 != navigator.userAgent.indexOf('Iceweasel')
		) ? true : false;
//alert(isNS4 + '/' + isIE4 + '/' + isIE5 + '/' + isNS6);

function getElementById(id) {
	if (isNS4)
		return document.layers[id];
	else if (isIE4)
		return document.all[id];
	else
		return document.getElementById(id);
}

//Устанавливает класс cn элементу e
function setClassName(e, cn) {
	e.className = cn;
}

//Получает объект, по переданному event'у
function getTargetFromEvent(event) {
	if (isFF)
		return event.currentTarget;
	else
		return event.srcElement;
}

var content = null;

function OnLoad() {
	for (i = 1; i <= TOP_MENU_ITEMS; i++) {
		eval('$("#" + TOP_MENU_ITEM_PREFIX + ' + i + ').mouseover(function (event) { ItemHover(event, ' + i + '); });');
		$("#" + TOP_MENU_SUBITEM_PREFIX + i).mouseover(function () { stopTimer(); });
		$("#" + TOP_MENU_SUBITEM_PREFIX + i).mouseout(function () { autoHide(); });
	}

	content = document.getElementById("content");
}

function ItemHover(event, i) {
	var target = getElementById(TOP_MENU_ITEM_PREFIX + i);

	showSubmenu(target, TOP_MENU_SUBITEM_PREFIX + i);
}

//Код, относящийся к выпадающим меню
var currentMenu = null;
var hideDelay = 500;
var timerStatus = false;	//false - таймер не запущен, true - таймер запущен

//Показывает, либо скрывает подменю
function switchDiv(div, visible) {
	if (isNS4 || isIE4) {
		if (!visible) {
			div.visibility = 'hidden';
		} else {
			div.visibility = 'visible';
		}
	} else
		if (!visible) {
			div.style.display = 'none';
		} else {
			div.style.display = '';
		}

	return 1;
}

//Возвращает сумму значений параметров элементов от текущего элемента по цепочке - до корневого родителя включительно
//Применительно к параметрам Left и Top - это их абсолютные значения для элемента
function getPos(e, property) {
	var pos = 0;
	while (e != null) {
		pos += e['offset' + property];
		e = e.offsetParent;
	}
	return pos;
}

function showSubmenuByTimeout() {
	var cell = _cell;
	var submenu = _submenu;
	var end = new Date().getTime();

	alert(end - start);

}

function showSubmenu(cell, submenu) {
	if (null != currentMenu) {
		switchDiv(currentMenu, false);
		if (null != selectedMenu)
			setClassName(selectedMenu, 'top_menu_item');
	}

	if (null != submenu) {
		setClassName(cell, 'top_menu_item_selected');
		selectedMenu = cell;

		submenu = getElementById(submenu);
		submenu.style.left = getPos(cell, 'Left') + 'px';
		submenu.style.top = getPos(cell, 'Top') + cell.offsetHeight + 'px';
		switchDiv(submenu, true);
		currentMenu = submenu;
	}
}

function autoHide() {
	autoHideTimer = setTimeout('showSubmenu(null, null)', hideDelay);
	timerStatus = true;

	return 1;
}

function stopTimer() {
	if (timerStatus) {
		clearTimeout(autoHideTimer);
		timerStatus = false;
	}

	return 1;
}

function insertVideo(url, splash, comment) {
	var flashvars = {
		way : url,
		swf : "/swf/uflvplayer_500x375.swf",
		w : "400",
		h : "340",
		skin : "noneblue",
		pic : splash,
		autoplay : "0",
		tools : "1",
		q : "",
		volume : "70",
		comment : comment
	};
	var params = {
		bgcolor : "#d0dbe5",
		wmode : "transparent",
		allowFullScreen : "always"
	};
	var attributes = {
		id : "video",
		style : "z-index : 0;"
	};
	swfobject.embedSWF("/swf/uflvplayer_500x375.swf", "video", "400", "340", "9.0.0", "/swf/expressInstall.swf", flashvars, params, attributes);
}
