var oCluster;
var aPolyRegion = null;
var oMapa;
var clusterMarkerIcon;
var bChangeMapZoom = true;

function newMarker(markerLocation, aLokalizacja) 
{
	var baseIcon = new GIcon();  
	baseIcon.iconSize = new GSize(parseInt(aLokalizacja[4][2]), parseInt(aLokalizacja[4][3])); //32,32);  
	baseIcon.iconAnchor = new GPoint(parseInt(aLokalizacja[4][4]), parseInt(aLokalizacja[4][5])); //16,32);  
	baseIcon.infoWindowAnchor = new GPoint(9,31);; 

	var oIcon = new GIcon(baseIcon, 
			aLokalizacja[4][0], // 'http://maps.google.com/mapfiles/kml/pal5/icon6.png'
			null,				 //
			aLokalizacja[4][1] // 'http://maps.google.com/mapfiles/kml/pal5/icon6s.png' , icon: oIcon
	);

	var oMarker = new GMarker(markerLocation, {title: aLokalizacja[2], icon: oIcon});
	GEvent.addListener(oMarker, 'click', function() {
		oMarker.openInfoWindowHtml(aLokalizacja[3]);
	});
	return oMarker;
} 

function silnikMapy(aMarkery, aGrafiki)
{
	if(aGrafiki[0] != '')
	{
		clusterMarkerIcon=new GIcon();
		clusterMarkerIcon.image = aGrafiki[0];
		clusterMarkerIcon.iconSize = new GSize(parseInt(aGrafiki[1]), parseInt(aGrafiki[2]));
		clusterMarkerIcon.iconAnchor = new GPoint(9,31);
		clusterMarkerIcon.infoWindowAnchor=new GPoint(9,31);
	}

	oMapa = new GMap2(document.getElementById('mapaGoogle'));
	oMapa.setCenter(new GLatLng(51.917168,19.160156),6);

	oMapa.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(7, 7)));
	oMapa.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 28)));
	
	var aMarker, aTablicaMarkerow = [];

	for (var i=0; i<aMarkery.length; i++) 
	{
		aMarker = newMarker(new GLatLng(aMarkery[i][0], aMarkery[i][1]), aMarkery[i]);
		aTablicaMarkerow.push(aMarker);
	}

	var sHtml='<div class="htmlControlCluster" style="padding:0px 3px 3px 3px">Włącz grupowanie: <input type="checkbox" checked="checked" onclick="toggleClustering()" /></div>';
	var oControl=new HtmlControl(sHtml);
	oMapa.addControl(oControl, new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7,7)));

	oCluster = new ClusterMarker(oMapa, { minClusterSize: 2, clusterMarkerIcon: clusterMarkerIcon });
	oCluster.clusterMarkerTitle='W tym miejscu jest %count lokalizacje';
	oCluster.clusterMarkerIcon = clusterMarkerIcon;
	oCluster.removeMarkers();
	oCluster.addMarkers(aTablicaMarkerow);
	oCluster.fitMapToMarkers(true);
	
	oMapa.savePosition();
}

function toggleClustering() 
{
	oCluster.clusteringEnabled = !oCluster.clusteringEnabled;
	oCluster.refresh(true);
}

function usunRegiony() 
{
    if(aPolyRegion !== null) 
        oMapa.removeOverlay(aPolyRegion)
}

function zaladujMiasto(sNazwa)
{
	if(sNazwa != 0)
	{
		var geo = new GClientGeocoder();
		geo.getLatLng(sNazwa, function(aPunkt){
			if (aPunkt)
			{
				console.log(aPunkt);
			
				var lat = aPunkt.lat();
				var lng = aPunkt.lng();
				
				oMapa.setZoom(10);
				bChangeMapZoom=false;
				oMapa.panTo(new GLatLng(lat,lng));
				
				GEvent.addListener(oMapa, "moveend", function(){
					GEvent.clearListeners(oMapa,"moveend");
				});
			}
			else
			{
				alert("Brak współrzędnych geolokalizacyjnych dla tych danych");
			}
		});
	}
}

function zaladujWojewodzta(iIdWoj)
{
	usunRegiony();
	if(iIdWoj > 0)
	{
		$.getScript("modules/kontakt/wtyczki/googlemapslist/js/baza/"+iIdWoj+".js", function(data, textStatus) {
			eval("var aRegionObj = " + data);
			eval("aPolyRegion = new GPolyline("+aRegionObj[0]["region"]+",\"#000\")");
			oMapa.addOverlay(aPolyRegion);
			
			if((iIdWoj == 13) || (iIdWoj == 8)) 
			{
				oMapa.setZoom(8);
				bChangeMapZoom=false;
			} 
			else 
			{
				oMapa.setZoom(7);
				bChangeMapZoom=false;
			}
			
			eval("oMapa.panTo("+aRegionObj[0]["center"]+")");
			GEvent.addListener(oMapa, "moveend", function(){
				GEvent.clearListeners(oMapa,"moveend");
			});
		});
	}
	else
	{
		oMapa.setCenter(new GLatLng(51.917168,19.160156),6);
	}
}
