try
{
	google.load("maps", "2");
}
catch(e)
{

}


$(document).ready(function(){
	if($("#google_map_canvas").length > 0) {
		var showAllData = false;
		if($("#map_shop_detail_data").length > 0){
			showAllData = true;
		}
		initialize(showAllData);
		$(window).unload(function(){
			GUnload();
		});
	}
});

var marks = [];

var iconType = [];
var map;
var markerClusterer = null;

function showHideMarker(id, icon) {
	if (id.checked) {
		for(var i=0; i<marks[icon].length; i++) {
			marks[icon][i].show();
		}
	}
	else { 
		for(var i=0; i<marks[icon].length; i++) { 
			marks[icon][i].hide();
		}
	} 
}

function createMarker(point, icon, info_basic, info_adv, title, temperature, showAll) {
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.iconSize = new GSize(19, 32);
	
	temperature = Math.round(temperature);
	if(temperature < -8)
		temperature = -8;
	else if(temperature > 35)
		temperature = 35;
	
	
	blueIcon.image = httpPath+"templates/teplarnacb/images/icons/g-dots2/" + temperature + ".png";
	blueIcon.iconAnchor = new GPoint(9, 32);

	var mark;
	if(!showAll)
	{
		mark = new GMarker(point, {
			title: title, 
			icon: blueIcon,//iconType[icon], 
			hide: false
		});
	}
	else
	{
		mark = new GMarker(point, {
			title: title, 
			hide: false
		});	
	}
	if(marks[icon] == undefined) {
		marks[icon] = [];
	}
	marks[icon].push(mark);

	var tabs = [];
	tabs[0] = new GInfoWindowTab("Základní", info_basic);
	/*tabs[1] = new GInfoWindowTab("Další tab", info_adv);*/

	GEvent.addListener(mark, "click", function() {
		mark.openInfoWindowTabsHtml(tabs);
	});
	GEvent.addListener(mark, "dblclick", function() {
		map.setMapType(G_SATELLITE_MAP);
		map.setCenter(point, 17);
	});
	//mark.openInfoWindowHtml(tabs);

	return mark;
}

function initialize(showAll) {
	if(GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("google_map_canvas"));
		// stred CR -> 49.79545,15.457764
		var center_gps_x = 48.731398;
		var center_gps_y = 14.21113;
		var map_zoom = 11;

		if($("#gMapCenter").length > 0) {
			// přenastavení centrování
			var val = $("#gMapCenter").attr("class").replace("center-coordinates:","").split('/');
			center_gps_x = val[0];
			center_gps_y = val[1];
			map_zoom = 13;
		}
		
		var latlng = new GLatLng(center_gps_x, center_gps_y);
		var defaultIcon = new GIcon(G_DEFAULT_ICON);

		var lang_id = $("#google_map_canvas").attr("class").replace("lang_","");

		iconType[0] = new GIcon(defaultIcon);

		/*map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setMapType(G_PHYSICAL_MAP);*/
		map.addControl(new GLargeMapControl3D());

		/* Načtení markerů */
		
		function loadMarkers()
		{
			GDownloadUrl(httpPath+"ajax/presentation/google_maps/getXMLofPointsTeplarna.php?lang_id="+lang_id, function(data) {
				map.clearOverlays();
			
				var xml = GXml.parse(data);
				var stanice = xml.documentElement.getElementsByTagName("stanice");
				for(var i = 0; i < stanice.length; i++) {
					var title = stanice[i].getAttribute("name");
					var info_basic = '<p><strong>Stanice ' + title + '</strong></p>';
					var info_adv = "";
					var temperature = stanice[i].getAttribute("temperature");
					var items = stanice[i].getElementsByTagName("row");
					if(showAll && items.length > 0)
					{
						info_basic += '<ul style="font-size:10px;">';
						for(j = 0; j < items.length; j++)
						{
							var li_style = 'list-style-type:none;margin-left:0px;padding-left:15px;background:url(\'http://www.teplarna-cb.cz/templates/teplarnacb/images/icons/li-square.gif\') no-repeat left 5px;';
							info_basic += '<li style="' + li_style + '">';
							info_basic += '<strong>' + items[j].getAttribute('name') + ':</strong> ';
							info_basic += items[j].getAttribute('var') + ' ';
							info_basic += items[j].getAttribute('value');
							info_basic += '</li>';
						}
						info_basic += '</ul>';
					}
					else
					{
						info_basic += '<span style="font-size:20px;">' + (Math.round(temperature * 10 ) / 10) + '&deg;C</span>';
					}
					var icon = 1;
					var point = new GLatLng(parseFloat(stanice[i].getAttribute("lat")), parseFloat(stanice[i].getAttribute("lng")));
					var marker = createMarker(point, icon, info_basic, info_adv, title, temperature, showAll);
					map.addOverlay(marker);
				}
			});
		}
		loadMarkers();
		setInterval(function(){
			loadMarkers();
		}, 300000);	
		/* Konec načtení markerů */
		
		// cela cr -> 7
		map.setCenter(latlng, map_zoom, G_HYBRID_MAP);
		map.setUIToDefault();
		//map.setMapType(G_SATELLITE_MAP);
		GEvent.addListener(map, "singlerightclick", function(){ 
			map.setCenter(latlng, 11, G_HYBRID_MAP);
		//map.setMapType(G_SATELLITE_MAP); 
		});
	// map.enableGoogleBar();
	}
}
