var GameSearch = new Class( {
	
	options: {
		inputText: 'Search For A Game',
		blankText: 'Start typing to search...',
		emptyText: 'No games could be found with that name, <a href="mailto:support@myinternetservices.com?subject=Game Request">send a request</a> for this game.'
	},
	
	initialize: function() {
		if( !$( 'gameServerSearch' ) ) return;
		
		this.container = $( 'gameServerSearch' );
		this.form = this.container.getElement( 'form' ).addEvent( 'submit', this.disableForm.bindWithEvent( this ) );
		this.input = this.form.getElement( 'input[name=query]' );
		
		this.searching = false;
		this.keepFocus = false;
		this.resultList = new Element( 'ul' );
		this.selected = false;
		this.hasResults = false;
		this.browsing = false;
		this.request = null;
		
		this.input.addEvents( {
			'focus': this.onFocusEvent.bind( this ),
			'blur': this.onBlurEvent.bind( this ),
			'keyup': this.onKeyUpEvent.bind( this ),
			'keydown': this.onKeyDownEvent.bindWithEvent( this )
		} ).set( {
			'value': this.options.inputText,
			'autocomplete': 'off'
		} );
		
		this.results = new Element( 'div', {
			'class': 'autocomplete-results',
			'html': '<p>'+ this.options.blankText +'</p>',
			'styles': {
				'left': this.input.getPosition().x,
				'top': this.input.getPosition().y + this.input.getSize().y,
				'visibility': 'hidden'
			},
			'events': {
				'mouseenter': function() { this.keepFocus = true; }.bind( this ),
				'mouseleave': function() { this.keepFocus = false; }.bind( this )
			}
		} ).inject( this.container, 'after' );
	
	},
	
	disableForm: function( e ) { e.stop(); },
	
	onFocusEvent: function() {
		if( this.input.get( 'value' ) == this.options.inputText ) this.input.erase( 'value' );
		this.results.setStyle( 'visibility', 'visible' );
	},
	
	onBlurEvent: function() {
		if( this.keepFocus ) return;
		
		if( this.input.get( 'value' ) == '' ) {
			this.input.set( 'value', this.options.inputText );
			this.searching = false;
			this.results.set( 'html', '<p>'+ this.options.blankText +'</p>' );
		}
		
		this.results.setStyle( 'visibility', 'hidden' );
	},
	
	onKeyUpEvent: function() {
		if( !this.searching ) {
			this.results.empty().adopt( this.resultList );
			this.searching = true;
		}
		
		if( this.browsing ) return;
		if( this.request != null ) this.request.cancel();
		
		var query = this.input.get( 'value' );
		
		if( query == '' ) {
			this.results.set( 'html', '<p>'+ this.options.blankText +'</p>' );
		
		} else {
			this.request = new Request.JSON( {
				url: Meta.get( 'url_root' ) +'/gaming/servers/search',
				onComplete: this.populateResults.bind( this )
			} ).get( {
				'query': query,
				'json': '1'
			} );
		}
	
	},
	
	onKeyDownEvent: function( e ) {
		
		if( this.hasResults == false ) return;
		
		this.resultList.getElements( 'li' ).set( 'class', '' );
		this.browsing = false;
		
		if( e.key == 'down' ) {
			e.stop();
			
			if( this.selected == false ) 
				this.selected = this.resultList.getFirst( 'li' );
			else
				this.selected = this.selected.getNext( 'li' ) ? this.selected.getNext( 'li' ) : this.resultList.getFirst( 'li' );
			
			this.selected.set( 'class', 'selected' );
			this.browsing = true;
			
		} else if( e.key == 'up' ) {
			e.stop();
			
			if( this.selected == false ) 
				this.selected = this.resultList.getLast( 'li' );
			else
				this.selected = this.selected.getPrevious( 'li' ) ? this.selected.getPrevious( 'li' ) : this.resultList.getLast( 'li' );
			
			this.selected.set( 'class', 'selected' );
			this.browsing = true;
			
		} else if( e.key == 'enter' && this.selected != false ) {
			e.stop();
			
			this.results.setStyle( 'visibility', 'hidden' );
			this.input.set( 'value', this.selected.getElement( 'a' ).get( 'text' ) );
			this.browsing = true;
			
			window.location = this.selected.getElement( 'a' ).get( 'href' );
		
		}
	
	},
	
	populateResults: function( r ) {
		
		var results = r.results;
		
		this.results.empty();
		this.hasResults = false;
		
		if( results.length == 0 ) {
			this.results.set( 'html', '<p>'+ this.options.emptyText +'</p>' );
		
		} else {
			
			this.hasResults = true;
			this.resultList.empty();
			this.results.adopt( this.resultList );
			
			results.each( function( result ) {
				var alias	= result.alias_long == ''
					? result.alias
					: result.alias_long;
				this.resultList.adopt(
					new Element( 'li' ).adopt(
						new Element( 'a', {
							'href': Meta.get( 'url_root' ) +'/gaming/servers/'+ alias,
							'text': result.title
						} )
					)
				)
			}, this );
		
		}
	
	}
	
} );

window.addEvent( 'domready', function() {
	
	new GameSearch;
	new ServerSpecial;
	
} );