var pos_x;
var pos_y;
var clearCartinaRectTimeout;
var zoomRectWidth = [
	0, 0, 0, 0, 0, 0, 0, 0, 120, 50, 30,
];
var zoomRectHeight = [
	0, 0, 0, 0, 0, 0, 0, 0, 70, 40, 20,
];

function getMousePos( event )
{
	var cartinaItalia = document.getElementById( 'cartinaItalia' );

	if( window.ActiveXObject ) {

		pos_x = window.event.x;
		pos_y = window.event.y;

	} else {
		var top = 0, left = 0;
		var elm = cartinaItalia;

		while( elm ) {

			left += elm.offsetLeft;
			top  += elm.offsetTop;
			elm   = elm.offsetParent;
		}

		pos_x = event.pageX - left;
		pos_y = event.pageY - top;
	}
}

function clickMappa( event )
{
	var f = document.forms[ 'cartina' ];
	
	getMousePos( event );

	f.elements[ 'clickX' ].value = pos_x;
	f.elements[ 'clickY' ].value = pos_y;
	
	event.cancelBubble = true;

	f.submit();
}

function resizeCartinaRect()
{
	var f = document.forms[ 'cartina' ];
	var els = document.getElementsByName( 'zoom' );
	var zoom = 0;
	var rect = $( 'cartinaZoomRect' );
	
	for( var i = 0; !zoom && ( i < els.length ); i++ ) {
		var el = els[ i ];
		
		if( el.checked )
			zoom = parseInt( el.value );
	}

	if( parseInt( rect.style.width ) != zoomRectWidth[ zoom ] ) {

		rect.style.width  = zoomRectWidth[ zoom ] + 'px';
		rect.style.height = zoomRectHeight[ zoom ] + 'px';
	}
}

function cartinaMouseOver()
{
	var rect = $( 'cartinaZoomRect' );

	if( clearCartinaRectTimeout ) {
		
		window.clearTimeout( clearCartinaRectTimeout );
		
		clearCartinaRectTimeout = null;		
	}

	resizeCartinaRect();

	if( rect.style.display != '' )
		rect.style.display = '';
}

function cartinaMouseOut()
{
	clearCartinaRectTimeout = window.setTimeout( 'clearCartinaRect()', 500 );
}

function clearCartinaRect()
{
	$( 'cartinaZoomRect' ).style.display = 'none';

	window.clearTimeout( clearCartinaRectTimeout );
	
	clearCartinaRectTimeout = null;		
}

function cartinaRectMouseOver()
{
	cartinaMouseOver();
}

function cartinaMouseMove( event )
{	
	var rect = $( 'cartinaZoomRect' );
	var w = parseInt( rect.style.width );
	var h = parseInt( rect.style.height );

	getMousePos( event );

	if( !isNaN( w ))
		rect.style.left = ( pos_x - ( w / 2 )) + 'px';

	if( !isNaN( h ))
		rect.style.top = ( pos_y - ( h / 2 )) + 'px';
}

