/**
 *  @version $Id: gMap.js 21 2009-04-30 10:21:11Z paulinad $
 *	@package frontend
 *	@author kamilag
 */
	var gMapCountryId = 0;
	var gMapCityId = 0;
	var gMapPage = 1;
	
	if( lang == 'pl' )
	{
		var gMapPoint = 6;
		var gMapX = 19.204102;
		var gMapY = 52.025459;
	}
	else
	{
		var gMapPoint = 2;
		var gMapX = 23.0;
		var gMapY = 26.0;
	}
	
	var gMapMarkers = [];
	var markerClusterer = null;
	
	/**
	 * Maksymalna wartość Zooma przy której następuje grupowanie
	 * 
	 * @author arturg
	 */
	var clusterMaxZoom = 8;

	/**
	 * Zoom przy pokazywaniu dystrybutora
	 * 
	 * @author arturg
	 */
	var distribZoom = 16;
	
	var ifUpdateGMapCities = true;
	var ifUpdateGMapMarkers = true;
	var ifCenterProvince = true;
	var ifCenterCity = false;
	
	var styles = [{
        url: '../../images/icons/group_ico_53.png',
        height:53,
        width: 52,
        //opt_anchor: [18, 22],
        opt_textColor: '#FFFFFF'
      },
      {
        url: '../../images/icons/group_ico_56.png',
        height: 56,
        width: 55,
        //opt_anchor: [20, 24],
        opt_textColor: '#FFFFFF'
      },
      {
        url: '../../images/icons/group_ico_66.png',
        height: 66,
        width: 65,
        //opt_anchor: [25, 29],
        opt_textColor: '#FFFFFF'
      }];
	
	
	/**
	* Funkcja ladujaca duza mape google
	* @author paulinad
	*/
	//function loadBigGMDynamic1()
	//{
	//	google.load( "maps", "2", {"callback" : loadBigGM12 } );
	//}
	
	
	/**
	* Funkcja ladujaca duza mape
	* @author paulinad
	*/
	function loadBigGM12()
	{
		if ( GBrowserIsCompatible() ) 
		{
			if( $( "map" ) )
			{
				GMarker.prototype.Info=function( category )  
				{
					this.openInfoWindow( category );
				}
				map = new GMap2( $( "map" ) ); 
				var point = new GLatLng( gMapY, gMapX );
				typymapy = new TypyMapy();  
				map.addControl(typymapy);  
				GEvent.addListener(map,'maptypechanged',function()  
				{  
				    typymapy.odswiez();  
				}); 
				map.setCenter( point, gMapPoint ); 
				map.setMapType( G_HYBRID_MAP );
				map.addControl( new GLargeMapControl3D() );
				//map.addControl( new GMapTypeControl() );
				
				geocoder = new GClientGeocoder();
				
				//GEvent.addListener( map, "infowindowopen", function()
				//		{
				//			if ( map.getZoom() < 7 )
				//			{
				//				//map.setZoom( 7 );
				//			}
				//		});
				
				updateGMapElems();
			}
		} 
	}
	
	
	function loadBigMapForPrint( )
	{
		if ( GBrowserIsCompatible() ) 
		{
			if( $( "map" ) )
			{
				map = new GMap2( $( "map" ) ); 
				point = new GLatLng( data[0]["pos"][1], data[0]["pos"][0] );
				map.setCenter( point, 15 ); 
				map.setMapType( G_NORMAL_MAP );
				
				updateGMapMarkers();
			}
		} 
	}
	
	function updateGMapElems()
	{
		new Ajax( ajaxSendUrl,
				{
					method: 'post',
					data: "getElems=1"+"&gmapProv="+gMapCountryId+"&gmapCity="+gMapCityId+"&gmapPage="+gMapPage,
					onComplete: function()
								{	
									
									if( ifUpdateGMapMarkers )
									{
										deleteMarkers()
										updateGMapMarkers();
									}
									//var pom = false;
									/*if( ifCenterCity )
									{
										ifCenterCity = false;
										if( !centerGMapCity() )
										{
											ifCenterProvince = true;
										}
									}*/
									if( ifCenterProvince )
									{
										ifCenterProvince = false;
										centerGMapProvince();
									}
									if( $( 'gmapProv' ) )
									{
										$( 'gmapProv' ).value = gMapCountryId;
									}
									if( $( 'gmapCity' ) )
									{
										$( 'gmapCity' ).value = gMapCityId;
									}
								}	,
					update: $( 'gMapListOuter' ),
					onFailure: function()
								{
								},
					evalScripts: true
				}
			).request();
		if( ifUpdateGMapCities )
		{
			//updateGMapCities();
		}
									
	}
	
	function deleteMarkers()
	{
		map.clearOverlays();
		if (markerClusterer != null) {
		
		markerClusterer.clearMarkers();
		gMapMarkers = [];
		 }
	}
	
	function updateGMapMarkers()
	{
		if( data == undefined )
		{
			data = [];
		}
		deleteMarkers();
		//var blueIcon = new GIcon( G_DEFAULT_ICON );
		//var markerOptions = { icon: blueIcon, draggable: false };
		for ( var k=0; k<data.length; k++ ) 
		{
			point = new GLatLng( data[k]["pos"][1], data[k]["pos"][0] );
			iconUrl = '';//data[k]["icon"];
			mapmark = createMarkerWidthIcon( point, iconUrl, data[k], 0 );
			//map.addOverlay( mapmark );
		}
		//alert( data.length );
		//alert( gMapMarkers.length );
		markerClusterer = new MarkerClusterer( map, gMapMarkers, { maxZoom: clusterMaxZoom, styles: styles } ); //, { maxZoom: 1, gridSize: 80, styles: styles[-1] } );
		
	}
	
	function updateGMapCities()
	{
		new Ajax( ajaxSendUrl,
				{
					method: 'post',
					data: "getSearcher=1"+"&gmapProv="+gMapCountryId+"&gmapCity="+gMapCityId+"&gmapPage="+gMapPage,
					onComplete: function()
								{	
									//centerGMapCity();
									//ifCenterProvince = false;
								}	,
					update: $( 'gMapSearcher' ),
					onFailure: function()
								{
								},
					evalScripts: true
				}
			).request();
	}
	
	
	function changeGMapCountry( countryStr )
	{
		countryId = countryStr.split(";");
		//if( ( countryId[0] >= 0 ) && ( countryId[0] <= 16 ) )
		if( ( countryId[0] >= 0 ) )
		{
			
			//if( ( countryId[1] != '' ) && ( countryId[2] != '' ) )
			//{
				correctGMapFilter( countryId[0], gMapCityId, gMapPage, countryId[1], countryId[2], countryId[3] );
			//}
			if( !map )
			{
				loadBigGMDynamic();
			}
			else
			{
				updateGMapElems();
			}
		}
		
	}
	
	function changeGMapCity( cityId )
	{
		if( !isNaN( cityId )  )
		{
			correctGMapFilter( gMapProvinceId, cityId, gMapPage );
			if( !map )
			{
				loadBigGMDynamic();
			}
			else
			{
				updateGMapElems();
			}
		}
	}
	
	function changeGMapPage( page )
	{
		correctGMapFilter( gMapProvinceId, gMapCityId, page );
		if( !map )
		{
			loadBigGMDynamic();
		}
		else
		{
			updateGMapElems();
		}
	}
	
	function correctGMapFilter( countryId, city, page, gmX, gmY, zoom )
	{
		var oldCountry = gMapCountryId;
		var oldCity = gMapCityId;
		var oldPage = gMapPage;
		ifUpdateGMapCities = false;
		ifUpdateGMapMarkers = false;
		ifCenterProvince = false
		if( countryId != oldCountry )
		{
			city = 0;
			ifUpdateGMapCities = true;
			ifCenterProvince = true;
		}
		if( ( countryId != oldCountry ) 
			|| ( city != oldCity ) )
		{
			page = 1;
			ifUpdateGMapMarkers = true;
		}
		if( ( city != oldCity ) && ( city > 0 ) )
		{
			ifCenterCity = true;
		}
		gMapCountryId = countryId;
		gMapCityId = city;
		gMapPage = page;
		if( ( gmX != '' ) && ( gmY != '' ) )
		{
			gMapX = gmX;
			gMapY = gmY;
		}
		else
		{
			zoom = 2;
			gMapX = 23.0;
			gMapY = 26.0;
		}
		gMapPoint = zoom;
		
	}
	
	
	function createMarkerWidthIcon( latlng, url, gdata, type )
	{
		var gicon = getIcon( url );
		var markerOptions = {icon: gicon, title: gdata["title"] };
		var markers = new GMarker( latlng, markerOptions );
		markers.isVisible = 0;
		markers.id = gdata["id"];
		if( type != 1 )
		{
			GEvent.addListener( markers, "click", function()
										{
											gmapShowInfo( markers.id, false );
										}
			);
		}
		
		gMapMarkers.push( markers );
		//gMapMarkers[markers.id] = markers;
		return markers;
	}
	
	var gi;
	
	function gmapShowInfo( id, setZoom )
	{
		if (typeof(setZoom) == 'undefined' )
		{
			setZoom = true;
		}
		
		/*if( gMapMarkers[id] != undefined )
		{
			if( gMapMarkers[id].myContent != '' )
			{
				gMapMarkers[id].openInfoWindowHtml( gMapMarkers[id].myContent );
			}
			else
			{
				new Ajax( ajaxSendUrl,
				{
					method: 'post',
					data: "getInfo=1&gmapElemId="+id,
					onComplete: function( zmienna )
								{	
									gMapMarkers[id].myContent = zmienna;
									gMapMarkers[id].openInfoWindowHtml( zmienna );
								},

					onFailure: function()
								{
								},
					evalScripts: true
				}
				).request();
			}
		}*/
		//alert( 'jjj' );
		for( gi=0; gi<gMapMarkers.length; gi++ )
		{
			if( gMapMarkers[gi].id  == id )
			{
				//alert( 'id='+id );
				//alert( gMapMarkers[gi].id );
				//alert( 'gi='+gi);
				zmianna12 = gi;
					new Ajax( ajaxSendUrl,
					{
						method: 'post',
						data: "getInfo=1&gmapElemId="+id,
						onComplete: function( zmienna )
									{	
										//gMapMarkers[i].myContent = zmienna;
										//alert( 'zmianna12='+zmianna12 );
										//gMapMarkers[zmianna12].openInfoWindowHtml( zmienna );
										
										// Wyświetlenie okna z dymkiem
										map.openInfoWindowHtml( gMapMarkers[zmianna12].getLatLng(), zmienna );
										
										// Zmiana Zooma do wartości dla dystrybutora
										if ((setZoom === true) & ( map.getZoom() < distribZoom ))
										{
											map.setCenter( gMapMarkers[zmianna12].getLatLng(), distribZoom );
										}
									},
	
						onFailure: function()
									{
									},
						evalScripts: true
					}
					).request();
				//}
			}
		}
	}
	
	// ustawienie ikonki markera
	function getIcon( url )
	{
		var blueIcon = new GIcon( G_DEFAULT_ICON );
		//if( ( url != '' ) )
		{
			blueIcon.iconSize = new GSize( 27, 37 );
			blueIcon.iconAnchor = new GPoint( 14, 37 );
			blueIcon.shadow = ghost+'/images/icons/ico_shade.png';
			blueIcon.shadowSize = new GSize( 27, 37 );
			//blueIcon.infoWindowAnchor=new GPoint(16,0);
			//blueIcon.image = url;
			blueIcon.image = ghost+'/images/icons/ico.png';
		}
		//var markerOptions = { icon: blueIcon, draggable: false };
		return blueIcon; //markerOptions;
	}
	
	function centerGMapProvince( provId )
	{		
		var point = new GLatLng( gMapY, gMapX );
		map.setCenter( point, parseInt( gMapPoint, 10 ) );
		return true;
	}
	
	function centerGMapCity()
	{
		if( gMapCityCentres[gMapCityId] != undefined )
		{
			//alert( 'gMapCityId: ' + gMapCityId + '; x: ' + gMapCityCentres[gMapCityId][0]+ '; y:'+gMapCityCentres[gMapCityId][1] );
			var point = new GLatLng( gMapCityCentres[gMapCityId]["pos"][1], gMapCityCentres[gMapCityId]["pos"][0] );
			map.setCenter( point, 8 );
			return true;
		}
		return false;
	}
	
	/**
	 * @author		unknown
	 */
	function showAddress( address, text )
	{
		if( !map )
		{
			loadBigGMDynamic();
		}
		if( $( "opISV" ) && $( "op" ) )
		{
			country = $( "opISV" ).value;
			countryId = $( "op" ).value.split(";");
			if( parseInt( countryId, 10 ) > 0 )
			{
				address = address + " " + country;
			}
		}
		if( geocoder )
		{
			geocoder.getLatLng( address, function( point )
										{
											if( !point )
											{
												alert( text );
											}
											else
											{
												map.setCenter( point, 14 );
												map.openInfoWindow( map.getCenter(), document.createTextNode( address ) );
											}
										}
			);
		}
	}
	
	function TypyMapy() {}  
	TypyMapy.prototype = new GControl();  
	
	TypyMapy.prototype.initialize = function(mapa)
{
	var lista 	= document.createElement("ul");
			lista.id	= 'typymapy';
			
			var przycisk1 = document.createElement('li');
			var przycisk2 = document.createElement('li');
			var przycisk3 = document.createElement('li');

			przycisk1.innerHTML = 'Hybrid'; //'hybryda';
			przycisk2.innerHTML = 'Satellite'; //'satelita';
			przycisk3.innerHTML = 'Map'; //'mapa';

			
			przycisk1.title = 'Show imagery with street names'; //'pokaz zdjecia satelitarne';
			przycisk2.title = 'Show satellite imagery'; //'pokaz zdjecia satelitarne';
			przycisk3.title = 'Show street map'; //'pokaz mape';

			przycisk1.onclick = function() { mapa.setMapType(G_HYBRID_MAP); return false; };
			przycisk2.onclick = function() { mapa.setMapType(G_SATELLITE_MAP); return false; };
			przycisk3.onclick = function() { mapa.setMapType(G_NORMAL_MAP); return false; };
			
			lista.appendChild(przycisk1);
			lista.appendChild(przycisk2);
			lista.appendChild(przycisk3);
			
			this.przycisk1 = przycisk1;
			this.przycisk2 = przycisk2;
			this.przycisk3 = przycisk3;
			this.mapa = mapa;			
			mapa.getContainer().appendChild(lista);
			return lista;

}

TypyMapy.prototype.getDefaultPosition = function()
{
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(5, 5));
}

TypyMapy.prototype.odswiez = function()
{
	if(this.mapa.getCurrentMapType() != G_HYBRID_MAP)
				this.przycisk1.className = 'nieaktywny';
			else
				this.przycisk1.className = 'aktywny';
				
			if(this.mapa.getCurrentMapType() != G_SATELLITE_MAP)
				this.przycisk2.className = 'nieaktywny';
			else
				this.przycisk2.className = 'aktywny';
				
			if(this.mapa.getCurrentMapType() != G_NORMAL_MAP)
				this.przycisk3.className = 'nieaktywny';
			else
				this.przycisk3.className = 'aktywny';


		
}



