$(document).ready(function(){
 var nav = navigator.userAgent.toLowerCase();
    a =  /(chrome)(?:.*chrome)?[ \/]([\w.]+)/.exec(nav) || /(safari)(?:.*version)?[ \/]([\w.]+)/.exec(nav) || /(opera)(?:.*version)?[ \/]([\w.]+)/.exec(nav) || /(ie) ([\w.]+)/.exec(nav) || !/compatible/.test(nav) && /(firefox)(?:.*firefox)?[ \/]([\w.]+)/.exec(nav) || [];
    if ( nav.indexOf('macintosh') != -1 ){
        os = 'macintosh';
    } else if ( nav.indexOf('windows') != -1 ){
        os = 'windows';
    } else if ( nav.indexOf('linux') != -1 ){
        os = 'linux';
    }
    var	browser = {
        name: a[1] || "",
        version: a[2] || "0",
        os: os
    }
    $("body").addClass( browser.name + ' ' + browser.name+parseInt(browser.version) + ' ' + browser.os );

    var BASEURL = '../';
		
	$(".palavra").hover(function(){
		$(this).stop();						 
		$(this).animate({"opacity":"0.8"},300, function(){
    	});
															 
	 });
	$(".palavra").animate({"opacity":"0"},0);
	$(".palavra").mouseout(function(){
									
		$(this).stop();
		$(this).animate({"opacity":"0","display":"none"},300, function(){												
		});
																 
	 });
		$('.descDepoimentosDosClientes li:first').addClass('selected').css('display','block');
		intervaloCliente = setInterval( passClientesNext , 12000 );
		
	
	$('.telefone').mask('(99)9999-9999');
	//validacao do cadastro 
    $('#faleConosco').submit(function(){
		var retorno = true;
        var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
            if($("#nome").val()==''){
				alert('Digite um nome');
				return false;
            }			
			if(!er.test($("#email").val())){
				alert('Digite um email válido');
				return false;
			}
			if($("#assunto").val()==''){
				alert('Digite um assunto');
				return  false;
            }
			if($("#telefone").val()==''){
				alert('Digite um telefone');
				return  false;
            }
			if($("#mensagem").val()==''){
				alert('Digite uma mensagem');
				return  false;
            }
						
			return retorno;
		});
});

function passClientesNext() {
	clearInterval(intervaloCliente);

	if ( $(".descDepoimentosDosClientes li strong.secondTitlePages:last").html() == $(".descDepoimentosDosClientes li.selected strong.secondTitlePages ").html()){
		$(".descDepoimentosDosClientes li.selected").removeClass('selected').fadeOut();
		$(".descDepoimentosDosClientes li:first").addClass('selected').fadeIn();
	}else {
	    $(".descDepoimentosDosClientes li.selected").removeClass('selected').fadeOut().next().addClass('selected').fadeIn();	
	}
	intervaloCliente = setInterval( passClientesNext , 12000 );
	
}
function passClientesPrev() {
	if ( $(".descDepoimentosDosClientes li:first").html() == $(".descDepoimentosDosClientes li.selected").html()){
		$(".descDepoimentosDosClientes li.selected").removeClass('selected').fadeOut();
		$(".descDepoimentosDosClientes li strong.secondTitlePages:last").parent().addClass('selected').fadeIn();
	}else {
	    $(".descDepoimentosDosClientes li.selected").removeClass('selected').fadeOut().prev().addClass('selected').fadeIn();	
	}
	
}
function changeType(tipo){
	
	if(tipo=="localizar"){
	//alert("loc");
		$("#Digiteendereco").fadeOut("slow");
		$("#Digiteendereco").css("display","none");
		$('#destino').val("");
		selectUnity($('#unidadeAtual').val());
	}else if(tipo=="rota"){
		$("#Digiteendereco").fadeIn("fast");
	}



} 
function selectUnity(address){

	
	if(address!=0){
									
		$('#unidadeAtual').val(address);
		
		address = address.replace(/ /g,'+')

		codeAddress(address);
		
		
	}
	
}
var geocoder;

var directionDisplay;
var directionsService;
var map;

  function initialize() {
 directionsService = new google.maps.DirectionsService();
    directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(-13.612874, -56.670952);
    var myOptions = {
      zoom:3,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: chicago
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
  }




    
function codeAddress(address) {
	initialize() ;
	//set_Map();
	//setOptions();
    var myOptions = {
      zoom:16,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
    }	
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);	
	var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map, 
			zoom: 17,
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
}

  function tracarRota() {
    initialize();
  var start = $('#destino').val();//document.getElementById("start").value;
  var end =  $('#unidadeAtual').val();//document.getElementById("end").value;
   // alert(start);
   // alert(end);
    var request = {
        origin:start, 
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    }); }

