var oCurrentThermoList = null;

var iTopFix = (navigator.userAgent.indexOf('Safari') >= 0)? -1 : -2;

function showThermoList( oListItem ) {
	if (oCurrentThermoList) {
		if ( oCurrentThermoList == oListItem ) { hideThermoList(); return true; }
		hideThermoList();
	}
	var iTopOfsset = getPageY(oListItem);
	var iLeftOfsset = getPageX(oListItem);
	var oSubList = oListItem.eINS;
	if (oSubList) {
		cmnSet_class(oSubList, 'visible');
		cmnSet_class(oListItem, 'open');
		cmnRemove_class(oListItem, 'hover');
		oSubList.style.top = 0;
		// нужно найти текущий пункт (с классом selected)
		var iOffsetCurrent = 0;
		var iOffsetWidth = 0;
		for ( var iItemIdx = 0; ( oItem = oSubList.getElementsByTagName('div')[iItemIdx] ); iItemIdx++ ) {
			if ( cmnMatch_class(oItem, 'selected') ) iOffsetCurrent = oItem.offsetTop;
			if ( oItem.offsetWidth > iOffsetWidth ) iOffsetWidth = oItem.offsetWidth;
		}
		oSubList.style.left = iLeftOfsset;
		// смещаем список на найденую высоту
		oSubList.style.top = iTopFix - iOffsetCurrent + iTopOfsset;
		
		// задаем ширину (из за Firefox 1.0?)
		oSubList.style.width = iOffsetWidth + 2;

		if ( iOffsetCurrent > iTopOfsset ) {
			// показываем стрелку
			var oArrowDiv = oSubList.getElementsByTagName('p')[0];
			if ( oArrowDiv ) {
				cmnSet_class(oArrowDiv, 'visible');
				oArrowDiv.style.top = iOffsetCurrent - iTopOfsset - 3;
				oArrowDiv.onmouseover = startTermoListScroll;
				oArrowDiv.onmouseout = stopTermoListScroll;
			}
		}


		oCurrentThermoList = oListItem;
	}
}

function hideThermoList() {
	if ( oCurrentThermoList ) {
		var oSubList = oCurrentThermoList.eINS;
		if (oSubList) {
			cmnRemove_class(oSubList, 'visible');
		}
		cmnRemove_class(oCurrentThermoList, 'open');
		cmnRemove_class(oCurrentThermoList, 'hover');
		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.eINS;
		if (oSubList) {
			var iOffsetCurrent = parseInt(oSubList.style.top) + 15;
			oSubList.style.top = iOffsetCurrent;
			var oArrowDiv = oSubList.getElementsByTagName('p')[0];
			if (oArrowDiv) {
				if (iOffsetCurrent < 0) {
					oArrowDiv.style.top = -2 - iOffsetCurrent;
					startTermoListScroll();
				} else {
					cmnRemove_class(oArrowDiv, 'visible');
					stopTermoListScroll();
				}
			}
		}
	}
}

