function SwinxsLocator( map ){
	this.Map = map;

	this.Center = this.Map.getCenter();

	map.setMapType( G_PHYSICAL_MAP );
	map.addControl( new GLargeMapControl3D() );

	var self = this;

	this.Swinxes = [];

	this.FindOnZoom = true;

	GEvent.addListener(map, "zoom", function(){
		if ( self.FindOnZoom == true ){
			self.PrepareFindSwinxes();
		}
	});

	GEvent.addListener(map, "moveend", function(){
		self.PrepareFindSwinxes();
  });

	this.FindSwinxes_Timeout = null;

}

SwinxsLocator.Swinxes = new Object();

SwinxsLocator.prototype.PrepareFindSwinxes = function(){
	if ( this.FindSwinxes_Timeout != null ){
		this.FindSwinxes_Timeout = clearTimeout( this.FindSwinxes_Timeout );
	}
	var self = this;
	this.FindSwinxes_Timeout = setTimeout( function(){
		self.FindSwinxes();
	}, 500 );
}

SwinxsLocator.prototype.FindSwinxes = function(){
	this.FindOnZoom = false;
	if ( this.Map.getZoom() >= 12 ){
		this.Map.setZoom( 11 );
	}
	var bounds = this.Map.getBounds( );
	var self = this;
	$.getJSON( 
		"../swinxs/find_swinxes_callback.php", 
		{  
			n_lat: bounds.getNorthEast().lat(),
			s_lat: bounds.getSouthWest().lat(),
			w_lng: bounds.getSouthWest().lng(),
			e_lng: bounds.getNorthEast().lng(),
			game: null
		}, 
		function(json){
			if ( json != null ){
				if ( json.Swinxes ){
					self.DisplaySwinxes( json.Swinxes );
				}
			}
			self.FindOnZoom = true;
		} 
	);
}

SwinxsLocator.prototype.DisplaySwinxes = function( Swinxes ){
	var length = Swinxes.length;
	// this.Map.clearOverlays();

	for( var i in SwinxsLocator.Swinxes ){
		SwinxsLocator.Swinxes[ i ].Active = false;
	}

	for( var i = 0; i < length; i ++  ){
		this.DisplaySwinxs( Swinxes[ i ] );
	}
	
	for( var i in SwinxsLocator.Swinxes ){
		if ( SwinxsLocator.Swinxes[ i ].Active == false ){
			SwinxsLocator.Swinxes[ i ].Marker.hide();
		}
	}
}

SwinxsLocator.prototype.DisplaySwinxs = function( SwinxsProperties ){
	var Swinxs = this.BuildSwinxs( SwinxsProperties );
}

SwinxsLocator.prototype.BuildSwinxs = function( SwinxsProperties ){

	if ( typeof( SwinxsLocator.Swinxes[ SwinxsProperties.id ] ) == 'object' ){
		SwinxsLocator.Swinxes[ SwinxsProperties.id ].Active = true;
		SwinxsLocator.Swinxes[ SwinxsProperties.id ].Marker.show();
		return;
	}

	var Icon = new GIcon();
	Icon.image = '../themes/swinxs/media/swinxs/map_small.png';
	Icon.shadow = null;
	Icon.iconSize = new GSize( 65, 75 );
	Icon.shadowSize = null;
	Icon.iconAnchor = new GPoint( 32, 75 );
	Icon.infoWindowAnchor = new GPoint( 32, 75 );
	Icon.infoShadowAnchor = new GPoint( 132, 75 );

	var Swinxs = {
		id: SwinxsProperties.id,
		Properties: SwinxsProperties,
		Marker: new GMarker( new GLatLng( SwinxsProperties.lat, SwinxsProperties.lng ), { icon: Icon } ),
		Active: true
	};
	var self = this;
	GEvent.addListener(Swinxs.Marker, "click", function(){
		Swinxs.Marker.openExtInfoWindow(
			self.Map,
			"InfoWindow",
			SwinxsProperties.Info,
			{beakOffset: 0,paddingX:60,paddingY:20 }
		);
		Swinxs.Marker.hide();
	});

	GEvent.addListener( this.Map, 'extinfowindowbeforeclose', function(){
		try {
			Swinxs.Marker.show();
		} catch (e) {}
	} );

  this.Map.addOverlay( Swinxs.Marker );

	SwinxsLocator.Swinxes[ Swinxs.id ] = Swinxs;
}

SwinxsLocator.Instance = null;

SwinxsLocator.Init = function( map ){
	if ( map ){
		SwinxsLocator.Instance = new SwinxsLocator( map );
		SwinxsLocator.Instance.FindSwinxes();
	}
}

SwinxsLocator.ReInit = function( ){
  setTimeout( function(){
    SwinxsLocator.Instance.Map.setCenter( SwinxsLocator.Instance.Center );
  }, 200 );
}

