var clPromoStream;

var ConnectorPromo = new Class( {

	latitude :false,
	longitude :false,
	map :false,
	placeIcon :false,
	placesPoints:[],
	placesData:[],

	initialize : function(map, latitude, longitude) {
		this.map = map;
		this.latitude = latitude;
		this.longitude = longitude;
		if (this.map) {
			this.loadPlaces();
		}
	},

	setupPlacesIcon : function() {
		if (!this.placeIcon) {
			var icon = new GIcon();
			icon.image = "/images/maps/dot.png";
			icon.iconSize = new GSize(6, 7);
			icon.iconAnchor = new GPoint(3, 3);
			icon.infoWindowAnchor = new GPoint(3, 3);
			this.placeIcon = icon;
		}
		return this.placeIcon;
	},

	loadPlaces : function() {

		var southWest = this.map.getBounds().getSouthWest();
		var northEast = this.map.getBounds().getNorthEast();

		new Ajax("/places/js/functions/nearby.php", {
			method :'get',
			data :'json=json&ne=' + northEast.toUrlValue() + '&sw='
					+ southWest.toUrlValue(),
			onComplete : function(r) {
				var r = Json.evaluate(r);
				if (r) {
					if (r.total.toInt() > 0) {
				
						r.data.each( function(item, index) {
							this.placesData.push( item );
							var point = new GLatLng( Number( item.latitude ), Number( item.longitude ) );
							this.placesPoints.push( point );
							var marker = new GMarker(point, {icon:this.setupPlacesIcon(), draggable: false, title: item.name });
							
							GEvent.addListener( marker, 'click', function( point ) {
								
								if( this.placesPoints.indexOf( point ) ){
									var i = this.placesPoints.indexOf( point );
									if( this.placesData[i] ){
										window.location = '/places/item/' + this.placesData[i].vg_id;
									}
								}
								
							}.bind(this));
							
							this.map.addOverlay( marker );
							
						}.bind(this));
					}
				}
			}.bind(this),
			onFailure : function() {

			}.bind(this)
		}).request();

	}

});