gestorMarkers =
{
	zoomLimits: { top: 0, bottom: 0 },
	scriptGetMarkers: [],
	map: null,
	markers: [],
	manager: null,
	img2hide: [],
	refreshVisibilityF: null,

	init: function (obMap, zoomTop, zoomBottom, arrScripts)
	{
		this.map = obMap;
		this.manager = new GMarkerManager(this.map);
		this.zoomLimits.top = zoomTop;
		this.zoomLimits.bottom = zoomBottom;
		this.scriptGetMarkers = arrScripts;

		var gestor = this;

		this.refreshVisibilityF = function ()
		{
			for ( var i in gestor.img2hide ) { gestor.visible(false, gestor.img2hide[i]); };
		};

		GEvent.addListener(this.map, "load", function () { gestor.pintarMarkers(); });
		GEvent.addListener(this.map, "moveend", function () { gestor.pintarMarkers(); });
	},

	pintarMarkers: function ()
	{
		if ( this.map.getZoom() > this.zoomLimits.top || this.map.getZoom() < this.zoomLimits.bottom ) { return; };

		var gestor = this;
		var coords = this.map.getBounds();

		for ( var j in this.scriptGetMarkers )
		{		
			$.get(this.scriptGetMarkers[j], { latNE: coords.getNorthEast().lat(), lonNE: coords.getNorthEast().lng(), latSO: coords.getSouthWest().lat(), lonSO: coords.getSouthWest().lng()  },
				function(data, textStatus)
				{
					var arrMarkers = [];
							
					for ( var i in data )
					{
						var existe = false;
						for ( var h in gestor.markers )
						{
							if ( data[i]['desc'] == gestor.markers[h].dataPdi['desc'] && data[i]['img'] == gestor.markers[h].dataPdi['img'] && data[i]['lat'] == gestor.markers[h].dataPdi['lat'] && data[i]['long'] == gestor.markers[h].dataPdi['long'] ) { existe = true; break; };
						};
						if ( existe ) { continue; };

						var cmIcon = new GIcon();
						cmIcon.image = data[i]['img'];
						cmIcon.iconSize = new GSize(data[i]['ancho'], data[i]['alto']);
						cmIcon.iconAnchor = new GPoint(6, 6);
						cmIcon.infoWindowAnchor = new GPoint(6, 0);

						var cmMarker = new GMarker(new GLatLng(data[i]['lat'], data[i]['long']), cmIcon);
						arrMarkers.push(cmMarker);
						gestor.markers.push(cmMarker);
						cmMarker.dataPdi = data[i];
						var handleF = function () { this.openInfoWindowHtml(this.dataPdi.desc); };
						GEvent.addListener(cmMarker, "click", handleF);
					};

					if ( arrMarkers.length > 0 )
					{
						gestor.manager.addMarkers(arrMarkers,  gestor.zoomLimits.bottom,  gestor.zoomLimits.top);					
						gestor.manager.refresh();
					};

					gestor.refreshVisibilityF();
				},
			'json');
		};
	},

	visible: function (state, img)
	{
		var imgInArray = false;
		for ( var i in this.img2hide ) { if ( this.img2hide[i] == img ) { imgInArray = i; break; }; };

		if ( state && imgInArray !== false ) { this.img2hide.splice(i, 1); }
		else if ( !state && !imgInArray ) { this.img2hide.push(img); };

		for ( var i in this.markers )
		{
			if ( this.markers[i].dataPdi.img == img )
			{
				if ( state )
				{
					this.markers[i].Q.icon.iconSize.width = this.markers[i].dataPdi['ancho'];
					this.markers[i].Q.icon.iconSize.height = this.markers[i].dataPdi['alto'];
					this.markers[i].show();
					this.manager.refresh();
				}
				else
				{
					this.markers[i].hide();
					this.markers[i].Q.icon.iconSize.width = 0;
					this.markers[i].Q.icon.iconSize.height = 0;
				};
			};
		};
	}
};