/**
 * +project.basics - "Classe" com os mÃ©todos padrÃµes do framework
 * +INIT jQuery - eventos de carregamento da pÃ¡gina da jQuery
 */
var project = {};

/**
 * @classDescription "Classe" padrÃ£o do framework
 */
project.basics = (function($) {
	
    /**
     * public
     */
    return {
		
        /**
         * @method translate
         * @param {String} index 
         * @return {String} expressÃ£o na lÃ­ngua utilizada na pÃ¡gina
         */
        translate : function(index) {
            var words = new Array();
			
            if($('html[lang=pt-br]')[0]) {
                words['newWindow'] = 'Este link abre uma nova janela';
            }
            return (words[index]) ? words[index] : false;
        },
		
		
        /**
         * @method links aÃ§Ãµes conforme atributos e eventos de alguns links
         */
        links : function() {
            $('a[target="_blank"]').each(
                function(elm) {
                    $(this).attr("title", $(this).attr('title') +  ' ( '+ project.basics.translate('newWindow') +' ) ');
                }
                ); 
        }		
    }
})(jQuery);


/**
 * @classDescription "Classe" referente a interface do projeto
 */
project.userInterface = (function($) {
    /**
     * @method loginTab - controla a aba de login da home
     * @private
     */
    function sideTab(maxWidth) {
        var sideTab = $('.side-tab'),
        form  = sideTab.find('form'); 
	
        sideTab.addClass('closed').find('h2 a').click(function(){
            if(sideTab.hasClass('closed')) {
                form.animate({
                    maxWidth: maxWidth
                }, function(){
                    sideTab.removeClass('closed');
                });
            } else {
                form.animate({
                    maxWidth: 0
                }, function(){
                    sideTab.addClass('closed');
                });
            }
			
            return false;
        });
    }
	
	
    /**
     * public
     */
    return {
        /**
         * @method textReplacement Ã© referente a substituiÃ§Ã£o de uma fonte de sistema utilizando alguma tÃ©cnica js
         */
        textReplacement: function() {
            // ajusta o tamanho da fonte de acordo com a nova
            $('#menu a, #wrapper > footer h2').css('font-size', '22px');
            $('#content h2').css('font-size', '28px');
			
            // executa o cufon
            Cufon.replace('#menu a, #wrapper > footer h2, #content h2, .fontEspecial',{
                hover: true
            });
        },
	
	
        /**
         * @method home
         */
        home: function() {
           
            // abre/fecha aba de login
            sideTab(251);
            if($('#clients ul')[0] != undefined){		
                $('#clients ul').cycle({ 
                    fx: 'scrollLeft' 
                });
            }
        },
			
        hotsiteAdministrationHome: function() {
            
            
            
            // abre/fecha aba contratar
            sideTab(601);
                
            if($('#overview nav')[0] != undefined){	
                $('#overview nav').jcarousel({
                    wrap: 'last',
                    visible: 3	
                });            
            }
        }
    }
})(jQuery);


/**
  * INIT jQuery
  */
jQuery(document).ready(
    function() {
        // adiciona a classe js se o browser do usuÃ¡rio possuir suporte a javascript
        $('html').addClass('js');
        
        // habilita o atributo html5 placeholder para input e textarea nos browsers que nÃ£o o suportam
        $('input, textarea').placeholder();
    	
        // aÃ§Ãµes conforme atributos e eventos de alguns links
        project.basics.links();
		
        // substitui a fonte de sistema utilizada por outro tipo de fonte
        project.userInterface.textReplacement();
		
        // PÃ¡ginas - abaixo as aÃ§Ãµes relativas as pÃ¡ginas do projeto

        if($('.default #home')[0]!=undefined)
            project.userInterface.home();
		
        if($('.hotsite #home')[0]!=undefined) {
            project.userInterface.hotsiteAdministrationHome();
        }
			
    }
    );
        
        
