var map = null;
var gdir = null;
var marker = null;

var selectedOffice = 0;

var add = new Array();
var lat = new Array();
var lon = new Array();
var zoo = new Array();

// FIN
add[0] = "<strong>K. Hartwall Oy Ab</strong><br/>Kay Hartwallin tie 2<br/>FIN-01150 S&ouml;derkulla<br/>Finland<br/>Tel. +358-9 413 18 33<br/>Fax +358-9 272 1484<br/>contact-khw@k-hartwall.com";
lat[0] = 60.290185536334604;
lon[0] = 25.33921480178833;
zoo[0] = 15;

// UK
add[1] = "<strong>K. Hartwall Ltd</strong><br/>Queensway Industrial Estate<br/>Glenrothes, Fife<br/>Scotland KY7 5QJ<br/>Tel. +44 1592 753 745<br/>Fax +44 1592 753 747<br/>contact-khw@k-hartwall.com";
lat[1] = 56.198616;
lon[1] = -3.159685;
zoo[1] = 15;

// DK
add[2] = "<strong>K. Hartwall A/S</strong><br/>Fynsvej 60<br/>5500 Middelfart, Denmark<br/>Tel. +45-63 41 2044<br/>Fax +45-64 41 31 44<br/>contact-khw@k-hartwall.com";
lat[2] = 55.50030079975177;
lon[2] = 9.766802787780762;
zoo[2] = 15;

// CZ
add[3] = "<strong>K. Hartwall s.r.o.</strong><br/>Masarykovo n&aacute;mesti 9<br/>593 01 Bystrice nad Pern&scaron;tejnem<br/>Czech Republic";
lat[3] = 49.523233245962146;
lon[3] = 16.26053810119629;
zoo[3] = 15;

function closeMap() {
	document.getElementById("gmaps_contentWrapper").style.display = "none";
	document.getElementById("contactMap").style.visibility = "visible";
}

function initMap(office) {
	document.getElementById("gmaps_contentWrapper").style.display = "block";
	document.getElementById("contactMap").style.visibility = "hidden";
	selectedOffice = office;
	map = new GMap2(document.getElementById("gmaps_mapCanvas"));
    map.setCenter(new GLatLng(lat[selectedOffice], lon[selectedOffice]), zoo[selectedOffice]);
	
	marker = new GMarker(new GLatLng(lat[selectedOffice], lon[selectedOffice]));
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(
			add[selectedOffice],
			{borderSize: 12, beakOffset: -2, paddingX: 5, paddingY: 5, width: 200 }
			
		); 			
	});
	
	map.addOverlay(marker);
	
	gdir = new GDirections(map);
	GEvent.addListener(gdir, "addoverlay", dirOnAddOverlay);
	GEvent.addListener(gdir, "error", dirOnError);
	GEvent.addListener(gdir, "load", dirOnLoad);

	
	map.addControl(new GLargeMapControl());
	
	GEvent.trigger(marker, 'click');
}

function directionsFrom(address) {
	gdir.load("from: " + address + " to: " + lat[selectedOffice] + ", " + lon[selectedOffice] , {"locale":"en", "getSteps":true});
}

function dirOnLoad() {
	
	var nRoutes = gdir.getNumRoutes();
	
	for(var i = 0; i < nRoutes; i++) {
		var route = gdir.getRoute(i);
		var nSteps = route.getNumSteps();
		
		$("#gmaps_mapdirections").empty();
		
		for(var j = 0; j < nSteps; j++) {
			var step = route.getStep(j);

			$("#gmaps_mapdirections").append("<div class='row'><span class='number'>"+ (j+1) + ". </span>" + step.getDescriptionHtml() + "</div>");
		}
	}
}

function dirOnError() {
	var code = gdir.getStatus().code; 
	
	switch(code) {
	case G_GEO_UNKNOWN_ADDRESS:
		$("#mapdirections").html("<p><strong>Given address could not be found.</strong></p>");
		break;
		
	case G_GEO_BAD_REQUEST:
		$("#mapdirections").html("<p><strong>A directions request could not be successfully parsed.</strong></p>");
		break;
		
	case G_GEO_SERVER_ERROR:
		$("#mapdirections").html("<p><strong>A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.</strong></p>");
		break;
		
	case G_GEO_MISSING_QUERY:
		$("#mapdirections").html("<p><strong>The HTTP q parameter was either missing or had no value. For geocoding requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.</strong></p>");
		break;
		
	case G_GEO_UNKNOWN_ADDRESS:
		$("#mapdirections").html("<p><strong>No corresponding geographic location could be found for the specified address. This may be due to the fact that the address is relatively new, or it may be incorrect.</strong></p>");
		break;
		
	case G_GEO_UNAVAILABLE_ADDRESS:
		$("#mapdirections").html("<p><strong>The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.</strong></p>");
		break;
		
	case G_GEO_UNKNOWN_DIRECTIONS:
		$("#mapdirections").html("<p><strong>No route was found.</strong></p>");
		break;
		
	case G_GEO_BAD_KEY:
		$("#mapdirections").html("<p><strong>The given key is either invalid or does not match the domain for which it was given.</strong></p>");
		break;
		
	case G_GEO_TOO_MANY_QUERIES:
		$("#mapdirections").html("<p><strong>The given key has gone over the requests limit in the 24 hour period.</strong></p>");
		break;
		
	default:
		$("#mapdirections").html("<p><strong>An unknown error occurred.\nError code: " + code + "</strong></p>");
	}
}

function dirOnAddOverlay() {}

function dirReplaceMarker(origMarker, opts) {
	newMarker = new GMarker(origMarker.getLatLng(), opts);
	
	map.addOverlay(newMarker);
	map.removeOverlay(origMarker);
	
	GEvent.addListener(newMarker, 'click', function() {
		GEvent.trigger(origMarker,'click');
	});
	
	return(newMarker);
}


