var map;
var centerLatitude = 47.527760;
var centerLongitude = 19.118414;
var startZoom = 12;
var runing = 0;
var lastCity = 'budapest';
var gswla = 0;
var gswlo = 0;

	//create an icon for the clusters
	var iconCluster = new GIcon();
	iconCluster.image = "map_markergroup.png"; //"http://googlemapsbook.com/chapter7/icons/cluster.png";
	iconCluster.shadow = "http://googlemapsbook.com/chapter7/icons/cluster_shadow.png";
	//iconCluster.iconSize = new GSize(26, 25);
	iconCluster.iconSize = new GSize(27, 33);
	iconCluster.shadowSize = new GSize(22, 20);
	iconCluster.iconAnchor = new GPoint(13, 25);
	iconCluster.infoWindowAnchor = new GPoint(13, 1);
	iconCluster.infoShadowAnchor = new GPoint(26, 13);

	//create an icon for the pins
	var iconSingle = new GIcon();
	iconSingle.image = "map_marker.png"; //"http://googlemapsbook.com/chapter7/icons/single.png";
	iconSingle.shadow = "map_marker_shadow.png"; //"http://googlemapsbook.com/chapter7/icons/single_shadow.png";
	//iconSingle.iconSize = new GSize(12, 20);
	iconSingle.iconSize = new GSize(25, 35);
	iconSingle.shadowSize = new GSize(40, 34);
	iconSingle.iconAnchor = new GPoint(15, 20);
	iconSingle.infoWindowAnchor = new GPoint(13, 7);
	iconSingle.infoShadowAnchor = new GPoint(13, 13);

function init() 
{
	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
		updateMarkers();
		GEvent.addListener(map,'zoomend',function() 
		{
			//updateMarkers();
			updateMarkers($('#tipus option:selected').text(),$('#telepules').val(),$('#ar').val(),$('#mit option:selected').text());
		});
		GEvent.addListener(map,'moveend',function() 
		{
			updateMarkers($('#tipus option:selected').text(),$('#telepules').val(),$('#ar').val(),$('#mit option:selected').text());
			//updateMarkers();
		});
		//G_SATELLITE_MAP.getMinimumResolution = function(){return 6};
		//G_SATELLITE_MAP.getMaximumResolution = function(){return 12}; 
		G_NORMAL_MAP.getMinimumResolution = function(){return 8};
		G_NORMAL_MAP.getMaximumResolution = function(){return 18}; 
		
		/**
		 * Checkbox <div> made a GControl()
 		*/
		function MenuControl(){};
		MenuControl.prototype = new GControl();
		MenuControl.prototype.initialize = function(map)
		{
  			var more = document.getElementById("header");
  			map.getContainer().appendChild(more);
  			return more;
		}
		MenuControl.prototype.getDefaultPosition = function() 
		{
  			return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(100, 10));
		}

		map.addControl(new MenuControl());



	}	
}

function updateMarkers(tipus,telepules,ar,mit) 
{
	if (runing==1)
		return;
	else
		runing = 1;
		
	var tipvar = '';
	if (tipus != null)
   		  tipvar = '&ti=' + tipus;

	var telepvar = '';
	if (telepules != null)
	{
		if(telepules.toLowerCase()!=lastCity)
		{
   		  telepvar = '&te=' + telepules;
   		  geoEncode(telepules);
   		  lastCity = telepules;
   		}
   	}
   	var arvar = '';
	if (ar != null)
   		  arvar = '&ar=' + ar;
   	
	var mitvar = '';
	if (mit != null)
   		  mitvar = '&mi=' + mit;
   		  
	
	//create the boundary for the data to provide
	//initial filtering
	var bounds = map.getBounds();
	//var zoom = map.getBoundsZoomLevel(bounds);
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();

	var swla = southWest.lat();
	var swlo = southWest.lng();
	if ((gswla!=0 && gswlo!=0) )	
	{
		if(Math.abs(gswla-swla)>0.007 || Math.abs(gswlo-swlo)>0.007)	
		{
			//remove the existing points
			map.clearOverlays();
	
			var getVars = 'ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue() + tipvar + telepvar + arvar + mitvar;
			//log the URL for testing
			//GLog.writeUrl('http://ing.atlan.hu/map/cluster.php?'+getVars);
			//retrieve the points
			var request = GXmlHttp.create();
			request.open('GET', 'http://webdomus.hu/map/cluster.php?'+getVars, true);
			request.onreadystatechange = function() 
			{
				if (request.readyState == 4) 
				{
					var jscript = request.responseText;
					var points;
					eval(jscript);
					//create each point from the list
					for (i in points) 
					{
						var point = new GLatLng(points[i].lat,points[i].lng);
						//var marker = createMarker(point,points[i].type);
						var marker = createMarker(point,points[i].type,points[i].id,points[i].kep,points[i].ar,points[i].sz);
						map.addOverlay(marker);
					}
				}
			}
			request.send(null);
			runing = 0;
			
		}
	}
	else
	{
		gswla = swla; //az első alkalommal beállítjuk
		gswlo = swlo; //az első alkalommal beállítjuk
		
		//remove the existing points
			map.clearOverlays();
	
			var getVars = 'ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue() + tipvar + telepvar + arvar + mitvar;
			//log the URL for testing
			//GLog.writeUrl('http://ing.atlan.hu/map/cluster.php?'+getVars);
			//retrieve the points
			var request = GXmlHttp.create();
			request.open('GET', 'http://webdomus.hu/map/cluster.php?'+getVars, true);
			request.onreadystatechange = function() 
			{
				if (request.readyState == 4) 
				{
					var jscript = request.responseText;
					var points;
					eval(jscript);
					//create each point from the list
					for (i in points) 
					{
						var point = new GLatLng(points[i].lat,points[i].lng);
						//var marker = createMarker(point,points[i].type);
						var marker = createMarker(point,points[i].type,points[i].id,points[i].kep,points[i].ar,points[i].sz);
						map.addOverlay(marker);
					}
				}
			}
			request.send(null);
			runing = 0;
	}
	
}


