﻿$(document).ready(function() {
	var indicazioni = '';
	var aqui = '';
	var daqui = '';
	var partenza = '';
	var arrivo = '';
	var ottieni = '';
	if ($('body').hasClass('eng')) {
	  indicazioni = 'Directions';
	  aqui = 'To here';
	  daqui = 'From here';
	  partenza = 'Start address';
	  arrivo = 'End address';
	  ottieni = 'Get directions';
	}
	else if ($('body').hasClass('fra')) {
	  indicazioni = 'Directions';
	  aqui = 'S’y rendre';
	  daqui = 'En partir';
	  partenza = 'Adresse de départ';
	  arrivo = 'Adresse d\'arrivée';
	  ottieni = 'Obtenir les indications';
	}
	else if ($('body').hasClass('esp')) {
	  indicazioni = 'Directions';
	  aqui = 'To here';
	  daqui = 'From here';
	  partenza = 'Start address';
	  arrivo = 'End address';
	  ottieni = 'Get directions';
	}
	else {
	  indicazioni = 'Indicazioni stradali';
	  aqui = 'A qui';
	  daqui = 'Da qui';
	  partenza = 'Indirizzo di partenza';
	  arrivo = 'Indirizzo di arrivo';
	  ottieni = 'Ottieni indicazioni';
	}
	var Candis = new google.maps.LatLng(45.035410, 7.876236);
	var center = new google.maps.LatLng(45.046017, 7.876236);
	MYMAP.init('#map', Candis, center, 14, indicazioni, aqui, daqui, partenza, arrivo, ottieni);
});

var MYMAP = {
  map: null,
  bounds: null
}
var directionsService = new google.maps.DirectionsService();
function getDirections() {
	$("#directions p.istruzioni").remove();	
	var saddr = $("#saddr").val();
	var daddr = $("#daddr").val();
    var request = {
        origin:saddr, 
        destination:daddr,
        travelMode: google.maps.DirectionsTravelMode.DRIVING,
		language: "IT"
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
	  else {
    	alert('Error: ' + status);
	  }
    });
}

MYMAP.init = function(selector, latLng, center, zoom, indicazioni, aqui, daqui, partenza, arrivo, ottieni) {
  var myOptions = {
    zoom:zoom,
    center: center,
    scrollwheel:false,
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
    mapTypeId: google.maps.MapTypeId.ROADMAP 
  }
  this.map = new google.maps.Map($(selector)[0], myOptions);
  this.bounds = new google.maps.LatLngBounds();
	  var image = new google.maps.MarkerImage('../img/gmaps/image.png',
		  new google.maps.Size(39, 66),
		  new google.maps.Point(0,0),
		  new google.maps.Point(20, 66));
	  var shadow = new google.maps.MarkerImage('../img/gmaps/shadow.png',
		  new google.maps.Size(72,66),
		  new google.maps.Point(0,0),
		  new google.maps.Point(20,66));
	  var shape = {
		  coord: [24,0,27,1,29,2,30,3,31,4,32,5,33,6,34,7,35,8,35,9,36,10,36,11,37,12,37,13,38,14,38,15,38,16,38,17,38,18,38,19,38,20,38,21,38,22,38,23,37,24,37,25,37,26,36,27,36,28,35,29,35,30,34,31,34,32,34,33,33,34,33,35,32,36,32,37,31,38,31,39,30,40,30,41,30,42,29,43,29,44,28,45,28,46,27,47,27,48,26,49,26,50,25,51,25,52,25,53,24,54,24,55,23,56,23,57,22,58,22,59,21,60,21,61,21,62,20,63,20,64,19,65,18,65,18,64,17,63,17,62,16,61,16,60,16,59,15,58,15,57,14,56,14,55,13,54,13,53,12,52,12,51,12,50,11,49,11,48,10,47,10,46,9,45,9,44,8,43,8,42,8,41,7,40,7,39,6,38,6,37,5,36,5,35,4,34,4,33,4,32,3,31,3,30,2,29,2,28,1,27,1,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,1,12,1,11,2,10,2,9,3,8,3,7,4,6,5,5,6,4,7,3,9,2,11,1,13,0],
		  type: 'poly'
	  };

	var marker = new google.maps.Marker({
		position: latLng,
		map: MYMAP.map,
		title:"CANDIS srl",
		icon: image,
		shadow: shadow,
		shape: shape
	});
    directionsDisplay = new google.maps.DirectionsRenderer();
	directionsDisplay.setMap(MYMAP.map);
    directionsDisplay.setPanel(document.getElementById("directions"));
	
	var html='<div style="font: 13px/1.3 \'Lucida Sans Unicode\',\'Lucida Grande\', Garuda, Arial, sans-serif; overflow:hidden; width:251px;"><img src="../img/logo_small.gif" alt="Logo Candis" width="150" height="53" style="margin:0; padding:0" /><p style="line-height:1.5; margin:13px 0 0; padding:0; font-size:13px;">Via Vittorio Emanuele, 46<br />10020 Andezeno (TO)<br />ITALY</p>';
	// The info window version with the "to here" form open
	var to_html = html + '<p style="margin:13px 0 0;  padding:0; font-size:13px;"><strong>'+indicazioni+'</strong>: <strong>'+aqui+'</strong> - <span class="daqui">'+daqui+'</span></p>' +
	   '<form action="javascript:getDirections()" style="margin:0;padding:0">' +
	   '<label for="saddr" style="display:block;margin:0;padding:13px 0 3px;">'+partenza+':</label>' +
	   '<input type="text" name="saddr" id="saddr" value="" style="width:245px; height:16px; margin:0 0 10px; float:left" />' +
	   '<button type="submit" id="vai" name="vai" style="padding:2px 4px;margin:0">'+ottieni+'</button>' +
	   '<input type="hidden" id="daddr" value="45.035410, 7.876236"/></form></div>';
	// The info window version with the "from here" form open
	var from_html = html + '<p style="margin:13px 0 0; padding:0; font-size:13px;"><strong>'+indicazioni+'</strong>: <span class="aqui">'+aqui+'</span> - <strong>'+daqui+'</strong></p>' +
	   '<form action="javascript:getDirections()" style="margin:0;padding:0">' +
	   '<label for="daddr" style="display:block;margin:0;padding:13px 0 3px;">'+arrivo+':</label>' +
	   '<input type="text" name="daddr" id="daddr" value="" style="width:245px; height:16px; margin:0 0 10px; float:left" />' +
	   '<button type="submit" id="vai" name="vai" style="padding:2px 4px;margin:0">'+ottieni+'</button>' +
	   '<input type="hidden" id="saddr" value="45.035410, 7.876236"/></form></div>';
	html = html + '<p style="margin:13px 0 0; padding:0; line-height:1.5; font-size:13px"><strong>'+indicazioni+'</strong>: <span class="aqui">'+aqui+'</span> - <span class="daqui">'+daqui+'</span></p></div>';
	
	var infoWindow = new google.maps.InfoWindow({
    	//maxWidth: 300
	});
	infoWindow.setContent(html);
	infoWindow.open(MYMAP.map, marker);
	
	google.maps.event.addListener(marker, 'click', function() {
	infoWindow.setContent(html);
		infoWindow.open(MYMAP.map, marker);
	});
	
	$("span.aqui").live('click',function(){
		infoWindow.setContent(to_html);
		infoWindow.open(MYMAP.map, marker);
	});
	$("span.daqui").live('click',function(){
		infoWindow.setContent(from_html);
		infoWindow.open(MYMAP.map, marker);
	});
}
