var oCurrentThermoList = null;

var iTopFix = (navigator.userAgent.indexOf('Safari') >= 0)? -2 : -3;

function showThermoList( oListItem ) {
	if (oCurrentThermoList) {		
		if ( oCurrentThermoList == oListItem ) { hideThermoList(); return true; }
		hideThermoList();
	}
	var iTopOfsset = getPageY(oListItem);
	var oSubList = oListItem.getElementsByTagName('ins')[0];
	if (oSubList) {
		// нужно найти текущий пункт (с классом current)
		var iOffsetCurrent = 0;
		var iOffsetWidth = 0;
		for ( var iItemIdx = 0; ( oItem = oListItem.getElementsByTagName('div')[iItemIdx] ); iItemIdx++ ) {
			if ( matchClass(oItem, 'current') ) iOffsetCurrent = oItem.offsetTop;
			if ( oItem.offsetWidth > iOffsetWidth ) iOffsetWidth = oItem.offsetWidth;
		}
		// смещаем список на найденую высоту
		oSubList.style.top = iTopFix - iOffsetCurrent;
		
		// задаем ширину (из за Firefox 1.0?)
		oSubList.style.width = iOffsetWidth;

		if ( iOffsetCurrent > iTopOfsset ) {
			// показываем стрелку
			var oArrowDiv = oSubList.getElementsByTagName('p')[0];
			if ( oArrowDiv ) {
				addClass(oArrowDiv, 'visible');
				oArrowDiv.style.top = iOffsetCurrent - iTopOfsset - 3;
				oArrowDiv.onmouseover = startTermoListScroll;
				oArrowDiv.onmouseout = stopTermoListScroll;
			}
		}

		addClass(oSubList, 'visible');
		addClass(oListItem, 'open');    
		removeClass(oListItem, 'over');				

		oCurrentThermoList = oListItem;
	}
}

function hideThermoList() {
	if ( oCurrentThermoList ) {
		var oSubList = oCurrentThermoList.getElementsByTagName('ins')[0];
		if (oSubList) {
			removeClass(oSubList, 'visible');
		}
		removeClass(oCurrentThermoList, 'open');
		removeClass(oCurrentThermoList, 'over');
		oCurrentThermoList = null;
	}
}


var iScrollTO = 0

function startTermoListScroll() {
	iScrollTO = setTimeout('scrollThermoListDown()', 50);
}

function stopTermoListScroll() {
	if (iScrollTO != 0 ) {
		clearTimeout(iScrollTO);
		iScrollTO = 0;
	}
}

function scrollThermoListDown() {
	if ( oCurrentThermoList ) {
		var iTopOfsset = getPageY(oCurrentThermoList);
		var oSubList = oCurrentThermoList.getElementsByTagName('ins')[0];
		if (oSubList) {
			var iOffsetCurrent = Math.abs(parseInt(oSubList.style.top)) - 15;
			oSubList.style.top = -2 - iOffsetCurrent;
			var oArrowDiv = oSubList.getElementsByTagName('p')[0];
			if (oArrowDiv) {
				if (iOffsetCurrent > iTopOfsset) {
					oArrowDiv.style.top = iOffsetCurrent - iTopOfsset;
					startTermoListScroll();
				} else {
					removeClass(oArrowDiv, 'visible');
					stopTermoListScroll();
				}
			}
		}
	}
}

function initThermoItem( oList ) {
	var link;
	for ( var iItemIdx = 0; ( oItem = oList.childNodes[iItemIdx] ); iItemIdx++ ) {
		if ( oItem.nodeType == 1 ) {
			
			if (link = oItem.getElementsByTagName('A')[0] ) {				
				link.onmouseover = function(evt) {
					var item = this.parentNode.parentNode;					
					if(item.getElementsByTagName('ins')[0]){						
						if (!matchClass(item, 'open')) addClass(item, 'over');
						}
					if (evt) evt.cancelBubble = true;
						else event.cancelBubble = true;
				}
				link.onmouseout = function(evt) {
					var item = this.parentNode.parentNode;					
					if(item.getElementsByTagName('ins')[0]){
						removeClass(item, 'over');
					}
					if (evt) evt.cancelBubble = true;
						else event.cancelBubble = true;
				}
			}
			
			if ( oItem.getElementsByTagName('ins')[0] ) {
				/* emulating hover */				
				oItem.onmouseover = function(evt) {										
					
					var evt = evt? evt : event;
					var coords = getPositionedEventCoord(evt);					
					if(this.offsetWidth - coords.left < 26 || matchClass(this, 'last')) {						
						if (!matchClass(this, 'open')) addClass(this, 'over');
					}					
					
				}
				oItem.onmouseout = function(evt) { removeClass(this, 'over'); }
				oItem.onclick = function(evt) {					
					if (evt) evt.cancelBubble = true;
						else event.cancelBubble = true;
					showThermoList( this );
				}

				/* link */
				var oLink = oItem.getElementsByTagName('div')[0].getElementsByTagName('a')[0];
				if (oLink) {
					oLink.onclick = function(evt) {
						if (evt) evt.cancelBubble = true;
							else event.cancelBubble = true;
					}
				}
			}
		}
	}
}

function initThermo() {
	for (var iListIdx = 0; ( oList = document.getElementsByTagName('ul')[iListIdx] ); iListIdx++) {
		if ( matchClass(oList, 'thermo') ) initThermoItem(oList);
	}
}

addEvent(window, 'load', initThermo);
addEvent(document, 'click', hideThermoList);
