
var FIT_WIDTH = "FitWidth", BEST_FIT = "BestFit", ZOOM_100 = "Zoom100", ZOOM_EXACT = "ZoomExact";
var isInitialized = false;

//Controls
var f = null, fEv;
var edScale;
var flashTD;
//End controls

var lastClickedPart = null;
var lastClickedTime = null;

var scaleMode = FIT_WIDTH;
var currScale = null; //0 - 1
//var scaleCorrect; //
var shX = 0, shY = 0;
var originalWidth, originalHeight;


var isLeftBtnDown = false;
var privMouseX = null, privMouseY = null;
var currFlashCursor = "auto";

var reChkZoom = new RegExp( "^\\s*\\d+[\\.,]{0,1}\\d*[%]{0,1}\\s*$", "" );

var btnLT, btnUp, btnRT, btnL, btnR, btnLB, btnD, btnRB;

var lastShiftIsPressed = false, lastCtrlIsPressed = false;

var f2HTMLProtocol = 0;
var timerId = null;

var timerIdTooltip = null;

var lastOverPart = null, currOverTime = null, currOverPart = null;
function onOverPart( pn )
{
	var dt = new Date();
	if( pn == "null" ) pn = null;
	currOverPart = pn, currOverTime = dt.getTime();
	checkTooltip();
	//window.status = pn == null ? "null":pn;
}
function checkTooltip()
{
	if( lastOverPart != null && (currOverPart == null || currOverPart.length == 0) )
	{
		lastOverPart = currOverPart;
		removeTooltip();
	}
	else if( currOverPart != null )
	{
		var dt = new Date();
		if( currOverTime == null )
			currOverTime = dt.getTime();
		else if( currOverPart != lastOverPart && dt.getTime() - currOverTime > 700 )
		{
			lastOverPart = currOverPart;
			showTooltip();
		}
		else if( f.GetVariable("isMouseClicked") == "true" )
		{
			f.SetVariable( "isMouseClicked", false );
			removeTooltip();
		}
	}	
}
var isTooltipVisible = false;
function removeTooltip()
{
	if( isTooltipVisible )
	{
		var d = document.getElementById( awsDrVIDMap.idTooltip );
		d.style.visibility = "hidden";
		d.style.display = "none";
		//edScale.value = "*";
		currOverPart = null; currOverTime = null;
		isTooltipVisible = false;
	}
}
function showTooltip()
{
	if( !isTooltipVisible )
	{
		//edScale.value = lastOverPart;

		var x = Math.round( toHTML2X(parseFloat(f.GetVariable("lastMX"))) );
		var y = Math.round( toHTML2Y(parseFloat(f.GetVariable("lastMY"))) );

//alert( f.GetVariable("MovieClip._width") );

		var d = document.getElementById( awsDrVIDMap.idTooltip );
		d.innerHTML = getDescription( lastOverPart );

		var k1, k2;
		d.style.left = k1 = (getClientLeft(f) + x + 15).toString() + "px";
		d.style.top = k2 = (getClientTop(f) + y + 15).toString() + "px";
		
		//alert( k1 + "; " + k2 );
		//d.style.left = 600;
		//d.style.top = 220;

		d.style.visibility = "visible";
		d.style.display = "block";

		currOverTime = null;
		isTooltipVisible = true;
	}
}
function getDescription( pn ) 
{
	var partNumber = pn.toLowerCase();
	var hts = findHotspotDescriptors( pn );
	
	//if no parts matched to "pn" - doing finding parts using narrowed partnumber
	if( awsDrVIDMap.keyIsPartNumber == "true" && hts.length == 0 )
	{
		var idx = pn.lastIndexOf( "-" );
		if( idx > 0 )
		{
			pn = pn.substring( 0, idx );
			hts = findHotspotDescriptors( pn );
		}
	}

	if( hts.length == 0 )
	{
		if( awsDrVIDMap.partNotFoundTooltip.length == 0 )
			return "Part# " + pn;
		else
			return replaceVals( awsDrVIDMap.partNotFoundTooltip, [/\{0\}/gi, pn] );
	}
	else
	{
		var res = "";
		for( var i = 0; i < hts.length; ++i )
		{
			if( res.length != 0 )
				res += "<br/>";
			res += hts[i].partNumber + ": " + hts[i].description;
		}
		return res;
	}
	/*var hotspot = awsHotspots[ partNumber.toLowerCase() ];
	if( typeof(hotspot) == "undefined" )
		return "Part: " + pn;
	else
		return hotspot.description;*/
}
function findHotspotDescriptors( pn )
{
//alert("test");
	var res = new Array();
	var key = pn.toLowerCase();
	for( var i = 0; i < awsHotspots.length; ++i )
	{
		var ht = awsHotspots[ i ];
		var targetKey = (awsDrVIDMap.keyIsPartNumber == "true" ? ht.partNumber:ht.id);
		if( key == targetKey.toLowerCase() )
			res[ res.length ] = ht;
	}
	
	return res;
}
function onPartClicked( pn_ )
{
	removeTooltip();
	
	/*var idxSemicolon = pn_.indexOf( ";" );
	var isCtrlPressed = (pn_.substr( 0, idxSemicolon ).toLowerCase() == "true");
	var pn = pn_.length > idxSemicolon + 1 ? pn_.substr(idxSemicolon + 1):"";*/
	var pn = pn_;

	if( /*lastShiftIsPressed*/getLastShiftIsPressed() == true )
		return;

	if( /*lastCtrlIsPressed*/ getLastCtrlIsPressed() == true )
	{
		/*var rowEmpty = document.getElementById( "idPartsEmpty" );
		if( rowEmpty != null )
			rowEmpty.parentNode.removeChild( rowEmpty );
		addToShoppingCart( pn );*/
		//alert( 'Shopping cart is removed.' );
		//executePostback( awsDrVIDMap.serverControlUniqueID, "addToShoppingCart" );
		execute_AddShoppingCart( pn );
	}
	else
	{
		//navigateToPartInfo( pn );
		//executePostback( awsDrVIDMap.serverControlUniqueID, "navigateToPartInfo" );
		execute_NavigateToPartInfo( pn );
	}

	/*if( lastClickedPart == pn )
	{
		var dtCurr = (new Date()).getTime();
		if( dtCurr - lastClickedTime < 500 )
		{
			lastClickedPart = null;
			lastClickedTime = null;
			//alert( "Part " + pn + " is selected." );
			
			if( isCtrlPressed )
			{
				var rowEmpty = document.getElementById( "idPartsEmpty" );
				if( rowEmpty != null )
					rowEmpty.parentNode.removeChild( rowEmpty );
				addToShoppingCart( pn );
			}
			else
			{
				if( window.navigate )
					window.navigate( "PartInformation.aspx?pn=" + escape(pn) );
				else
					location = "http://www.aha.ru";
			}
		}
		else
			lastClickedTime = dtCurr;
	}
	else
		lastClickedPart = pn, lastClickedTime = (new Date()).getTime();*/
}

