var DynamicSearchForm = new Class( {
	initialize: function( wrapper ) {
		this.wrapper		= wrapper;
		
		this.createDynamicWrap();
		
		this.dynWrap		= this.wrapper.getElement( 'div.search-dyn' );
		this.dynWrapResults	= this.wrapper.getElement( 'ul.results-dyn' );
		this.searchQuery	= this.wrapper.getElement( 'input.search-query' );
		this.searchForm		= this.wrapper.getElement( 'form.search-form' );
		
		this.url			= 'http://maxipod.meioemensagem.com.br';
		this.loading		= '<li class="message">Pesquisando <img src="http://maxipod.meioemensagem.com.br/wp/wp-content/themes/maxipod2008/images/spinner.gif" /></li>'
		this.ajax			= null;
		this.slide			= new Fx.Slide( this.dynWrapResults );
		this.origSFColor	= this.searchQuery.getStyle( 'color' );
		this.emptySFColor	= '#a0a0a0';
		this.emptySFText	= 'Digite sua busca';
		
		this.makeDynamic();
	},
	
	createDynamicWrap: function() {
		var searchDyn		= new Element( 'div' );
		searchDyn.addClass( 'search-dyn' );
		var resultsDyn		= new Element( 'ul' );
		resultsDyn.addClass( 'results-dyn' );
		searchDyn.adopt( resultsDyn );
		var message			= new Element( 'li' );
		message.addClass( 'message' );
		message.innerHTML	= 'Não há resultados';
		resultsDyn.adopt( message );
		this.wrapper.adopt( searchDyn );
	},
	
	focusGained: function( e ) {
		if ( this.searchQuery.getStyle( 'color' ) == this.emptySFColor ) {
			this.searchQuery.setStyle( 'color', this.origSFColor );
			this.searchQuery.value = '';
		}
	},
	
	focusLost: function( e ) {
		if ( this.searchQuery.value.trim() == '' ) {
			this.searchQuery.setStyle( 'color', this.emptySFColor );
			this.searchQuery.value = this.emptySFText;
		}
		var slideOut = this.slideOut.bind( this );
		setTimeout( slideOut, 1000 );
	},
	
	keyPressed: function( e ) {
		new Event( e ).stop;
		
		try {
			this.ajax.cancel();
		} catch ( e ) { }
		
		if ( this.searchQuery.value.trim().length < 3 ) {
			this.slide.slideOut();
			return;
		}
		
		if ( this.dynWrap.getStyle( 'display' ) != 'block' ) {
			this.dynWrap.setStyle( 'display', 'block' );
		}
		
		var date = new Date();
		var data = 'seed=' + date.getTime() + '&search_dyn=1&' + this.searchForm.toQueryString();
		
		this.dynWrapResults.innerHTML = this.loading;
		
		this.slide.slideIn();
		
		this.ajax = new Request.HTML(
			{
				data:		data,
				method:		'get',
				update:		this.dynWrapResults,
				url:		this.url
			}
		)
		this.ajax.addEvent( 'onComplete', this.slideIn.bindWithEvent( this ) );
		this.ajax.send();
	},
	
	makeDynamic: function() {
		/* dynamic search form */
		this.slide.slideOut();
		this.searchQuery.addEvent( 'keyup', this.keyPressed.bindWithEvent( this ) );
		this.searchQuery.addEvent( 'focus', this.focusGained.bindWithEvent( this ) );
		this.searchQuery.addEvent( 'blur', this.focusLost.bindWithEvent( this ) );
		this.searchQuery.setStyle( 'color', this.emptySFColor );
		this.searchQuery.value = this.emptySFText;
	},
	
	slideIn: function() {
		this.slide.slideIn();
	},
	
	slideOut: function() {
		var dontDisplay = function() {
			this.dynWrap.setStyle( 'display', 'none' );
		};
		this.slide.slideOut().chain( dontDisplay.bind( this ) );
	}
} );

window.addEvent( 'domready', function() {
	new DynamicSearchForm( $( 'top-search' ) );
} );

window.addEvent( 'load', function() {
	window.parent.resizeIframe( document.body.offsetHeight + 20 );
	//window.parent.resizeCaller();
	
	var loc = new String( window.location.href );
	var ai = loc.indexOf( '#' );
	if ( ai >= 0 ) {
		var anchor = new String( loc.substring( ai + 1, loc.length ) );
		if ( anchor.length > 0 ) {
			$( anchor ).scrollIntoView( true );
		}
	}
} );
