// -- Custom
var _Custom = {

  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 iWidth = 740, iHeight = 580, 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');

  },

  Menu: {

    Menus: null,
		
		Imagens: { Aberto: null, Fechado: null },
		
		fechar: function(el) {
			getElementsByClass('aberto', 'li', el).each(function(elem) { 
				remClass(elem, 'principal'); 
				remClass(elem, 'aberto'); 
				addClass(elem, 'fechado'); 
			}); 
			getAll('img', el).each(function(elem) { 
				elem.parentNode.replaceChild(_Custom.Menu.Imagens.Fechado.cloneNode(false), elem); 
			}); 
		}, 
		
    toggle: function(e) {
      var src = e.currentTarget; 
      e.preventDefault(); 

      if (src.tagName.toLowerCase() == 'a') {
				if (!hasClass(src.parentNode, 'aberto')) _Custom.Menu.fechar(src.parentNode.parentNode); 
				swapClass(src.parentNode, 'principal');
			}
      swapClass(src.parentNode, 'fechado');
      swapClass(src.parentNode, 'aberto');
			
      if (hasClass(src.parentNode, 'aberto')) 
				src.replaceChild(_Custom.Menu.Imagens.Aberto.cloneNode(false), getAll('img', src)[0]); 
			else {
				src.replaceChild(_Custom.Menu.Imagens.Fechado.cloneNode(false), getAll('img', src)[0]); 
				_Custom.Menu.fechar(src.parentNode); 
			} 

    },

    iniciar: function() {
			var that = this; 
			if (isNull(getElem('ct-menu'))) return false; 
			this.Menus = getElementsByClass('principal', 'a', 'ct-menu').merge(filter(getAll('span', 'ct-menu'), function(el) { return el.parentNode.tagName.toLowerCase() == 'li'; })); 
			this.Imagens.Fechado = document.createElement('img'); 
			this.Imagens.Fechado.setAttribute('src', '/_images/menu_seta_fechado.gif');
			this.Imagens.Fechado.setAttribute('alt', '+');
			this.Imagens.Aberto = this.Imagens.Fechado.cloneNode(false)
			this.Imagens.Aberto.setAttribute('src', '/_images/menu_seta_aberto.gif');
			this.Imagens.Aberto.setAttribute('alt', '-');
			this.Imagens.Aberto.className = 'aberto'
			each(this.Menus, function(el) { el.appendChild(that.Imagens.Fechado.cloneNode(false)) }); 
			addEvent(this.Menus, 'click', this.toggle); 
		}

  },

	Planos: {
	
		reset: function() {
			$('#ct-planos div li').each(function() {
				if ($(this).prev('li').length > 0) 
					$(this).css('left', (parseInt($(this).prev('li').css('left').replace(/px/, '')) + 222) + 'px'); 
			});
		}, 
		
		toggle: function(e) {
			if ($(this).parent().hasClass('show')) return false; 
			$('#ct-planos .show').removeClass('show'); 
			$('#ct-planos div li:first').css('left', 0); 
			_Custom.Planos.reset();
			$(this).parent().addClass('show'); 
			$(this).parent().parent().find('ul').addClass('show'); 
			$('#ct-planos div a.navegacao').remove(); 
			_Custom.Planos.toggleSetas();
			return false;
		}, 
	
		toggleSetas: function() {
			if ($('#ct-planos ul.show li:first').position().left >= 0) 
				$('#ct-planos ul.show').parent().find('a.anterior').remove(); 
			else if ($('#ct-planos ul.show').parent().find('a.anterior').length == 0)
				$('#ct-planos ul.show').parent().append('<a class="navegacao anterior" href="#anterior">Anterior</a>'); 
			
			if ($('#ct-planos ul.show li:last').position().left == ($('#ct-planos ul.show li').length < 4 ? $('#ct-planos ul.show li').length - 1 : 3) * 222) 
				$('#ct-planos ul.show').parent().find('a.proximo').remove(); 
			else if ($('#ct-planos ul.show').parent().find('a.proximo').length == 0)
				$('#ct-planos ul.show').parent().append('<a class="navegacao proximo" href="#anterior">Próximo</a>'); 
		}, 
		
		iniciar: function() {
			$('#ct-planos h3 a').click(this.toggle);
			$('#ct-planos div').click(function(e) {
				var direcao, src = $(e.target); 
				if (src.is('a') && src.hasClass('navegacao')) {
					direcao = ($(src).hasClass('proximo')) ? -1 : 1; 
					$(src).parent().find('li').each(function() {
						$(this).animate({ left: ($(this).position().left + (222 * direcao)) + 'px' }, 'normal', '', _Custom.Planos.toggleSetas); 
					}); 
					return false; 
				} 
			});
			_Custom.Planos.reset();
		}
	
	}, 
	
	imprimir: function() {
		var oInput, oP

		each(getElementsByClass('ct-impressao'), function(elConteiner) {
															
			oInput = document.createElement('input')
			oInput.setAttribute('type', 'button')
			oInput.setAttribute('value', 'Imprimir')
			oInput.setAttribute('alt', 'Imprimir')
			oInput.setAttribute('id', 'fechar')
			
			oP = document.createElement('p')
			oP.className = 'submit'
			
			oP.appendChild(oInput)
			if (elConteiner.hasChildNodes)
				elConteiner.insertBefore(oP, elConteiner.firstChild)
			else
				elConteiner.appendChild(oP)
				
			each(getElementsByClass('legenda', 'p', elConteiner), function(el) { el.parentNode.removeChild(el) })
			
			addEvent(oInput, 'click', function() { window.print() })
			
		})
		
	},
	
	toggle: function() {
		$('.toggle-botao').bind('click', function(e) {
			$('.toggle-conteudo').eq($('.toggle-botao').index(this)).toggle('fast'); 
			return false; 
		});
		$('.toggle-botao').wrapInner('<a href="#conteudo"></a>');
	}, 
	
  iniciar: function() {
    var o; 
		
    // Acrescenta as classes "script" e "jquery" no elemento "body"
    $('body').addClass('jquery');
    addClass(getAll('body')[0], 'script');
		
		// Cache das imagens dos planos
		$.cacheImage(['/_images/superior/planos/poa/empresarial/diferencial-flex.jpg', '/_images/superior/planos/poa/empresarial/executivo-4000.jpg', '/_images/superior/planos/poa/empresarial/master.jpg', '/_images/superior/planos/poa/empresarial/padrao-4000.jpg', '/_images/superior/planos/poa/individual/diferencial-flex.jpg', '/_images/superior/planos/serra/empresarial/diferencial-flex.jpg', '/_images/superior/planos/serra/empresarial/exclusivo.jpg', '/_images/superior/planos/serra/empresarial/executivo-2000.jpg', '/_images/superior/planos/serra/empresarial/padrao-2000.jpg', '/_images/superior/planos/serra/empresarial/personale.jpg', '/_images/superior/planos/serra/individual/diferencial-flex.jpg', '/_images/superior/planos/serra/individual/exclusivo.jpg', '/_images/superior/planos/serra/individual/executivo-2000.jpg', '/_images/superior/planos/serra/individual/padrao-2000.jpg', '/_images/superior/planos/serra/individual/plano-torcedor.jpg'])

    // Menu principal
		this.Menu.iniciar(); 
		
		// Planos
		this.Planos.iniciar(); 

		// Ações para formulários que enviam dados
		if ((Page.inCurrent('vestibular/') && getElem('resumo')) || Page.isCurrent('alteracao-cadastral.asp', 'cadastre-seu-curriculo.asp', 'fale-conosco.asp', 'seja-um-prestador-fatima.asp', 'planos-de-saude/declaracao.asp', 'pessoa-protegida/cadastro.asp'))
			Page.lock(getAll('form', 'conteudo'))
		
		if (Page.isCurrent('juventude/cadastro.asp', 'sercaxias/cadastro.asp'))
			Page.lock(getAll('form'))
		
		// Popups de imagem
    $('.popup-imagem').each(function() { 
			var els = ($(this).is('a')) ? $(this) : $(this).find('a'); 
			els.click(_Custom.PopupImagem); 
		}); 

		// Abre hotsite do Fátima Life em popup
    $('.hotsite-fatima-life').each(function() { 
			var els = ($(this).is('a')) ? $(this) : $(this).find('a'); 
			els.click(Popup.eventFeatures('width=800,height=620')); 
		}); 
	
		// Toggle Rede Credenciada
		$('.rede-credenciada dt').wrapInner('<a href=""#conteudo""></a>');
		$('.rede-credenciada dt').click(function() {
			$(this).next('dd').toggle(); 
			return false; 
		});
		
		// Toggle Geral 
		this.toggle(); 
		
		// Dica do Perfil
		//$('#ct-perfil li a').mouseover(function() { $(this).parent().find('p').fadeIn('normal') });
		//$('#ct-perfil li a').mouseout(function() { $(this).parent().find('p').fadeOut('normal') });
		
		
		// Scroll da ilustração dos planos
		if (Page.inCurrent('produtos-e-servicos') && Page.inCurrent('planos-de-saude')) $('#ct-extras').stickyfloat({ duration: 400 });

		// Cria botão Imprimir
		_Custom.imprimir()
		
    	// Abre links com classe 'externo' em nova página
   		 $('a.externo').click(Popup.event); 

		// 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') })
			}

		// Popup da página inicial
		// if (Page.getCurrent() == '/default.asp') Popup.raw('popup_curriculos.asp', '_blank', 'width=500, height=296, top=0, left=' + ((screen.width - 800) / 2) + ',scrollbars=1'); 
  
  }

}