// JavaScript Document
var Common = {
        
    'openTrainamento': function() {

        var formId = "formTrainament";
        var title = "APRESENTAÇÃO";
        
        $('body').append("<div id='"+formId+"' title='"+title+"' style='display:none'>CARREGANDO...</div>");
        $('#'+formId).dialog({
            autoOpen: false,
            height: 550,
            width: 820,
            modal: true,
            close: function() {
                $('#'+formId).dialog("destroy");
                $('#'+formId).remove();
                w = null;
            }
        });
        var w = $("#"+formId).dialog("open");            
        $('#'+formId).load('load-treinamento.php');

    }
}



//-----------------------------------------------------------------------------//
// VALIDA E-MAIL
//-----------------------------------------------------------------------------//

function validaEmail(email)
{
    if(email == "")
    {
        alert('Você precisa digitar um endereço de e-mail! ');
        return false;
    }

    if(email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1)
    {
        alert('Você precisa digitar um endereço de e-mail valido! ');
        return false;
    }

    return true;
}

function formataMoeda(campo,evt) {
   
    //para evitar caracteres alfas.
    if(((evt.keyCode < 96) || (evt.keyCode > 105)) && ((evt.keyCode < 48) || (evt.keyCode > 57)) ){
        campo.value = campo.value.replace(String.fromCharCode(evt.keyCode).toLowerCase(),"");
    }
    str = campo.value;

    while(str.search(",") != -1)
        str = str.replace(",","");
   
    var i = 0;

    while(i< str.length){
        if(str.substr(i,1) == ".")
            str = str.replace(".","");
        i++;
    }

    part1 = str.substr(0,str.length - 2);
    while(part1.search(" ") != -1)
        part1 = part1.replace(" ","");

    part2 = str.substr(str.length - 2,2);
    res = "";
    i = part1.length;
    sob = i % 3;
    if((sob != 0) && (i > 2))
        res = part1.substr(0,sob) + ".";
    else
        res = part1.substr(0,sob);
    j = 1;
    part1 = part1.substr(sob);
    i = 0;
    while(i < part1.length){
        if(j == 3){
            if(i + 1 == part1.length)
                res = res + part1.substr(i-2,3);
            else res = res + part1.substr(i-2,3) + ".";
        }
        i++;
        j = j<3?j+1:1;
    }
    campo.value = res + "," + part2;
	   
    if (campo.value == ',')
        campo.value = '';

}

//Formata moeda
// ex: number_format(result,2, ',', '.');

function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

//---------------------------------------------------------------//
// MASK
//---------------------------------------------------------------//

function MASK(form) {
	
    $(form + ' input[mask=telefone]').each( function() {
		
        $(this).mask("(99) 9999-9999");
	
    });

    $(form + ' input[mask=cep]').each( function() {
		
        $(this).mask("99999-999");
	
    });

    $(form + ' input[mask=cpf]').each( function() {
		
        $(this).mask("999.999.999-99");
	
    });

    $(form + ' input[mask=cnpj]').each( function() {
		
        $(this).mask("99.999.999/9999-99");
	
    });

    $(form + ' input[mask=data]').each( function() {
		
        $(this).mask("99/99/9999");
	
    });

    $(form + ' input[mask=moeda]').each( function() {
		
        $(this).keyup( function(event) {
			
            formataMoeda(this,event);
								
        });

    });
	
    $(form + ' input[mask=dia_mes]').each( function() {
		
        $(this).mask("99/99");
	
    });
	
    $(form + ' input[mask=time]').each( function() {
		
        $(this).mask("99:99:99");
	
    });
	
    $(form + ' input[mask=numero]').each( function() {
		
        $(this).keyup( function(event) {
								
            if(((event.keyCode < 96) || (event.keyCode > 105)) && ((event.keyCode < 48) || (event.keyCode > 57)) ){
                $(this).val( $(this).val().replace(String.fromCharCode(event.keyCode).toLowerCase(),"") );
            }

            //verifica se não é numero
            if (isNaN($(this).val()))
                $(this).val("");

		
        });

    });

}




