/** Scripts principais do site. */

function show(mElement){
	return $(mElement).setStyle('display', 'block');
}

function hide(mElement){
	return $(mElement).setStyle('display', 'none');
}

function pixalize(mNumber){
	return parseInt(mNumber).toString() + 'px';
}

var oLogin = $extend(
	new Storage({
		formId: 'frmLogin',
		btLogin: 'bt-login',
		formStartHeight: 1,
		formFinalHeight: 29
	}),	{
	
		init: function(){
			if(getGlobal('areaRestrita')){
				show(this.Store.get('formId')).setStyle('height', pixalize(this.Store.get('formFinalHeight')));
				return;
			}
			
			/** Comportamento do botão para exibir o formulário. */
			$(this.Store.get('btLogin')).addEvent('click', (function(oEvent){
				new Event(oEvent).stopPropagation();
				this.fx();
			}).bind(this));
			
			/** Comportamento do formulário. */
			$(this.Store.get('formId')).addEvent('submit', function(oEvent){
				new Event(oEvent).preventDefault();
				this.send({ evalResponse: true });
			});
		},
		
		fx: function(){
			var oForm = show($(this.Store.get('formId')));
			var oFx = oForm.effect('height', {duration: 750, wait: false});
			
			oFx
				.start(pixalize(this.Store.get('formStartHeight')), pixalize(this.Store.get('formFinalHeight')))
				.chain(function(){ $('email').focus(); });
		}
		
	}
);

var oSearch = $extend(
	new Storage({
		formId: 'frmSearch',
		inputId: 'q',
		buttonId: 'search-button',
		defaultText: 'Pesquise no site',
		enterKey: 13
	}), {
		
		init: function(){
			var oForm	 = $(this.Store.get('formId'));
			var oInput	 = $(this.Store.get('inputId'));
			var oButton	 = $(this.Store.get('buttonId'));
			var sText	 = this.Store.get('defaultText');
			var fnSearch = function(){ oForm.fireEvent('submit'); };
			
			/** Form. */
			oForm.addEvent('submit', (function(oEvent){
				if(oEvent)
					new Event(oEvent).preventDefault();
				
				this.search();
			}).bind(this));
			
			/** Input. */
			oInput.addEvents({
				'focus': function(){
					if(sText == this.value)
						this.value = '';
					
					this.setStyle('color', '#008FAD');
				},
				
				'blur': function(){
					if('' == this.value){
						this.value = sText;
						this.setStyle('color', '#BBB');
					}
				}
			}).value = sText;
			
			/** Button. */
			oButton.addEvent('click', fnSearch);
			
			delete oForm;
			delete oInput;
			delete oButton;
		},
		
		search: function(){
			var sQ = $(this.Store.get('inputId')).getValue();
			
			if('' == sQ){
				alert('Informe o termo que deseja pesquisar!');
				return false;
			}
			
			location.href = getGlobal('enderecoNormal') + 'pesquisa.php?_q=' + escape(sQ);
		}
		
	}
);

var oMenu = {
	
	init: function(){
		this.fx();
	},
	
	fx: function(){
		$$('.menu a').each(function(oElement){
			oElement.removeEvents();
			
			if(oElement.hasClass('selected'))
				return;
			
			var oFx = oElement.effects({duration: 400, wait: false});
			var hColors = {
				normal: {
					color: '#888',
					backgroundColor: '#F3F3F3'
				},
				
				hover: {
					color: '#8EB4E1',
					backgroundColor: '#FFF'
				}
			};
			
			oElement.addEvents({
				'mouseenter': function(){
					oFx.start({
						'color': [hColors.normal.color, hColors.hover.color],
						'background-color': [hColors.normal.backgroundColor, hColors.hover.backgroundColor]
					});
				},
				
				'mouseleave': function(){
					oFx.start({
						'color': [hColors.hover.color, hColors.normal.color],
						'background-color': [hColors.hover.backgroundColor, hColors.normal.backgroundColor]
					});
				}
			});
		});
	}
	
};

window.addEvent('domready', function(){
	//oLogin.init();
	oSearch.init();
	oMenu.init();
});