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://podcast.hsm.com.br';
		this.loading		= '<li class="message">Pesquisando <img src="http://podcast.hsm.com.br/wp/wp-content/themes/hsm/images/spinner.gif" /></li>'
		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;
		
		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 ) );
	}
} );
var SortableSidebar = new Class( {
	initialize: function() {
		this.makeSortable();
	},
	
	makeSortable: function() {
		/* make sidebar sortable */
		var sortables = new Sortables( '#sidebar-col-1', {
			clone:		true,
			handle:		'.widget-title',
			opacity:	0.4,
			revert:		{
				duration:	512,
				transition:	'circ:out'
			}
		} );
	}
} );
var ToggableSidebar = new Class( {
	initialize: function( sidebar ) {
		this.sidebar	= sidebar;
		this.cookie		= new Hash.Cookie( this.sidebar, {
			duration: 3600
		} );
		this.slide		= new Array();
		this.toggle		= new Array();
		
		this.makeToggable();
		this.initializeCookie();
	},
	
	doToggle: function( e, i ) {
		e = new Event( e ).stop();
		this.setToggle( i );
	},
	
	initializeCookie: function() {
		if ( this.cookie.get( 'toggle' ) ) {
			this.loadCookie();
			return;
		}
		var theToggle	= new Array();
		$( this.sidebar ).getChildren().each( function( element, i ) {
			theToggle[ i ] = {
				'state': 'open'
			};
		} );
		var settings = {
			'toggle':	theToggle
		}
		this.cookie.extend( settings );
	},
	
	loadCookie: function() {
		var toggle = this.cookie.get( 'toggle' );
		for ( i = 0; i < toggle.length; i++ ) {
			if ( !$( this.sidebar + '-toggle-' + i ) ) {
				continue;
			}
			if ( toggle[ i ].state == 'close' ) {
				this.slide[ i ].toggle();
				$( this.sidebar + '-toggle-' + i ).addClass( 'toggle-closed' );
			}
		}
	},
	
	makeToggable: function() {
		$( this.sidebar ).getChildren().each( function( element, i ) {
			var content	= element.getElement( 'div.widget-content' );
			
			this.slide[ i ]				= new Fx.Slide( content );
			this.toggle[ i ]			= new Element( 'div' );
			this.toggle[ i ].id			= this.sidebar + '-toggle-' + i;
			this.toggle[ i ].title		= 'Clique aqui para expandir / contrair';
			this.toggle[ i ].innerHTML	= '<!-- spacer -->';
			this.toggle[ i ].addClass( 'toggle' );
			this.toggle[ i ].addEvent( 'click', this.doToggle.bindWithEvent( this, i ) );
			
			var title_wrap = element.getElement( 'div.widget-title' );
			title_wrap.adopt( this.toggle[ i ] );
			this.toggle[ i ].injectAfter( element.getElement( 'div.widget-title-top' ) );
		}, this );
	},
	
	setToggle: function( i ) {
		var setProp = function() {
			if ( this.toggle[ i ].hasClass( 'toggle-closed' ) ) {
				this.toggle[ i ].removeClass( 'toggle-closed' );
				this.setCookie( i, 'open' );
			} else {
				this.toggle[ i ].addClass( 'toggle-closed' );
				this.setCookie( i, 'close' );
			}
		};
		this.slide[ i ].toggle().chain( setProp.bind( this ) );
	},
	
	setCookie: function( i, theState ) {
		var theToggle			= this.cookie.get( 'toggle' );
		theToggle[ i ]			= {
			'state': theState
		};
		var settings			= {
			'toggle': theToggle
		};
		this.cookie.extend( settings );
		var toggle = this.cookie.get( 'toggle' );
	}
} );

window.addEvent( 'domready', function() {
	new DynamicSearchForm( $( 'busca-wrap' ) );
	//new SortableSidebar();
	//new ToggableSidebar( 'sidebar-col-1' );
	
	//resizeIFrame();
} );

//window.addEvent( 'load', resizeIFrame );

function resizeIFrame() {
	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 );
		}
	}
}
