var NXC = NXC || {};
NXC.MapAllLink = new Class( {
	Implements: [Options, Events],

	options: {
		'markerImage':     false,
		'getProfileURL':  'index.php/profile',
		'profileBlockID': 'navigation-base-profile-view'
	},

	initialize: function( allLinkID, getChildsURL, selectedParentCSS, loaderBox, options ) {
		this.setOptions( options );

		this.allLink           = document.id( allLinkID );
		this.profileBlock      = document.id( this.options.profileBlockID );
		this.getChildsURL      = getChildsURL;
		this.selectedParentCSS = selectedParentCSS;

		this.loaderBox = loaderBox;
		
		this.install();
    },

	install: function() {
		var markerIcon = new GIcon( G_DEFAULT_ICON );
		if( this.options.markerImage ) {
			new Asset.image( this.options.markerImage );
			markerIcon.image    = this.options.markerImage;
			markerIcon.iconSize = new GSize( 20, 32 );	
		}
		this.markerOptions = { icon:markerIcon };

		this.allLink.addEvent( 'click', function( e ) {
			e.stop();
			
			if( this.selectedParentCSS == false ) {
				var parentNode = 160;
			} else {
				selectedParent = $$( this.selectedParentCSS )[0];
				var parentNode = selectedParent.retrieve( 'node_id' );				
			}

			this.loaderBox.show();
			new Request.JSON( {
				'url': this.getChildsURL + '/' + parentNode,
				onSuccess: function( childNodes ) {
					this.loaderBox.hide();

					window.map.clearOverlays();
					childNodes.each( function( el ) {
						this.addLocation( el );
					}.bind( this ) );
				}.bind( this )
			} ).send();
		}.bind( this ) );
	},
	
	addLocation: function( el ) {
		if( el.lat && el.lng ) {
			var point  = new GLatLng( el.lat, el.lng );
			var marker = new GMarker( point, this.markerOptions )
			GEvent.addListener( marker, 'click', function() {
  				this.loaderBox.show();

				new Request.HTML( {
					'url': this.options.getProfileURL + '/' + el.value + '/1',
					update: this.profileBlock,
					onSuccess: function( responseTree, responseElements, responseHTML, responseJavaScript ) {
						this.loaderBox.hide();
					}.bind( this )
				} ).send();
			}.bind( this ) );
			window.map.addOverlay( marker );
		}
	}
} );
