var map;
var geocoder;
var marker = null;
var scale = 6
var attitude = 0;
var longitude = 0;

function addAddressToMap(response) {
	if (!response || response.Status.code != 200) {
		map.setCenter(new GLatLng(41.9, 12.3), scale, G_NORMAL_MAP);
		attitude = 41.9
		longitude = 12.3
	} else {
		var place = response.Placemark[0];
		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		map.setCenter(new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]), scale, G_NORMAL_MAP);
		attitude = place.Point.coordinates[1]
		longitude = place.Point.coordinates[0]
	}
	return true;
}



function findLocation(address) {
	showLocation(address);
}
function load(x,y,s, opis, click) {
	scale = s

	map = new GMap2(document.getElementById("map"));

	geocoder = new GClientGeocoder();

	map.addControl(new GLargeMapControl());
	map.enableDoubleClickZoom();
	//map.enableScrollWheelZoom();

	if (opis) {
		findLocation(opis)
	} else {
		map.setCenter(new GLatLng(x,y), s, G_HYBRID_MAP);
	}


	if (click) GEvent.addListener(map,"click", function(overlay,latlng) {
		if (overlay) return
		var tileCoordinate = new GPoint();
		var tilePoint = new GPoint();
		var currentProjection = G_NORMAL_MAP.getProjection();
		tilePoint = currentProjection.fromLatLngToPixel(latlng, map.getZoom());
		tileCoordinate.x = Math.floor(tilePoint.x / 256);
		tileCoordinate.y = Math.floor(tilePoint.y / 256);

		document.getElementById('longitude').value=latlng.lat();
		document.getElementById('attitude').value=latlng.lng();

		point = new GLatLng(latlng.lat(),latlng.lng());

		if (marker!=null) marker.remove();
		if (markerm!=null) markerm.remove();
		marker = new GMarker(point);
		map.addOverlay(marker);
	});


}

function showLocation(address) {
	geocoder.getLocations(address, addAddressToMap);

}

function cont(check) {
	if (check)
	map.enableContinuousZoom()
	else
	map.disableContinuousZoom();
}

function createMarker(point, html, idCountry){

	// Create our "tiny" marker icon
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "http://srv.slsystems.pl/sylwek/all/public_html/design/img/mapPoint.png";

	// Set up our GMarkerOptions object
	markerOptions = { icon:blueIcon };


	var marker = new GMarker(point, markerOptions);
	GEvent.addListener(marker, "mouseover", function() {marker.openInfoWindowHtml(html);});
	if (idCountry.length<5)
	GEvent.addListener(marker, "click", function(overlay, latlng) { if (latlng) document.location.href = 'index.php?m=Country_Edit&idCountry=' + idCountry });
	else
	GEvent.addListener(marker, "click", function(overlay, latlng) { if (latlng) document.location.href = idCountry });
	return marker;
};


function hideOverflow(check) {
	if (check) {
		map.getContainer().style.overflow="hidden";
	} else {
		map.getContainer().style.overflow="visible";
	}
}

function wheelevent(e)
{
	if (!e){
		e = window.event
	}
	if (e.preventDefault){
		e.preventDefault()
	}
	e.returnValue = false;
}

function esasZoomOut(){
	var paragraphs = map.getContainer().getElementsByTagName('p').length;
	if(paragraphs > 6){
		map.zoomOut();
	}
}



