$(document).ready(function()
{	
	$('#districtImage_0').show(); // Anfangs das Übersichtsbild anzeigen
	
	$('.districtMapLink').mouseover ( mapLinkOver );
	$('.districtText').mouseover ( mapLinkOver );
	//$('.districtResultDot').mouseover ( mapLinkOver );
	$('.districtMapLink').mouseout ( mapOut );
	$('.districtMapLink').click ( mapLinkClicked );
	//$('.districtResultDot').click ( mapLinkClicked );
	//$('.districtResultDot').each ( positionResultDot );
});

function positionResultDot ()
{
	var imgOffset = $('#districtMapContainer').offset();
	
	var provinceID = parseInt ($(this).attr ("provinceID"));
	var districtID = parseInt ($(this).attr ("districtID"));
	var mapLink = null;
	
	$('.districtMapLink').each ( function () 
	{
		var pid = $(this).attr ("provinceID");
		var did = $(this).attr ("districtID");
		
		if ( (provinceID == pid) && (districtID == did) )
		{
			mapLink = $(this);
		}
	});
	
	if (mapLink != null)
	{
		var coords = mapLink.attr ("coords");
		var vals = coords.split(",");
		var maxX = -1;
		var minX = 9999999;
		var maxY = -1;
		var minY = 9999999;
		
		for (i = 0; i < vals.length; i++)
		{
			var val = parseInt (vals[i]);
			
			if (i % 2 == 0)
			// x-wert
			{
				if (val > maxX)
				{
					maxX = val;
				}
				
				if (val < minX)
				{
					minX = val;
				}				
			}
			else
			// y-wert
			{
				if (val > maxY)
				{
					maxY = val;
				}
				
				if (val < minY)
				{
					minY = val;
				}				
			}
		}

		var x = (minX + maxX) / 2 + imgOffset.left;
		var y = (minY + maxY) / 2 + imgOffset.top;
		
//		alert (provinceID + "\nX: " + minX + ", " + maxX + " => " + x + "\nY: " + minY + ", " + maxY + " => " + y);
		
//		var x = imgOffset.left;// - ( $('#districtImage_0').width() / 2 );
//		var y = imgOffset.top;// - ( $('#districtImage_0').height() / 2 );
	
		
		$(this).css ("left", x + "px");
		$(this).css ("top", y + "px");

	}
}

function mapLinkClicked ()
{
	var provinceID = parseInt ($(this).attr ("provinceID"));
	var districtID = parseInt ($(this).attr ("districtID"));
	
	var url = "index.php?side=hsuche&provinceID=" + provinceID + "&districtID=" + districtID;
	
	if (districtID > 0)
	{
		url += "&displayResult=true#resultTable";
	}
	
	//window.location.href = url;
	
}

function mapLinkOver ()
{
	var provinceID = parseInt ($(this).attr ("provinceID"));
	var districtID = parseInt ($(this).attr ("districtID"));
	
	$('#districtResultDot_' + provinceID + '_' + districtID).hide();

	$('.districtText').removeClass("highlight");
	$('.districtImage').hide();
	$('.districtInfo').hide();
	$('#districtInfo_0').show();
	
	//alert("Over " + $('.districtImage').size());
	
	if (districtID > 0)
	{
		$('#districtInfo_' + districtID).show();
		$('#districtImage_' + districtID).show();
		$('#districtText' + districtID).addClass("highlight");
	}
	else if (provinceID > 0)
	{
		$('#districtInfo_' + provinceID).show();
		$('#districtImage_' + provinceID).show();
		$('#districtText' + provinceID).addClass("highlight");
	}
}

function mapOut ()
{
	$('.districtImage').hide();
	$('#districtImage_0').show();
	//alert($('.districtImage').size());
	$('.districtInfo').hide();
	$('#districtInfo_0').show();
	$('.districtResultDot').show();
	$('.districtText').removeClass("highlight");

}
