var Search = new Class( {
	Implements: [Options],

	options: {
		'searchFormID':             'search-form',
		'keywordInputID':           'search-keyword',
		'keywordDefaultValue':      false,
		'minKeywordLength':         2,
		'categoreisBlockID':        'categories-navigation',
		'searchResultsContainerID': 'search-results-container',
		'searchResultsBlockID':     'search-results-block',
		'backToCategoriesCSSPath':  '.back-to-categories',
		'searchLoadingText':        'Подождите, выполняется поиск...'
	},

	initialize: function( options ) {
		this.setOptions( options );

		this.searchForm             = document.id( this.options.searchFormID );
		this.keywordInput           = document.id( this.options.keywordInputID );
		this.categoreisBlock        = document.id( this.options.categoreisBlockID );
		this.searchResultsContainer = document.id( this.options.searchResultsContainerID );
		this.searchResultsBlock     = document.id( this.options.searchResultsBlockID );
		this.backToCategories       = $$( this.options.backToCategoriesCSSPath );

		if( this.options.keywordDefaultValue === false ) {
			this.options.keywordDefaultValue = this.keywordInput.get( 'value' );
		}

		this.installKeywordEvents();
		this.installBackToCategories();
		this.installFormEvents();
	},

	installKeywordEvents: function() {
		this.keywordInput.addEvent( 'focus', function() {
			this.keywordInput.set( 'value', '' );
		}.bind( this ) );
		this.keywordInput.addEvent( 'blur', function() {
			if( this.keywordInput.get( 'value' ) == '' ) {
				this.keywordInput.set( 'value', this.options.keywordDefaultValue );
			} else {
				this.keywordInput.set( 'value', '' );
			}
		}.bind( this ) );
	},

	installBackToCategories: function() {
		this.backToCategories.each( function( el ) {
			el.addEvent( 'click', function( e ) {
				e.stop();
				
				this.categoreisBlock.setStyle( 'display', 'block' );
				this.searchResultsContainer.setStyle( 'display', 'none' );
				this.keywordInput.set( 'value', this.options.keywordDefaultValue );
			}.bind( this ) );	
		}.bind( this ) );
	},

	installFormEvents: function() {
		this.searchForm.addEvent( 'submit', function( e ) {
			e.stop();
			var keyword = this.keywordInput.get( 'value' );
			if( keyword.length < this.options.minKeywordLength ) {
				alert( 'Поисковый стринг должен быть больше ' + this.options.minKeywordLength + ' символов' );
				return false;
			}

			this.keywordInput.set( 'value', this.options.searchLoadingText );
			new Request.HTML( {
				'url': this.searchForm.get( 'action' ),
				'method': 'post',
				'update': this.searchResultsBlock,
				'onSuccess': function( responseTree, responseElements, responseHTML, responseJavaScript ) {
					this.categoreisBlock.setStyle( 'display', 'none' );
					this.searchResultsContainer.setStyle( 'display', 'block' );
					
					this.installMarkerShowers();

					this.keywordInput.set( 'value', keyword );
				}.bind( this )
			} ).send( 'keyword=' + keyword );
		}.bind( this ) );
	},
	
	installMarkerShowers: function() {
		$$( '.search-result-link' ).each( function( el ) {
			el.addEvent( 'click', function( e ) {
				e.stop();

				if( window.mapWrapper ) {
					var object = window.mapWrapper.objects.get( el.get( 'id' ).replace( 'search-result', '' ) );				
					window.mapWrapper.clearMap( true );
					window.mapWrapper.showPlacemark( object, true );	
				}
			}.bind( this ) );
		}.bind( this ) );
	}
} );