function navigateToPartInfo( pn )
{	
	navigateTo( "PartInformation.aspx?pn=" + escape(pn ? pn:this.getAttribute("pid",0)) );
}

function clc()
{
	if( document.all && event.button == 2 )
	{			
		event.returnValue = false;
		event.cancelBubble = true;
		//event.srcElement.parentNode.focus();
		//idFr.focus();
		//event.srcElement.focus();
		//alert();
		return false;
	}
	if( document.layers && e.which == 3 )
	{
		//alert(rightClickMsg);
		//alert(  );
		return false;
	}
	return true;
}

function setFRef()
{
	if( f == null )
	{
		fEv = f = (isIEBrowser && !isOperaBrowser) ? document.getElementById(awsDrVIDMap.idFla):document.getElementById(awsDrVIDMap.idFlaEmb);
		if( isOperaBrowser )
			fEv = document.getElementById( awsDrVIDMap.idFla );
	}
}
function initVars()
{
	/*f = document.getElementById( "idFla" );	
	if( f == null )
		f = document.getElementById( "idFlaEmb" );
	if( f == null )
	{
		alert( "Can not find Falsh" );
		return false;
	}*/

	/*fEv = f = document.getElementById( (isIEBrowser && !isOperaBrowser) ? "idFla":"idFlaEmb" );
	if( isOperaBrowser )
		fEv = document.getElementById( "idFla" );*/
	setFRef();


	//alert( document.getElementById("idFla").toString() );
	//alert( document.getElementById("idFlaEmb") );
	
	loadZoom();
	loadShiftX();
	loadShiftY();
	loadZoomMode();

	edScale = document.getElementById( awsDrVIDMap.idScale );
	flashTD = document.getElementById( awsDrVIDMap.idFlashTD );

	return true;
}
var onUnloadOld;
function initEvents()
{
	//document.getElementById( "idBody" ).onunload = onUnload;
	onUnloadOld = document.body.onunload;
	document.body.onunload = onUnload;	

	var bp = f.GetVariable( "sImgURL" );

	btnLT = new ButtonDescr( awsDrVIDMap.idBtnLeftTop, bp + "LeftTop_btn.gif", bp + "LeftTop_btn_d.gif" ),
	btnUp = new ButtonDescr( awsDrVIDMap.idBtnUp, bp + "Up_btn.gif", bp + "Up_btn_d.gif" ),
	btnRT = new ButtonDescr( awsDrVIDMap.idBtnRightTop, bp + "RightTop_btn.gif", bp + "RightTop_btn_d.gif" ),
	btnL = new ButtonDescr( awsDrVIDMap.idBtnLeft, bp + "Left_btn.gif", bp + "Left_btn_d.gif" ),
	btnR = new ButtonDescr( awsDrVIDMap.idBtnRight, bp + "Right_btn.gif", bp + "Right_btn_d.gif" ),
	btnLB = new ButtonDescr( awsDrVIDMap.idBtnLeftBottom, bp + "LeftBottom_btn.gif", bp + "LeftBottom_btn_d.gif" ),
	btnD = new ButtonDescr( awsDrVIDMap.idBtnDown, bp + "Down_btn.gif", bp + "Down_btn_d.gif" ),
	btnRB = new ButtonDescr( awsDrVIDMap.idBtnRightBottom, bp + "RightBottom_btn.gif", bp + "RightBottom_btn_d.gif" );

	edScale.onkeypress = onInputScale;
	if( edScale.captureEvents ) edScale.captureEvents( Event.KEYPRESS );
	if( document.captureEvents )
	{
		//window.captureEvents( Event.KEYDOWN );
		document.captureEvents( Event.KEYDOWN );
		document.captureEvents( Event.KEYUP );

		document.captureEvents( Event.MOUSEDOWN );
		document.captureEvents( Event.MOUSEUP );
		document.captureEvents( Event.MOUSEMOVE );
		document.captureEvents( Event.MOUSEOUT );
	}


	var btn = document.getElementById( awsDrVIDMap.idBtnUp );
	btn.onclick = onUp;
	
	btn = document.getElementById( awsDrVIDMap.idBtnPrint );
	btn.onclick = onPrintFlash;
	
	btn = document.getElementById( awsDrVIDMap.idBtnZoomIn );
	btn.onclick = onZoomIn;
	
	btn = document.getElementById( awsDrVIDMap.idBtnZoomOut );
	btn.onclick = onZoomOut;
	
	btn = document.getElementById( awsDrVIDMap.idBtnFitWidth );
	btn.onclick = onFitWidth;
	
	btn = document.getElementById( awsDrVIDMap.idBtnActSize );
	btn.onclick = onActSize;
		
	
	btn = document.getElementById( awsDrVIDMap.idBtnFit );
	btn.onclick = onBestFit;
	
	btn = document.getElementById( awsDrVIDMap.idBtnLeft );
	btn.onclick = onLeft;
	
	btn = document.getElementById( awsDrVIDMap.idBtnRight );
	btn.onclick = onRight;

	btn = document.getElementById( awsDrVIDMap.idBtnDown );
	btn.onclick = onDown;

	btn = document.getElementById( awsDrVIDMap.idBtnLeftTop );
	if( btn )
		btn.onclick = onLeftUp;

	btn = document.getElementById( awsDrVIDMap.idBtnRightTop );
	if( btn )
		btn.onclick = onRightUp;

	btn = document.getElementById( awsDrVIDMap.idBtnLeftBottom );
	if( btn )
		btn.onclick = onLeftDown;

	btn = document.getElementById( awsDrVIDMap.idBtnRightBottom );
	if( btn )
		btn.onclick = onRightDown;


	//btn = document.getElementById( "idBody" );
	btn = document.body;
	if( btn )
	{
		btn.oncontextmenu = cancelContextMenu;
		if( isIEBrowser )
			btn.onselectstart = cancelSelect;
	}
/*btn.onselect = onSel;
fEv.onselect = onSel;
window.onselect = onSel;*/


	document.onkeydown = onKeyDownPage;
	document.onkeyup = onKeyUpPage;
//window.onkeydown = onKeyDownPage;	
//fEv.parentNode.onkeydown = onKeyDownPage;

	fEv.onmousedown = onMouseDownFlash;
	//document.getElementById("idBody").onmouseup = onMouseUpFlash;
	document.body.onmouseup = onMouseUpFlash;
	fEv.onmousemove = onMouseMoveFlash;
	fEv.onmouseout = onMouseOutFlash;

	//f.SetVariable( "tabEnabled", false );
	//f.SetVariable( "focusEnabled", false );

	//f.OnReadyStateChange = rds;
	//f.addEventListener( 'OnProgress', rds, false );

	if( isIEBrowser )
		fEv.onfocusout = onLostFocus; //MozUserFocus permits this


	document.onmouseup = onMouseGlobal;
	document.onmousedown = onMouseGlobal;
//reportBrowser();
	//document.getElementById("idBody").style.MozUserSelect = "enabled";
	//document.getElementById("idBody").style.MozUserFocus = "normal";

	//f.style.MozUserSelect = "disabled";
	//f.parentNode.style.MozUserSelect = "disabled";
	//f.style.MozUserFocus = "ignore";

	//document.onfocusin = onfocus;

	f.SetVariable( "pumpingMode", f2HTMLProtocol = 0 );
	f.SetVariable( "testRequest", 1 );
	window.setTimeout( checkF2HTMLProtocol, 1000 );

	timerIdTooltip = window.setInterval( checkTooltip, 100 );
	
	//document.getElementById( "idBody" ).nvt = navigateToPartInfo;
}
function processTest( arg )
{
	f.SetVariable( "testRequest", 2 );
}
function checkF2HTMLProtocol()
{
	if( f.GetVariable("testRequest") != 2 && f2HTMLProtocol < 2 )
	{
		f.SetVariable( "pumpingMode", ++f2HTMLProtocol );
		f.SetVariable( "testRequest", 1 );
		window.setTimeout( checkF2HTMLProtocol, 1000 );
	}
	else if( f2HTMLProtocol == 2 )
		timerId = window.setInterval( pumpCommand, 100 )/*, edScale.value = f2HTMLProtocol.toString() + " protocol is selected"*/;
	//else
		//edScale.value = f2HTMLProtocol.toString() + " protocol is selected";
}
function onUnload()
{
//alert( "onUnload" );
//window.open( "http://www.microsoft.com" );
	if( timerId != null )
		window.clearInterval( timerId ), timerId = null;
	if( timerIdTooltip != null )
		window.clearInterval( timerIdTooltip ), timerIdTooltip = null;
		
	if( onUnloadOld ) onUnloadOld();
}
function pumpCommand()
{
	if( f.GetVariable("isCmdSet") == 1 )
	{
		var c = f.GetVariable("lastCmd"), p = f.GetVariable("lastPrm");
		f.SetVariable( "isCmdSet", 0 );
//alert( "cmd=" + c + "; p=" + p );
		ptr_DoFSCommand( c, p );
	}
}
/*function onfocus( e )
{
	var ev = new InterEvent( e );	
	if( ev.srcEl == f )
	{
		f.blur();
		return ev.cancelEvent();
	}
}*/

