function timeLineTwitter(){
	var timeTwitte = $('#tweet-id-1 .twtr-tweet-text .twtr-timestamp').attr('time');
	timeTwitte = H(timeTwitte);
	$('#tweet-id-1 .twtr-tweet-text em, #tweet-id-1 .twtr-tweet-text .twtr-user').remove();

	var textTwitte = $('#tweet-id-1 .twtr-tweet-text p').html();

	$('#box-twitter blockquote, #box-twitter .time').fadeOut();

	window.setTimeout(function(){
		$('#box-twitter blockquote').html(textTwitte);
		$('#box-twitter .time').html(timeTwitte);
		$('#box-twitter blockquote, #box-twitter .time').fadeIn();
	}, 500);

}

var K = function () {
    var a = navigator.userAgent;
    return {
        ie: a.match(/MSIE\s([^;]*)/)
    }
} ();

var H = function (a) {

    var b = new Date();
    var c = new Date(a);
    if (K.ie) {
        c = Date.parse(a.replace(/( \+)/, ' UTC$1'))
    }

    var d = b - c;
    var e = 1000,
    minute = e * 60,
    hour = minute * 60,
    day = hour * 24,
    week = day * 7;

    if (isNaN(d) || d < 0) {
        return ""
    }

    if (d < e * 7) {
        return "Agora"
    }

    if (isNaN(d) || isNaN(minute) || isNaN(hour) || isNaN(day) || isNaN(week)) {
        alert(d + " - " + minute + " - " + hour + " - " + day + " - " + week);
    }

    if (d < minute) {
        var r = Math.floor(d / e);
        return r.toString() + " segundos atr&aacute;s";
    }

    
    if (d < minute * 2) {
        return "1 minuto atr&aacute;s"
    }
    if (d < hour) {
    var r = Math.floor(d / minute);
    return r.toString() + " minutos atr&aacute;s"
    }
    if (d < hour * 2) {
    return "1 hora atr&aacute;s"
    }
    if (d < day) {
    var r = Math.floor(d / hour);
    return r.toString() + " horas atr&aacute;s"
    }
    if (d > day && d < day * 2) {
    return "Ontem"
    }
    if (d < day * 365) {
    var r = Math.floor(d / day);
    return r.toString() + " dias atr&aacute;s"
    } else {
    return "mais de um ano atr&aacute;s"
    }

};



$(document).ready(function(){

	//Fonte replace
	Cufon.replace('.cufon300', { fontFamily: 'Museo 300' });
	Cufon.replace('.cufon500', { fontFamily: 'Museo 500' });
	Cufon.replace('.cufon700', { fontFamily: 'Museo 700' });
	Cufon.replace('.box-area-cliente a', { fontFamily: 'Museo 700', textShadow: '0px 0px 1px #CCC', color: '#E0217B' });

	$('.box-download a').click(function(){ return false; }).tooltip({ effect: 'slide', offset: [-60, 0] });
	$('.box-saiba-mais a[href=#]').click(function(){ return false; }).tooltip({ effect: 'slide', offset: [-40, 0] });
	$('.redes-sociais a[href=#]').click(function(){ return false; }).tooltip({ effect: 'slide', offset: [-10, 0] });
	$('.lk-breve').click(function(){ return false; }).tooltip({ effect: 'slide', offset: [10, 0] });
	$('.box-area-cliente a').click(function(){ return false; }).tooltip({ effect: 'slide', offset: [20, 10] });

	//Link rosa
	$('.lk-rosa').each(function(){
		var textLink = $(this).text();
		$(this).text('');
		$(this).append('<span class="bg-left"></span><span class="bg-link">' + textLink + '</span><span class="bg-right"></span>');
		Cufon.replace($(this).find('.bg-link'), { fontFamily: 'Museo 700', textShadow: '0px 0px 2px #666' });
	});

	//Banner HOME
	$('.home .navigation .descricao .descricao-01').stop(true, true).fadeIn();
	$('#imgBanner').jqFancyTransitions({ width: 400, height: 334, navigation : false, position: 'top', direction: 'left' });

	$('.lista-empresas a').click(function(){
		
		if(!$(this).parent().hasClass('ativo')){
			
			var idLink = $(this).attr('href');
			idLink = idLink.replace('#', '');
			
			$('.home .navigation .descricao li').stop(true, true).fadeOut();
			
			window.setTimeout(function(){
				$('.home .navigation .descricao .descricao-' + idLink).fadeIn();
			}, 500);

			$('.home .navigation li').removeClass('ativo');
			$(this).parent().addClass('ativo');	

		}
		return false;
	});
	
	//Box newsletter HOME
	$('.home .box-news input').focus(function(){
		$(this).prev().hide();
	}).blur(function(){
		if($(this).val().length < 1){
			$(this).prev().show();
		}
	});
	
	window.setTimeout(timeLineTwitter, 6000);	
	
	//Box abas (SERVIÇOS)
	if($('.servicos').length > 0){
		var linkConteudo = window.location.hash;
		if(linkConteudo.length > 0){
			$('.servicos .abas li').removeClass('ativo');
			$('.servicos .abas a[href='+ linkConteudo +']').parent().addClass('ativo');
			Cufon.replace('.servicos .abas a', { fontFamily: 'Museo 700' });

			$('.box-conteudo-abas').hide();
			$(linkConteudo).show();

		}
	}
	$('.servicos .abas a, .lk-veja-mais-digital, .lk-veja-mais-trade').live('click', function(){
		
		var linkConteudo = $(this).attr('href');
		
		$('.servicos .abas li').removeClass('ativo');
		$('.servicos .abas a[href='+ linkConteudo +']').parent().addClass('ativo');
		Cufon.replace('.servicos .abas a', { fontFamily: 'Museo 700' });
		
		$('.box-conteudo-abas').fadeOut(500);
		
		window.setTimeout(function(){
			$(linkConteudo).fadeIn();
		}, 500);


		return false;

	});
	
	
});


//-----------------------------------------------------------------------------//
// 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;
}



//---------------------------------------------------------------//
// 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("");

		
        });

    });

}



//--------------------------------------//
//      Validações  
//--------------------------------------//


var Validacao = {
    'Cadastro': function(form)
    {

        if ( $('#nome','#'+form).val() == "" || $('#nome','#'+form).val() == "Nome")
        {
            alert('Você precisa preencher o campo nome! ');
            $('#nome','#'+form).focus();
            return false;
        }

        if (!validaEmail($('#email','#'+form).val()))
        {
            $('#email','#'+form).focus();
            return false;
        }
        
        $.post("controller.php", $('#'+form).serialize(), function(retorno){
            alert(retorno);
            document.getElementById(form).reset();
            $('label','#'+form).show();
            return false;
        });
		
        return false;
    },
    
    'Contato': 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 ( $('#telefone','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo telefone! ');
            $('#telefone','#'+form).focus();
            return false;
        }

        if ( $('#assunto','#'+form).val() == "" || $('#assunto','#'+form).val() == 0)
        {
            alert('Você precisa selecionar um assunto! ');
            return false;
        }

        if ( $('#mensagem','#'+form).val() == "")
        {
            alert('Você precisa preencher o campo mensagem! ');
            $('#mensagem','#'+form).focus();
            return false;
        }
        
        $.post("controller.php", $('#'+form).serialize(), function(retorno){
            alert(retorno);
            document.getElementById(form).reset();
            return false;
        });
		
        return false;
    }
}
