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 hCoords = Common.Dom.getAbsoluteCoords( oListItem );
	var iTopOfsset = hCoords.iTop;
	var iLeftOfsset = hCoords.iLeft;
	var oSubList = oListItem.eINS;
	if (oSubList) {
		Common.Class.add(oSubList, 'visible');
		oSubList.style.display = 'block';
		Common.Class.add(oListItem, 'open');
		Common.Class.remove(oListItem, 'hover');
		oSubList.style.top = 0;
		// нужно найти текущий пункт (с классом selected)
		var iOffsetCurrent = 0;
		var iOffsetWidth = 0;
		for ( var iItemIdx = 0; ( oItem = oSubList.getElementsByTagName('div')[iItemIdx] ); iItemIdx++ ) {
			if ( Common.Class.match(oItem, 'selected') ) iOffsetCurrent = oItem.offsetTop;
			if ( oItem.offsetWidth > iOffsetWidth ) iOffsetWidth = oItem.offsetWidth;
		}
		oSubList.style.left = iLeftOfsset + 'px';
		// смещаем список на найденую высоту
		oSubList.style.top = (iTopFix + iTopOfsset - iOffsetCurrent ) + 'px';
		
		// задаем ширину (из за Firefox 1.0?)
		oSubList.style.width = iOffsetWidth + 2;

		if ( iOffsetCurrent > iTopOfsset ) {
			// показываем стрелку
			var oArrowDiv = oSubList.getElementsByTagName('p')[0];
			if ( oArrowDiv ) {
				Common.Class.add(oArrowDiv, 'visible');
				oArrowDiv.style.top = iOffsetCurrent - iTopOfsset - 3 + 'px';
				oArrowDiv.onmouseover = startTermoListScroll;
				oArrowDiv.onmouseout = stopTermoListScroll;
			}
		}


		oCurrentThermoList = oListItem;
	}
}

