function readValue( aName )
{
	try
	{
		if ( !requestFrame ) requestFrame = window.frames[ 'requestImage' ];
		var requestDoc = requestFrame.document;
		if ( requestDoc.getElementById( aName ) ) 
		{
			return requestDoc.getElementById( aName ).title;
		}
	}
	catch ( er )
	{
		;
	}
}

function clearEvent( e )
{
	if ( !e ) e = window.event;
	e.cancelBubble = true; 
	if ( e.preventDefault ) e.preventDefault();
	else if ( e.stopImmediatePropagation ) e.stopImmediatePropagation();
	else if ( e.stopPropagation ) e.stopPropagation();
}

function getObjOffset( obj )
{
	var curleft = 0, curtop = 0;
	var safariWorkaround = 0;
	if ( obj.offsetParent )
	{
		while ( obj )
		{
			if ( safariWorkaround ) curtop += parseInt( obj.offsetTop );
			curleft += parseInt( obj.offsetLeft );
			obj = obj.offsetParent;
			safariWorkaround = 1;
		}
	}
	else 
	{
		if ( obj.x ) curleft += obj.x;
		if ( obj.y ) curtop += obj.y;
	}

	return [ curleft, curtop ];
}

function refreshGeoParams()
{
	try
	{
		if ( !requestFrame ) requestFrame = window.frames[ 'requestImage' ];
		var requestDoc = requestFrame.document;
		if ( requestDoc.getElementById( 'scale' ) ) 
		{
			prevSc = sc;
			sc = parseInt( readValue( 'scale' ) );
			setSlider( sc );
		}
	}
	catch ( er )
	{
		;
	}
}

function getHTMLText( anID )
{
	if ( document.getElementById( anID ) )
		return document.getElementById( anID ).innerHTML;
	else
		return '';
}

function setHTMLText( anID, aText )
{
	if ( !aText ) return;

	try {
		if ( document.getElementById( anID ) ) document.getElementById( anID ).innerHTML = aText;
	}
	catch ( er )
	{
		;
	}
}

function resetChNames( aMapName )
{
	try {
		var actNum = 2;

		if ( aMapName == 'Москва' ) actNum = 0;
		if ( aMapName == 'Санкт-Петербург' ) actNum = 1;

		for ( var i = 0; i <= 2; i++ )
		{
			if ( i == actNum )
			{
				document.getElementById( 'img' + i ).style.display = 'block';
				document.getElementById( 'name' + i + '1' ).style.display = 'none';
				document.getElementById( 'name' + i + '0' ).style.display = 'block';
			}
			else
			{
				document.getElementById( 'img' + i ).style.display = 'none';
				document.getElementById( 'name' + i + '0' ).style.display = 'none';
				document.getElementById( 'name' + i + '1' ).style.display = 'block';
			}
		}
	}
	catch ( er )
	{
		;
	}
}

function updateChInfo()
{
	if ( !requestFrame ) requestFrame = window.frames[ 'requestImage' ];
	var requestDoc = requestFrame.document;

	if ( requestDoc.getElementById( 'chDescrStr' ) ) 
	{
		var chDescrStr = readValue( 'chDescrStr' );
		setHTMLText( 'chDescrStr', chDescrStr );
	}

	if ( document.getElementById( 'name00' ) ) resetChNames( gMapName );

}

function getForegroundMapImg()
{
	var map1_Z = window.document.getElementById( 'map1' ).style.zIndex;
	var map2_Z = window.document.getElementById( 'map2' ).style.zIndex;

	return ( map1_Z > map2_Z ) ? window.document.getElementById( 'map1' ) : window.document.getElementById( 'map2' );
}

function getBackgroundMapImg()
{
	var map1_Z = window.document.getElementById( 'map1' ).style.zIndex;
	var map2_Z = window.document.getElementById( 'map2' ).style.zIndex;

	return ( map1_Z > map2_Z ) ? window.document.getElementById( 'map2' ) : window.document.getElementById( 'map1' );
}

var saveChDesc;
function lockRefresh()
{
	readyToProcess = false;
	document.body.style.cursor = "wait";
	saveChDesc = getHTMLText( 'chDescrStr' );
	setHTMLText( 'chDescrStr', 'Обработка запроса. Пожалуйста подождите...' );
}

function unlockRefresh()
{
	document.body.style.cursor = "default";
	readyToProcess = true;
	setHTMLText( 'chDescrStr', saveChDesc );
}

var tmpImg = new Image( mapWidth, mapHeight );
tmpImg.onload = setMapFromBuffer;
var bNeedRefresh = false;

function setMapFromBuffer()
{
	// extraSize
	if ( !bNeedRefresh ) return;

	initOffsets();

	var fgMapImg =  getForegroundMapImg();
	var bgMapImg =  getBackgroundMapImg();

	bgMapImg.src = tmpImg.src;

	bgMapImg.className = 'dragMap';
	bgMapImg.style.left = parseInt( mapOrigLeft + 0 ) + 'px';
	bgMapImg.style.top = parseInt( mapOrigTop + 0 ) + 'px';
	bgMapImg.style.clip = 'rect(' + extraSize + 'px,' + parseInt( mapWidth + extraSize + 0 ) + 'px,' + parseInt( mapHeight + extraSize + 0 ) + 'px,' + extraSize + 'px)';

	bgMapImg.style.zIndex = parseInt( fgMapImg.style.zIndex ) + 1;

	fgMapImg.src = 'Images/blank.gif';
	fgMapImg.className = 'hide';

	theMap = bgMapImg;

	theMap.visibility = true;

	unlockRefresh();
	bNeedRefresh = false;
	updateChInfo();
}

function updateMap()
{
	try
	{
		if ( !requestFrame ) requestFrame = window.frames[ 'requestImage' ];
		var requestDoc = requestFrame.document;
		if ( requestDoc )
		{
			if ( requestDoc.getElementById( 'chartName' ) )
			{
				var tmpVal = readValue( 'chartName' );
				if ( tmpVal && tmpImg.src.indexOf( tmpVal ) == -1 ) 
				{
					tmpImg.src = tmpVal;
					bNeedRefresh = true;
					setTimeout( 'setMapFromBuffer()', 1000 );
				}
			}					

//--- block for search start ---
			updateSearchInfo();
//--- block for search finish---

//--- block for bookmarks start ---
			updateBookmarkInfo();
			gBookmark = false;
			releaseBookmarkBtn();
//--- block for bookmarks finish---

		}
	}
	catch ( er )
	{
		;
	}

	if ( !bNeedRefresh )
	{
		unlockRefresh();
	}
	refreshGeoParams();
	timeoutNum = 0;

//--- block for tracks start ---
	gStartT = false;
	gFinishT = false;
	clearSF();
//--- block for tracks finish---
}

function requestMap( action, aScale, offsetX, offsetY, scaleX, scaleY, cmt )
// offsetX, offsetY - new center in raster pixels
// scaleX, scaleY - relative point for scaling in raster pixels
{
	var tmpStr = "createImage.asp?a=" + action;
	tmpStr += "&sc=" + aScale;
	tmpStr += "&offX=" + offsetX;
	tmpStr += "&offY=" + offsetY;
	tmpStr += "&scX=" + scaleX;
	tmpStr += "&scY=" + scaleY;
	tmpStr += "&cmt=" + cmt;

	try {
		requestFrame.document.location.href = tmpStr;
		lockRefresh()
	}
	catch ( er ) {
		readyToProcess = true;
		document.body.style.cursor = "default";
	}
}