// JavaScript Document
var Contato = {
    'valida': function(form)
    {
        
        if ( $('#nome','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo nome! ');
            $('#nome','#'+form).focus();
            return false;
        }
        
        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }
        
        if ( $('#uf','#'+form).val() == 0)
        {
            alert('Você precisa preencher o campo estado! ');
            $('#uf','#'+form).focus();
            return false;
        }
        if ( $('#id_cidade','#'+form).val() == 0)
        {
            alert('Você precisa preencher o campo cidade! ');
            $('#id_cidade','#'+form).focus();
            return false;
        }
        
        if ( $('#mensagem','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo mensagem! ');
            $('#mensagem','#'+form).focus();
            return false;
        }
    },
    'loadCidade':function(id){
        $('#fcontact #id_cidade').html("<option value='0'>CARREGANDO...</option>");
        $('#fcontact #id_cidade').load('../load-cidade.php?id='+id);
    }
}


// JavaScript Document
var Ideias = {
    'valida': function(form)
    {
        
        if ( $('#nome','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo nome! ');
            $('#nome','#'+form).focus();
            return false;
        }
        
        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }
        
        if ( $('#uf','#'+form).val() == 0)
        {
            alert('Você precisa preencher o campo estado! ');
            $('#uf','#'+form).focus();
            return false;
        }
        if ( $('#id_cidade','#'+form).val() == 0)
        {
            alert('Você precisa preencher o campo cidade! ');
            $('#id_cidade','#'+form).focus();
            return false;
        }
        
        if ( $('#mensagem','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo mensagem! ');
            $('#mensagem','#'+form).focus();
            return false;
        }
    },
    'loadCidade':function(id){
        $('#fIdeias #id_cidade').html("<option value='0'>CARREGANDO...</option>");
        $('#fIdeias #id_cidade').load('../load-cidade.php?id='+id);
    }
}


var Newsletter = {
    'valida': function(form)
    {
        
        if ( $('#nome','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo nome! ');
            $('#nome','#'+form).focus();
            return false;
        }
        
        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }
    }
}

// JavaScript Document
var Hire = {
    'valida': function(form)
    {
        
        if ( $('#nome','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo nome! ');
            $('#nome','#'+form).focus();
            return false;
        }
        
        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }
        
        if ( $('#uf','#'+form).val() == 0)
        {
            alert('Você precisa preencher o campo estado! ');
            $('#uf','#'+form).focus();
            return false;
        }
        if ( $('#id_cidade','#'+form).val() == 0)
        {
            alert('Você precisa preencher o campo cidade! ');
            $('#id_cidade','#'+form).focus();
            return false;
        }
        
        if ( $('#mensagem','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo mensagem! ');
            $('#mensagem','#'+form).focus();
            return false;
        }
    },
    'loadCidade':function(id){
        $('#hire-form #id_cidade').html("<option value='0'>CARREGANDO...</option>");
        $('#hire-form #id_cidade').load('../load-cidade.php?id='+id);
    }
}

// JavaScript Document
var Demonstracao = {
    'valida': function(form)
    {
        
        if ( $('#nome','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo nome! ');
            $('#nome','#'+form).focus();
            return false;
        }
        
        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }
        
        if ( $('#uf','#'+form).val() == 0)
        {
            alert('Você precisa preencher o campo estado! ');
            $('#uf','#'+form).focus();
            return false;
        }
        if ( $('#id_cidade','#'+form).val() == 0)
        {
            alert('Você precisa preencher o campo cidade! ');
            $('#id_cidade','#'+form).focus();
            return false;
        }
        
        if ( $('#mensagem','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo mensagem! ');
            $('#mensagem','#'+form).focus();
            return false;
        }
    },
    'loadCidade':function(id){
        $('#fDemos #id_cidade').html("<option value='0'>CARREGANDO...</option>");
        $('#fDemos #id_cidade').load('../load-cidade.php?id='+id);
    }
}

// JavaScript Document
var Indique = {
    'valida': function(form)
    {
        
        if ( $('#nome','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo nome! ');
            $('#nome','#'+form).focus();
            return false;
        }
        
        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }
        
        if ( $('#nome_amigo','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo nome! ');
            $('#nome_amigo','#'+form).focus();
            return false;
        }
        
        if (!validaEmail($('#email_amigo','#'+form).val()))
        {
            $('#email_amigo','#'+form).focus();
            return false;
        }
    }
}
