var sThermoItemOver = 'over';                    /* класс элемента при наведении мыши  */
var sThermoItemOpen = 'open';                    /* класс элемента при открытом подменю  */
var sThermoItemNoMenu = 'nomenu';

var sThermoMenuPrefix = '_menu';
var sResizeTargetId = 'navigation';

var sThermoMenuBeforeHTML = '<div class="top-border"></div><ins class="tl"></ins><ins class="tr"></ins><ins class="bl"></ins><ins class="br"></ins><a class="cross">&nbsp;</a><div>';
var sThermoMenuAfterHTML = '</div><div class="bot-border"></div>'; 


// Объект Thermo - содержит градусник, одна штука
function Thermo() {

	var eThis = this;
	this.iZIndex = 100;
	this.aItems = new Array();
	$('ul.thermo li').each(function(i){
		eThis.aItems.push(new ThermoItem(eThis, $(this)));
	});
	return this;
}

// Скрывает меню
Thermo.prototype.hideMenu = function() {
	if (this && this.oItemOpened) {
		this.oItemOpened.hideMenu(true);
		this.oItemOpened = null;
	}
}



function ThermoItem(oThermo, eThermoItem) {

	this.eThermoItem = eThermoItem;
	this.oThermo = oThermo;
	this.eMenu = null;
	this.menuOpened = false;
	this.name = this.eThermoItem.attr('id');
	
	var eThis = this;

	// назначаем обработчиков событий
	//eThermoItem.mouseover(function(e){ eThis.itemOver() });
	eThermoItem.mouseout(function(e){ eThis.itemOut(e) });
	eThermoItem.mousedown(function(e){ return eThis.itemClick(e) });
	eThermoItem.mouseover(function(e){ eThis.itemOver(e) });
	
	this.createMenu();
	
	$(window).load(function(){
		eThis.resize();
	})
	eThis.resize();
	
	return this;
}


// Функция, вызываемая при наведении мыши на элемент градусника
ThermoItem.prototype.itemOver = function (e) {

	if (!this.eMenu) return;
	this.eThermoItem.addClass(sThermoItemOver);
	/*if (this.oThermo.oItemOpened) {
		this.oThermo.hideMenu();
		this.itemClick(e);
	}*/
}


// Функция... при убирания мыши с элемента градусника
ThermoItem.prototype.itemOut = function (e) {

	if (!this.eMenu) return;
	this.eThermoItem.removeClass(sThermoItemOver);
}


// Функция... при клике на элемент градусника
ThermoItem.prototype.itemClick = function (e) {

	if (!this.eMenu) {
		return true;
	}

	oClickedThermo = this;

	if (e.target && e.target.tagName && e.target.tagName.toLowerCase() != 'a') {

		if (!this.menuOpened) {
			this.showMenu();
		} else {
			this.hideMenu();
		}
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		if (e.preventDefault) e.preventDefault();
		else if (e.returnValue) e.returnValue = false;
	}
	return false;
}


//  показывает меню
ThermoItem.prototype.showMenu = function() {

	if (this && this.eThermoItem && this.oThermo) {
		
		// скрываем другого меню, если оно открыто
		if (this.oThermo.oItemOpened) this.oThermo.hideMenu();

		// показываем меню
		this.eThermoItem.css('z-index', ++this.oThermo.iZIndex);
		this.eThermoItem.removeClass(sThermoItemOver);
		this.eThermoItem.addClass(sThermoItemOpen);
		this.eMenu.css('left', this.eThermoItem.offset().left - 23);
		this.eMenu.show();
		this.menuOpened = true;
		this.oThermo.oItemOpened = this;
		oActiveThermo = this.oThermo;
	}
}


ThermoItem.prototype.hideMenu = function(clearOver) {

	if (this && this.eThermoItem && this.oThermo) {
		this.eMenu.hide();
		this.eThermoItem.removeClass(sThermoItemOpen);
		if (!clearOver) this.eThermoItem.addClass(sThermoItemOver);
		this.menuOpened = false;
		oActiveMenu = null;
	}
}


// создает меню
ThermoItem.prototype.createMenu = function() {

	var oMenu = document.getElementById(this.name + sThermoMenuPrefix);
	var eThis = this;
	if (oMenu) {
		oMenu.innerHTML = sThermoMenuBeforeHTML + oMenu.innerHTML + sThermoMenuAfterHTML;
		this.eMenu = $(oMenu);
		this.eMenu.mousedown(function(e){ eThis.menuDown(e) });
		this.eMenu.click(function(e){ eThis.menuClick(e) });
		
		var cross = this.eMenu.children('s.cross');
		if (cross.is('s')) {
			cross.click(function(e){ eThis.oThermo.hideMenu(); })
		}
	}
}


// нажатие на подменю
ThermoItem.prototype.menuDown = function(e) {

	e.stopPropagation();
	e.cancelBubble = true;
	return false;
}

// клик по подменю
ThermoItem.prototype.menuClick = function(e) {

	if (e.target && e.target.tagName && e.target.tagName.toLowerCase() == 'a') {
		if (this.oThermo) this.oThermo.hideMenu();
	}
	return false;
}


// подгоняем размеры
ThermoItem.prototype.resize = function() {
	return;
	var resizeTarget = document.getElementById(sResizeTargetId);
	var w = resizeTarget.rows[0].cells[0].offsetWidth - 2;
	if (!this.eThermoItem.prev('li').size()) w -= 4;
	if (this.eThermoItem.width() < w) {
		this.eThermoItem.width(w);
	}
}



var oActiveThermo = null;
var oClickedThermo = null;

$(window).load(function(){
	var thermo = new Thermo();
})

$(document).mousedown(function(e){
	closeActiveThermo(e);
})

function closeActiveThermo(e) {

	if (oActiveThermo && oActiveThermo != oClickedThermo){
		oActiveThermo.hideMenu();
		oActiveThermo = null;
	}
	return true;
}