
var gMap = function() {

	var map; /* mapový objekt */
	var geocoder; /* objekt převádějící adresu na souřadnice */
	var directions; /* počítá cestu mezi body */

	var activeLatLng = null; /* souřadnice aktuálně hledaného místa */
	var activeMarker = null; /* aktivní značka - z otevřenou bublinou */
	var points = new Array(); /* pole obsahující všechny body zájmu */
	var searchUsed = false; /* zda bylo použito hledání pro určení výchozího místa */

	var initialize = function() {
		map = new google.maps.Map2(document.getElementById("google-map"));
		map.setUIToDefault();

		geocoder = new google.maps.ClientGeocoder();
		geocoder.setBaseCountryCode('cs');

//		directions = new google.maps.Directions(map, document.getElementById("gmap_direction"));
		//GEvent.addListener(directions, "load", onDirectionsLoad);
	//	$('#gmap_direction_panel').hide();

		map.setCenter(new GLatLng(49.837982, 15.458887), 6);
		// přidání tlačítek pro rychlé zoomování
		addZoomButton('Hl.m. Praha',new GLatLng(50.07, 14.420242), 8);
		addZoomButton('Jihomoravský kraj',new GLatLng(49.189782, 16.610298), 7);
		addZoomButton('Zlínský kraj',new GLatLng(49.224772722794825, 17.655029296875), 8);
		
		addZoomButton('Ostrava',new GLatLng(49.825581, 18.29052), 8);
		addZoomButton('Celá ČR',new GLatLng(49.837982, 15.458887), 6);

		$('#gMap').submit(function() {
			showLocation();			
			return false;
		});
		

	  GEvent.addListener(map, "moveend", function() {
			//if (activeMarker == null) {
			//	printNearest(null, map.getCenter());
			//}
			//GLog.write(map.getCenter());
	  });

		showMarkers();

	};

	var addZoomButton = function(name, gLatLong, gLevel) {
			var button = document.createElement('option');
			button.innerHTML = name;
			$('#areaSelect').append(button);

			$(button).click(function() {
				if (!searchUsed) {
					activeLatLng = gLatLong;
				}
				map.setCenter(gLatLong, gLevel);
				printNearest();
			});
	}

	/**
	 * Vytvoří grafickou podobu bodu zájmu
	 */
	var createMarker = function(point, id, is_sale, is_service, data) {
		var mIcon = new google.maps.Icon(G_DEFAULT_ICON);
		if (is_service == 1) mIcon.image = "/gfx/servis-02.png";
		if (is_sale == 1) mIcon.image = "/gfx/sale-02.png";
		if (is_sale == 1 & is_service == 1) mIcon.image = "/gfx/sale-service.png";

		mIcon.iconSize = new google.maps.Size(23, 27);

		// Set up our GMarkerOptions object
		markerOptions = {
				icon:mIcon
		};
		var marker = new google.maps.Marker(point, markerOptions);
		marker.data = data;

		// Pri kliknutí se zobrazi infobox
		GEvent.addListener(marker, "click", function() {
			activeMarker = marker;
			printNearest(id, marker.getLatLng());
			marker.openInfoWindowHtml(this.data + routeButton(id), {
				maxWidth: 300
			});
		});
		
		GEvent.addListener(marker, 'infowindowclose', function() {
			activeMarker = null;
		});

		return marker;		
	};

	/**
	 * Vyhledá adresu zadanou ve formuláři
	 */
	var showLocation = function() {
		var address = document.forms['gMap'].search.value;
		geocoder.getLocations(address, addAddressToMap);		
		return false;
	};

	this.setActiveTo = function(id) {
		for (var c in points) {
			var p = points[c];
			if (p.id == id) {
				activeLatLng = new google.maps.LatLng(p.latitude, p.longitude);
			}
		}
		printNearest(id);
	}

	/**
	 * Zobrazí výsledek hledání na mapě
	 */
	var addAddressToMap = function(response) {
		
		if (!response || response.Status.code != 200) {
			text = 'Zadaná adresa nebyla nalezena';
			alert(text);
		} else {
			searchUsed = true;
			var place = response.Placemark[0];
			activeLatLng = new google.maps.LatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
			//var marker = new google.maps.Marker(activeLatLng);
			map.setCenter(activeLatLng, 10);

			printNearest();
		}
	};

	/**
	 * Zobrazí body přidané přes addPoint na mapě.
	 */ 
	var showMarkers = function() {
		for (var c in points) {
			var p = points[c];
			map.addOverlay(
					createMarker(
							new google.maps.LatLng(p.latitude, p.longitude), p.id, p.options.is_sale, p.options.is_service, p.text
					)
			);
		}
	};

	/**
	 * Veřejná metoda pro přidání nového bodu
	 */
	this.addPoint = function(id, latitude, longitude, text, options) {
		points[points.length] = {'id': id, 'latitude': latitude, 'longitude': longitude, 'text': text, 'options': options};
	};

	this.mapTo = function (lat, lng) {
		var point = new GLatLng(lat, lng);
		map.setCenter(point, 11);				
		map.addOverlay(new GMarker(point));
		//map.addOverlay(createMarker(new google.maps.LatLng(lat, lng), 1, 1, 0, 'Mlekarna'));
	}
	// inicializace
	google.load("maps", "2",{"other_params":"sensor=false"});
	google.setOnLoadCallback(initialize);

};

var myMap = new gMap();