function hideThermoList( bByDelay ) {
	if( bByDelay && !bHideThermoList ){
		return;
	}
	if ( oCurrentThermoList ) {
		var oSubList = oCurrentThermoList.eINS;
		if (oSubList) {
			Common.Class.remove(oSubList, 'visible');
			oSubList.style.display = 'none';
		}
		Common.Class.remove(oCurrentThermoList, 'open');
		Common.Class.remove(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 oSubList = oCurrentThermoList.eINS;
		if (oSubList) {
			var iOffsetCurrent = parseInt(oSubList.style.top) + 15;
			oSubList.style.top = iOffsetCurrent + 'px';
			var oArrowDiv = oSubList.getElementsByTagName('p')[0];
			if (oArrowDiv) {
				if (iOffsetCurrent < 0) {
					oArrowDiv.style.top = -2 - iOffsetCurrent + 'px';
					startTermoListScroll();
				} else {
					Common.Class.remove(oArrowDiv, 'visible');
					stopTermoListScroll();
				}
			}
		}
	}
}

function initThermoItem( oList ) {
	var eTemp, oLink, j, aeItem;
	for ( var iItemIdx = 0; ( oItem = oList.childNodes[iItemIdx] ); iItemIdx++ ) {
		if ( oItem.nodeType == 1 ) {
			oItem.eINS = oItem.getElementsByTagName('ins')[0]
			if ( oItem.eINS ) {
				eTemp = document.createElement( 'span' );
				eTemp.appendChild( document.createElement( 'span' ) );
				eTemp.className = 'bg';
				oItem.eINS.appendChild( eTemp );
				eTemp = document.createElement( 'p' );
				eTemp.className = 'up-arrow';
				oItem.eINS.appendChild( eTemp );
				oItem.eINS.style.position = 'absolute';
				oItem.eINS.style.display = 'none';
				/* emulating hover */
				oItem.onmouseover = function() { if (!Common.Class.match(this, 'open')) Common.Class.add(this, 'hover'); }
				oItem.onmouseout = function() { Common.Class.remove(this, 'hover'); }
				oItem.onclick = function(evt) {
					if (evt) evt.cancelBubble = true;
						else event.cancelBubble = true;
					showThermoList( this );
				}
				/*oItem.onmouseover = function(evt) {
					if (evt) evt.cancelBubble = true;
						else event.cancelBubble = true;
					showThermoList( this );
				}*/
				Common.Event.add( document, 'click', function(){ hideThermoList(); } )
				/* link */
				aeItem = oItem.getElementsByTagName( 'div' );
				for( j = 0 ; j < aeItem.length ; j++ ){
					if( Common.Class.match( aeItem[j], 'selected' ) ){
						eTemp = document.createElement( 'div' );
						eTemp.className = 'item';
						eTemp.innerHTML = aeItem[j].innerHTML;
						oItem.insertBefore( eTemp, oItem.eINS );
						oLink = eTemp.getElementsByTagName('a')[0];
						if (oLink) {
							oLink.onclick = function(evt) {
								if (evt) evt.cancelBubble = true;
									else event.cancelBubble = true;
							}
						}
						document.body.insertBefore( oItem.eINS, document.body.firstChild );
						//Common.Event.add( oItem.eINS, 'mouseout', function( e ){ bHideThermoList = true; setTimeout( 'hideThermoList(true)', 300 ); } )
						//Common.Event.add( oItem.eINS, 'mouseover', function( e ){ bHideThermoList = false; } )
						break;
					}
				}
			}
		}
	}
	eTemp = document.createElement( 'li' );
	eTemp.className = 'clear';
	oList.appendChild( eTemp );
}

function initThermo() {
	var eThermo = document.getElementById('thermo');
	if( eThermo ){
		initThermoItem( eThermo );
	}
}

function placeholder( eThis, sText, sClass_on_empty ){
	eThis.onfocus = function(){ if( eThis.value.length && eThis.value == sText ){ eThis.value = ''; } Common.Class.remove( this, sClass_on_empty ); eThis.select(); document.getElementsByTagName( 'body' )[0].className += ''; }
	eThis.onblur = function(){ if( !this.value.length || eThis.value == sText ){ Common.Class.add( this, sClass_on_empty ); this.value = sText; document.getElementsByTagName( 'body' )[0].className += ''; } }
	if( !eThis.value.length ){ eThis.onblur(); }
	if( !eThis.getAttribute( 'placeholder' ) ){ eThis.setAttribute( 'placeholder', sText ); }
}

function init_FAQ(){
	var aeQuestion = Common.Dom.getElementsByClassName( document.body, 'question' );
	if( aeQuestion.length ){
		var aeAnswer = Common.Dom.getElementsByClassName( document.body, 'answer' );
		for( var i = 0, eINS, j ; i < aeQuestion.length ; i++ ){
			if( aeAnswer[i] ){
				eINS = document.createElement( 'ins' );
				eINS.className = 'pseudo-link';
				for( j = 0 ; j < aeQuestion[i].childNodes.length - 1 ; j++ ){
					eINS.appendChild( aeQuestion[i].childNodes[j] );
					j--;
				}
				aeQuestion[i].insertBefore( eINS, aeQuestion[i].firstChild );
				Common.Event.add( eINS, 'click', UnHide_depended );
				eINS.eDepended = aeAnswer[i];
				Common.Class.add( aeAnswer[i], 'hidden' );
				Common.Class.add( aeQuestion[i], 'inited' );
			}
		}
	}
}

function UnHide_depended( oEvent ){
	oEvent = Common.Event.normalize( oEvent );
	if( Common.Class.match( oEvent.target.eDepended, 'hidden' ) ){
		Common.Class.add( oEvent.target.parentNode, 'active' );
		Common.Class.remove( oEvent.target.eDepended, 'hidden' );
	}else{
		Common.Class.remove( oEvent.target.parentNode, 'active' );
		Common.Class.add( oEvent.target.eDepended, 'hidden' );
	}
}
