$(document).ready
(
	function() 
	{
		$('#box_standortekarte').mouseover(function()
		{
			$("select.citychooser").blur();
		});
		
		$('select.citychooser option').mouseover(function()
		{
			//alert($(this).attr("value"));
			var optval = $(this).attr("value") != "" ? $(this).attr("value") : $(this).value();

			var encodedcity = encodeCityFromSelect(optval.toLowerCase());
			if(encodedcity != "")
				hervorhebestandort(encodedcity);
		});
		$('select.citychooser option').mouseout(function()
		{
			var encodedcity = encodeCityFromSelect($(this).attr("value").toLowerCase());
			if(encodedcity != "")
				unhervorhebestandort(encodedcity);
		});
	}
);



var standortekartenmarker = [];
function zeigestandortebox()
{
	if( ! $('#box_standortekarte').is(':visible') )
	{
		$('#box_standortekarte').slideDown('normal', function()
		{
			$.post
			(
				"/widgets/wdg.places2/ajax.standortdaten.php",
				{
				},
				function(data)
				{
					var locations = jQuery.parseJSON(data);

					var map = new google.maps.Map(document.getElementById('map_canvas'), 
					{
						zoom: 1,
						center: new google.maps.LatLng(0, 0),
						mapTypeId: google.maps.MapTypeId.ROADMAP,
						mapTypeControl: false,
						streetViewControl: false
					});

					//var infowindow = new google.maps.InfoWindow();
					var bounds = new google.maps.LatLngBounds();
			
					for (var i = 0; i < locations.length; i++) 
					{
						var place = locations[i];
						var myLatLng = new google.maps.LatLng(parseFloat(place[1]), parseFloat(place[2]));
						var image = new google.maps.MarkerImage(
							"http://www.rwtuev-akademie.de/templates/files/gmapsiconklein.png",
							// This marker is 71 pixels wide by 50 pixels tall.
							new google.maps.Size(30, 24),
							// The origin for this image is 0,0.
							new google.maps.Point(0,0),
							// The anchor for this image is the base of the flagpole at 24,50.
							new google.maps.Point(15, 24)
						);
						var shadow = new google.maps.MarkerImage(
							"http://www.rwtuev-akademie.de/templates/files/gmapsiconkleinschatten.png",
							// This marker is 71 pixels wide by 50 pixels tall.
							new google.maps.Size(40, 24),
							// The origin for this image is 0,0.
							new google.maps.Point(0,0),
							// The anchor for this image is the base of the flagpole at 24,50.
							new google.maps.Point(15, 24)
						);
						var shape = 
						{
							coord: [24,48,16,29,6,26,1,18,0,11,9,5,17,1,25,0,37,2,45,6,49,15,46,23,39,28,32,31],
							type: 'poly'
						};
						var marker = new google.maps.Marker({
							position: myLatLng,
							map: map,
							shadow: shadow,
							icon: image,
							/*shape: shape,*/
							/*icon: '/widgets/wdg.places2/information.png',*/
							title: "Bildungszentrum "+place[0],
							/*zIndex: place[3],*/
							dsturl: "/lehrgaenge_und_umschulungen/alle/"+place[4]+"/",
							cityencoded: place[4],
							city: place[0],
						});
						
						google.maps.event.addListener(marker, 'mouseover', (function(marker, i)
						{
							return function()
							{	
								hervorhebestandort(this.cityencoded);
								if( ! $("select.citychooser").attr("staticvalue") )
								{
									var selval = $("select.citychooser option:selected").val();
									//alert(selval);
									$("select.citychooser").attr({staticvalue:selval});
									
								}
								$("select.citychooser option[value='"+this.city+"']").attr('selected',true);

							}
						})(marker, i));

						google.maps.event.addListener(marker, 'mouseout', (function(marker, i)
						{
							return function()
							{	
								unhervorhebestandort(this.cityencoded);

								var sv = $("select.citychooser").attr("staticvalue");
								$("select.citychooser option[value='"+sv+"']").attr('selected',true);
							}
						})(marker, i));

						google.maps.event.addListener(marker, 'click', (function(marker, i)
						{
							return function()
							{
								hervorhebestandort(this.cityencoded);
								document.location.href = ""+this.dsturl;
							}
						})(marker, i));

						standortekartenmarker[place[4]] = marker;

						bounds.extend(myLatLng);
					}
					map.fitBounds(bounds);
					var listener = google.maps.event.addListener(map, "idle", function()
					{ 
						map.setZoom(map.getZoom()+1); 
						google.maps.event.removeListener(listener);
					});
				}
			);
		});
	}
}

function hervorhebestandort(placekey)
{
	//alert(placekey);
	standortekartenmarker[placekey].setIcon("http://www.rwtuev-akademie.de/templates/files/gmapsiconkleinaktiv.png");
}

function unhervorhebestandort(placekey)
{
	standortekartenmarker[placekey].setIcon("http://www.rwtuev-akademie.de/templates/files/gmapsiconklein.png");
}

function versteckestandortebox()
{
	return false;
	if( $('#box_standortekarte').is(':visible') )
	{
		$('#box_standortekarte').hide();
	}
}
