var oGMapsLocator = null;
var sUserZipcode = null;

google.load( "maps", "2.x" );
google.setOnLoadCallback( createGMapsLocator );
window.onUnload = google.maps.Unload;


function createGMapsLocator()
{
	oGMapsLocator = new GMapsLocator();
	oGMapsLocator.init();
};	


function GMapsLocator()
{
	this._map = document.getElementById( "map" );
	
	if ( typeof this._map != "undefined" )
	{
		this._startpoint      = new google.maps.LatLng( 52.2191914, 5.3815377 );
		this._zoom            = 7;
		this._showZoomControl = false;
		this._scaleControl    = false;
	
		this.geocoder = new google.maps.ClientGeocoder();
		this.geocoder.setBaseCountryCode( "nl" );
	
		window[ "$_oGMapsLocator" ] = this;
	}
};


GMapsLocator.prototype.init = function()
{
	this.map = new google.maps.Map2( this._map );

	this.gotoClientLocation( '3621BR' );	

//	var customUI = this.map.getDefaultUI();
//	customUI.controls.scalecontrol = false;
//	customUI.controls.menumaptypecontrol = false;
//	customUI.controls.maptypecontrol  = false;
//	customUI.controls.largemapcontrol3d = false;
//	this.map.setUI( customUI );	

	if ( this._map.offsetWidth < 400 )
	{
		this.map.addControl( new GSmallZoomControl() );
	}
	else
	{
		this.map.addControl( new GSmallMapControl() );
		this.map.addControl( new GMapTypeControl() );
	}

	this.getLocations();
};


GMapsLocator.prototype.getLocations = function()
{
	var oLocation = this._getLocations();
	var i = oLocation.location.length;
	while ( i-- > 0 )
	{
		var oTMP = oLocation.location[ i ];
		this.addMarker( oTMP.posn[ 0 ], oTMP.posn[ 1 ], oGMapsLocator.createIcon( oTMP.image ), oTMP.name, oTMP.category + '_' + i );
	}
};


GMapsLocator.prototype.addMarker = function ( nLat, nLang, oIcon, sWindowContents, sID )
{
	if( !oIcon )
		oIcon = null;

	var oPoint   = new google.maps.LatLng( nLat, nLang );
	var oMarker  = new google.maps.Marker( oPoint, oIcon );
	oMarker.id = sID;
	var map      = this.map;
	var fEventer = function()
	{
		map.openInfoWindowHtml( oPoint, sWindowContents );
	};

	google.maps.Event.addListener( oMarker, "click", fEventer );
	this.map.addOverlay( oMarker );
};

GMapsLocator.prototype.showInfoWindow = function( sContents, oPoint )
{
	this.map.openInfoWindowHtml( oPoint, sContents );
};

GMapsLocator.prototype.gotoClientLocation = function( sZipcode )
{
	var sZipSearch = document.forms[ 'zipsearchform' ].elements[ 'zip' ].value.replace( " ", "" );
	var sZipPrefill = document.forms[ 'zipsearchform' ].elements[ 'zip' ].title.replace( " ", "" );

	if ( sZipSearch != "" && sZipSearch.toLowerCase() != sZipPrefill.toLowerCase() )
		sZipcode = sZipSearch;
	
	if( sZipcode && this.moveToZipcode( sZipcode ) )
		return;

	if ( 	google.loader.ClientLocation &&
			google.loader.ClientLocation.longitude &&
			google.loader.ClientLocation.latitude )
		var oPoint =  new google.maps.LatLng( google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude );
	else
		var oPoint = this._startpoint;

	this.moveToPoint( oPoint );
};

GMapsLocator.prototype.moveToZipcode = function( sZipcode )
{
	sZipcode = sZipcode.replace( /(\s|-)/, "" );
	if( sZipcode.length < 4 || sZipcode.length > 6 )
		return false;
	
	var fCallback = function( oPoint )
	{
		if( !oPoint )
			return false;
		oGMapsLocator.moveToPoint( oPoint, 11 );
	}

	this.geocoder.getLatLng( sZipcode + ' , netherlands', fCallback );
	return true;
};

GMapsLocator.prototype.moveToPoint = function( oPoint, nZoom )
{
	if ( typeof nZoom == "undefined" )
		nZoom = this._zoom;

	this.map.setCenter( oPoint, nZoom );
};