function updateMarkersFilter(tipus,telepules,ar,mit) 
{
	if (runing==1)
		return;
	else
		runing = 1;
		
	var tipvar = '';
	if (tipus != null)
   		  tipvar = '&ti=' + tipus;

	var telepvar = '';
	if (telepules != null)
	{
		if(telepules.toLowerCase()!=lastCity)
		{
   		  telepvar = '&te=' + telepules;
   		  geoEncode(telepules);
   		  lastCity = telepules;
   		}
   	}
   	var arvar = '';
	if (ar != null)
   		  arvar = '&ar=' + ar;
   	
	var mitvar = '';
	if (mit != null)
   		  mitvar = '&mi=' + mit;
   		  
	
	//create the boundary for the data to provide
	//initial filtering
	var bounds = map.getBounds();
	//var zoom = map.getBoundsZoomLevel(bounds);
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	
			//remove the existing points
			map.clearOverlays();
	
			var getVars = 'ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue() + tipvar + telepvar + arvar + mitvar;
			//log the URL for testing
			//GLog.writeUrl('http://ing.atlan.hu/map/cluster.php?'+getVars);
			//retrieve the points
			var request = GXmlHttp.create();
			request.open('GET', 'http://webdomus.hu/map/cluster.php?'+getVars, true);
			request.onreadystatechange = function() 
			{
				if (request.readyState == 4) 
				{
					var jscript = request.responseText;
					var points;
					eval(jscript);
					//create each point from the list
					for (i in points) 
					{
						var point = new GLatLng(points[i].lat,points[i].lng);
						//var marker = createMarker(point,points[i].type);
						var marker = createMarker(point,points[i].type,points[i].id,points[i].kep,points[i].ar,points[i].sz);
						map.addOverlay(marker);
					}
				}
			}
			request.send(null);
			runing = 0;
			
	
	
}

function createMarker(point, type, id, kep, ar, sz) 
//function createMarker(point, type) 
{
	//create the marker with the appropriate icon
	if(type=='c') 
	{
		var marker = new GMarker(point,iconCluster);
		GEvent.addListener(marker, 'click',	function() {marker.openInfoWindowHtml('<p><b>Itt több ingatlanhirdetés található,<br /> nézd meg közelebbről a zoom segítségével!</b></p><p>(A bal felső sarokban: +/-)</p>');}	);
	} 
	else 
	{
		var marker = new GMarker(point,iconSingle);
		if($('#mit option:selected').text()=="eladó")
			GEvent.addListener(marker, 'click',	function() 
			{
				marker.openInfoWindowHtml('<div style="height:200px;width:250px;"><table><tr><td><a href="http://webdomus.hu/elado/'+id+'" target="_blank"><img src="'+ kep + '" style="width:120px;height:auto;" /></a></td><td style="padding:5px;"><h3>'+ar+' M Ft</h3><br /><h3>'+sz+'</h3></td></tr></table><h3><a href="http://webdomus.hu/elado/'+id+'" target="_blank">Részletek...</a></h3></div>');
				
			});
		else
			GEvent.addListener(marker, 'click',	function() 
			{
				marker.openInfoWindowHtml('<div style="height:200px;width:250px;"><table><tr><td><a href="http://webdomus.hu/kiado/'+id+'" target="_blank"><img src="'+ kep + '" style="width:120px;height:auto;" /></a></td><td style="padding:5px;"><h3>'+ar+' Ft</h3><br /><h3>'+sz+'</h3></td></tr></table><h3><a href="http://webdomus.hu/kiado/'+id+'" target="_blank">Részletek...</a></h3></div>');
				
			});
	}
	return marker;
}

function geoEncode(varos) 
{
	
	var geo = new GClientGeocoder(); 
	geo.getLocations(varos, function (result){
		if (result.Status.code == G_GEO_SUCCESS) {
			geocode = result.Placemark[0].Point.coordinates;
			var location = new GLatLng(geocode[1],geocode[0]);
			//map.removeOverlay(marker);
			map.setCenter(location, 12);
			
			//marker = new GMarker(location);
			//marker.setLatLng(location);
			//map.addOverlay(marker);
			
			
		} 
		/*else {
			var reason="Code "+result.Status.code;
			if (reasons[result.Status.code]) {
				reason = reasons[result.Status.code]
			} 
			//$("#add-point .error").html(reason).fadeIn();
			//geocode = false;
		}*/
	});
}

window.onload = init;
window.onunload = GUnload;

