var NXC = NXC || {};
NXC.GMapSearch = new Class( {
	initialize: function( mapBlockID, formID, resultsBlock, loaderBox, searchURL ) {
		if( GBrowserIsCompatible() == false ) {
			alert( 'Your browser isn`t compatible with google maps' );
			return false;
		}

		this.mapBlock     = $( mapBlockID );

		this.form         = $( formID );
    	this.xGMapInput   = this.form.getElementById( 'gMapSearchX' );
    	this.yGMapInput   = this.form.getElementById( 'gMapSearchY' );
    	this.radiusInput  = this.form.getElementById( 'gMapSearchRadius' );
    	this.classInput   = this.form.getElementById( 'gMapSearchClass' );
    	this.searchButton = this.form.getElementById( 'gMapSearchButton' );

		this.resultsBlock = $( resultsBlock );

    	this.loaderBox    = loaderBox;
    	
    	this.searchURL    = searchURL;
    },

	buildMap: function() {
		this.map = new GMap2( this.mapBlock );

		this.map.addControl( new GLargeMapControl(), new GControlPosition( G_ANCHOR_TOP_LEFT, new GSize( 10, 10 ) ) );
		this.map.addControl( new GMapTypeControl(), new GControlPosition( G_ANCHOR_TOP_RIGHT, new GSize( 10, 10 ) ) );
		this.map.addControl( new google.maps.LocalSearch(), new GControlPosition( G_ANCHOR_BOTTOM_RIGHT, new GSize( 10, 20 ) ) );
		this.map.setCenter( new GLatLng( 0, 0 ), 1 );

		GEvent.addListener( this.map, 'click', function( overlay, point ) {
			this.setInputsCenter( point );
		}.bind( this ) );

		this.xGMapInput.addEvent( 'change', function() {
			this.setCenterFromInputs();
		}.bind( this ) );
		this.yGMapInput.addEvent( 'change', function() {
			this.setCenterFromInputs();
		}.bind( this ) );

		this.form.addEvent( 'submit', function( e ) {
			e.stop();
			this.search();
		}.bind( this ) );
	},

	setInputsCenter: function( point ) {
		this.xGMapInput.set( 'value', point.lat() );
		this.yGMapInput.set( 'value', point.lng() );
	},

	setCenterFromInputs: function() {
		this.map.panTo( new GLatLng( this.xGMapInput.get( 'value' ).toFloat(), this.yGMapInput.get( 'value' ).toFloat() ) );
	},

	search: function() {
		this.loaderBox.show();

		new Request.HTML( {
			url: this.searchURL,
			method: 'post',
			update: this.resultsBlock, 
			onFailure: function( xhr ) {
				this.loaderBox.hide();
			}.bind( this ),
			onSuccess: function( responseTree, responseElements, responseHTML, responseJavaScript ) {
				this.resultsBlock.setStyle( 'display', 'block' );
				this.loaderBox.hide();
				this.installMapObjects();
			}.bind( this )
		} ).send( this.form.toQueryString() );
	},
	
	installMapObjects: function() {
		var tmp = this;
		this.mapObjects = $$( 'a.gmapobject' );
		this.mapObjects.each( function( el ) {
			el.addEvent( 'click', function( e ) {
				e.stop();
				var scroll = new Fx.Scroll( window );
				scroll.toElement( this.mapBlock );
				this.showMapWindow( el );
			}.bind( this ) );
		}.bind( this ) );
	},

	showMapWindow: function( el ) {
		var point = new GLatLng( el.get( 'lat' ), el.get( 'lng' ) );
		var html  = '<strong>' + el.get( 'html' ) + '</strong><br /><br /><div style="float: right;"><a href="' + el.get( 'href' ) + '">More info</div>';
		this.map.panTo( point ); 
		this.map.openInfoWindowHtml( point, html );
	}
} );
