var google_maps_load = 0;

var map;
var geocoder = new GClientGeocoder();
var blnMarker = 0;
var directions = null;

var google_maps_latitude_default = "53.380683";
var google_maps_altitude_default = "19.937229";
var google_maps_zoom_default     = 11;

var arrMarker = new Array();
var marker_id = 0;

function addMarker( uniqueID, groupID, latitude, altitude, strUrlIcon, blnDraggable, marker, strHtml, strLocation, strTyp )
{		
	var i = getArrMarkerID( uniqueID );	
	if ( i == null )
	{
		i = marker_id;
		marker_id++;
	}	
	arrMarker[i] = new Array( uniqueID, groupID, latitude, altitude, strUrlIcon, blnDraggable, marker, strHtml, strLocation, strTyp );		
}


function clearMarker( ID )
{
	for ( var i=0; i<arrMarker.length; i++ )
	{
		if ( arrMarker[i][0] == ID || arrMarker[i][1] == ID )
		{												
			if ( arrMarker[i][6] != null )			
			{				
				var marker = arrMarker[i][6];
				marker.remove();					
			}			
		}
	}
}

function clearMarkerNotID( uniqueID, typ )
{
	for ( var i=0; i<arrMarker.length; i++ )
	{
		if ( arrMarker[i][0] != uniqueID || arrMarker[i][9] != typ )
		{						
			if ( arrMarker[i][6] != null )			
			{
				var marker = arrMarker[i][6];
				marker.remove();					
			}			
		}
	}
}


function getArrMarkerID( uniqueID )
{
	for ( var i=0; i<arrMarker.length; i++ )
	{
		if ( arrMarker[i][0] == uniqueID )
		{			
			return i;
		}
	}
	
	return null;
}

function createMarker( groupID, uniqueID, strTyp )
{
	for ( var i=0; i<arrMarker.length; i++ )
	{
		if ( ( groupID != null && uniqueID != null && arrMarker[i][0] == uniqueID && arrMarker[i][1] == groupID ) ||
			 ( groupID == null && uniqueID != null && arrMarker[i][0] == uniqueID )	||
			 ( groupID != null && uniqueID == null && arrMarker[i][1] == groupID ) || 
			 ( strTyp  != null && uniqueID != null && arrMarker[i][0] == uniqueID && arrMarker[i][9] == strTyp )
		   )
		{			
			if ( arrMarker[i][2] != null && arrMarker[i][3] != null )
			{
				var marker = google_maps_create_marker( arrMarker[i][2], arrMarker[i][3], arrMarker[i][5], google_icon( arrMarker[i][4] ) );			
				setLocMarker( marker, i );		
				createInfoWindow( marker, i );						    		    
			    google_map.addOverlay(marker);		    		    
			    arrMarker[i][6] = marker;		    		    
		    }
		    else if ( arrMarker[i][8] != null )
		    {
		    	// rysowanie obszarow		    			    			    	
		    	var polygon = null;
		    	var arrLoc = arrMarker[i][8].split('|');
		    	var init = 0;
				for ( var j=0; j<arrLoc.length; j++ )
				{
					var arrLoc2 = arrLoc[j].split(';');			
					latitude = arrLoc2[0];
					altitude = arrLoc2[1];
					
					if ( init == 0 )
					{												
						var polygon = new GPolygon([
				            new GLatLng(latitude, altitude)		            
						  ], "#f33f00", 5, 1, "#ff0000", 0.2);
						
						google_map.addOverlay(polygon);						
						init = 1;						
					}
					else
					{
						polygon.insertVertex(j, new GLatLng(latitude, altitude) ); 
					}																									
				}	
				
				createInfoWindowPolygon( polygon, i );				
				arrMarker[i][6] = polygon;		 												
				   			    			    			    		    	
		    	// -----------------------
		    }
		}
	}
}


function createInfoWindowPolygon( polygon, i )
{
	var strHtml = getInfoWindow( i );	
	GEvent.addListener( polygon, "click", function( overlay, latlng ) { 				  									 					 					  					  
		  google_map.openInfoWindow( polygon.getBounds().getCenter(), strHtml );
	});	
}


function getInfoWindow( i )
{
	if ( arrMarker[i][7] == null || arrMarker[i][7] == "undefined" )
	{
		var strHtml = '';	
	}
	else
	{
		var strHtml = arrMarker[i][7];		
	}
		
	//strHtml += google_marker_form_search_loc( i );	
	return strHtml;
}

function createInfoWindow( marker, i )
{	
	var strHtml = getInfoWindow( i );
	
	GEvent.addListener(marker, "click", function() { 				  				
		 marker.openInfoWindowHtml( strHtml );
	});	
}

function setLocMarker( marker, i )
{
	GEvent.addListener(marker, "dragend", function(overlay, latlng){		
		var arrLoc = marker.getPoint().toUrlValue().split(',');		
		arrMarker[i][2] = arrLoc[0];
		arrMarker[i][3] = arrLoc[1];		
	});	
}



function google_maps_initialize( latitude, altitude, nameID ) 
{
	if ( google_maps_load == 0 )
	{	
    	google_maps_load = 1;
    	if (GBrowserIsCompatible()) 
    	{
    		google_map = new GMap2( document.getElementById(nameID) );
    		    		       
        	
        	if ( latitude == 0 && altitude == 0 )
        	{        		
        		google_map.setCenter(new GLatLng(google_maps_latitude_default,google_maps_altitude_default), google_maps_zoom_default);
        	}	
        	else
        	{        		
        		google_map.setCenter(new GLatLng(latitude, altitude), google_maps_zoom_default);
        	}        	        	        	  
			
			google_map.addControl(new GSmallMapControl());
        	google_map.addControl(new GMapTypeControl());
        	google_map.addControl(new GScaleControl());		           
    	}
	}
}

