﻿$(function() {

	// Js Hide e Show (CSS)
	
	$('.js-hide').removeClass('js-hide').hide();
	$('.js-show').show();
	
	
	// Destaques

	var destaques = $('#destaques')
	var destaques_nav = $('#destaques-nav');

	$('div', destaques).hide();

	$('div.conteudo:first', destaques).show();

	$('li:first', destaques_nav).addClass('active');

	$('li a', destaques_nav).click(function(e) {
		e.preventDefault();
		e.stopPropagation();

		$('li', destaques_nav).removeClass('active');
		$(this).parent().addClass('active');

		$('div', destaques).hide();

		$($(this).attr('href'), destaques).show();

		return false;
	});


	// Imagens Destaques

	$('#destaques-imgs').cycle({
		fx: 'fade',
		speed:  'fast',
		timeout:  5000,
		before: function (currSlideElement, nextSlideElement, options, forwardFlag) {
			nextSlideElementSrc = $(nextSlideElement).attr('src');

			slideModulos = (nextSlideElementSrc.indexOf('modulos') > -1);
			slideFirebird = (nextSlideElementSrc.indexOf('firebird') > -1);
			slideSintegra = (nextSlideElementSrc.indexOf('sintegra') > -1);
			slideNfp = (nextSlideElementSrc.indexOf('nfp') > -1);

			if (slideModulos || slideFirebird || slideSintegra || slideNfp) {
				$('#destaques-imgs-container').addClass('no-bg');
			}
			else {
				$('#destaques-imgs-container').removeClass('no-bg');
			}
		}
	});


	// Campo de Pesquisa

	var search_input = $('#search-input');

	search_input.bind('focus', function() {
		var ithis = $(this);
		var value = ithis.val();

		if (value == 'Pesquisar...') {
			ithis.removeClass('quiet');
			ithis.val('');
		}
	});

	search_input.bind('blur', function() {
		var ithis = $(this);
		var value = ithis.val();

		if (value == '') {
			ithis.addClass('quiet');
			ithis.val('Pesquisar...');
		}
	});
	

	
	// Popup das Telas

	var telas = $('#telas .tela');
	var telas_full = $('#telas-full .tela');

	$('a', telas).click(openTelasPopUp);
	$('a', telas_full).click(openTelasPopUp);
	
	
	function openTelasPopUp(e) {
		e.preventDefault();
		e.stopPropagation();
		
		var id = $(this).attr('id');
		var url = '/pop_telas.asp?t=' + id;
		
		var width = 700;
		var height = 600;
		
		var left = (screen.width - width) / 2;
		var top = (screen.height - height) / 2;
		
		var params = 'width=' + width + ', height=' + height+ ', top=' + top + ', left=' + left + ', directories=0, location=0, menubar=0, resizable=0, scrollbars=1, status=0, toolbar=0';
		
		var w = (window.open(url, 'pop_telas', params, false)); 
		
		if (window.focus) { w.focus(); }
		
		return false;
	}
	
	
	// Formulários
	
	$('input[rel*=cnpj]').bind('keypress', function() {
		$(this).attr('maxlength', 18);
		Mascara(this, Cnpj);
	});
	
	$('input[rel*=cep]').bind('keypress', function() {
		$(this).attr('maxlength', 9);
		Mascara(this, CEP);
	});
	
	
	// Módulos Selecionados
	
	$('#modulos-selecionados > legend').css('cursor', 'pointer').bind('click', function() {
		var tis = $(this);		
		var div = tis.parent().find('div.content');
		var src = $('img', tis).attr('src');
		
		if (div.is(':visible')) {
			src = src.replace('chevron', 'chevron-expand');
			$('img', tis).attr('src', src);
			
			tis.parent().addClass('chevron');
			div.slideUp();
		}
		else {			
			src = src.replace('chevron-expand', 'chevron');
			$('img', tis).attr('src', src);
			
			tis.parent().removeClass('chevron');
			div.slideDown();
		}
	});
	
	$('#modulos-selecionados a[href*=remover]').bind('click', function() {
		if ( confirm("Tem certeza que deseja remover este mdulo?") ) {
			return this;
		}
		
		return false;
	});
	
	
	// Animate Scrolling
	
	function filterPath(string) {
		return string.replace(/^\//,'').replace(/(index|default).[a-zA-Z]{3,4}$/,'').replace(/\/$/,'');
	}
	
	var locationPath = filterPath(location.pathname);
	
	$('a[href*=#]').each(function() {
		if (!$(this).parent().parent().parent().is('#destaques-nav')) {
		
			var thisPath = filterPath(this.pathname) || locationPath;
			
			if ( locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/,'') ) {
				var $target = $(this.hash), target = this.hash;
				
				if (target) {
					var targetOffset = $target.offset().top;
					
					$(this).click(function(e) {
						e.preventDefault();
						e.stopPropagation();
						
						$('html').animate({scrollTop: targetOffset}, 750, function() {
							//location.hash = target;
						});
						
						return false;
					});
				}
			}
		}
	});
	
	
	// Float Box	
	$('.float-box .close').click(function() {
		$(this).parent().animate({height: 'toggle', opacity: 'toggle' }, 'normal');
	});
	

	// Toggles
	function ToggleVisible() {
		var tis = $(this);
		
		els = tis.attr('value').split(",");
		
		elem1 = els[0];
		elem2 = els[1];
		
		var elem1 = $('#' + elem1);
		var elem2 = $('#' + elem2);
		
		if (tis.is(':checked')) {
			elem1.fadeIn();
			elem2.fadeOut();
		}
		else {
			elem1.fadeOut();
			elem2.fadeIn();
		}
		
		elem1.css('display', 'inline-block');
	}
	
	$('input.toggle-visible').bind('change', ToggleVisible);
});