/**
*   @version $Id$
*	@package frontend
*	@author piotrj
*/

	/** mapa google dla ogloszen */
	var gdMap = null;
	//var anGeocoder = null;
	/** marker na mapie google */
	var gdMarkers = [];
	/** altualne centrum mapy w formacie GLatLng */
	var gdMapCenter = null;

	var gdChosenProvince = '';

	var gdDistData = [];

	var cluster = null;

	/**
	* Funkcja ladujaca mape google do edycji w ogloszeniach
	* @author piotrj
	*/
	function loadGMaps()
	{
		if( GBrowserIsCompatible() )
		{
			if( gMapObj = document.getElementById( "map" ) )
			{
				gdMap = new GMap2( gMapObj );
				if( oMX = document.getElementById( "hidden_gmaps_x" ) )
				{
					gmaps_x = oMX.value;
				}
				if( oMY = document.getElementById( "hidden_gmaps_y" ) )
				{
					gmaps_y = oMY.value;
				}
				var zoomGMap = 6;
				if( oZoomInp = document.getElementById( "zoom_set" ) )
				{
					zoomGMap = oZoomInp.value*1;
				}
				if( ( gmaps_x == '' ) || ( gmaps_y == '' ) )
				{
					var point = new GLatLng( 19.090576, 52.366208 );
				}
				else
				{
					var point = new GLatLng( gmaps_y, gmaps_x );
				}
				gdMapCenter = point;
				gdMap.setCenter( point, zoomGMap );
				gdMap.addControl( new GLargeMapControl3D() );
				//gdMap.addControl( new GSmallMapControl() );
				//gdMap.addControl( new GMapTypeControl() );
				createMarkersForProvince();

				cluster = new ClusterMarker( gdMap, { clusterMarkerIcon: getIconCluster(),markers:gdMarkers } );
				cluster.clusterMarkerTitle = 'Ilość firm w rejonie: %count';
				cluster.clusterMarkerClick = function( args )
												{
													args.clusterMarker.openInfoWindowHtml( getFullClusterHTML( args.clusteredMarkers ) );
												};
				cluster.intersectPadding = -10;
				cluster.fitMapToMarkers();
				//cluster.clusteringEnabled = true;
			}
		}
	}

	function getIconCluster()
	{
		var blueIcon = new GIcon( G_DEFAULT_ICON );
		blueIcon.image = gdHost+'/images/icons/ico_28x28_plus.png';
		blueIcon.iconSize = new GSize( 28, 28 );
		//blueIcon.iconAnchor = new GPoint( 28, 14 );
		blueIcon.shadow = gdHost+'/images/icons/ico_28x28_shade.png';
		blueIcon.shadowSize = new GSize( 28, 28 );
		return blueIcon;
	}

	/**
	* Funkcja ladujaca mape google tylko do wyswietlenia w ogloszeniach
	* @author piotrj
	*/
	function getFullClusterHTML( clusterMarkersTab )
	{
		var fullHTML = '';
		for( var i = 0; i < clusterMarkersTab.length; i++ )
		{
			fullHTML += clusterMarkersTab[i].name+'<br /><br />';
		}
		return fullHTML;
	}

	/**
	* Funkcja ladujaca mape google tylko do wyswietlenia w ogloszeniach
	* @author piotrj
	*/
	function centerMapAtPoint( pointStr )
	{
		var pointTab = pointStr.split( ';' );
		var point = new GLatLng( pointTab[1]*1, pointTab[0]*1 );
		gdMap.setCenter( point, pointTab[2]*1 );
	}

	/**
	* Funkcja ladujaca mape google tylko do wyswietlenia w ogloszeniach
	* @author piotrj
	*/
	function setProvinceVisible( pointStr )
	{
		var pointTab = pointStr.split( ';' );
		gdChosenProvince = pointTab[3];
		var i = 0;
		var disp = '';
		while( oDiv = document.getElementById( 'provinceToHide'+i+'' ) )
		{
			disp = 'none';
			if( ( gdChosenProvince == '' ) || ( oDiv.name == gdChosenProvince ) )
			{
				disp = 'block';
			}
			oDiv.style.display = disp;
			i++;
		}
	}

	/**
	* Funkcja ladujaca mape google tylko do wyswietlenia w ogloszeniach
	* @author piotrj
	*/
	function createMarkersForProvince()
	{
		gdMap.closeInfoWindow();
		gdMap.clearOverlays();
		for( var i = 0; i < gdDistData.length; i++ ) 
		{
			if( ( gdChosenProvince == '' ) || ( gdChosenProvince == gdDistData[i]["province"] ) )
			{
				if( ( gdDistData[i]["pointX"] != '' ) && ( gdDistData[i]["pointX"] != 'null' ) && ( gdDistData[i]["pointY"] != '' ) && ( gdDistData[i]["pointY"] != 'null' ) )
				{
					point = new GLatLng( gdDistData[i]["pointX"], gdDistData[i]["pointY"] );
					//gdMap.addOverlay( createMarker( point, gdDistData[i] ) );
					createMarker( point, gdDistData[i] );
				}
			}
		}
	}

	function createMarker( latlng, gdata )
	{
		var more = '';
		if( gdata["dcompanydata"] )
		{
			more = ''+gdata["dcompanydata"]+'';
		}
		var myhtml = '<h4 style="font-size: 12px; color: #5c5757; font-weight: bold; margin: 0px; padding-top: 5px;">'+gdata["dcompany"]+'</h4>'+more;
		var markers = new GMarker( latlng, getIcon() );
		markers.isVisible = 1;
		markers.name = myhtml;
		markers.title = gdata["dcompany"];
		GEvent.addListener( markers, "click", function()
									{
										markers.openInfoWindowHtml( myhtml );
									}
		);
		gdMarkers.push( markers );
		return markers;
	}

	function getIcon()
	{
		var blueIcon = new GIcon( G_DEFAULT_ICON );
		blueIcon.image = gdHost+'/images/icons/ico_28x28.png';
		blueIcon.iconSize = new GSize( 28, 28 );
		//blueIcon.iconAnchor = new GPoint( 28, 14 );
		blueIcon.shadow = gdHost+'/images/icons/ico_28x28_shade.png';
		blueIcon.shadowSize = new GSize( 28, 28 );
		var markerOptions = { icon: blueIcon, draggable: false };
		var markerOptions = { icon: blueIcon, draggable: false };
		return markerOptions;
	}

	function showInfo( i )
	{
		gdMarkers[i].Info( gdMarkers[i].name );
	}

	GMarker.prototype.Info=function( text )  
	{
		this.openInfoWindow( text );
	}

//window.addEvent( 'domready', loadGMaps );