function getLastShiftIsPressed()
{
	return f.GetVariable("shiftPress") == "true" || lastShiftIsPressed == true;
}
function getLastCtrlIsPressed()
{
	return f.GetVariable( "ctrlPress" ) == "true" || lastCtrlIsPressed == true;
}

function onMouseGlobal( e )
{
	var ev = new InterEvent( e );
	lastShiftIsPressed = ev.shiftKey;
	lastCtrlIsPressed = ev.ctrlKey;

//alert( "Shift: " + lastShiftIsPressed.toString() + "; Ctrl: " + lastCtrlIsPressed );
//edScale.value = lastCtrlIsPressed;

	if( isIEBrowser && !isOperaBrowser )
		return true;

	var n = ev.srcEl.tagName.toLowerCase();
	if( n != "input" && n != "textarea" )
	{
		if( ev.srcEl != f || /*lastCtrlIsPressed*/getLastCtrlIsPressed() == true )
			return ev.cancelEvent();
		else
			return true;
	}
	else
		return true;
}

function onSel( e )
{
	alert( "Sel" );
}

function onLostFocus( e )
{
	//alert( document.hasFocus() );
	//edScale.value = document.hasFocus();
	f.SetVariable( "cancelZoomRequest", 1 );
}

function processFlashKeyDown( args )
{
	removeTooltip();
//edScale.value = args;
//alert( "From Flash: " + args );
	f.SetVariable( "cancelZoomRequest", 1 );
	if( args == "107" )
		onZoomIn();
	else if( args == "109" || args == "189"/*in NN*/ )
		onZoomOut();		
}
function processFlashZoomWindow( args )
{
	var bnd = args.split( ";" );
	var fx1, fy1, fx2, fy2;
	var x1 = toHTML2X( fx1=parseFloat(bnd[0]) );
	var y1 = toHTML2Y( fy1=parseFloat(bnd[1]) );
	var x2 = toHTML2X( fx2=parseFloat(bnd[2]) );
	var y2 = toHTML2Y( fy2=parseFloat(bnd[3]) );

	var m1 = clientWidth() / Math.abs( x1 - x2 );
	var m2 = clientHeight() / Math.abs( y1 - y2 );
	m1 = Math.min( m1, m2 );
		
	//var cx2 = getCenX(), cy2 = getCenY();

	var privScale = currScale;
	currScale *= m1;
	currScale = correctZoom( currScale ); updZoom(); scaleMode = ZOOM_EXACT; updZoomMode();
	//putZoom( currScale );
	updateZoom( currScale );

	var cx1 = (fx1 + fx2) / 2.0;
	var cy1 = (fy1 + fy2) / 2.0;

	compensateShift( cx1, cy1, getCenX(), getCenY() );
	correctShifts( currScale / privScale );
	//putShift( shX, shY );
	putTransform( shX, shY, currScale );
}

