var oCurrentThermoList = null;

var iTopFix = navigator.userAgent.indexOf('Safari') >= 0 ? -1 : -2;


function make_pseudo( eThis ){
	var eINS = document.createElement( 'ins' );
	eINS.className = 'pseudo-link';
	eThis.insertBefore( eINS, eThis.firstChild );
	eINS.appendChild( eThis.childNodes[1] );
	if( need_fix_hover() ){
		fix_hover( eINS, 'pseudo-link' );
	}
	return eINS;
}

if( need_fix_hover() ){
	Common.Event.add( window, 'load', function(){
		var aeLABEL = document.getElementsByTagName( 'label' );
		for( var i = 0 ; i < aeLABEL.length ; i++ ){
			fix_hover( aeLABEL[i] );
		}
	} );
}

function need_fix_hover(){
	return window.attachEvent;
}

function fix_hover( eThis, sClass ){
	sClass = ( sClass ? sClass + '_' : '' ) + 'hover';
	Common.Event.add( eThis, 'mouseover', function(){ Common.Class.add( eThis, sClass ); } )
	Common.Event.add( eThis, 'mouseout', function(){ Common.Class.remove( eThis, sClass ); } )
}

var ePopup_block = false, ePopup_button = false, bPopup_click = true, bPopup_close = true;;

Common.Event.add( document, 'click', function(){
	setTimeout(
		function(){
			close_popup ();
		}
		, 0
	);
} );


function close_popup () {
	if( !bPopup_click && ePopup_block ){
		ePopup_block.style.top = '-10000px';
		Common.Class.remove( ePopup_button, 'active' );
		ePopup_block = false;
		bPopup_close = true;
		
		var eParent = document.getElementsByTagName( 'form' )[0];
		eParent.submit();
	}
	bPopup_click = false;
}

function create_popup_block_by( sName ){
	var eButton = document.getElementById( sName + '_button' );
	var eBlock = document.getElementById( sName + '_block' );
		setTimeout(
			function(){
				create_popup_block( eButton, eBlock );
			}
			, 0
		);
}
function create_popup_block(eButton, eBlock){
	if( eButton && eBlock ){
		eBlock.style.display = 'none';
		var eINS = make_pseudo( eButton );
		var eParent = document.getElementsByTagName( 'form' )[0];
		var eClose = document.createElement( 'ins' );
		eBlock.appendChild( eClose );
		Common.Class.add( eClose, 'close_button' );
		if( need_fix_hover() ){
			fix_hover( eClose, 'close_button' );
		}
		Common.Event.add( eBlock, 'click', function( e ){ bPopup_click = true; } );
		try{
			var eOK = document.createElement( '<input type="button" value="OK" class="ok_button" />' );
		}catch(e){
			var eOK = document.createElement( 'input' );
			eOK.type = 'button';
			eOK.value = 'OK';
			Common.Class.add( eOK, 'ok_button' );
		}
		eBlock.appendChild( eOK );
		
		if(typeof eParent != 'undefined' ){
			eParent.appendChild( eBlock );
		}
		
		Common.Class.remove( eBlock, 'hidden' );
		
		create_popup_button (eButton, eBlock);
		
		Common.Event.add( [ eClose, eOK ], 'click', function( e ){
			bPopup_click = false;
			close_popup ();
			
			Common.Event.cancel( e );
		} );
		
		Common.Class.add( eBlock, 'popup_window' );
		var eDIV = document.createElement( 'div' );
		eBlock.appendChild( eDIV );
		eDIV.innerHTML = '<div class="f_tt"></div><div class="f_r"><div class="f_rr"></div><div class="f_b"><div class="f_bb"><div></div></div><div class="f_l"><div class="f_ll"><div></div></div><div class="f_c"><div class="popup_arrow"></div></div></div></div></div>';
		Common.Class.add( eDIV, 'shadow' );
	}
}



function create_popup_button (eButton, eBlock) {
		var eParent = document.getElementsByTagName( 'form' )[0];

		Common.Event.add( eButton, 'click', function( e ){
			var aeSelect = document.getElementsByTagName( 'select' );
			
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

			
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  
			if( bPopup_close ){
				if( ePopup_block ){
					close_popup ();
				}
				var hParent_coords = Common.Dom.getAbsoluteCoords( eParent );
				var hButton_coords = Common.Dom.getAbsoluteCoords( eButton );

				eBlock.style.left = hButton_coords.iLeft - hParent_coords.iLeft + 'px';
				eBlock.style.top = scrOfY + myHeight/2 - 220 + 'px';
				eBlock.style.display = 'block';
				ePopup_block = eBlock;
				ePopup_button = eButton;
				Common.Class.add( eButton, 'active' );
				if( aeSelect && eButton.runtimeStyle ){
					for( var i = 0 ; i < aeSelect.length ; i++ ){
						Common.Class.add( aeSelect[i], 'hidden' );
					}
				}
				bPopup_close = false;
			}else{
				bPopup_click = false;
				close_popup ();

			}
			Common.Event.cancel( e );
		} );
}