function google_maps_create_marker( latitude, altitude, blnDraggable, gicon )
{    	
	if ( latitude == "" )
	{
		latitude = google_maps_latitude_default;
	}
	
	if ( altitude == "" )
	{
		altitude = google_maps_altitude_default;
	}
		
	var point  = new GLatLng(latitude,altitude);		
	var marker = new GMarker(point, { draggable: blnDraggable, icon: gicon } );
                 	        
    return marker;
}


//function google_marker_form_search_loc( marker_id )
//{		
//	var strHtml = '';		
//	strHtml += '<input type="text" name="szukaj_w_poblizu" id="szukaj_w_poblizu"  onfocus="if( this.value == \'Szukaj w pobliżu...\' ) {this.value = \'\';}" onblur="if( this.value == \'\' ) {this.value = \'Szukaj w pobliżu...\';}" value="Szukaj w pobliżu..."><input type="button" name="szukaj" value="szukaj" onClick="google_maps_szukaj( '+marker_id+', document.getElementById(\'szukaj_w_poblizu\').value );"><br />';
//	strHtml += '<select name="pt_google_type" id="pt_google_type"><option value="1">Z tego miejsca</option><option value="2">Do tego miejsca</option></select>';
//	strHtml += '<input type="text" name="google_search_place" id="google_search_place" value="">';
//	strHtml += '<input type="button" name="szukaj" value="szukaj" onClick="google_narysuj_droge( '+marker_id+', document.getElementById(\'pt_google_type\').value, document.getElementById(\'google_search_place\').value );">';		
//	return strHtml;
//}



function google_maps_szukaj( marker_id, str )
{
	var s = str.replace(/^\s+|\s+$/g, '');
	
	if ( s.length >= 3 )
	{	
		$.get("/?option=com_maps&view=wyszukiwarka&str="+s+"&id="+arrMarker[marker_id][0]+"&typ="+arrMarker[marker_id][9], function(data) { 		 						
			 						$("#list_search").html('');
			 						$("#content_search").html(data);				 								 								 								 												 						
			 					});	
	}	 					
	else
	{
		alert( 'Szukany ciąg powinien mieć więcej co najmniej 3 znaki' );
	}
}


function google_narysuj_droge( marker_id, typ, adres )
{
	var latitude = 0;
	var altitude = 0;
	
	if ( arrMarker[marker_id][2] != null && arrMarker[marker_id][3] != null )
	{
		latitude = arrMarker[marker_id][2]; 
		altitude = arrMarker[marker_id][3]; 		    
    }
    else if ( arrMarker[marker_id][8] != null )
    {    			
		var arrLoc = arrMarker[marker_id][8].split('|');    	
		for ( var j=0; j<1; j++ )
		{
			var arrLoc2 = arrLoc[j].split(';');			
			latitude = arrLoc2[0];
			altitude = arrLoc2[1];
		}	
	}
	
	
	
	
	
	if ( typ == 1 )
	{
		var arr = new Array( new GLatLng(latitude, altitude), adres );	
	}
	else
	{
		var arr = new Array( adres, new GLatLng(latitude, altitude) );	
	}
	
	if ( directions != null )
	{
		directions.clear();
	}	
	
	directionsPanel = document.getElementById("route");					
    directions = new GDirections(google_map, directionsPanel);        
  	directions.loadFromWaypoints(arr);     	
  	
}

function openInfoMarker( uniqueID, strTyp )
{
	for ( var i=0; i<arrMarker.length; i++ )
	{
		if ( arrMarker[i][0] == uniqueID && arrMarker[i][9] == strTyp )
		{
			if ( strTyp == 'nocleg' )
			{
				arrMarker[i][6].openInfoWindowHtml(getInfoWindow( i )); 
			}
			else
			{													  									 				
				google_map.openInfoWindow( arrMarker[i][6].getBounds().getCenter(), getInfoWindow( i ) );
			}	
		}
	}
}


function google_icon( strUrl )
{
	var blueIcon = new GIcon(G_DEFAULT_ICON);
    if ( strUrl != null )
	{
		blueIcon.image = strUrl;		
	}
 	return blueIcon;
}


function google_maps_geocoder( nameID )
{	
	google_maps_initialize( 0, 0, nameID );
	/*
	if (GBrowserIsCompatible()) {
		geocoder = new GClientGeocoder();
		GEvent.addListener(google_map, "click", function(overlay, latlng){
			if (latlng) {							
				clearMarker( 'search' );
			    addMarker( 'search', 'search', latlng.lat(), latlng.lng(), null, true, null );
			    createMarker( 'search' );																				
			}
		});
		
	}
	*/	 
}

function google_maps_add_address_to_map(response) 
{
	  if (!response || response.Status.code != 200) {
	    alert("Przepraszamy, nie możemy znaleźć Twojego adresu na mapie. Kliknij na mape i wskaż go sam.");
	  } else {	    
	    var place = response.Placemark[0];	    
	    var point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);	    
	    
	    clearMarker( 'search' );
	    addMarker( 'search', 'search', place.Point.coordinates[1], place.Point.coordinates[0], null, true, null );
	    createMarker( 'search' );
	    google_map.setCenter(point);	    	    		
	  }
}

function google_maps_search_location( address ) 
{      
   geocoder.getLocations(address, google_maps_add_address_to_map);
}

function google_clear_marker()
{
	google_map.clearOverlays();
}