function putTransform( shiftX, shiftY, sc )
{
	f.SetVariable( "shX", shiftX == null ? null:shiftX.toString() );
	f.SetVariable( "shY", shiftY == null ? null:shiftY.toString() );

	if( sc != null )
	{
		var zoom = (sc * 100.0).toString();	

		f.SetVariable( "xMinTh", Math.ceil(5.0 / sc) );
		f.SetVariable( "yMinTh", Math.ceil(5.0 / sc) );

		f.SetVariable( "zoomX", zoom ), f.SetVariable( "zoomY", zoom );		
	}
	else
		f.SetVariable( "zoomX", 0 );

	f.SetVariable( "transformRequest", 1 );
}

function chkCursorHand( btnPressed )
{
	var res = (isLeftBtnDown && btnPressed);

	var csrShouldBe = res ? "hand":"auto";
	if( currFlashCursor != csrShouldBe )
	{
		currFlashCursor = csrShouldBe;
		f.SetVariable( "cursorRequest", currFlashCursor );
		//f.style.cursor = csrShouldBe;

		//if( res )
			f.SetVariable( "cancelZoomRequest", 1 );
	}
	return res;
}

function onMouseOutFlash( e )
{
	//isLeftBtnDown = false;
	//chkCursorHand();
	return true;
}
function onMouseDownFlash( e )
{
	var ev = new InterEvent( e );
	isLeftBtnDown = (ev.button == 1);
	chkCursorHand( ev.shiftKey );
	privMouseX = ev.cliX;
	privMouseY = ev.cliY;
	return true;
}
function onMouseUpFlash( e )
{
	var ev = new InterEvent( e );
	if( ev.button == 1 )
	{
		isLeftBtnDown = false;
		f.SetVariable( "cancelZoomRequest", 1 );
	}
	chkCursorHand( ev.shiftKey );
	return true;
}
function onMouseMoveFlash( e )
{

	var ev = new InterEvent( e );
//edScale.value = isLeftBtnDown;
	//if( ev.button && ev.button != 1 && ev.button != 3 )
		//isLeftBtnDown = false;

	var isDragging = chkCursorHand( ev.shiftKey );

	if( privMouseX != null )
	{
		if( isDragging )
		{
			var dx = ev.cliX - privMouseX;
			var dy = ev.cliY - privMouseY;
			shX += dx; shY += dy;
			correctShifts();
			putShift( shX, shY );
			scaleMode = ZOOM_EXACT; updZoomMode();
			//putTransform( shX, shY, null );
		}
	}
	privMouseX = ev.cliX;
	privMouseY = ev.cliY;
	return true;
}
function onKeyUpPage( e )
{
	var ev = new InterEvent( e );	
	chkCursorHand( ev.shiftKey );
	return true;
	//alert( ev.code );
}
function onKeyDownPage( e )
{	
	removeTooltip();

	var handled = true;
	var ev = new InterEvent( e );
	var n = ev.srcEl.tagName.toLowerCase();
		

	chkCursorHand( ev.shiftKey );
	f.SetVariable( "cancelZoomRequest", 1 );
//alert( ev.code ); //109 -; + 107

	if( n != "input" && n != "textarea"  )
	{
		if( ev.code == 107 || (isOperaBrowser && ev.code == 43) )
			onZoomIn();
		else if( ev.code == 109 || (isOperaBrowser && ev.code == 45) )
			onZoomOut();
		else
			handled = false;	
	}
	else
		handled = false;
	
	if( handled )
		return ev.cancelEvent();
	else
		return true;
}

