var Map = {
	map: null,
	directions: null,
	points: [
		{'text':'Piazza di Spagna', 'lat':'41.905437', 'lon':'12.482185'},
		{'text':'Trevi Fountain', 'lat':'41.901127', 'lon':'12.482872'},
		{'text':'Spanish Steps', 'lat':'41.905663', 'lon':'12.483044'},
		{'text':'Via Condotti', 'lat':'41.905056', 'lon':'12.480683'},
		{'text':'Piazza Navona', 'lat':'41.899082', 'lon':'12.473388'},
		{'text':'Termini Train Station ', 'lat':'41.901638', 'lon':'12.501154'},
		{'text':'Via V. Veneto', 'lat':'41.908123', 'lon':'12.489223'},
		{'text':'Pantheon', 'lat':'41.899097', 'lon':'12.476735'},
		{'text':'Piazza del Popolo', 'lat':'41.910103', 'lon':'12.476306'}
	],

	markers: [],

	initMap: function() {
		// Init map object
		this.resizeMap();
		this.map = new GMap2(document.getElementById("map_contents"));
		//this.map.setCenter(new google.maps.LatLng(41.909434,12.490154), 15);
		this.map.setCenter(new google.maps.LatLng(41.907739,12.486799), 15);
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
		// init directions object
		this.directions = new GDirections(this.map, document.getElementById("directions"));
	    GEvent.addListener(this.directions , "load", this.onGDirectionsLoad);
    	GEvent.addListener(this.directions , "error", this.handleDirErrors);

		// Resize on every window resize event
		window.onresize = Map.resizeMap;


	},

	initMain: function() {
		var latlng = new GLatLng(41.909434,12.490154);
		var marker = new GMarker(latlng)

	    this.map.addOverlay(marker);

		GEvent.addListener(marker, "click", function() {
	      /*marker.openInfoWindowHtml("<div style='height:120px;width:300px;'><h3 style='color:black'>Hotel Victoria Roma</h3><table border=0><tr><td>Via Campania, 41<br />"+
							"00187 Roma (RM), Italy<br/>"+
							"06 423701</td><td><img src='http://base.googlehosted.com/base_media?q=maps:www.booking.com/images/hotel/org/283/283935.jpg&size=2&hl=en' /></td></tr></table> </div>");
	    	});*/			marker.openInfoWindowHtml("<div><h3 style='color:black'>Hotel Victoria Roma</h3></div>");	    	});		marker.openInfoWindowHtml("<div><h3 style='color:black'>Hotel Victoria Roma</h3></div>");
	},

	initPoints: function() {

		var e = function(marker, i) {
			return function() {
				marker.openInfoWindowHtml(Map.points[i].text);
			}
		}




		var latlng, marker;
		for(var i = 0; i<this.points.length; i++) {
			var blueIcon = new GIcon(G_DEFAULT_ICON);
			//blueIcon.image = "http://maps.google.com/mapfiles/ms/micons/blue-dot.png";
			//blueIcon.image = "http://maps.google.com/intl/ru_ALL/mapfiles/markerTransparent.png";
			//blueIcon.image = "images/google/marker";
			//blueIcon.image = "http://babotel.com/victoriahotel/images/google/marker1.png";
			blueIcon.shadow = "http://maps.google.com/intl/en_ALL/mapfiles/ms/micons/msmarker.shadow.png";
			blueIcon.shadowSize = new GSize(59,32);
			latlng = new GLatLng(this.points[i].lat,this.points[i].lon);
			if(i<=9){
				blueIcon.image = "images/google/marker"+(i+1)+".png";
				blueIcon.iconSize = new GSize(20, 34);
			}else{
				blueIcon.image = "http://maps.google.com/mapfiles/ms/micons/blue-dot.png";
				blueIcon.iconSize = new GSize(32, 32);
			}
			marker = new GMarker(latlng, {icon: blueIcon});
			this.markers[i] = marker;
		    this.map.addOverlay(marker);
			GEvent.addListener(marker, "click", e(marker, i));
		}
	},

	initSide: function() {
		var panel = $("#points");
		for (var i = 0; i < this.points.length; i++) {
			panel.append("<div class=\"markerHelper\" marker=\""+i+"\">"+(i+1)+") "+this.points[i].text+"</div>");
		}

		$('.markerHelper').bind('click', Map.helperClick);

	},

	handleDirErrors: function() {
		error = Map.directions.getStatus().code;

		if (error == 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: " + error);
		} else if (error == 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: " + error);
		} else if (error == 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: " + error);
		} else if (error == 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: " + error);
		} else if (error == G_GEO_BAD_REQUEST) {
			alert("A directions request could not be successfully parsed.\n Error code: " + error);
		} else {
			//alert("An unknown error occurred. Error code: " + error);
		}
	},

	onGDirectionsLoad : function() {

	},

	loadDir: function(from, to) {
		// As default, en_US locale is used.
		// TODO: add locale depending on site language
		this.directions.load("from: " + from + " to: " + to, {locale:"en_US"});
	},


	search: function() {
		from = $('#dir_from').attr('value');
		to = $('#dir_to').attr('value');
		if(from=="" || from==null){
			alert('The field "From" should be filled.');
			return;
		}
		if(to=="" || to==null){
			alert('The field "To" should be filled.');
			return;
		}
		Map.loadDir(from, to);

	},

	helperClick: function() {
		// 1. getMarker
		m_id = parseInt($(this).attr('marker'));
		marker = Map.markers[m_id];
		point = Map.points[m_id];
		// 2. make directions
		Map.loadDir(point.lat+", "+point.lon, "Via Campania, 41, 00187 Roma (RM), Italy (Hotel Victoria Roma)");
		// 3. Open marker window
		marker.openInfoWindowHtml(Map.points[m_id].text);
	},

	resizeMap: function() {
		var width = document.getElementById('content').offsetWidth;
		width = parseInt(width);
		//$('.mapElement').attr('width', (parseInt(width) - 220)+"px;");
		document.getElementById('mapElement').style.width = (width - 240);
		document.getElementById('map_contents').style.width = (width - 240);
		//$('#map_contents').attr('width', (parseInt(width) - 220)+"px;");
	}
}


google.load("maps", "2.x");
// Call this function when the page has been loaded
function init() {
	Map.initMap();
	Map.initMain();
	Map.initPoints();
	Map.initSide();	//Map.loadDir("41.905056, 12.480683", "Via Campania, 41, 00187 Roma (RM), Italy (Hotel Victoria Roma)");
}


/*
	directions = new GDirections(map);

    GEvent.addListener(directions , "load", onGDirectionsLoad);
    GEvent.addListener(directions , "error", handleErrors);

	// To provide result of directions, use appropriate locale
	directions.load("from: 41.905056, 12.480683 to: Via Campania, 41, 00187 Roma (RM), Italy (Hotel Victoria Roma)", {locale:"en_US"});
	//directions.load("from: 41.905056,12.480683 to: 41.909434,12.490154");
	//directions.load('from: New York, NY to: Chicago, IL');


	//map.setCenter(new google.maps.LatLng(41.909434,12.490154), 16);
}
*/



google.setOnLoadCallback(init);
