﻿//<![CDATA[
// global arrays to hold copies of the markers and html used by the side_bar
var gmarkers = [];
var htmls = [];
var side_bar_html = "";
var i = 0;	

// global "map" variable
var map;		

// This function picks up the side_bar click and opens the corresponding info window
function myclick(i) {
  gmarkers[i].openInfoWindowHtml(htmls[i]);
}

function init() {
var komvos_makri =  new GLatLng(40.857290, 25.753600);
var komvos_axd =  new GLatLng(40.875920, 25.859490);
var town_hall =  new GLatLng(40.845030, 25.874420);
var vanikioti =  new GLatLng(40.845710, 25.861190);
var systima =  new GLatLng(40.850826, 25.866585);
var npsystima =  new GLatLng(40.842200, 25.873010);
var pe_evrou =  new GLatLng(40.844064, 25.864935);
//var pe_evrou =  new GLatLng(40.844790, 25.875740);
var bus_station =  new GLatLng(40.846030, 25.875140);
var train_station =  new GLatLng(40.845180, 25.878360);
var train_station_old =  new GLatLng(40.848330, 25.891650);
var airport =  new GLatLng(40.855776,25.94534);
var port =  new GLatLng(40.843740, 25.877030);
var hospital =  new GLatLng(40.861910, 25.799310);

if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(40.850404,25.87482), 14);
	gdir = new GDirections(map, document.getElementById("route"));
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
}

map.addOverlay(createMarker(systima, "[A] 2ο Σ. Προσκόπων", "2o Σύστημα Προσκόπων<br>Αλεξανδρούπολης", 0));
map.addOverlay(createMarker(komvos_axd, "[B] Έξοδος Αλεξ/πολης", "Κεντρική Έξοδος Εγνατίας Οδού<br>προς Αλεξανδρούπολη<br>& προς Προσκοπικό Καταφύγιο Κίρκης", 1));
map.addOverlay(createMarker(komvos_makri, "[C] Έξοδος Μάκρης", "Έξοδος Εγνατίας οδού<br>προς Μάκρη<br>& προς Κατασκηνωτικό Κέντρο Μάκρης", 2));
map.addOverlay(createMarker(town_hall, "[D] Δημαρχείο", "Δημαρχείο Αλεξανδρούπολης", 3));
map.addOverlay(createMarker(vanikioti, "[E] Ρέμα Βανικιώτη", "Ρέμα Βανικιώτη", 4));
map.addOverlay(createMarker(bus_station, "[F] Κ.Τ.Ε.Λ. Έβρου", "Κ.Τ.Ε.Λ. Έβρου", 5));
map.addOverlay(createMarker(train_station, "[G] Ο.Σ.Ε. Κεντρικός", "Ο.Σ.Ε.<br>Κεντρικός Σταθμός Αλεξ/πόλης", 6));
map.addOverlay(createMarker(train_station_old, "[H]  Ο.Σ.Ε. Παλιός", "Ο.Σ.Ε.<br>Παλιός Σταθμός Αλεξ/πόλης", 7));
map.addOverlay(createMarker(npsystima, "[I] 1ο Σ. Ν/Προσκόπων", "1ο Σύστημα Ναυτοπροσκόπων<br>Αλεξανδρούπολης", 8));
map.addOverlay(createMarker(airport, "[J] Αεροδρόμιο", "Αεροδρόμιο Αλεξανδρούπολης<br>&quot;Δημόκριτος&quot;<br>( AXD )", 9));
map.addOverlay(createMarker(port, "[K] Λιμάνι", "Λιμάνι Αλεξανδρούπολης", 10));
map.addOverlay(createMarker(hospital, "[L] Νοσοκομείο", "Γενικό Περιφεριακό Νοσοκομείο<br>Αλεξανδρούπολης", 11));
map.addOverlay(createMarker(pe_evrou, "[M] Περιφ. Εφ. Έβρου", "Περιφεριακή Εφορία Προσκόπων<br>Ν. Έβρου ", 12));

document.getElementById("side_bar").innerHTML = side_bar_html;

//map.addControl(new GMapTypeControl());
map.addControl(new GHierarchicalMapTypeControl());
map.addControl(new GScaleControl());
map.addControl(new GSmallMapControl());
map.addMapType(G_PHYSICAL_MAP);

}

function setDirections(fromAddress, toAddress, locale) {
  document.getElementById("route").style.display = "block";	
  gdir.load("from: " + fromAddress + " to: " + toAddress,
			{ "locale": locale });
}

function resetmap () {
	i = 0;
	side_bar_html = "";
	document.getElementById("route").style.display = "none";
	document.getElementById("side_bar").innerHTML = "";
	gdir.clear();
	init();
}

function createMarker(point, name, text, index) {
	var baseIcon = new GIcon();
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	//baseIcon.infoShadowAnchor = new GPoint(18, 25);	
	var letter = String.fromCharCode("A".charCodeAt(0) + index);
	var icon = new GIcon(baseIcon);
	icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";  
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {    
			marker.openInfoWindowHtml("<b>" + text + "</b>");  
			});
	// save the info we need to use later for the side_bar
	gmarkers[i] = marker;
	htmls[i] = text;
	// add a line to the side_bar html
	side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br>';
	i++;  
	return marker;
}

function onGDirectionsLoad(){}

function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	 alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	 alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	 alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	 alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	
   else alert("An unknown error occurred.");
}

window.onload = init;
//]]>

