﻿// ------ Map basics ------ 

function codeAddress(latitude, longitude) {
    latlng = new google.maps.LatLng(latitude, longitude);
	var myOptions = {
		zoom: 15,
		center: latlng,
		backgroundColor: '#e2e1dd',
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	var marker = new google.maps.Marker({
		position: latlng, 
		map: map
	});   
	
	// Mask

	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
     
	//Set height and width to mask to fill up the whole screen
	$('#mask').css({'width':maskWidth,'height':maskHeight});
			
	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
               
	//Set the popup window to center
	$("#map_canvas_outer").css('top',  winH/2-$("#map_canvas_outer").height()/2 + $(window).scrollTop() + "px");
	$("#map_canvas_outer").css('left', winW/2-$("#map_canvas_outer").width()/2);

	//transition effect    
	$('#mask').fadeTo("fast",0.5);   
	$("#map_canvas_outer").show("fast", function() {google.maps.event.trigger(map, 'resize'); map.setCenter(latlng);});
	//$("#map_canvas_outer").fadeTo("fast",1, function(){ google.maps.event.trigger(map, 'resize'); map.setCenter(latlng);


}

// ------ Directions function ------ 

function calcRoute() {
	if(directionsDisplay != null) {
		directionsDisplay.setMap(null);
		directionsDisplay = null;

	}
	directionsService = new google.maps.DirectionsService();
	directionsDisplay = new google.maps.DirectionsRenderer();

	
	var start = $('input[name=directions]').val();
	var end = latlng;
	var request = {
		origin:start,
		destination:end,
		travelMode: google.maps.TravelMode.DRIVING
	};


	directionsDisplay.setMap(map);
	directionsService.route(request, function(result, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(result);
		}
		else alert("Osoitetta ei löytynyt!");
	});
}

