/** Scripts principais do site. */

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

oUtils.namespace('oApp.Search', 'oApp.Sidebar', 'oApp.Mask', 'oApp.ComboBox');

oApp.Search = Object.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(oEvent){ oEvent.stop(); this.search(); }).bind(this);
			
			/** Form. */
			oForm.observe('submit', fnSearch);
			
			/** Input. */
			oInput
				.observe('focus', function(){
					if(sText == $F(this))
						this.setValue('');
				})
				.observe('blur', function(){
					if('' == $F(this))
						this.setValue(sText);
				})
				.setValue(sText);
			
			/** Button. */
			oButton.observe('click', fnSearch);
			
			delete oForm;
			delete oInput;
			delete oButton;
		},
		
		search: function(){
			var sQ = $F(this.Store.get('inputId'));
			
			if('' == sQ){
				alert('Informe o termo que deseja pesquisar!');
				return false;
			}
			
			location.href = getGlobal('enderecoNormal') + 'site/pesquisa/?_q=' + escape(sQ);
		}
		
	}
);

oApp.Sidebar = Object.extend(
	new Storage({
		containerId: 'container',
		elementBaseId: 'floatbar-',
		shadowId: 'floatbar-shadow'
	}), {
		
		init: function(){
			$$('.sidebar a').each(function(oElement){
				var sOwnerId = oElement.identify();
				var sFloatMenuId = this.Store.get('elementBaseId') + getGlobal('sidebarItens')[sOwnerId];
				
				if(!$(sFloatMenuId))
					return;
				
				oElement
					.observe('mouseenter', function(){
						oApp.Sidebar.showFloatMenu(sOwnerId, sFloatMenuId);
						$(document.body).addClassName('no-select');
					})
					.observe('mouseleave', function(){
						oApp.Sidebar.hideFloatMenu(sFloatMenuId);
						$(document.body).removeClassName('no-select');
					});
				
				$(sFloatMenuId)
					.observe('mouseenter', function(){
						oApp.Sidebar.showFloatMenu(sOwnerId, sFloatMenuId);
						$(sOwnerId).addClassName('selected');
					})
					.observe('mouseleave', function(){
						oApp.Sidebar.hideFloatMenu(sFloatMenuId);
						$(sOwnerId).removeClassName('selected');
					});
			}, this);
		},
		
		showFloatMenu: function(sOwnerId, sFloatMenuId){
			var oContainer	= $(this.Store.get('containerId'));
			var oFloatMenu	= $(sFloatMenuId).show();
			var oShadow		= $(this.Store.get('shadowId'));
			
			var iTop = $(sOwnerId).viewportOffset().top + document.viewport.getScrollOffsets().top + 8;
			var iWindowHeight = document.viewport.getHeight() + document.viewport.getScrollOffsets().top;
			
			var iFloatMenuHeight = oFloatMenu.getDimensions().height;
			var iExpectedArea = iFloatMenuHeight + iTop + 6;
			
			if(iWindowHeight < iExpectedArea)
				iTop = iWindowHeight - iFloatMenuHeight - 6;
			
			if(Prototype.Browser.IE)
				iTop -= parseInt(oContainer.getStyle('margin-top'));
			
			oFloatMenu.setStyle({top: pixalize(iTop)});
			
			if(!oShadow){
				var oShadow = new Element('div', {
					'id': this.Store.get('shadowId'),
					'class': 'floatbar-shadow'
				});
				
				oContainer.insert(oShadow);
			}
			
			oShadow.show().setOpacity(.09).clonePosition(oFloatMenu, {
				offsetTop: 3,
				offsetLeft: 3
			});
		},
		
		hideFloatMenu: function(sFloatMenuId){
			var oFloatMenu = $(sFloatMenuId);
			
			oFloatMenu.hide();
			$(this.Store.get('shadowId')).hide();
		}
		
	}
);

oApp.Mask = {
	
	show: function(oContext){
		var oEl = $('mask');
		
		if(!oContext)
			oContext = $('container');
		
		if(!oEl){
			oEl = new Element('div', {id: 'mask'}).setOpacity(0.4);
			$('container').insert(oEl);
		}
		
		oApp.ComboBox.hide();
		return oEl.show().clonePosition(oContext).setStyle('top:0; left:0;');
	},
	
	hide: function(){
		oApp.ComboBox.show();
		return $('mask').hide();
	}
	
};

oApp.ComboBox = {
	
	isIE6: function(){
		return oUtils.browser.ie6;
	},
	
	show: function(){
		if(this.isIE6())
			$$('select').invoke('setStyle', 'visibility:show;');
	},
	
	hide: function(){
		if(this.isIE6())
			$$('select').invoke('setStyle', 'visibility:hidden;');
	}
	
};

document.observe('dom:loaded', function(){
	oApp.Search.init();
});
