/** Scripts principais do site. */

var oApp = {
	
	show: function(mElement){
		return $(mElement).setStyle('display', 'block');
	},
	
	hide: function(mElement){
		return $(mElement).setStyle('display', 'none');
	},
	
	pixalize: function(mNumber){
		return parseInt(mNumber).toString() + 'px';
	},
	
	layoutFix: function(){
		this.nav();
		this.menu();
	},
	
	nav: function(){
		var iWidth = 0;
		
		$$('.nav a').each(function(oElement){
			iWidth += oElement.getSize().size.x + oElement.getStyle('margin-right').toInt();
		});
		
		$('nav-items-container').setStyle('width', this.pixalize(iWidth));
	},
	
	menu: function(){
		$('inner-content').setStyle('height', this.pixalize($('content').getSize().size.y));
	}
	
};

var oLogin = $extend(
	new Storage({
		formId: 'frmLogin',
		mainId: 'top-bar',
		togglerId: 'btn-login',
		fieldsCntId: 'login-fields',
		focusField: 'email'
	}), {
		
		init: function(){
			var oForm = $(this.Store.get('formId'));
			
			if(!oForm)
				return;
			
			oForm.addEvent('submit', function(oEvent){
				if(oEvent)
					new Event(oEvent).preventDefault();
				
				this.send({ evalResponse: true });
			});
		},
		
		show: function(){
			var oToggler = $(this.Store.get('togglerId'));
			var oFieldsCnt = $(this.Store.get('fieldsCntId'));
			
			new Fx.Style(oToggler, 'opacity').start(1, 0).chain(function(){
				new Fx.Style(oLogin.Store.get('mainId'), 'width', {duration: 1000}).start(460).chain(function(){
					oToggler.setStyle('display', 'none');
					oFieldsCnt.setOpacity(0).setStyle('display', 'inline');
					
					new Fx.Style(oFieldsCnt, 'opacity').start(0, 1).chain(function(){
						$(oLogin.Store.get('focusField')).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	 = oInput.getProperty('title');
			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 = '';
				},
				
				'blur': function(){
					if('' == this.value)
						this.value = sText;
				}
			}).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('enderecoNormalLang') + 'pesquisa.php?_q=' + escape(sQ);
		}
		
	}
);

window.addEvent('domready', function(){
	oApp.layoutFix();
	
	oLogin.init();
	oSearch.init();
});
