// -- Custom
var _Custom = {
	
	addFakeHover: function(els, classe) {
		classe = classe || 'hover'
		addEventDict(els, 
			{ 
				'mouseover' : function(e) { addClass(e.currentTarget, classe) },
				'focus' : function(e) { addClass(e.currentTarget, classe) },
				'mouseout' : function(e) { remClass(e.currentTarget, classe) },
				'blur' : function(e) { remClass(e.currentTarget, classe) }
			}
		)
	},
	
	PopupImagem: function(e) {
		
		e.preventDefault()
		
		var src = e.currentTarget
		
		var width = parseInt(src.getAttribute('href').split('&').filter(function(el) { if (el.substr(0, 2) == 'w=') return true })[0].substr(2))
		var height = parseInt(src.getAttribute('href').split('&').filter(function(el) { if (el.substr(0, 2) == 'h=') return true })[0].substr(2))
		var bLegenda = (src.getAttribute('href').indexOf('Legenda=') != -1)  
		
		var iWidth = 740, iHeight = 550, iTop, iLeft
		
		iWidth += 20 // scrollbars
		
		iTop = 30
		iLeft = parseInt((screen.width - iWidth) / 2)
		
		return Popup.link(src, 'width=' + iWidth + ',height=' + iHeight + ',top=' + iTop + ',left=' + iLeft + ',scrollbars=1,location=0,statusbar=0,menubar=0')

	}, 

	Banner: {
		
		banners: null,
		timeout: null,
		total: 0,
		indexCurrent: 1,
				
		atualizar: function(index) {
			
			_Custom.Banner.indexCurrent = (_Custom.Banner.indexCurrent == _Custom.Banner.total) ? 1 : _Custom.Banner.indexCurrent + 1; 
			
			if (isdef(index)) {
				window.clearTimeout(_Custom.Banner.timeout); 
				_Custom.Banner.indexCurrent = index; 
			}
			
			var objBanner = _Custom.Banner.banners[_Custom.Banner.indexCurrent - 1]

			var responseId = objBanner.getElementsByTagName("id"); 
			var responseDescricao = objBanner.getElementsByTagName("descricao"); 
			var responseExtensao = objBanner.getElementsByTagName("extensao"); 
			var responseURL = objBanner.getElementsByTagName("url"); 
			var oP = getElem('ct-flash'); 
			var oA = filter(getAll('a', oP), function(el) { return el.parentNode == oP; }); 
			var oImg = getAll('img', oP); 
			
			if (responseURL.length == 0) {
				if (oA.length > 0) oA[0].parentNode.removeChild(oA[0]); 
				oP.appendChild(oImg[0])
			} else {
				if (oA.length == 0) {
					oA = oP.appendChild(document.createElement('a')); 
					oA.appendChild(oImg[0])
				} else
					oA = oA[0]
				oA.setAttribute('href', responseURL[0].childNodes[0].nodeValue); 
				if (responseURL[0].getAttribute('externo') == '0')
					oA.className = ''
				else
					oA.className = 'externo'
			}
			oImg[0].setAttribute('src', '/_images/banners/' + responseId[0].childNodes[0].nodeValue + '.' + responseExtensao[0].childNodes[0].nodeValue)
			oImg[0].setAttribute('alt', responseDescricao[0].childNodes[0].nodeValue)
			
			each(getAll('li', oP), function(el, pos) { 
				if (pos + 1 == _Custom.Banner.indexCurrent) addClass(el, 'on'); else remClass(el, 'on'); 
			})
			
			_Custom.Banner.timeout = window.setTimeout("_Custom.Banner.atualizar()", 5000); 

		},
				
		init: function() {
			var objAjax = new AjaxObject('http://' + window.location.host + '/_include/asp/xml.asp?Acao=Banner', function(status, XML) { 
				if (status == 200 || status == 304) { 

					_Custom.Banner.total = parseInt(XML.getElementsByTagName('banner').length)
					_Custom.Banner.banners = XML.getElementsByTagName('banner')
					
					if (_Custom.Banner.total > 0) {
						var oUl = document.createElement('ul')
						for (i = 1; i <= _Custom.Banner.total; i++) {
							oA = document.createElement('a')
							oA.setAttribute('href', '#')
							oA.appendChild(document.createTextNode(i))
							oLi = document.createElement('li')
							oLi.appendChild(oA)
							oUl.appendChild(oLi)
							addEvent(oA, 'click', function(e) { 
								var index = getAll('a', oUl).indexOf(e.currentTarget) + 1; 
								e.preventDefault(); 
								_Custom.Banner.atualizar(index) 
							})
						}
						getElem('ct-flash').appendChild(oUl)
						addClass(getAll('li', oUl)[0], 'on')
						_Custom.Banner.timeout = window.setTimeout("_Custom.Banner.atualizar()", 4000); 
					}
				
				}
			})
			objAjax.update()	
		}

	},


	init: function() {
		var o
		
		// Adiciona classe 'script' no elemento 'body'
		addClass(getAll('body')[0], 'script')
		
		// Hover dos submenus
		_Custom.addFakeHover(getElementsByClass('submenu', 'li', 'ct-menu'))
		addEvent(getAll('a', 'ct-menu').filter(function(el) { return el.parentNode.className == 'submenu' }), 'click', function(e) { e.preventDefault() })
		
		// Texto da busca
		if (Page.getCurrent() != '/default.asp') {
			var sTextoDefault = 'Pesquisar no site'
			var o = getElementsByClass('text', 'input', 'ct-busca')
			addEventDict(o, {
				'focus': function(e) { 
					if (e.currentTarget.value == sTextoDefault) 
						e.currentTarget.value = '' 
					else
						e.currentTarget.select()
				},
				'blur': function(e) { 
					if (e.currentTarget.value == '') e.currentTarget.value = sTextoDefault 
				}
			})
		}
		
		// Abre links com classe 'externo' em nova página
		addEvent(getElementsByClass('externo', 'a'), 'click', Popup.event)		

		// Popups de imagem
		if ((o = getElem('imagens'))) addEvent(getAll('a', o), 'click', _Custom.PopupImagem)
		each(getElementsByClass('galeria'), function(el) { addEvent(getAll('a', el), 'click', _Custom.PopupImagem) })
		
		// Memorial
		if (Page.isCurrent('memoria-e-identidade-cic.asp')) addEvent(getAll('a', 'memorial'), 'click', Popup.eventFeatures('width=955, height=600'))
		
		// Locações
		if (Page.isCurrent('locacao-de-salas.asp')) addEvent(getAll('a', 'fs-sala'), 'click', Popup.eventFeatures('width=635, height=600, scrollbars=1,'))
		
		// Popup Locação
		if (Page.isCurrent('popup-locacoes.asp')) addEvent(getAll('a')[0], 'click', function(e) { e.preventDefault(); window.opener.location='/servicos/locacao-de-salas.asp'; window.close(); })

		// Mesas RA
		if (Page.isCurrent('reuniao-almoco.asp', 'sdgsdg.asp')) addEvent(getAll('a', 'mesas'), 'click', _Custom.PopupImagem)

		// Relatórios de Gestão
		if (Page.inCurrent('relatorios-de-gestao')) addEvent(getAll('a', 'gestao'), 'click', Popup.eventFeatures('width=785, height=600'))
		
		// Indique amigo
		if ((o = getElem('indique'))) addEvent(getAll('a', o), 'click', Popup.eventFeatures('width=505, height=515, scrollbars=1, top=50, left=' + parseInt((screen.width - 700) / 2)))


		// Ações para formulários que enviam dados
		if (Page.isCurrent('inscricao.asp'))
			addEvent(getAll('form', 'conteudo'), 'submit', Page.lock())
	
		// Popup da página inicial
		if (Page.getCurrent() == '/default.asp')
			if ((o = getElem('popup'))) {
				var oA = document.createElement('a')
				oA.setAttribute('href', '#')
				oA.setAttribute('title', 'Fechar')
				oA.className = 'fechar'
				oA.appendChild(document.createTextNode('Fechar'))
				o.appendChild(oA)
				addEvent(oA, 'click', function(e) { e.preventDefault(); addClass(e.currentTarget.parentNode, 'hide') })
				setTimeout(function(e) { addClass('popup', 'hide') }, 20000)

			}	
			
	}
	
}
