/*
 MAP - Clubsational Frontend Results Map
 (c) Timothy Anido 2006/2007
 version 2.2
 
creates a virtual grid relative to the map-references
for each subregion map, and moves the map marker to
the approximate location of each club on mouseover

 requires:
 - xoo.js
*/

var tween;
var duration;
var iconDisplacementX, iconDisplacementY;
var offset, grid;
var timer, timeout;
var endX, endY;
var currentX, currentY;
var animationX, animationY;
var started;

function init( )
{
	tween = easing.easeIn;
	duration = 1;
	iconDisplacementX = 11;
	iconDisplacementY = 21;
	started = false;

	if( region == 'illawarra-and-shoalhaven' )
	{
		offset = new Object( { 
			'left': 22, 'right': 94,
			'top': 28, 'bottom': 24 
		} );
		grid = new Object( { 
			'x' : { 'increments': 9, 'start': 49 },
			'y' : { 'increments': 11, 'start': 26 } 
		} );
	}
	if( region == 'southern-tablelands-and-south-coast' )
	{
		offset = new Object( { 
			'left': 50, 'right': 108,
			'top': 10, 'bottom': 6 
		} );
		grid = new Object( { 
			'x' : { 'increments': 11, 'start': 45 },
			'y' : { 'increments': 17, 'start': 24 } 
		} );
	}
	if( region == 'sydney' ) {
		offset = new Object( { 
			'left': -7, 'right': -17,
			'top': 23, 'bottom': 26 
		} );
		grid = new Object( { 
			'x' : { 'increments': 6, 'start': 53 },
			'y' : { 'increments': 4, 'start': 23 } 
		} ); 
	}
	if( region == 'riverina-and-south-west' ) {
		offset = new Object( { 
			'left': 10, 'right': 10,
			'top': 40, 'bottom': 75 
		} );
		grid = new Object( { 
			'x' : { 'increments': 47, 'start': 1 },
			'y' : { 'increments': 26, 'start': 15 } 
		} ); 
	}
	if( region == 'central-west' ) {
		offset = new Object( { 
			'left': 9, 'right': 12,
			'top': 33, 'bottom': 58 
		} );
		grid = new Object( { 
			'x' : { 'increments': 40, 'start': 17 },
			'y' : { 'increments': 25, 'start': 2 } 
		} ); 
	}
	if( region == 'far-north-coast' ) {
		offset = new Object( { 
			'left': 44, 'right': 93,
			'top': 12, 'bottom': 14 
		} );
		grid = new Object( { 
			'x' : { 'increments': 16, 'start': 56 },
			'y' : { 'increments': 20, 'start': 0 } 
		} ); 
	}
	if( region == 'new-england-and-north-west' ) {
		offset = new Object( { 
			'left': 2, 'right': 0,
			'top': 43, 'bottom': 55 
		} );
		grid = new Object( { 
			'x' : { 'increments': 34, 'start': 32 },
			'y' : { 'increments': 19, 'start': 0 } 
		} ); 
	}
	if( region == 'newcastle' ) {
		offset = new Object( { 
			'left': 2, 'right': 1,
			'top': 44, 'bottom': 88 
		} );
		grid = new Object( { 
			'x' : { 'increments': 17, 'start': 45 },
			'y' : { 'increments': 8, 'start': 14 } 
		} ); 
	}
	if( region == 'central-coast' ) {
		offset = new Object( { 
			'left': 24, 'right': 0,
			'top': 68, 'bottom': 60
		} );
		grid = new Object( { 
			'x' : { 'increments': 7, 'start': 54 },
			'y' : { 'increments': 4, 'start': 21 } 
		} ); 
	}
	
	if( region == 'sydney-city-and-east' ) {
		offset = new Object( { 
			'left': 7, 'right': 0,
			'top': 68, 'bottom': -50
		} );
		grid = new Object( { 
			'x' : { 'increments': 2, 'start': 57 },
			'y' : { 'increments': 2, 'start': 25 } 
		} ); 
	}
	
	//debug( );
}

function moveMarker ( x, y )
{	

	if( currentX == x && currentY == y ) return;
	else { currentX = x; currentY = y; }

	endX = Math.round( ( ( $( 'map' ).offsetWidth - offset.right ) / grid.x.increments ) * (x-grid.x.start) - iconDisplacementX + offset.left);
	endY = Math.round( ( ( $( 'map' ).offsetHeight - offset.bottom ) / grid.y.increments ) * (y - grid.y.start) - iconDisplacementY + offset.top );
	
	startX = $( 'marker' ).offsetLeft;
	startY = $( 'marker' ).offsetTop;
	
	if( $( 'marker' ).offsetLeft == endX && $( 'marker' ).offsetTop == endY ) return;
	if( started ) {
		animationX.stop( );
		animationY.stop( );
	}
	animationX = $( 'marker' ).animate( { 'property': 'left', 'start': startX, 'end': endX, 'fps': 200, 'duration': .3 } );
	animationY = $( 'marker' ).animate( { 'property': 'top', 'start': startY, 'end': endY, 'fps': 200, 'duration': .3 } );
	
	started = true;
}

function animateM( ) {
	if( timer < duration )
	{
		o = $( 'marker' );
		oL = o.offsetLeft;
		oT = o.offsetTop;
		
		o.style.left = (changeX * tween( timer * (1000/25) / (duration*1000) )) + "px";
		o.style.top = (changeY * tween( timer * (1000/25) / (duration*1000) )) + "px";
		$('info-large').innerHTML = ( o.style.top );
		timer++;
		
		timeout = setTimeout("animateM()", 10);
	}
}

function debug( )
{
		x = offset.left;
		space = ($( 'map' ).offsetWidth - offset.right) / grid.x.increments;
		for( i=0; i<grid.x.increments; i++)
		{
			line = document.createElement('div');
			line.style.borderLeft = "1px solid #999";
			line.style.height = $( 'map' ).offsetHeight + "px";
			line.style.position = "absolute";
			line.style.left = i * space + x + "px";
			$( 'map' ).appendChild( line );
		}

		y = offset.top;
		space = ($( 'map' ).offsetHeight - offset.bottom) / grid.y.increments;
		for( i=0; i<grid.y.increments; i++)
		{
			line = document.createElement('div');
			line.style.borderTop = "1px solid #999";
			line.style.width = $( 'map' ).offsetWidth + "px";
			line.style.position = "absolute";
			line.style.top = i * space + y + "px";
			$( 'map' ).appendChild( line );
		}		
}

window.addEvent('load', init);
