(function($)
{
    
    /* Carrossel da home */
    var homeCarroussel  = fess.carroussel.init
    (
        {
            next        : '#home-carroussel a.next',
            previous    : '#home-carroussel a.previous',
            slidable    : '#home-carroussel div.mask ul',
            children    : '#home-carroussel div.mask li',
            speed       : 'slow',
            walkBy      : 3
        }
    );
    
    /* Galeria: carrossel de fotos */
    var photosCarroussel = fess.carroussel.init
    (
        {
            next        : '#photos div.gallery a.next',
            previous    : '#photos div.gallery a.previous',
            slidable    : '#photos div.mask ul',
            children    : '#photos div.mask li',
            speed       : 'slow',
            walkBy      : 2
        }
    );
    
    /* Galeria: no clique, seleciona a foto como ativa */
    $('#photos div.mask a').bind
    (
        'click',
        function(e)
        {
            $('#photos div.current').css({'background-image': 'url('+this.getAttribute('data-image-medium')+')'}).find('a').attr('href', this.getAttribute('href'));
            
            e.preventDefault();
            e.stopPropagation();
        }
    );
    
    $('#photos .current a').bind
    (
        'click',
        function(e)
        {
             
            var config = { page: this.getAttribute('href')};
            
            if(window['tb_show'])tb_show('', config.page + '?TB_iframe=false&modal=true');
            
            e.preventDefault();
            e.stopPropagation();
            
            return true
        }
    );
    
    
    
    /* Trabalhe conosco: formulário */
    $('#content form.trabalhe-conosco').bind
    (
        'submit',
        function(e)
        {
            try
            {                        
                if(fess.utils.trim(this.elements['nome'].value) == '')
                    throw {message: 'Por favor, informe o seu nome.', source: this.elements['nome'], action: 'clear'};
                
                if(this.elements['dataNascimento'].value == '' || this.elements['mesNascimento'].value == '' || this.elements['anoNascimento'].value == '')
                    throw {message: 'Por favor, informe sua data de nascimento corretamente.', source: this.elements['dataNascimento']};
                else if(!fess.utils.verify.date(this.elements['dataNascimento'].value + '/' + this.elements['mesNascimento'].value + '/' + this.elements['anoNascimento'].value))
                    throw {message: 'A data de nascimento informada é inválida.', source: this.elements['dataNascimento']};
                
                if(this.elements['estadoCivil'].value == '')
                    throw {message: 'Por favor, informe o seu estado civil.', source: this.elements['estadoCivil']};
                
                if(fess.utils.trim(this.elements['email'].value) == '')
                    throw {message: 'Por favor, informe o seu endereço de e-mail.', source: this.elements['email']};
                else if(!fess.utils.verify.email(this.elements['email'].value))
                    throw {message: 'O endereço de e-mail informado é inválido.', source: this.elements['email'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['cpf'].value) == '')
                    throw {message: 'Por favor, informe o seu CPF.', source: this.elements['cpf']};
                else if(!fess.utils.verify.cpf(this.elements['cpf'].value))
                    throw {message: 'O número de CPF informado é inválido.', source: this.elements['cpf'], action: 'clear'};
                
                if(this.elements['formacao'].value == '')
                    throw {message: 'Por favor, informe sua formação.', source: this.elements['formacao']};
                
                if(fess.utils.trim(this.elements['endereco'].value) == '')
                    throw {message: 'Por favor, informe o seu endereço.', source: this.elements['endereco']};
                
                if(fess.utils.trim(this.elements['numero'].value) == '')
                    throw {message: 'Por favor, informe o número do seu endereço.', source: this.elements['numero']};
                
                if(fess.utils.trim(this.elements['bairro'].value) == '')
                    throw {message: 'Por favor, informe o seu bairro.', source: this.elements['bairro']};
                
                if(fess.utils.trim(this.elements['cep'].value) == '')
                    throw {message: 'Por favor, informe o seu CEP.', source: this.elements['cep']};
                else if(!fess.utils.verify.numeric(this.elements['cep'].value) || this.elements['cep'].value.length != this.elements['cep'].getAttribute('maxlength'))
                    throw {message: 'O número de CEP informado é inválido.', source: this.elements['cep'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['cidade'].value) == '')
                    throw {message: 'Por favor, informe a sua cidade.', source: this.elements['cidade']};
                
                if(fess.utils.trim(this.elements['estado'].value) == '')
                    throw {message: 'Por favor, informe o seu estado.', source: this.elements['estado']};
                
                if(fess.utils.trim(this.elements['dddtelefoneResidencial'].value) == '')
                    throw {message: 'Por favor, informe o prefixo do seu telefone residencial.', source: this.elements['dddtelefoneResidencial']};                        
                else if(!fess.utils.verify.numeric(this.elements['dddtelefoneResidencial'].value))
                    throw {message: 'O prefixo do seu telefone residencial é inválido.', source: this.elements['dddtelefoneResidencial'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['telefoneResidencial'].value) == '')
                    throw {message: 'Por favor, informe o número do seu telefone residencial.', source: this.elements['telefoneResidencial']};                        
                else if(!fess.utils.verify.numeric(this.elements['telefoneResidencial'].value))
                    throw {message: 'O número do seu telefone residencial é inválido (digite somente números).', source: this.elements['dddtelefoneResidencial'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['areaAtuacao'].value) == '')
                    throw {message: 'Por favor, informe sua área de atuação.', source: this.elements['areaAtuacao']};
                
                if(fess.utils.trim(this.elements['curriculo'].value) == '')
                    throw {message: 'Por favor, selecione o seu currículo.', source: this.elements['curriculo']};
            }
            catch(error)
            {
                alert(error.message);
                if('source' in error)
                {
                    error.source.focus();
                    if((error.action || '') == 'clear')error.source.value = '';
                }
                
                e.preventDefault();
                e.stopPropagation();
            }
        }
    ).find(':text:eq(0)').focus();
    
    
    
    
    /* Contato: formulário */
    $('#content form.contato').bind
    (
        'submit',
        function(e)
        {
            try
            {                        
                if(fess.utils.trim(this.elements['nome'].value) == '')
                    throw {message: 'Por favor, informe o seu nome.', source: this.elements['nome'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['prefixoResidencial'].value) == '')
                    throw {message: 'Por favor, informe o prefixo do seu telefone residencial.', source: this.elements['prefixoResidencial']};                        
                else if(!fess.utils.verify.numeric(this.elements['prefixoResidencial'].value))
                    throw {message: 'O prefixo do seu telefone residencial é inválido.', source: this.elements['prefixoResidencial'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['numeroResidencial'].value) == '')
                    throw {message: 'Por favor, informe o número do seu telefone residencial.', source: this.elements['numeroResidencial']};                        
                else if(!fess.utils.verify.numeric(this.elements['numeroResidencial'].value))
                    throw {message: 'O número do seu telefone residencial é inválido (digite somente números).', source: this.elements['prefixoResidencial'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['email'].value) == '')
                    throw {message: 'Por favor, informe o seu endereço de e-mail.', source: this.elements['email']};
                else if(!fess.utils.verify.email(this.elements['email'].value))
                    throw {message: 'O endereço de e-mail informado é inválido.', source: this.elements['email'], action: 'clear'};

                if(fess.utils.trim(this.elements['mensagem'].value) == '')
                    throw {message: 'Por favor, digite a sua mensagem.', source: this.elements['mensagem'], action: 'clear'};
                    
                $('button.submit', this).attr('disabled', 'disabled');
            }
            catch(error)
            {
                alert(error.message);
                if('source' in error)
                {
                    error.source.focus();
                    if((error.action || '') == 'clear')error.source.value = '';
                }
                
                e.preventDefault();
                e.stopPropagation();
            }
        }
    ).find(':text:eq(0)').focus();
    
    /* Perguntas frequentes: accordion */
    $('#content ul.perguntas-frequentes h2 a, #content ul.tratamento h2 a').bind
    (
        'click',
        function(e)
        {
            var target = this, openAction = function()
            {
                $(this).parents('li').removeClass('active');
                var item = $(target).parents('li');
                item.find('p, img.content').slideDown('medium', function(){item.addClass('active');return true;});
            }, hidable = $(this).parents('ul').find('li.active p, li.active img.content');
            
            if($(this).parents('li.active').length != 0)return false;
            
            if(hidable.slideUp('medium', openAction).length == 0)openAction.apply([this]);
            
            e.preventDefault();
            e.stopPropagation();
            return true;
        }
    );
    
    /* Leads */
    $('#leads a').bind
    (
        'click',
        function(e)
        {
            switch(this.parentNode.className)
            {
                /* Agende uma consulta */
                case 'agende':var config = {page: window['interacoesSiteRoot'] + 'agende_visita.php?localidade=Engravida&' + ((new Date()).getTime()), width: 570, height: 510, openOn: 'thickbox'};break;
                /* Biomédicas */
                case 'biomedicos':var config = {page: window['interacoesSiteRoot'] + 'biomedicos_online.php?localidade=Engravida&' + ((new Date()).getTime()), width: 570, height: 510, openOn: 'window'};break;
                /* Ligamos pra você */
                case 'ligamos':var config = {page: window['interacoesSiteRoot'] + 'ligamos_voce.php?localidade=Engravida&' + ((new Date()).getTime()), width: 570, height: 510, openOn: 'thickbox'};break;
            }
            
            if(window['tb_show'] && config.openOn == 'thickbox')
                tb_show('', config.page + 'TB_iframe=true&modal=false&width=' + config.width + '&height=' + config.height);
            else
                window.open(config.page, 'chatInteracaoWindow', 'width=' + config.width + ', height=' + config.width + ', toolbar=no');
            
            e.preventDefault();
            e.stopPropagation();
        }
    );
    
    /* Leads: Agende uma visita - validação */
    $('#agende-visita').bind
    (
        'submit',
        function(e)
        {
            try
            {                        
                if(fess.utils.trim(this.elements['nome'].value) == '')
                    throw {message: 'Por favor, informe o seu nome.', source: this.elements['nome'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['email'].value) == '')
                    throw {message: 'Por favor, informe o seu endereço de e-mail.', source: this.elements['email']};
                else if(!fess.utils.verify.email(this.elements['email'].value))
                    throw {message: 'O endereço de e-mail informado é inválido.', source: this.elements['email'], action: 'clear'};
                
                if(this.elements['sugestaoData'].value == '' || this.elements['sugestaoMes'].value == '' || this.elements['sugestaoAno'].value == '')
                    throw {message: 'Por favor, informe a sugestão de data corretamente.', source: this.elements['sugestaoData']};
                else if(!fess.utils.verify.date(this.elements['sugestaoData'].value + '/' + this.elements['sugestaoMes'].value + '/' + this.elements['sugestaoAno'].value))
                    throw {message: 'A data de agendamento informada é inválida.', source: this.elements['sugestaoData']};
                
                if(fess.utils.trim(this.elements['telefonePrefixo'].value) == '')
                    throw {message: 'Por favor, informe o prefixo do seu telefone.', source: this.elements['telefonePrefixo']};                        
                else if(!fess.utils.verify.numeric(this.elements['telefonePrefixo'].value))
                    throw {message: 'O prefixo do seu telefone é inválido.', source: this.elements['telefonePrefixo'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['numeroPrefixo'].value) == '')
                    throw {message: 'Por favor, informe o número do seu telefone.', source: this.elements['numeroPrefixo']};                        
                else if(!fess.utils.verify.numeric(this.elements['numeroPrefixo'].value))
                    throw {message: 'O número do seu telefone é inválido (digite somente números).', source: this.elements['numeroPrefixo'], action: 'clear'};

                if(fess.utils.trim(this.elements['sugestaoHorario'].value) == '')
                    throw {message: 'Por favor, selecione um horário para a visita.', source: this.elements['sugestaoHorario'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['convenio'].value) == '')
                    throw {message: 'Por favor, selecione um convênio.', source: this.elements['convenio'], action: 'clear'};
                    
                $('button.submit', this).attr('disabled', 'disabled');
            }
            catch(error)
            {
                alert(error.message);
                if('source' in error)
                {
                    error.source.focus();
                    if((error.action || '') == 'clear')error.source.value = '';
                }
                
                e.preventDefault();
                e.stopPropagation();
            }
            
            return true;
        }
    ).find(':text:eq(0)').focus();
    
    /* Leads: biomédicos online - validação */
    $('#biomedicos-online').bind
    (
        'submit',
        function(e)
        {
            try
            {                        
                if(fess.utils.trim(this.elements['nome'].value) == '')
                    throw {message: 'Por favor, informe o seu nome.', source: this.elements['nome'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['email'].value) == '')
                    throw {message: 'Por favor, informe o seu endereço de e-mail.', source: this.elements['email']};
                else if(!fess.utils.verify.email(this.elements['email'].value))
                    throw {message: 'O endereço de e-mail informado é inválido.', source: this.elements['email'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['telefonePrefixo'].value) == '')
                    throw {message: 'Por favor, informe o prefixo do seu telefone.', source: this.elements['telefonePrefixo']};                        
                else if(!fess.utils.verify.numeric(this.elements['telefonePrefixo'].value))
                    throw {message: 'O prefixo do seu telefone é inválido.', source: this.elements['telefonePrefixo'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['numeroPrefixo'].value) == '')
                    throw {message: 'Por favor, informe o número do seu telefone.', source: this.elements['numeroPrefixo']};                        
                else if(!fess.utils.verify.numeric(this.elements['numeroPrefixo'].value))
                    throw {message: 'O número do seu telefone é inválido (digite somente números).', source: this.elements['numeroPrefixo'], action: 'clear'};

                if(fess.utils.trim(this.elements['assunto'].value) == '')
                    throw {message: 'Por favor, selecione um assunto.', source: this.elements['assunto'], action: 'clear'};
                    
                $('button.submit', this).attr('disabled', 'disabled');
                
            }
            catch(error)
            {
                alert(error.message);
                if('source' in error)
                {
                    error.source.focus();
                    if((error.action || '') == 'clear')error.source.value = '';
                }
                
                e.preventDefault();
                e.stopPropagation();
            }
            
            return true;
        }
    ).find(':text:eq(0)').focus();
    
    /* Leads: ligamos pra você - validação */
    $('#ligamos-voce').bind
    (
        'submit',
        function(e)
        {
            try
            {                        
                if(fess.utils.trim(this.elements['nome'].value) == '')
                    throw {message: 'Por favor, informe o seu nome.', source: this.elements['nome'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['email'].value) == '')
                    throw {message: 'Por favor, informe o seu endereço de e-mail.', source: this.elements['email']};
                else if(!fess.utils.verify.email(this.elements['email'].value))
                    throw {message: 'O endereço de e-mail informado é inválido.', source: this.elements['email'], action: 'clear'};

                if(fess.utils.trim(this.elements['periodo'].value) == '')
                    throw {message: 'Por favor, selecione um período.', source: this.elements['periodo'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['convenio'].value) == '')
                    throw {message: 'Por favor, selecione um convênio.', source: this.elements['convenio'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['telefonePrefixo'].value) == '')
                    throw {message: 'Por favor, informe o prefixo do seu telefone.', source: this.elements['telefonePrefixo']};                        
                else if(!fess.utils.verify.numeric(this.elements['telefonePrefixo'].value))
                    throw {message: 'O prefixo do seu telefone é inválido.', source: this.elements['telefonePrefixo'], action: 'clear'};
                
                if(fess.utils.trim(this.elements['numeroPrefixo'].value) == '')
                    throw {message: 'Por favor, informe o número do seu telefone.', source: this.elements['numeroPrefixo']};                        
                else if(!fess.utils.verify.numeric(this.elements['numeroPrefixo'].value))
                    throw {message: 'O número do seu telefone é inválido (digite somente números).', source: this.elements['numeroPrefixo'], action: 'clear'};
                    
                $('button.submit', this).attr('disabled', 'disabled');
            }
            catch(error)
            {
                alert(error.message);
                if('source' in error)
                {
                    error.source.focus();
                    if((error.action || '') == 'clear')error.source.value = '';
                }
                
                e.preventDefault();
                e.stopPropagation();
            }
            
            return true;
        }
    ).find(':text:eq(0)').focus();
    
    /* Compartilhar no Facebook */
    $('#content .tratamento .share a.facebook, #content .perguntas-frequentes .share a.facebook').bind
    (
        'click',
        function(e)
        {
            e.preventDefault();
            e.stopPropagation();
                        
            var text = 'Engravida - Clínica de Reprodução Humana: '+$(this).parent().data('title');
            window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent($(this).attr('href')) + '&t=' + encodeURI(text), '_blank', 'width=500px, height=500px');
            
            return true;
        }
    );
    
    /* Compartilhar no Twitter */
    $('#content .tratamento .share a.twitter, #content .perguntas-frequentes .share a.twitter').bind
    (
        'click',
        function(e)
        {
            e.preventDefault();
            e.stopPropagation();
                        
            var text = $(this).parent().data('title');
            text = (text.length > 70) ? text.substr(0,70) + '...' : text;
            text = 'Engravida - Clínica de Reprodução Humana: ' + text;
            
            window.open('http://twitter.com/share?url=' + encodeURIComponent($(this).attr('href')) + '&text='+ encodeURI(text), '_blank', 'width=500px, height=500px');
            
            return true;
        }
    );
    /* Link do mapa de Localização */
    $('#content .info a.map').bind
    (
        'click',
        function(e)
        {
            var config =
            {
                //page: 'http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=pt-BR&amp;geocode=&amp;q=Avenida+Ang%C3%A9lica,+916,+S%C3%A3o+Paulo,+Brasil&amp;aq=0&amp;sll=37.0625,-95.677068&amp;sspn=54.753001,135.263672&amp;ie=UTF8&amp;hq=&amp;hnear=Av.+Ang%C3%A9lica,+916+-+Santa+Cec%C3%ADlia,+S%C3%A3o+Paulo,+01228-000,+Brasil&amp;ll=-23.529602,-46.655073&amp;spn=0.038954,0.057421&amp;z=14&amp;iwloc=A&amp;output=embed',
                page: 'mapa_localizacao.php',
                width: 670,
                height: 495
            }
            if(window['tb_show'])tb_show('', config.page + 'TB_iframe=true&KeepThis=true&width=' + config.width + '&height=' + config.height);
            
            e.preventDefault();
            e.stopPropagation();
            
            return true;
        }
    );
})(jQuery);