function onClickFlash( e )
{
  //alert( 'jj' );
	var ev = new InterEvent( e );
	if( ev.button != 1 )
		return true;

	//alert( ev.offsetX + ", " + ev.offsetY + ": " + ev.layerX + ", " + ev.layerY );
	/*alert( "clientX = " + ev.clientX + " clientY = " + ev.clientY + "\r\n" +
		"offsetX = " + ev.offsetX + " offsetY = " + ev.offsetY + "\r\n" +
		"x = " + ev.x + " y = " + ev.y + "\r\n" );*/
	
	//1 - left btn	
	//alert( ev.cliX.toString() + "; " + ev.cliY.toString() + "; " + ev.button );

	f.SetVariable( "ctlPointX",  Math.round(getCenX()) );
	f.SetVariable( "ctlPointY",  Math.round(getCenY()) );
	//alert( shX + "; " + shY );
	f.SetVariable( "ctlSize", 1 );

	return ev.cancelEvent();
}

function init()
{
//alert( awsDrVIDMap.idBtnZoomIn.tagName );
	if( isInitialized ) return;
	if( !initVars() )
		return;

	var tstVar = null;
	var tstVar2 = null;
	var isNetError = null;
	try
	{
//f = document.getElementById( "idDrw_idFlaEmb" );
		//tstVar = f.GetVariable( "drw.srcWidth" );
		
		isError = f.GetVariable( "isNetworkError" );
		tstVar = f.GetVariable( "isLoaded" );
		tstVar2 = f.GetVariable( "srcWidth" );
		//tstVar = f.GetVariable( "isLoaded" );
	}
	catch( e )
	{
	}
//alert( tstVar );	
	if( isNetError == "true" )
	{
		isInitialized = true;
		return;
	}
	else if( !tstVar || tstVar != "true" || !tstVar2 )
	{
//alert( f.TGetPropertyAsNumber( "/drw", 9 ) );	
		setTimeout( init, 1000, "javascript" );
		return;
	}

	isInitialized = true;
	initEvents();	

	//drawing should be <PARAM NAME="Scale" VALUE="noscale"> and <PARAM NAME="SAlign" VALUE="LT">	
	var w = f.TGetPropertyAsNumber("/", 8),
		h = f.TGetPropertyAsNumber("/", 9);
	originalWidth = w - perc( w, 1.0 );
	originalHeight = h - perc( h, 1.0 );
	
	f.SetVariable( "_visible", false );
	//f.ScaleMode = 3;
	//f.AlignMode = 8 + 2; //LT
	//f.setAttribute( "salign", "BR" );
	//f.setAttribute( "scale", "noscale" );
	

	//alert( w + "; " + h );

	//if( !isIEBrowser ) f.captureEvents( Event.KeyPress );	
	//if( !isIEBrowser ) window.captureEvents( Event.KEYPRESS );	
	//alert( Event.KEYPRESS );

	//f.SetZoomRect( 110, 11110, drawingWidth(), drawingHeight()/2 );
//alert( f.PercentLoaded().toString() );	
	//scaleCorrect = safeGet(f.TGetPropertyAsNumber( "/", 8 )) / safeGet(f.GetVariable( "srcWidth" ));
//alert( scaleCorrect.toString() + "; " + f.PercentLoaded().toString() );	

	//bestFit();
	//updateZoom( currScale );
	
	if( scaleMode == FIT_WIDTH )
	{
		var shKeep = shY;
		onFitWidth();
		putTransform( shX, shY = shKeep, currScale );
		updShiftY();		
	}
	else if( scaleMode == BEST_FIT )
	{
		bestFit();
		updateZoom( currScale ); 
	}
	else if( scaleMode == ZOOM_100 )
		onActSize();
	else// if( scaleMode == ZOOM_EXACT )
	{
		//edScale.value = (currScale * 100.0).toString() + "%";
		updateZoom( currScale ); 
		//onExactScale();
		putTransform( shX, shY, currScale );
	}

	//hotspots separated by ,,
	var selectedHts = getHiddenStr( awsDrVIDMap.hiddenSelectedHotspots );
	if( selectedHts.length > 0 )
		selectParts( getHiddenStr(awsDrVIDMap.hiddenSelectedHotspots), true, false );

	f.SetVariable( "visibleReq", 1 );
	//document.getElementById( "idFlaEmb" ).style.visibility = "visible";
	//f.style.visibility = "visible";
}
function bestFit()
{
	var privScale = currScale;

	var m1 = clientWidth() / safeGet(originalWidth);
	var m2 = clientHeight() / safeGet(originalHeight);
	currScale = m1 < m2 ? m1:m2; updZoom(); scaleMode = BEST_FIT; updZoomMode();

	var w = safeGet(originalWidth) * currScale;
	var h = safeGet(originalHeight) * currScale;
	shX = (clientWidth() - w) / 2.0; 
	shY = (clientHeight() - h) / 2.0; 

	//putZoom( currScale );
	//updateZoom( currScale );
	if( privScale == null )
		correctShifts();
	else
		correctShifts( currScale / privScale );
	//putShift( shX, shY );

	updShiftX(), updShiftY();
	putTransform( shX, shY, currScale );
//alert( shX + "; " + shY + "; " + currScale );	
}
function drawingWidth()
{
	//return safeGet(f.TGetPropertyAsNumber( "/", 8 )) * 25.4 / 75.0;	
	//return safeGet( f.GetVariable("drw.srcWidth") );
	return safeGet( f.GetVariable("srcWidth") );
}
function drawingHeight()
{
	//return safeGet(f.TGetPropertyAsNumber( "/", 9 )) * 25.4 / 75.0;	
	//return safeGet( f.GetVariable("drw.srcHeight") );
	return safeGet( f.GetVariable("srcHeight") );
}
function clientWidth()
{//alert( f.clientWidth + "; " + f.clientHeight);
	//in NN6 clientWidth is not defined
	return safeGet(f.clientWidth ? f.clientWidth:f.offsetWidth);
	//return 607;
}
function getFlashSize()
{
//alert( "PP" );
	setFRef();
	f.SetVariable( "flaWidth", clientWidth() );
	f.SetVariable( "flaHeight", clientHeight() );	
	//alert( clientHeight() );
	
	//document.all['idFla'].SetVariable( "flaWidth", 600 );
	//document.all['idFla'].SetVariable( "flaHeight", 400 );
}
function clientHeight()
{
	return safeGet(f.clientHeight ? f.clientHeight:f.offsetHeight);
	//return 450;
}
function safeGet( v )
{
	return v == 0 ? 1:v;
}
function getZoom()
{
	return f.TGetPropertyAsNumber( "/", 2 );
}
function putShift( shx, shy )
{
	//f.TSetProperty( "/", 0, Math.round(shx) );
	//f.TSetProperty( "/", 1, Math.round(shy) );
	f.TSetProperty( "/", 0, shx.toString() );
	f.TSetProperty( "/", 1, shy.toString() );
}
function putZoom( newZoom )
{
	//var zoom = Math.round( newZoom * 100.0 );	
	var zoom = (newZoom * 100.0).toString();
	//alert( zoom + "; " + zoom.toLocaleString() );
	//alert( zoom + " " + typeof(zoom) );
	f.TSetProperty( "/", 2, zoom );
	f.TSetProperty( "/", 3, zoom );
	
	f.SetVariable( "xMinTh", Math.ceil(5.0 / newZoom) );
	f.SetVariable( "yMinTh", Math.ceil(5.0 / newZoom) );
}
function updateZoom( newZoom )
{	
	if( typeof(newZoom) == "undefined" )
	{
		var prnVal = f.TGetPropertyAsNumber("/", 2);
		edScale.value = formatFloat2( prnVal ) + "%";
	}
	else
		edScale.value = formatFloat2( newZoom * 100.0 ) + "%";
}
function onBestFit()
{	
	bestFit();
	//updateZoom();
	updateZoom( currScale );
}
function onActSize()
{
	var privScale = currScale;	
	var cx1 = getCenX(), cy1 = getCenY();
	
	currScale = 1; updZoom(); scaleMode = ZOOM_100; updZoomMode();
	//putZoom( currScale );
	updateZoom( currScale );

	compensateShift( cx1, cy1, getCenX(), getCenY() );
	correctShifts( currScale / privScale );
	//putShift( shX, shY );
	putTransform( shX, shY, currScale );
}
function correctZoom( zoom )
{
	//var actZoom = zoom * scaleCorrect * 100.0;
	var actZoom = zoom * 100.0;
	if( actZoom < 0.5 )
		//return 0.5 / scaleCorrect / 100.0;
		return 0.5 / 100.0;
	else if( actZoom > 500 )
		//return 500.0 / scaleCorrect / 100.0;
		return 500.0 / 100.0;

	return zoom;
}
function onZoomOut()
{
	var privScale = currScale;	
	var cx1 = getCenX(), cy1 = getCenY();
	
	currScale /= 1.25;
	currScale = correctZoom( currScale ); updZoom(); scaleMode = ZOOM_EXACT; updZoomMode();
	//putZoom( currScale );
	updateZoom( currScale );

	compensateShift( cx1, cy1, getCenX(), getCenY() );
	correctShifts( currScale / privScale );
	//putShift( shX, shY );
	putTransform( shX, shY, currScale );
}
function onZoomIn()
{
	var privScale = currScale;
	
	var cx1 = getCenX(), cy1 = getCenY();

	currScale *= 1.25;
	currScale = correctZoom( currScale ); updZoom(); scaleMode = ZOOM_EXACT; updZoomMode();
	//putZoom( currScale );
	updateZoom( currScale );

	compensateShift( cx1, cy1, getCenX(), getCenY() );
	correctShifts( currScale / privScale );
	//putShift( shX, shY );
	putTransform( shX, shY, currScale );
}
function onFitWidth()
{
	var privScale = currScale;
	var cx1 = getCenX(), cy1 = getCenY();

	currScale = clientWidth() / safeGet(originalWidth);	updZoom(); scaleMode = FIT_WIDTH; updZoomMode();
	updateZoom( currScale );

	if( privScale == null )
		shX = shY = 0;
	else
	{
		compensateShift( cx1, cy1, getCenX(), getCenY() );
		correctShifts( currScale / privScale );
	}
	
	putTransform( shX = 0, shY, currScale );	
	updShiftX(), updShiftY();
}
function compensateShift( cx1, cy1, cx2, cy2 )
{
	shX = shX + (cx2 - cx1) * currScale;
	shY = shY + (cy2 - cy1) * currScale;	
	updShiftX(), updShiftY();
}
function pixelsWidthToFlashNotScaled( v )
{
	return v * 1.6;
}
function pixelsHeightToFlashNotScaled( v )
{
	return v * 2.9;
}
function getCenX()
{
	/*var v1 = f.TGetPropertyAsNumber( "/", 8 );	
	var v2 = pixelsWidthToFlashNotScaled( clientWidth() );
	v1 = v2;
	return (v1/2.0 - shX) / currScale;*/
	return toFlash2X( clientWidth()/2.0 );
}
function getCenY()
{	
	/*var v1 = f.TGetPropertyAsNumber( "/", 9 ); //_width	
	var v2 = pixelsHeightToFlashNotScaled( clientHeight() );		
	v1 = v2;
	return (v1/2.0 - shY) / currScale;*/
	return toFlash2Y( clientHeight()/2.0 );
}
function onInputScale( e )
{
	var ev = new InterEvent( e );

	if( ev.code == 13 )
	{		
		onExactScale();
		ev.cancelEvent();
		return false;
	}
	return true;
}
function onExactScale()
{
	var v = parseFloat( edScale.value );
	if( (reChkZoom.test(edScale.value) == false) || (isNaN(v) == true) )
	{		
		alert( "Incorrect zoom factor format: '" + edScale.value + "'. Float value is expected." );
		edScale.focus(); edScale.select();
	}
	else
	{			
		if( v < 0.1 || v > 500 )
		{
			alert( "Incorrect zoom factor value: '" + edScale.value + "'. 0.1% - 500% is expected." );
			edScale.focus(); edScale.select();
		}
		else
		{
			var privScale = currScale;
			var cx1 = getCenX(), cy1 = getCenY();

			//currScale = v / 100.0 / scaleCorrect; 
			currScale = v / 100.0; updZoom(); scaleMode = ZOOM_EXACT; updZoomMode();
			//putZoom( currScale );
			updateZoom( currScale );

			compensateShift( cx1, cy1, getCenX(), getCenY() );
			correctShifts( currScale / privScale );
			//putShift( shX, shY );
			putTransform( shX, shY, currScale );
		}
	}
}
function onPrintFlash()
{
	f.SetVariable( "printRequest", 1 );
	shX = shY = 0;
	currScale = 1.0; updZoom(); scaleMode = ZOOM_100; updZoomMode();
	updateZoom( currScale );
	
	updShiftX(), updShiftY();

	//correctShifts();
	
	//f.SetVariable( "ctlPointX", Math.round(getCenX()) );
	//f.SetVariable( "ctlPointY", Math.round(getCenY()) );	
	//f.SetVariable( "ctlSize", 1 );
	
	//f.SetVariable( "ctlPointX", Math.round(toFlash2( f.TGetPropertyAsNumber("/", 8) )) );
	//f.SetVariable( "ctlPointY", Math.round(toFlash2( f.TGetPropertyAsNumber("/", 9) )) );	
}
function onUp()
{
	//f.Pan( 0, -25, 1 );
	shY += stepY();
	correctShifts();
	putShift( shX, shY );
	scaleMode = ZOOM_EXACT; updZoomMode();

//selectParts( "84591,,82391", true, true );
}

