var map;
var geocoder;
var marker = null;
var scale = 6
var attitude = 0;
var longitude = 0;
var myPoint = null;

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
	
	initialize();
	
	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, url, showAlwaysHtml){

	// Create our "tiny" marker icon
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "http://srv.slsystems.pl/rafal/h2/public_html/design/img/mapPoint.png";

	// Set up our GMarkerOptions object
	markerOptions = { icon:blueIcon };

	var marker = new GMarker(point, markerOptions);
	
	if(html.length>1){
		if(showAlwaysHtml){
			marker.openInfoWindowHtml(html)
		}
		GEvent.addListener(marker, "mouseover", function() {marker.openInfoWindowHtml(html);});
	}
	if (url.length>5){
		GEvent.addListener(marker, "click", function(overlay, latlng) { document.location.href = url });
	}
	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();
	}
}

function showAddress( address ) {
	if(geocoder) {
		geocoder.getLatLng(
			address,
			function( point ) {
				if( point ) {
					attitude = point.x;
					longitude = point.y;
				}
				if( showAddressBack ) {
					showAddressBack();
				}
			}
		);
	}
}

function initialize() {
	map = new GMap2( document.getElementById( 'map') );
	geocoder = new GClientGeocoder();
}
