
var map = new YMap(document.getElementById('map'));

var hasInitialized = 0;

var isIE = 0;



function init_map ( ) {
	
	var cornwall = new YGeoPoint ( 50.2981125512805, -5.17181396484375 ); // start map over cornwall
	
	if ( navigator.appName == "Microsoft Internet Explorer" ) // check if we're using IE which has problems with resizing the map
		isIE = 1;
	
	// Zoom scroller
	map.addZoomLong ( );

	map.setMapType ( YAHOO_MAP_REG ); // Street Map

	// Display the map centered on a geocoded location
	//map.drawZoomAndCenter("San Francisco", 3);
	map.drawZoomAndCenter ( cornwall, 9 );

	// Update HTML fields on first load and when something changes
	YEvent.Capture(map, EventsList.endMapDraw, firstMapDraw);
	YEvent.Capture(map, EventsList.changeZoom, UpdateToHTML);
	YEvent.Capture(map, EventsList.endPan, UpdateToHTML);
}

function firstMapDraw ( ) { // make the endMapDraw event only function once - when it first loads.
	
	if ( !hasInitialized ) {
		UpdateToHTML ( );
	}
	
	hasInitialized = 1;
}

function UpdateToHTML ( ) {
	latlon = map.getCenterLatLon ();

	zoom = map.getZoomLevel ();

	size = map.getContainerSize ();

	document.getElementById ( 'latitude-input' ).value = latlon.Lat;
	document.getElementById ( 'longitude-input' ).value = latlon.Lon;

	document.getElementById ( 'zoom-input' ).value = zoom;

	document.getElementById ( 'width-input' ).value = size.width;
	document.getElementById ( 'height-input' ).value = size.height;
}

function UpdateToMap ( ) {
	
	size = new YSize (
		document.getElementById ( 'width-input' ).value,
		document.getElementById ( 'height-input' ).value
	);
	
	if ( isIE ) { // fix bug with IE that reizes two pixels to much because of borders.
		size.width -= 2;
		size.height -= 2;
	}
	
	map.resizeTo ( size );
	
	
	latlon = new YGeoPoint(
		document.getElementById ( 'latitude-input' ).value,
		document.getElementById ( 'longitude-input' ).value
	);
	map.panToLatLon ( latlon );


	zoom = document.getElementById ( 'zoom-input' ).value;
	map.setZoomLevel ( zoom );

}


function increase ( elementId, increment ) {
	var value = parseInt( document.getElementById ( elementId ).value );
	value += increment;
	document.getElementById ( elementId ).value = value;
	UpdateToMap ( );
}

function decrease ( elementId, increment ) {
	var value = parseInt( document.getElementById ( elementId ).value );
	value -= increment;
	document.getElementById ( elementId ).value = value;
	UpdateToMap ( );
}

window.onload = init_map;
