
var map = null;
var geocoder = null;
var tmp = new Array(); tmp['omittedLevels'] = '';
var setVars = new Array();
var marker = null;
var searchRecurrence = 0;
setVars['searchRecurrence'] = 0;
var imNotSleepJet = true;


function varDump()
{
    //fSetVars();
    var str = '';
    for( v in setVars )
    {
	str += "setVars['"+ v +"']" + ' => ' + setVars[v] + "\n";
    }
    alert(str);
}

function fSetVars()
{
    setVars['mapPoint']	= map.getCenter();
    setVars['mapBounds']= map.getBounds();
    setVars['mapZoom']	= map.getZoom();
    //setVars['gMapW'] = setVars['mapPoint'].y;
    //setVars['gMapL'] = setVars['mapPoint'].x;
    setVars['gMapW'] = setVars['markerPoint'].y;
    setVars['gMapL'] = setVars['markerPoint'].x;
	
	/**
	* tutaj tylko do celow Adamowych...:
	*/
	//var form = document.forms.gMapForm;
	//form.gMapAccuracy.value = setVars['gMapAccuracy'];
	//form.gMapW.value = setVars['gMapW'];
	//form.gMapL.value = setVars['gMapL']
}

function postalizeMapVars()
{
    fSetVars();
    var post = '&';
    for( v in setVars )
    {
	post += v + '=' + setVars[v] + '&';
    }
    return post;
}

/**
* zwraca wszystkie ustawienia mapki
*/
function getGmapParams()
{
    fSetVars();
    return setVars;
}

function resetMap()
{
    setVars['gMapAccuracy'] = tmp['gMapAccuracy']; 
    setVars['markerPoint'] = setVars['mapPoint'] = tmp['defaultMarkerPoint'];
    map.returnToSavedPosition();
    marker.setPoint( tmp['defaultMarkerPoint']);
    marker.openInfoWindowHtml( info['infoHeader']+info['confirmSet']+info['infoFooter']);
    //getGmapParams();
    fSetVars();
}

function showMap(w,h)

{
//alert(w);
    if( imNotSleepJet )
    {
	imNotSleepJet = false;
	setTimeout('showMap('+w+','+h+')', 500);
	return;
    }
   // var infoMsg = info['infoHeader'];
	if(w > 0)
	{
		//alert(w+' '+h);
		//alert('mapa ze wspolrzednych');
		setVars['gMapW'] = w;
		setVars['gMapL'] = h;
		setVars['gMapAccuracy'] = 4;
		var point = new GLatLng( setVars['gMapW'], setVars['gMapL'] );
		setVars['mapZoom'] = getZoomByAccuracy( setVars['gMapAccuracy'] );
		//alert(setVars['gMapW']):
		
	}
	else
	{
	//alert('mapa z nazwy');
    var point = new GLatLng( setVars['gMapW'], setVars['gMapL'] );
	//alert(point);
    setVars['mapZoom'] = getZoomByAccuracy( setVars['gMapAccuracy'] );
	}
    map = new GMap2( document.getElementById ? document.getElementById("mapDiv") : document.all.mapDiv );
    map.enableContinuousZoom();
    map.savePosition();
    //map.addControl(new GLargeMapControl());
    //map.addControl(new GSmallMapControl());
    map.addControl(new GScaleControl());
    tmp['defaultMarkerPoint'] = setVars['markerPoint']  = setVars['mapPoint'] = point;
    map.setCenter(point, setVars['mapZoom']);
    searchRecurrence=0;
    tmp['omittedLevels']='';
    //infoMsg += info['setMarker']+info['infoFooter'];

    //map.openInfoWindowHtml(point, "<div style='background-color:#fff; margin:0px; width:210px; height:65px;'>"+infoMsg+"</div>");
    marker = new GMarker(point, {draggable: true});

    GEvent.addListener(marker, "dragstart", function() {
	    map.closeInfoWindow();
	    });
    
    /* ograniczenia zoomowania */
    /*
    GEvent.addListener(map, "zoomend", function( oldLevel, newLevel ) {
	    if( newLevel > 16 )
	    {
		map.setZoom(16);
	    }
	    else if( newLevel < 8 )
	    {
		map.setZoom(8);
	    }
	    });
	    */

    GEvent.addListener(marker, "dragend", function() {
	    setVars['markerPoint']  = marker.getPoint();

	    if( setVars['gMapAccuracy'] < 7 )
	    {
		setVars['mapZoom'] += 1;
		map.setZoom( setVars['mapZoom'] );
	    }
	    map.panTo( setVars['markerPoint']);
	    setVars['gMapAccuracy'] = 8;
	    setTimeout("fSetVars()",100);
	    //fSetVars();
	    });

    map.addOverlay(marker);
   // marker.openInfoWindowHtml("<div style='background-color:#fff; margin:0px; width:210px; height:65px;'>"+infoMsg+"</div>");
    fSetVars();
}





function findGeoCode( address, infoField )
{
    if( infoField )
    {
	infoField.value += address+"\n";
    }
    geocoder = new GClientGeocoder();

    if(geocoder)
    {
	geocoder.getLocations( address, function(point)
	    {
		var coordinates;
		if ( point.Status.code != 200 )
		{
		    address = address.replace(/^[^,]*,\s*/, '');
		    searchRecurrence ++;
		    if( searchRecurrence  > 5 )
		    {
			findGeoCodeFalse( point.Status.code );
			return;
		    }
		    findGeoCode( address, infoField );
		}
		else 
		{
		    setVars['address'] = address;
		    coordinates = point.Placemark[0].Point.coordinates;
		    setVars['gMapAccuracy'] = tmp['gMapAccuracy'] = point.Placemark[0].AddressDetails.Accuracy;
		    setVars['gMapL'] = coordinates[0];
		    setVars['gMapW'] = coordinates[1];
		    setVars['markerPoint'] = setVars['mapPoint'] =  new GLatLng( coordinates[1], coordinates[0] );
		    findGeoCodeSuccess();
		}
	    }
	);
    }
    else
    {
	alert( info['error'] );
    }
}