function onDown()
{
	//f.Pan( 0, 25, 1 );
	shY -= stepY();
	correctShifts();
	putShift( shX, shY );
	scaleMode = ZOOM_EXACT; updZoomMode();

//selectParts( "85160", true, true );
}
function onLeft()
{
	//f.Pan( -25, 0, 1 );
	shX += stepU();
	correctShifts();
	putShift( shX, shY );
	scaleMode = ZOOM_EXACT; updZoomMode();
}
function onRight()
{
	//f.Pan( 25, 0, 1 );
	shX -= stepU();
	correctShifts();
	putShift( shX, shY );
	scaleMode = ZOOM_EXACT; updZoomMode();
}
function onLeftUp()
{
	shX += stepU();
	shY += stepU();
	correctShifts();
	putShift( shX, shY );
	scaleMode = ZOOM_EXACT; updZoomMode();
}
function onRightUp()
{
	shY += stepU();
	shX -= stepU();
	correctShifts();
	putShift( shX, shY );
	scaleMode = ZOOM_EXACT; updZoomMode();
}
function onLeftDown()
{
	shX += stepU();
	shY -= stepU();
	correctShifts();
	putShift( shX, shY );
	scaleMode = ZOOM_EXACT; updZoomMode();
}
function onRightDown()
{
	shX -= stepU();
	shY -= stepU();
	correctShifts();
	putShift( shX, shY );
	scaleMode = ZOOM_EXACT; updZoomMode();
}

