var Map = {

	map: null,

	directions: null,

	points: [							   

		{'text':'Golf Club Jesolo', 'lat':'45.511159', 'lon':'12.603722'},

		{'text':'Aqualandia', 'lat':'45.499128', 'lon':'12.604065'},

		{'text':'Pista Azzurra', 'lat':'45.5114', 'lon':'12.628784'},

		{'text':'Jesolo Tourist Port', 'lat':'45.493834', 'lon':'12.586212'},

		{'text':'Congress Centre', 'lat':'45.505384', 'lon':'12.634277'},

		{'text':'Play Village', 'lat':'45.513565', 'lon':'12.641144'},

		{'text':'Marina Club', 'lat':'45.500572', 'lon':'12.594109'},

		{'text':'Villa Giulia Beauty & Wellness', 'lat':'45.545553', 'lon':'12.638741'},

		{'text':'Cortellazzo ', 'lat':'45.543148', 'lon':'12.721825'},

		{'text':'Nautica Boat Service', 'lat':'45.537137', 'lon':'12.722511'},

		{'text':'Pineta', 'lat':'45.530884', 'lon':'12.709122'}

	],

	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(45.426445,12.362741), 13);

		this.map.setCenter(new google.maps.LatLng(45.899517,8.51099),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(45.899517,8.51099);

		var marker = new GMarker(latlng)



	    this.map.addOverlay(marker);



		GEvent.addListener(marker, "click", function() {

			marker.openInfoWindowHtml("<div><h5 style='color:black'>Lido Palace</h3></div>");

	    	});

		marker.openInfoWindowHtml("<div><h5 style='color:black'>Lido Palace</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<=21){

				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 = document.getElementById('points');
//		var str="";
//		for (var i = 0; i < this.points.length; i++) {
//			
//			str=str+"<div class=\"markerHelper\" onclick='Map.helperClick("+i+")' marker=\""+i+"\">"+(i+1)+") "+this.points[i].text+"</div>";
//			
//		}
//        
//		panel.innerHTML=str;
//		
//		//$('.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 = document.getElementById('dir_from').value;

		to = document.getElementById('dir_to').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(m_id) {

		// 1. getMarker
		
		//m_id = parseInt($(this).attr('marker'));

		//alert(m_id);
		marker = Map.markers[m_id];

		point = Map.points[m_id];

		// 2. make directions

		//Map.loadDir(point.lat+", "+point.lon, "Hotel Sant`Elena - Calle Buccari 10 - Sant`Elena - 30132 Venezia");

		Map.loadDir(point.lat+", "+point.lon, "Via Levantina, 2° acc. al mare, 3 30016 Lido di Jesolo (VE)");

		// 3. Open marker window

		marker.openInfoWindowHtml(Map.points[m_id].text);

	},



	resizeMap: function() {

		var width = document.getElementById('container').offsetWidth;

		width = parseInt(width);

		//$('.mapElement').attr('width', (parseInt(width) - 220)+"px;");

		document.getElementById('map_contents').style.width = 500;//(width - 192);//(width - 240); were 130 ! change 06.08 for ie compability

		document.getElementById('mapElement').style.width = 500;//(width - 192);//(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);


