// -- Custom
var _Custom = {
	
	addFakeHover: function(els, classe) {
		classe = classe || 'over'
		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) }
			}
		)
	},
	
	bannerPopup: function() {
		var oPopup = getElementsByClass('banner-popup')[0]
		var oP = document.createElement('p')
		var oA = document.createElement('a')
		oA.setAttribute('id', 'fechar')
		oA.setAttribute('href', '#')
		oA.setAttribute('title', 'Fechar')
		oA.appendChild(document.createTextNode('Fechar'))
		oP.appendChild(oA)
		oPopup.appendChild(oP)
		addClass(oPopup, 'show', false)
		addEvent(oA, 'click', function(e) { remClass(oPopup, 'show'); e.preventDefault() })
	},
	
	imagemNoticia: function(src, width, height) {
		var scrollbars = 0, iWidth = width, iHeight = height
		iWidth = (width > 700) ? 700 : width
		iHeight = parseInt((iWidth / width) * height)
		if (iHeight > 550) { iWidth += 20; iHeight = 550; scrollbars = 1 }
		iWidth += 48; iHeight += 48
		return Popup.link(src, 'width=' + iWidth + ',height=' + iHeight + ',top=' + ((screen.height - iHeight) / 2) + ',left=' + ((screen.width - iWidth) / 2 - 50) + ',scrollbars=1,location=0,statusbar=0,menubar=0')
	},
	
	Labels: {
		focus: function(e) {
			var src = (isdef(e.currentTarget)) ? e.currentTarget : e; 
			addClass(getAll('span', src.parentNode)[0], 'hide'); 
			if (isdef(e.currentTarget)) src.select(); 
		},
		blur: function(e) {
			var src = (isdef(e.currentTarget)) ? e.currentTarget : e; 
			var oSpan = getAll('span', src.parentNode)[0]; 
			if (src.value == '') remClass(getAll('span', src.parentNode)[0], 'hide'); 
		}
	},
	
	EmpresasLogos: {
		
		Empresas: null,
		aMostradas: [],
		
		Buffer: {
			Empresas: [],
			adicionar: function(quantidade) {
				var oA, oImg, oEmpresa
				quantidade = quantidade || 1
				
				if (_Custom.EmpresasLogos.Buffer.Empresas.length == _Custom.EmpresasLogos.Empresas.length) return false
				
				for (i = 1; i <= quantidade; i++) {
					oEmpresa = _Custom.EmpresasLogos.Empresas[_Custom.EmpresasLogos.Buffer.Empresas.length]
					
					oA = getElementsByClass('empresa', 'a', 'ct-empresa')[0].cloneNode(true)
					oImg = getAll('img', oA)[0]
					
					sChaveAtual = oA.getAttribute('href').substring(oA.getAttribute('href').indexOf('=') + 1, oA.getAttribute('href').lastIndexOf('&'))
					sIdAtual = oA.getAttribute('href').substr(oA.getAttribute('href').lastIndexOf('&') + 1)
					
					sChaveNova = escape(oEmpresa.getElementsByTagName("razao-social")[0].childNodes[0].nodeValue)
					sIdNovo = 'idEmpresa=' + oEmpresa.getElementsByTagName("id")[0].childNodes[0].nodeValue
					
					oA.setAttribute('href', oA.getAttribute('href').replace(sChaveAtual, sChaveNova))
					oA.setAttribute('href', oA.getAttribute('href').replace(sIdAtual, sIdNovo))
					oImg.setAttribute('alt', escape(oEmpresa.getElementsByTagName("nome-fantasia")[0].childNodes[0].nodeValue))
					oImg.setAttribute('src', oImg.getAttribute('src').replace(sIdAtual, sIdNovo))
					
					_Custom.EmpresasLogos.Buffer.Empresas.push(oA)
				} 
			}
		}, 
		
		slide: function() {
			
			var oA = getElementsByClass('empresa', 'a', 'ct-empresa')[0]
			
			addClass(oA, 'hide', false)
			
			var index
			do {
				index	= parseInt(Math.random() * _Custom.EmpresasLogos.Buffer.Empresas.length)
			} while (_Custom.EmpresasLogos.aMostradas.contains(index))
			
			oA.parentNode.replaceChild(_Custom.EmpresasLogos.Buffer.Empresas[index], oA)
			
			_Custom.EmpresasLogos.aMostradas.push(index)
			if (_Custom.EmpresasLogos.aMostradas.length == _Custom.EmpresasLogos.Empresas.length) _Custom.EmpresasLogos.aMostradas = []
			_Custom.EmpresasLogos.Buffer.adicionar()
			
			remClass(oA, 'hide', false)
			
			$('#ct-empresa a').inner_slide('#ct-empresa', 'left', { duration: 'slow' });
			
			setTimeout("_Custom.EmpresasLogos.slide()", 3000);
			
		},
		
		iniciar: function() {
			
			if ((o = getElem('ct-empresa'))) {
				
				var objAjax = new AjaxObject('http://' + window.location.host + '/_include/asp/xml.asp?Acao=EmpresaLogo', function(responseStatus, responseXML) { 
					if (responseStatus == 200 || responseStatus == 304) { 
					
						_Custom.EmpresasLogos.Empresas = responseXML.getElementsByTagName('empresa')
						if (_Custom.EmpresasLogos.Empresas.length > 1) {
							each(_Custom.EmpresasLogos.Empresas, function(empresa, i) {
								var oImg = getAll('img', 'ct-empresa')[0]
								if (oImg.getAttribute('src').substring(oImg.getAttribute('src').indexOf('=') + 1, oImg.getAttribute('src').indexOf('&')) == empresa.getElementsByTagName('id')[0].childNodes[0].nodeValue)
									_Custom.EmpresasLogos.aMostradas.push(i)
							})
							_Custom.EmpresasLogos.Buffer.adicionar(3)
							setTimeout("_Custom.EmpresasLogos.slide()", 1000)
						}
						
					}
				})
				objAjax.update()	
				
			}
			
		}
		
	},
	
	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 oDiv = getElem('ct-flash'); 
			var oA = filter(getAll('a', oDiv), function(el) { return el.parentNode == oDiv; }); 
			var oImg = getAll('img', oDiv); 
			
			if (responseURL.length == 0) {
				if (oA.length > 0) oA[0].parentNode.removeChild(oA[0]); 
				oDiv.appendChild(oImg[0])
			} else {
				if (oA.length == 0) {
					oA = oDiv.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', oDiv), function(el, pos) { 
				if (pos + 1 == _Custom.Banner.indexCurrent) addClass(el, 'on'); else remClass(el, 'on'); 
			})
			
			_Custom.Banner.timeout = window.setTimeout("_Custom.Banner.atualizar()", 4000); 

		},
				
		iniciar: 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()	
		}

	},
	
	iniciar: function() {
		var o
		
		/* Adiciona classe "script" no elemento "body" */
		addClass(getAll('body')[0], 'script', false)

		/* Menus */
		if ((o = getElem('ct-menu-principal'))) {
			var oSubmenus = filter(getAll('li', o), function(el) { return el.parentNode == o && getAll('ul', el).length > 0 })
			each(oSubmenus, function(el) {
				addEvent(el, 'click', function(e) { 
					each(oSubmenus, function(elem) { remClass(elem, 'ativo') })
					if (!hasClass(el.parentNode, 'aberto')) addClass(el, 'ativo')
				})
				addEvent(getAll('a', el)[0], 'click', function(e) { e.preventDefault() })
			})
		}
		
		if ((o = getElem('ct-menu-secundario')))
			each(filter(getAll('li', o), function(el) { return el.parentNode == o && getAll('ul', el).length > 0 }), function(elem) {
				_Custom.addFakeHover(elem, 'hover')
				addEvent(getAll('a', elem)[0], 'click', function(e) { e.preventDefault() })
			})
			
		/* Label Painel de Empresas */
		if ((o = getElem('ct-painel'))) {
			addEventDict(getElementsByClass('text', 'input', 'ct-painel'), { 'focus': _Custom.Labels.focus, 'blur': _Custom.Labels.blur }); 
			addEventDict(getAll('span', 'f-painel'), { 
				'click': function(e) { _Custom.Labels.focus(getAll('input', e.currentTarget.parentNode)[0]); getAll('input', e.currentTarget.parentNode)[0].focus() }
			}); 
			each(getElementsByClass('text', 'input', 'ct-painel'), function(el) { _Custom.Labels.focus(el); _Custom.Labels.blur(el) });
		}

		/* Ações para formulários que enviam dados */
		if (Page.isCurrent('contato.asp'))
			addEvent(getAll('form', 'conteudo'), 'submit', Page.lock())
			
		/* Claro */
		if (Page.isCurrent('telefonia-movel-3g/default.asp')) 
			addEvent(getAll('a', 'modelos'), 'click', Popup.eventFeatures('width=400, height=400, top=' + ((screen.height - 400) / 2) + ', left=' + ((screen.width - 400) / 2) + ', scrollbars=1'))
		
		/* Abre links com classe 'externo' em nova página */
		addEvent(getElementsByClass('externo', 'a'), 'click', Popup.event)
		
	}
	
}

/* Banner e Logotipos */
if (!Page.inCurrent('webmaillogin') && !Page.inCurrent('popup')) 
	addLoadEvent(function() { 
		_Custom.EmpresasLogos.iniciar(); 
		_Custom.Banner.iniciar() 
	})