var paddingWPerc = 5.0, parringHPerc = 5.0;
function correctShifts( corrScale )
{
	var bLeft, bRight, bTop, bBtm, bLT=0, bRT=0, bLB=0, bRB=0;

	var pad1 = perc( clientWidth(), paddingWPerc );
	var pad2 = perc( clientHeight(), parringHPerc );

	var m1 = clientWidth() - pad1;
	if( shX >= m1 )
		shX = m1, bLeft = false;
	else
		bLeft = true, bLT |= 1, bLB |= 2;

	var m2 = clientHeight() - pad2;
	if( shY >= m2 )
		shY = m2, bTop = false;
	else
		bTop = true, bLT |= 2, bRT |= 2;

	var w = f.TGetPropertyAsNumber( "/", 8 );
	if( typeof(corrScale) != "undefined" ) w *= corrScale;
	m1 = w - pad1;
	if( shX < 0 && Math.abs(shX) >= m1 )
		shX = -m1, bRight = false;
	else		
		bRight = true, bRT |= 1, bRB |= 1;

	var h = f.TGetPropertyAsNumber( "/", 9 );
	if( typeof(corrScale) != "undefined" ) h *= corrScale;
	m2 = h - pad2;
	if( shY < 0 && Math.abs(shY) >= m2 )
		shY = -m2, bBtm = false;
	else
		bBtm = true, bLB |= 1, bRB |= 2;

	btnLT.enable( bLT > 0 );
	btnUp.enable( bTop );
	btnRT.enable( bRT > 0 );
	btnL.enable( bLeft );
	btnR.enable( bRight );
	btnLB.enable( bLB > 0 );
	btnD.enable( bBtm );
	btnRB.enable( bRB > 0 );
	
	updShiftX(), updShiftY();
}
function stepU()
{
	return Math.min( stepX(), stepY() );
}
function stepX()
{
	//return Math.round( toFlash(clientWidth()) / 3 * scaleCorrect  );
	//return Math.round( safeGet(f.GetVariable( "srcWidth" )) / 3 * scaleCorrect );
	//return Math.round( safeGet(f.GetVariable( "srcWidth" )) / 3 );
	return perc( clientWidth(), 25.0 );
}
function stepY()
{
	//return Math.round( toFlash(clientHeight()) / 3 * scaleCorrect );
	//return Math.round( safeGet(f.GetVariable( "srcHeight" )) / 3 * scaleCorrect );
	//return Math.round( safeGet(f.GetVariable( "srcHeight" )) / 3 );
	return perc( clientHeight(), 25.0 );
}
function toFlash( val )
{
	var m1 = safeGet(clientWidth()) / safeGet(f.GetVariable( "srcWidth" ));
	var m2 = safeGet(clientHeight()) / safeGet(f.GetVariable( "srcHeight" ));
	var sc = m1 < m2 ? m1:m2;

	return val / sc;
}
function toFlash2X( val )
{
	return (-shX + val) / currScale;
}
function toFlash2Y( val )
{
	return (-shY + val) / currScale;
}
function toHTML2X( val )
{
	return val * currScale + shX;
}
function toHTML2Y( val )
{
	return val * currScale + shY;
}

