$(function(){
	var er = new RegExp(/\b[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z]{2,4}\b/);
	
	// Vitrine Home
    var
        vt = $('div#vitrineContent'),
        img = $('#vitrineContent img'),
        total = img.size() - 1;
        
	setTimeSlide(0);
    
    function setTimeSlide(val){ imgTime = setTimeout(function(){changeImg(val)},6000); }
    function changeImg(nImg){
    
		v = nImg + 1;
		img.hide();
		vt.prepend(img.eq(v));
		img.fadeIn('slow');
        
		if(v == total){ v = -1 }
		setTimeSlide(v);
        
    }
    function stopTime(){clearTimeout(imgTime);}
	$('a.newWindow').click(function(e){
        e.preventDefault();
        window.open($(this).attr('href'));
    });
	$('#content').prepend('<img id="loader" src="img/loader.gif" alt="" />');
	
	/* MENU */
	$('#menu ul li a').click(function(e){
    
		e.preventDefault(); e.stopPropagation();
		var
            cl = $(this).attr('class'),
            idLink = $(this).attr('rel'),
            allContent = $('#home,#empresa,#servicos,#portfolio,#clientes,#contactPage');
		
		if(idLink == "ok" && cl != 'portfolio'){
			
            allContent.hide();
			$('#content div#' + cl).show();
            
		}else{
        
			$('img#loader').fadeIn('fast');
            
            var
                href = $( this ).attr('href').replace('http://www.teamdesign.com.br/',''),
                link = href.replace('.html',''),
                dataJs = "param=" + link;
                
			$( this ).attr('rel','ok');
            
			$.ajax({
            
				url: href,
				data: dataJs,
				success: function(msg){
                
					$('img#loader').fadeOut('fast');
					allContent.hide();
					$('#content').prepend(msg);
                    
                    /* portfolio */
                    $('div#portfolio a').click(function(e){
                    
                        e.stopPropagation(); e.preventDefault();
                        $('#contentPort').remove();
                        $('#portfolio').prepend('<div id="contentPort"><img id="loader" src="img/loader.gif" style="margin-left:-20px" alt="" /></div>');
                        $('#maskPort').addClass('ajaxOpacity');
                        portName = $(this).attr('id');
                        
                        ajaxPort( portName );
                        

                        
                    })
                    
					$('form input#assunto').focus();
					$('form').submit(function(){
						var 
                            assunto = $('input#assunto'),
                            nome = $('input#nome'),
                            email = $('input#email'),
                            telefone = $('input#telefone'),
                            comentario = $('textarea#comentario'),
                            turing = $('input#turing'),
                            nturing = $('input#nturing'),
                            feedback = $('div#errorMessage');
                            
						if(assunto.val() == ""){
							feedback.fadeIn('slow').html('<p>O campo <strong>Assunto</strong> deve ser preenchido!</p>');
							assunto.focus();
							return false;
						}else if(nome.val() == ""){
							feedback.fadeIn('slow').html('<p>O campo <strong>Nome</strong> deve ser preenchido!</p>');
							nome.focus();
							return false;
						}else if(!er.test(email.val())){
							feedback.fadeIn('slow').html('<p>Você deve preencher um <strong>E-mail</strong> válido!</p>');
							email.focus();
							return false;
						}else if(telefone.val() == ""){
							feedback.fadeIn('slow').html('<p>O campo <strong>Telefone</strong> deve ser preenchido!</p>');
							telefone.focus();
							return false;
						}else if(comentario.val() == ""){
							feedback.fadeIn('slow').html('<p>O campo <strong>Mensagem</strong> deve ser preenchido!</p>');
							comentario.focus();
							return false;
						}else if(turing.val() == ""){
							feedback.fadeIn('slow').html('<p>O texto em <strong>Azul</strong> deve ser preenchido!</p>');
							turing.focus();
							return false;
						}else{
							$.ajax({
								type: "POST",
								url: 'mail.html',
								data: 'assunto=' + assunto.val() + '&nome=' + nome.val() + '&email=' + email.val() + '&telefone=' + telefone.val() + '&comentario=' + comentario.val() + '&turing=' + turing.val() + '&nturing=' + nturing.val(),
								success: function(message){
									feedback.fadeIn('slow').html(message);
								}
							});
						}
						return false;
					})
					
					$('input,textarea,div').click(function(){ $('div#errorMessage').fadeOut('fast'); })
					$('input,textarea').keypress(function(){ $('div#errorMessage').fadeOut('fast'); })
				}
			});
		}
		$('#menu a').css('font-weight','normal').css('color','#7D7D7D');
		$(this).css('font-weight','bold').css('color','#A20606');
	})
    

    
    
	/* CONTATO */
	$('form input#assunto').focus();
	$('form').submit(function(){

        var 
            assunto = $('input#assunto'),
            nome = $('input#nome'),
            email = $('input#email'),
            telefone = $('input#telefone'),
            comentario = $('textarea#comentario'),
            turing = $('input#turing'),
            nturing = $('input#nturing'),
            feedback = $('div#errorMessage');
        
		if(assunto.val() == ""){
			feedback.fadeIn('slow').html('<p>O campo <strong>Assunto</strong> deve ser preenchido!</p>');
			assunto.focus();
			return false;
		}else if(nome.val() == ""){
			feedback.fadeIn('slow').html('<p>O campo <strong>Nome</strong> deve ser preenchido!</p>');
			nome.focus();
			return false;
		}else if(!er.test(email.val())){
			feedback.fadeIn('slow').html('<p>Você deve preencher um <strong>E-mail</strong> válido!</p>');
			email.focus();
			return false;
		}else if(telefone.val() == ""){
			feedback.fadeIn('slow').html('<p>O campo <strong>Telefone</strong> deve ser preenchido!</p>');
			telefone.focus();
			return false;
		}else if(comentario.val() == ""){
			feedback.fadeIn('slow').html('<p>O campo <strong>Mensagem</strong> deve ser preenchido!</p>');
			comentario.focus();
			return false;
        }else if(turing.val() == ""){
            feedback.fadeIn('slow').html('<p>O texto em <strong>Azul</strong> deve ser preenchido!</p>');
            turing.focus();
            return false;
        }else{
			$.ajax({
				type: "POST",
				url: 'mail.html',
				data: 'assunto=' + assunto.val() + '&nome=' + nome.val() + '&email=' + email.val() + '&telefone=' + telefone.val() + '&comentario=' + comentario.val() + '&turing=' + turing.val() + '&nturing=' + nturing.val(),
				success: function(message){
					feedback.fadeIn('slow').html(message);
				}
			});
		}
		return false;
	})

    $('input,textarea,div').click(function(){ $('div#errorMessage').fadeOut('fast'); })
    $('input,textarea').keypress(function(){ $('div#errorMessage').fadeOut('fast'); })

})



function ajaxPort(ptName){

    $.ajax({
        type: 'post',
        url: 'portfolio/' + ptName + '.php',
        success: function(msg){
            $('#contentPort').html(msg);
            $('#contentPort h3 span a').click(function(e){
                e.preventDefault();
                $('#contentPort').fadeOut('fast');
                $('#maskPort').removeClass('ajaxOpacity');
            })
        }
    })

}