﻿	var directionDisplay;
	var directionsService = new google.maps.DirectionsService();
	var map;

$(document).ready(function(){
	
	
	/* submenu cardápio */
	
	/*
	
	$('#menu li').mouseenter(function(){
		$(this).children('.submenu').fadeIn('fast');
	})
	$('#menu li').mouseleave(function(){
		$(this).children('.submenu').fadeOut('fast');
	})
	
	*/
	
	/*
	if($.browser.msie && (parseInt($.browser.version) <= 7)){
		$('hr.linha_estilizada').replaceWith("<span class='linha_estilizada'></span>");
	}
	*/
	
	$('div#header #menu .submenu').not('.submenu_ativo').css('display', 'none');
	
	$('#menu li').hover(
		function(){
			$(this).children('.submenu').not('.submenu_ativo').fadeIn('fast');
		},
		function(){
			$(this).children('.submenu').not('.submenu_ativo').fadeOut('fast');
		}
	)
	
	/* slider - home */
	
	$('.box_central_home_1').cycle({speed:1000, timeout:6000})
	
	
	/* galeria de imagem - 1 */
	
	$('.galeria_1 .thumb li').click(function(){
		index = $('.galeria_1 .thumb').find('li').index(this);
		$('.galeria_1 .image').find('li').not(':eq(' + index + ')').fadeOut('fast');
		$('.galeria_1 .image').find('li').eq(index).fadeIn('slow');
		$('.galeria_1 .thumb li').removeClass('img_atual');
		$('.galeria_1 .thumb li').not(this).addClass('transparencia');
		$(this).addClass('img_atual');
	})
	
	$('.galeria_1 .thumb li').mouseenter(function(){
		if($(this).hasClass('img_atual')) return false;
		$(this).removeClass("transparencia");
	})
	$('.galeria_1 .thumb li').not('.img_atual').mouseleave(function(){
		if($(this).hasClass('img_atual')) return false;
		$(this).addClass("transparencia");
	})
	
	/* galeria de imagem - boutique */
	
	$('.galeria_boutique .thumb li').click(function(){
		index = $('.galeria_boutique .thumb').find('li').index(this);
		$('.galeria_boutique .image').find('li').not(':eq(' + index + ')').fadeOut('fast');
		$('.galeria_boutique .image').find('li').eq(index).fadeIn('slow');
		$('.galeria_boutique .thumb li').removeClass('img_atual');
		$('.galeria_boutique .thumb li').not(this).addClass('transparencia');
		$(this).addClass('img_atual');
	})
	
	$('.galeria_boutique .thumb li').mouseenter(function(){
		if($(this).hasClass('img_atual')) return false;
		$(this).removeClass("transparencia");
	})
	$('.galeria_boutique .thumb li').not('.img_atual').mouseleave(function(){
		if($(this).hasClass('img_atual')) return false;
		$(this).addClass("transparencia");
	})
	
	/* galeria de imagem - cozinha atelier */
	
	$('.galeria_atelier .thumb li').click(function(){
		index = $('.galeria_atelier .thumb').find('li').index(this);
		$('.galeria_atelier .image').find('li').not(':eq(' + index + ')').fadeOut('fast');
		$('.galeria_atelier .image').find('li').eq(index).fadeIn('slow');
		$('.galeria_atelier .thumb li').removeClass('img_atual');
		$('.galeria_atelier .thumb li').not(this).addClass('transparencia');
		$(this).addClass('img_atual');
	})
	
	$('.galeria_atelier .thumb li').mouseenter(function(){
		if($(this).hasClass('img_atual')) return false;
		$(this).removeClass("transparencia");
	})
	$('.galeria_atelier .thumb li').not('.img_atual').mouseleave(function(){
		if($(this).hasClass('img_atual')) return false;
		$(this).addClass("transparencia");
	})
	
	/* --- mapa da parte de localização */
	
	/* previne que o mapa seja chamado numa página onde o componente do DOM não esteja presente */
	if(verificaURL2() == 'contato' || verificaURL2() == 'contato/'){
		iniciaMapa('Rua Afonso Bráz, 511 - São Paulo - SP - Brasil');
		$('form.form_mapa .btn_mapa').click(function(e){
			e.preventDefault();	
			endereco = $('input.input_mapa').val();
			comoChegar(endereco,'Rua Afonso Bráz, 511 - São Paulo - SP - Brasil');
		})	
	}
	
})
	
	function verificaURL(){
		url = window.location.href;
		i = url.indexOf("?");
		if(i == -1) return false;
		return url.substring(i + 1);
	}

	function verificaURL2(){
		url = window.location.href;
		secao = url.search(/php/i);
		if(secao == -1) return false;
		return url.substring(secao + 4);
	}




	function iniciaMapa(endereco){
		directionsDisplay = new google.maps.DirectionsRenderer();
    //var endereco = new google.maps.LatLng(41.850033, -87.6500523);
    var endereco = codeAddress(endereco);
    var myOptions = {
      zoom:16,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: endereco
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	}

	function comoChegar(endereco1, endereco2){
		directionsDisplay.setMap(map);
		
	  var request = {
        origin:endereco1, 
        destination:endereco2,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
		
		directionsService.route(request, function(response, status){
      if (status == google.maps.DirectionsStatus.OK){
        directionsDisplay.setDirections(response);
      }
    });	
	}
	
  function codeAddress(address) {
		geocoder = new google.maps.Geocoder();
		
		//alert(geocoder.geocode);
		
    //var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status){
			//alert(results[0].geometry.location);
      if (status == google.maps.GeocoderStatus.OK){
				//alert('OK');
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location
        });
				return results[0].geometry.location;
      } else {
        alert("Geocode was not successful for the following reason: " + status);
				return false;
      }
    });
	}