function updZoom()
{	
	updateHidden( awsDrVIDMap.hiddenZoom, currScale );
}
function loadZoom()
{
	currScale = getHiddenNum( awsDrVIDMap.hiddenZoom );
}
function updShiftX( x )
{	
	updateHidden( awsDrVIDMap.hiddenShiftX, shX );
}
function loadShiftX()
{
	shX = getHiddenNum( awsDrVIDMap.hiddenShiftX );
}
function updShiftY( y )
{	
	updateHidden( awsDrVIDMap.hiddenShiftY, shY );
}
function loadShiftY()
{
	shY = getHiddenNum( awsDrVIDMap.hiddenShiftY );
}
function updZoomMode( zm )
{	
	updateHidden( awsDrVIDMap.hiddenZoomMode, scaleMode );
}
function loadZoomMode()
{
	scaleMode = getHiddenStr( awsDrVIDMap.hiddenZoomMode );
}
function updateHidden( name, val )
{
	var hdf = document.getElementsByName( name )[ 0 ];
	hdf.value = val.toString();
}
function getHiddenStr( name )
{
	var hdf = document.getElementsByName( name )[ 0 ];
	return hdf.value;
}
function getHiddenNum( name )
{
	var hdf = document.getElementsByName( name )[ 0 ];
	return parseFloat( hdf.value );
}

//empty or null partNumbers causes all parts to participate in operation
var oSelectionState = new Object();
function selectParts( partNumbers/*string separated by ,,: awsDrVIDMap.hotspotsSeparator*/, isSel, storeSelection )
{
	if( partNumbers == null )
		partNumbers = ""; 
	if( partNumbers.length == 0 && storeSelection == true )
		oSelectionState = new Object();
	var trgColor = isSel == true ? awsDrVIDMap.selectionColor:"0x0";
	f.SetVariable( "colorToSelect", trgColor );
	f.SetVariable( "partNumbersToSelect", partNumbers );
	f.SetVariable( "setColorReq", 1 );

	var pns = partNumbers.split( awsDrVIDMap.hotspotsSeparator );
	for( var i = 0; i < pns.length; ++i )
		oSelectionState[ pns[i] ] = (isSel == true);

	if( storeSelection == true )
	{
		var val = "";
		for( var key in oSelectionState )
		{
			if( oSelectionState[key] == true )
			{
				if( val.length > 0 )
					val += awsDrVIDMap.hotspotsSeparator;
				val += key;
			}
		}
		updateHidden( awsDrVIDMap.hiddenSelectedHotspots, val )
	}
}


