
function getPicon(type) {
	var icon = new GIcon(G_DEFAULT_ICON);
	if (type=="parking") {
		icon.image = "/images/parking.png";
	} else {
		icon.image = "/images/garage.png";
	}
	icon.shadow=null;
	icon.iconSize = new GSize(27, 27);
	icon.iconAnchor = new GPoint(15, 27);
	icon.infoWindowAnchor =new GPoint(15, 10);
	return icon;
}



function listScroller(idElement)
{

	var divOffset = $('#list').offset().top;
	var pOffset = $('#list .plist:eq('+ idElement +')').offset().top;
	var pScroll = pOffset - divOffset - 10;
	$('#list').animate({scrollTop: '+=' + pScroll + 'px'}, 500);
	$("#list li").css("background","#f7f7f7");
	$("#list li:eq("+ idElement +")").css("background","#eee");
}


function drawCircle(center, radius, nodes, liColor, liWidth, liOpa, fillColor, fillOpa)
{
// Esa 2006
	//calculating km/degree
	var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
	var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;

	//Loop
	var points = [];
	var step = parseInt(360/nodes)||10;
	for(var i=0; i<=360; i+=step)
	{
	var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), center.lng() +
	(radius/lngConv * Math.sin(i * Math.PI/180)));
	points.push(pint);
	bounds.extend(pint); //this is for fit function
	}
	points.push(points[0]); // Closes the circle, thanks Martin
	fillColor = fillColor||liColor||"#3399ff";
	liWidth = liWidth||2;
	var poly = new GPolygon(points,liColor,liWidth,liOpa,fillColor,fillOpa);
	map.addOverlay(poly);
}


(function($) {

	parkeditMap=function(response) {
		if (!response || response.Status.code != 200) {
			alert("Nie udalo sie zdekodowac adresu");
		} else {
			place = response.Placemark[0];
			//if (place.AddressDetails.Accuracy >= 4) {
				address = place.address.split(",");
				var city = address[address.length-2];
				city = city.replace(/^\s*[0-9\-]+\s*/,'');
				city = city.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

				if (city.length>0) {
					$("input[name='pcity']").val(city);
					if (place.AddressDetails.Accuracy >= 6) {
						if (typeof(place.AddressDetails.Country.AdministrativeArea) != 'undefined'){
						
							var street = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
							$("input[name='paddr']").val(street);
						}
					}
				}
				

			//}
		}
	}

	garageeditMap=function(response) {
		if (!response || response.Status.code != 200) {
			alert("Nie udalo sie zdekodowac adresu");
		} else {
			place = response.Placemark[0];
			//if (place.AddressDetails.Accuracy >= 4) {
				address = place.address.split(",");
				var city = address[address.length-2];
				city = city.replace(/^\s*[0-9\-]+\s*/,'');
				city = city.replace(/^\s\s*/,'').replace(/\s\s*$/,'');
				if (city.length>0) {
					$("input[name='gcity']").val(city);
					if (place.AddressDetails.Accuracy >= 6) {
						if (typeof(place.AddressDetails.Country.AdministrativeArea) != 'undefined'){
		
							var street = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
							$("input[name='gaddr']").val(street);
						}
					}
				}
					
			//}
		}
	}
	
	moveInfo=function(response) {
		if (!response || response.Status.code != 200) {
			alert("Nie udalo sie zdekodowac adresu");
		} else {
			place = response.Placemark[0];
			var street="";
			if ((typeof(place.AddressDetails)!='undefined') && (place.AddressDetails.Accuracy >= 6)) {
				if (typeof(place.AddressDetails.Country.AdministrativeArea)!='undefined') { 
					street = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
					street = street.replace(/\/+/, ' ');
				}
			}
			var address = place.address.split(",");
			var city = address[address.length-2];
			
			city = city.replace(/^\s*[0-9\-]+\s*/,'');
			city = city.replace(/^\s\s*/,'').replace(/\s\s*$/,'');
			street.length>0 ? street=street + "/" : street ="";
			city.length>0 ? city=city +"/" : city ="";
		
			street=street.replace(/(\s+)/g,'+');
			city=city.replace(/(\s+)/g,'+');
		
			var url = window.location.toString();
			var query_string = url.split("?");
			var qsa="";
			if (query_string.length > 1) {
				qsa = '?' + query_string[1];
			}
			
			
			$(location).attr("href", "/parking/"+ city + street + "r" +range +"/lt" + place.Point.coordinates[1] + "/lg" + place.Point.coordinates[0] + qsa);
			
		}

	}
	
	movegInfo=function(response) {
		if (!response || response.Status.code != 200) {
			alert("Nie udalo sie zdekodowac adresu");
		} else {
			place = response.Placemark[0];
			var street="";
			if ((typeof(place.AddressDetails)!='undefined') && (place.AddressDetails.Accuracy >= 6)) {
				if (typeof(place.AddressDetails.Country.AdministrativeArea)!='undefined') { 
					street = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
					street = street.replace(/\/+/, ' ');
				}
			}
			var address = place.address.split(",");
			var city = address[address.length-2];
			
			city = city.replace(/^\s*[0-9\-]+\s*/,'');
			city = city.replace(/^\s\s*/,'').replace(/\s\s*$/,'');
			street.length>0 ? street=street + "/" : street ="";
			city.length>0 ? city=city +"/" : city ="";

			street=street.replace(/(\s+)/g,'+');
			city=city.replace(/(\s+)/g,'+');
		
			var url = window.location.toString();
			var query_string = url.split("?");
			var qsa="";
			if (query_string.length > 1) {
				qsa = '?' + query_string[1];
			}
			$(location).attr("href", "/garaz/"+ city +  street + "r" +range +"/lt" + place.Point.coordinates[1] + "/lg" + place.Point.coordinates[0] + qsa);
	
		}

	}
})(jQuery);

function mark_parking(lat,lng,city) {
		var point = new GLatLng(lat,lng);
		marker = new GMarker(point,{draggable: true });
		map.clearOverlays();
		map.addOverlay(marker);
		map.setCenter(point, 12);
		GEvent.addListener(marker, "dragend", function(position) {
			geo = new GClientGeocoder(new GGeocodeCache());
			geo.getLocations(position, parkeditMap);
		});
		$("input[name='plat']").val(lat);
		$("input[name='plng']").val(lng);
					
		$("input[name='pcity']").val(city);
		
		if ($('#didumean').is(':visible')) {
			$('#didumean').hide();
		}
}

function mark_garage(lat,lng,city) {
		var point = new GLatLng(lat,lng);
		marker = new GMarker(point,{draggable: true });
		map.clearOverlays();
		map.addOverlay(marker);
		map.setCenter(point, 12);
		GEvent.addListener(marker, "dragend", function(position) {
			geo = new GClientGeocoder(new GGeocodeCache());
			geo.getLocations(position, garageeditMap);
		});
		$("input[name='glat']").val(lat);
		$("input[name='glng']").val(lng);
		
		$("input[name='gcity']").val(city);
		if ($('#didumean').is(':visible')) {
			$('#didumean').hide();
		}
}


function mark_zone(lat,lng,city) {
	var point = new GLatLng(lat,lng);
	map.setCenter(point, 12);
	$("input[name='pzcity']").val(city);					
	if ($('#didumean').is(':visible')) {
		$('#didumean').hide();
	}
}