function initThermoItem( oList ) {
	for ( var iItemIdx = 0; ( oItem = oList.childNodes[iItemIdx] ); iItemIdx++ ) {
		if ( oItem.nodeType == 1 ) {
			oItem.eINS = oItem.getElementsByTagName('ins')[0]
			if ( oItem.eINS && !oItem.getElementsByTagName('p')[0].offsetHeight ) {
				document.body.appendChild( oItem.eINS );
				/* emulating hover */
				oItem.onmouseover = function() { if (!cmnMatch_class(this, 'open')) cmnSet_class(this, 'hover'); }
				oItem.onmouseout = function() { cmnRemove_class(this, 'hover'); }
				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 getPageY( oElement ) {
	var iPosY = oElement.offsetTop;
	while ( oElement.offsetParent != null ) {
		oElement = oElement.offsetParent;
		iPosY += oElement.offsetTop;
		if (oElement.tagName == 'BODY') break;
	}
	return iPosY;
}

function getPageX( oElement ) {
	var iPosX = oElement.offsetLeft;
	while ( oElement.offsetParent != null ) {
		oElement = oElement.offsetParent;
		iPosX += oElement.offsetLeft;
		if (oElement.tagName == 'BODY') break;
	}
	return iPosX;
}

function initThermo() {
	for (var iListIdx = 0; ( oList = document.getElementsByTagName('ul')[iListIdx] ); iListIdx++) {
		if ( cmnMatch_class(oList, 'thermo') ) initThermoItem(oList);
	}
}

var eCity_chooser = false;

function init_cities_list(){
	eCity_chooser = document.getElementById( 'city_chooser' );
	if( eCity_chooser ){
		eCity_chooser = eCity_chooser.getElementsByTagName( 'ul' )[0];
		cmnSet_class( eCity_chooser, 'inited' );
		for( var i = 0 ; i < eCity_chooser.childNodes.length ; i++ ){
			if( cmnMatch_class( eCity_chooser.childNodes[i], 'selected' ) ){
				eCity_chooser.childNodes[i].onclick = function(){
					eCity_chooser.bClick = true;
					var iTop_offset = getPageY( this );
					cmnSet_class( eCity_chooser, 'visible' );
					eCity_chooser.style.top = '-' + this.offsetTop;
					if( iTop_offset < this.offsetTop ){ eCity_chooser.style.top = '-' + ( iTop_offset - 5 ); }
				}
			}
		}
		cmnAdd_event( document, 'click', function(){
			if( eCity_chooser.bClick ){
				eCity_chooser.bClick = false;
			}else{
				cmnRemove_class( eCity_chooser, 'visible' );
			}
		});
	}
}

function init_exchange_tables(){
	var aeExchange = document.getElementById( 'subnavigation' );
	if( aeExchange ){
		aeExchange = aeExchange.getElementsByTagName( 'caption' );
		for( var i = 0 ; i < aeExchange.length ; i++ ){
			if( i != aeExchange.length - 1 ){ cmnSet_class( aeExchange[i].parentNode, 'hidden' ); }
			aeExchange[i].onclick = function(){
				for( var i = 0 ; i < aeExchange.length ; i++ ){
					cmnSet_class( aeExchange[i].parentNode, 'hidden' );
				}
				cmnRemove_class( this.parentNode, 'hidden' );
			}
		}
	}
}

//cmnAdd_event( document, 'click', hideThermoList );

var eHTML = document.getElementsByTagName( 'html' )[0];

function init_theme( sLabel ){
	if( sLabel ){
		var eParent = document.getElementById( 'made-in' );
		if( eParent ){
			var eDIV = document.createElement( 'div' );
			eDIV.id = 'themes';
			eDIV.innerHTML = '<span class="label">' + sLabel + '</span> <a href="?theme=" class="null" onclick="return set_theme( \'\' );">0</a> <a href="?theme=a" class="a" onclick="return set_theme( \'a\' );">a</a> <a href="?theme=b" class="b" onclick="return set_theme( \'b\' );">b</a> <a href="?theme=c" class="c" onclick="return set_theme( \'c\' );">c</a>';
			eParent.insertBefore( eDIV, eParent.firstChild );
		}
	}
	var sTheme = false;
	if( location.href.match( /theme=/ ) ){
		sTheme = location.href.replace( /.*theme=([abc]?).*/, '$1' );
	}else{
		sTheme = cmnGet_cookie( 'theme' );
	}
	if( sTheme != false ){
		set_theme( sTheme );
	}
}

function set_theme( sTheme ){
	cmnRemove_class( eHTML, '[abc]' );
	cmnSet_class( eHTML, sTheme );
	cmnSet_cookie( 'theme', sTheme, new Date( ( new Date ).getFullYear() + 2, 1, 1 ) );
	return false;
}

function check_blur(){
	var eContent = document.getElementById( 'content' );
	if( eContent && eContent.firstChild.firstChild.offsetHeight != eContent.lastChild.firstChild.offsetHeight ){ cmnSet_class( document.getElementsByTagName( 'html' )[0], 'no_blur' ); }
}

//cmnAdd_event( window, 'load', check_blur );
