/**
*   @version $Id: googleMaps.js 84 2009-05-06 11:37:04Z kamilag $
*	@package cms googleMaps
*	@author kamilag
*/
	var map = null;
	var geocoder = null;
	var markers;
	var mapmark = [];
	var gmarkers = [];
	
	/**
	* Funkcja ladujaca mape google do edycji w ogloszeniach
	* @author piotrj
	*/
	function loadGMDynamic()
	{
		google.load( "maps", "2", {"callback" : loadGM} );
	}

	/**
	* Funkcja ladujaca mape google
	* @author kamilag
	* @param string gmaps_x - wspolrzedna x punktu wyswietlanego na mapie
	* @param string gmaps_y - wspolrzedna y punktu wyswietlanego na mapie
	* @param string txt - informacja wyswietlana podczas dodawania punktu na mapie
	*/
	//function loadGM( gmaps_x, gmaps_y, txt )
	function loadGM()
	{ 
		var txt = 'tekst jak sie bla bla bla';
		if( GBrowserIsCompatible() ) 
		{ 
			if( gMapObj = $( "map" ) )
			{
				GMarker.prototype.Info=function( category )  
				{
					this.openInfoWindow( category );
				}
				map = new GMap2( gMapObj ); 
				/*if( ( gmaps_x == '' ) || ( gmaps_y == '' ) )
				{
					var point = new GLatLng( 54.44162184889164, 18.56011390686035 ); 
					var number = 10;  
					$( "hidden_gmaps_y" ).value = '';
					$( "hidden_gmaps_x" ).value = '';
				}
				else
				{
					var point = new GLatLng( gmaps_y, gmaps_x );
					var number = 16;  
					$( "hidden_gmaps_y" ).value = gmaps_y;
					$( "hidden_gmaps_x" ).value = gmaps_x;
				}*/
				if( oMX = $( "hidden_gmaps_x" ) )
				{
					gmaps_x = oMX.value;
				}
				if( oMY = $( "hidden_gmaps_y" ) )
				{
					gmaps_y = oMY.value;
				}
				var number = 10;
				if( ( gmaps_x == '' ) || ( gmaps_y == '' ) )
				{
					var point = new GLatLng( 53.42360955801476, 14.556884765625 );
				}
				else
				{
					var point = new GLatLng( gmaps_y, gmaps_x );
				}
				map.setCenter( point, number );
				map.addControl( new GSmallMapControl() );
				map.addControl( new GMapTypeControl() );
				if( ( gmaps_x != '' ) || ( gmaps_y != '' )  )
				{
					map.addOverlay( createMarker( point ) );
				}
				GEvent.addListener( map, 'click', function( p, latlng )  
													{
														addMarkerOnMap( latlng, txt );
													}
				);
			}
		}  
	}

	/**
	* Funkcja dodajaca marker na mape
	* @author kamilag
	* @param  latlng - wspolrzedne ( y, x ) punktu wyswietlanego na mapie
	*/
	function createMarker( latlng )
	{
		//var markerOptions = { icon: new GIcon( G_DEFAULT_ICON ), draggable: true };
		markers = new GMarker( latlng,  cfGetIcon( true ) );
		GEvent.addListener( markers , 'dragend' , function()  
		{
			point = markers.getLatLng();
			$( "hidden_gmaps_y" ).value = point.lat();
			$( "hidden_gmaps_x" ).value = point.lng();
		}
		);
		return markers;
	}

	/**
	* Funkcja dodajaca marker na mape o zadanym adresie
	* @author kamilag
	* @param  string txt - informacja wyswietlana gdy dany adres nie zostal odnaleziony
	*/
	function addMarker( txt, txt2 )
	{
		street = $( "street" ).value;
		city = $( "city" ).value;
		number = $( "number" ).value;
		country = 'Polska';
		if( city == '' )
		{	
			city = 'Szczecin';
		}
		if( !map )
		{
			loadGMDynamic();
		}
		//alert( country );
		//if( ( street != '' ) && ( city != '' ) && ( country != '' ) )
		//{
			address = country +' '+ city +' '+street+' '+number;
		if( typeof( GClientGeocoder ) == 'function' )
		{
			geocoder = new GClientGeocoder();
			if( geocoder )
			{
				geocoder.getLatLng( address, function( point )
											{
												if( !point )
												{
													alert( txt );
												}
												else
												{
													map.clearOverlays();
													map.setCenter( point, 14 );
													map.addOverlay( createMarker( point ) );
													$( "hidden_gmaps_y" ).value = point.lat();
													$( "hidden_gmaps_x" ).value = point.lng();
												}
											}
										);
			}
		}
		else
		{
			setTimeout( "addMarker( '"+txt+"', '"+txt2+"' )", 500 );
		}
		//}
		//else
		//{
		//	alert( txt2 );
		//}
	}

	/**
	* Funkcja dodajaca marker po klikniecu na mape
	* @author kamilag
	* @param  latlng wspolrzedne ( y, x ) punktu wyswietlanego na mapie
	* @param string  txt -  informacja wyswietlana podczas dodawania punktu na mapie
	*/
	function addMarkerOnMap( latlng, txt )
	{
		if( ( latlng != null ) || ( latlng != undefined ) )
		{
			//if( confirm( txt ) )
			//{
				map.clearOverlays();
				map.addOverlay( createMarker( latlng ) );
				$( "hidden_gmaps_y" ).value = latlng.lat();
				$( "hidden_gmaps_x" ).value = latlng.lng();
				latlng = null;
			//}
		}
	}

	/**
	* Funkcja ladujaca mape google do edycji w ogloszeniach
	* @author piotrj
	*/
	function loadGMapsForViewDynamic()
	{
		google.load( "maps", "2", {"callback" : loadGMapsForView} );
	}

	/**
	* Funkcja ladujaca mape google tylko do wyswietlenia w ogloszeniach
	* @author piotrj
	*/
	function loadGMapsForView()
	{
		if( GBrowserIsCompatible() )
		{
			if( gMapObj = $( "map" ) )
			{
				GMarker.prototype.Info=function( category )  
				{
					this.openInfoWindow( category );
				}
				map = new GMap2( gMapObj );
				if( oMX = $( "hidden_gmaps_x" ) )
				{
					gmaps_x = oMX.value;
				}
				if( oMY = $( "hidden_gmaps_y" ) )
				{
					gmaps_y = oMY.value;
				}
				if( ( gmaps_x == '' ) || ( gmaps_y == '' ) )
				{
					var point = new GLatLng( 53.42360955801476, 14.556884765625 );
				}
				else
				{
					var point = new GLatLng( gmaps_y, gmaps_x );
				}
				var zoomGMap = 12;//8;
				//var point = new GLatLng( gmaps_y, gmaps_x );
				map.setCenter( point, zoomGMap );
				map.addControl( new GSmallMapControl() );
				map.addControl( new GMapTypeControl() );
				//map.addOverlay( createMarkerOnMapForView( point ) ); //createMarkerWidthIcon( point, iconUrl, 0, data[k] )
				if( data[0] )
				{
					iconUrl = data[0]["icon"];
					map.addOverlay( createMarkerWidthIcon( point, iconUrl, 0, data[0] ) );
				}
			}
		}
	}

	/**
	* Funkcja dodajaca zakotwiczony marker na mape
	* @author piotrj
	* @param  latlng - wspolrzedne ( y, x ) punktu wyswietlanego na mapie
	*/
	function createMarkerOnMapForView( latlng )
	{
		var markerOptions = { icon: new GIcon( G_DEFAULT_ICON ), draggable: false };
		var mark = new GMarker( latlng,  markerOptions );
		return mark;
	}
	
	
	
	/**
	* Funkcja ladujaca duza mape google
	* @author piotrj
	*/
	function loadBigGMDynamic()
	{
		google.load( "maps", "2", {"callback" : loadBigGM } );
	}
	
	/**
	* Funkcja ladujaca duza mape
	* @author kamilag
	* @param  city 
	*/
	function loadBigGM()
	{
		if ( GBrowserIsCompatible() ) 
		{ 
			if( $( "map" ) )
			{
				GMarker.prototype.Info=function( category )  
				{
					this.openInfoWindow( category );
				}
				map = new GMap2( $( "map" ) ); 
				var point = new GLatLng( 53.42360955801476, 14.556884765625 );
				map.setCenter( point, 11 );  
				map.addControl( new GLargeMapControl() );
				map.addControl( new GMapTypeControl() );
				
				geocoder = new GClientGeocoder();
				/*if( city != '' )
				{
					setMap( city );
				}*/
				getProducts( 'users', '', '' );
				changeCategoryFilter( 'users' );
			}
		} 
	}
	
	/* 
	* Funkcja ustawia ikonke markera
	* @author kamilag
	* @param drag - czy mozna przesuwac
	*/
	function cfGetIcon( drag )
	{
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		if( ( typeof( iconUrl ) != 'undefined' )  || ( typeof( ghost ) != 'undefined' ) )
		{
			blueIcon.iconSize = new GSize( 32, 39 );
			blueIcon.iconAnchor = new GPoint( 16, 39 );
			blueIcon.shadow = ghost+'/images/icons/ico00_shadow.png';
			blueIcon.shadowSize = new GSize( 36, 39 );
			blueIcon.image = iconUrl;
			
			var markerOptions = { icon: blueIcon, draggable: drag };
		}	
		return markerOptions;
	}
	
	// ustawienie ikonki markera
	function getIcon( url )
	{
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		if( ( url != '' ) )
		{
			blueIcon.iconSize = new GSize( 32, 39 );
			blueIcon.iconAnchor = new GPoint( 16, 39 );
			blueIcon.shadow = ghost+'/images/icons/ico00_shadow.png';
			blueIcon.shadowSize = new GSize( 36, 39 );
			//blueIcon.infoWindowAnchor=new GPoint(16,0); 
			blueIcon.image = url;
			var markerOptions = { icon: blueIcon, draggable: false };
		}
		else
		{
			blueIcon.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
			blueIcon.iconSize = new GSize( 12, 20 );
			blueIcon.shadow =  'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
			blueIcon.shadowSize = new GSize( 22, 20 );
			var markerOptions = { icon: blueIcon, draggable: false };
		}	
		return markerOptions;
	}


	function getProducts( category, province, subcategory, page )
	{
		//alert( ajaxSendUrl );
		//gmapsChangeCategory( category, url );
		/*if( $( 'subCat' ) && ( $( 'subCat' ).value != '' ) )
		{
			if( ( subcategory == '' )  ||( ( subcategory != '' ) && ( subcategory == $( 'subCat' ).value ) ) )
			{
				subcategory = $( 'subCat' ).value;
			}
		}*/
		if( $( 'subCat' ) &&  subcategory == '' )
		{
			subcategory = $( 'subCat' ).value;
		}
		
		if( $( 'provinceCat' ) &&  province == '' )
		{
			province = $( 'provinceCat' ).value;
		}

		if( ( category == '' ) && ( $( 'catMain' ) ) )
		{
			category = $( 'catMain' ).value;
		}
		//alert( 'subcategory='+subcategory );
		//alert( 'province='+province );
		//alert( 'category='+category );
		new Ajax( ajaxSendUrl,
				{
					method: 'post',
					data: "cat="+category+"&province="+province+"&subcategory="+subcategory+"&page="+page,
					onComplete: function()
								{	
									if( $( 'subCat' ) )
									{
										$( 'subCat' ).value = subcategory;
									}
									gmapsChangeCategory( category );
								}	,
					update: $( 'list' ),
					onFailure: function()
								{
								},
					evalScripts: true
				}
			).request();
	}
	
	function changeCategoryFilter( category )
	{
			new Ajax( ajaxSendUrl_1,
				{
					method: 'post',
					data: "cat="+category,
					/*onComplete: function()
								{	
									gmapsChangeCategory( category, url );
								}	,*/
					update: $( 'category' ),
					onFailure: function()
								{
								},
					evalScripts: true
				}
			).request();
	}

	function gmapsChangeCategory( category ) 
	{
		if( category == undefined )
		{
			category = '';
		}
		if( data == undefined )
		{
			data = [];
		}
		map.closeInfoWindow();
		deleteMarker() ;
		html = '';
		for ( var k=0; k<data.length; k++ ) 
		{
			if( category == data[k]["category"] )
			{
				point = new GLatLng( data[k]["pos"][1], data[k]["pos"][0] );
				iconUrl = data[k]["icon"];
				mapmark = createMarkerWidthIcon( point, iconUrl, 0, data[k] );
				map.addOverlay( mapmark );
			}
		}
	}
	
	function gmapsProvinceFilter( province, category )
	{
		map.closeInfoWindow();
		deleteMarker() ;
		html = '';
		alert( province );
		for ( var k=0; k<data.length; k++ ) 
		{
			if( province == data[k]["province"] )
			{
				point = new GLatLng( data[k]["pos"][1], data[k]["pos"][0] );
				iconUrl = data[k]["icon"];
				mapmark = createMarkerWidthIcon( point, iconUrl, 0, data[k] );
				map.addOverlay( mapmark );
			}
			else if ( ( province == '' ) && ( category == data[k]["category"] ) )
			{
				point = new GLatLng( data[k]["pos"][1], data[k]["pos"][0] );
				iconUrl = data[k]["icon"];
				mapmark = createMarkerWidthIcon( point, iconUrl, 0, data[k] );
				map.addOverlay( mapmark );
			}
		}
	}
	
	
  	function createMarkerWidthIcon( latlng, url, type, gdata )
	{
		gicon = getIcon( url );
		if( type == 1 )
		{
			gcategory = 'text';
		}
		else
		{
			gcategory = gdata["category"];
		}
		var markers = new GMarker( latlng,  gicon );
		markers.isVisible = 0;
		markers.mycategory = gcategory;
		if( type == 0 )
		{
			markers.title = gdata["title"];
			markers.id = gdata["id"];
		}
		if( type != 1 )
		{
			GEvent.addListener( markers, "click", function()
										{
											//markers.openInfoWindowHtml( putHtml() );
											new Ajax( ajaxSendUrl_2,
											{
												method: 'post',
												data: "id="+gdata["id"]+ "&cat="+gcategory+"&sub="+gdata["subcategory"],
												onComplete: function( zmienna )
															{	
																markers.openInfoWindowHtml( zmienna );
															},
							
												onFailure: function()
															{
															},
												evalScripts: true
											}
											).request();
										}
			);
		}
		gmarkers.push( markers );
		return markers;
	}
	
	//GMarker.prototype.Info=function( category )  
	//{
	//	this.openInfoWindow( category );
	//}
	
	function showInfo( x, category, subcategory )
	{
		getInfoText( x, category, subcategory );
	}

	function showInfo_1( x, zmienna )
	{
		for( var i=0; i<gmarkers.length; i++ ) 
		{
			if( gmarkers[i].id == x )
			{
				//gmarkers[i].Info( gmarkers[i].name );
				gmarkers[i].Info( zmienna );
			}
		}
	}


	function getInfoText( x, category, subcategory )
	{
		new Ajax( ajaxSendUrl_2,
				{
					method: 'post',
					data: "id="+x+"&cat="+category+"&sub="+subcategory,
					onComplete: function( zmienna )
								{	
									showInfo_1( x , zmienna );
								},
					onFailure: function()
								{
								},
					evalScripts: true
				}
			).request();
	}




	function deleteMarker() 
	{
		map.clearOverlays();
	}




	function showAddress( address )
	{
		if( !map )
		{
			loadBigGMDynamic();
		}
		if( geocoder )
		{
			geocoder.getLatLng( address, function( point )
										{
											if( !point )
											{
												alert( address + " nie ma" );
											}
											else
											{
												map.setCenter( point, 14 );
												map.openInfoWindow( map.getCenter(), document.createTextNode( address ) );
											}
										}
			);
		}
	}
	
	/**
	* Opis: funkcja przelacza taby ma mapie
	*/
	function switchGMapTabs( nr )
	{
		/*if( nr != lastOpenedMainTab )
		{
			$( 'tabSwitcher'+lastOpenedMainTab+'' ).className = 'gmapTabOff';
			$( 'tabSwitcher'+nr+'' ).className = 'gmapTabOn';
			$( 'tabArow'+lastOpenedMainTab+'' ).style.display = 'none';
			$( 'tabArow'+nr+'' ).style.display = 'block';
			lastOpenedMainTab = nr;
			new Ajax( ajaxSendUrl,
				{
					method: 'post',
					data: "currentPar="+id,
					onFailure: function()
								{
								},
					update: $( 'mapList' ),
					evalScripts: true
				}
			).request();
		}*/
		if( !map )
		{
			loadBigGMDynamic();
		}
		if( $$( '.exTopLeftL_On' )[0] )
		{
			$$( '.exTopLeftL_On' )[0].className = 'exTopLeftL_Off';
		}
		if( $$( '.exTopRightL_On' )[0] )
		{
			$$( '.exTopRightL_On' )[0].className = 'exTopRightL_Off';
		}
		if(	$$( '.exTopMidL_On' )[0] )
		{
			$$( '.exTopMidL_On' )[0].className = 'exTopMidL_Off';
		}
		$( 'tab_'+nr ).className = 'exTopLeftL_On';
		
		if( nr != 4 )
		{
			$( 'kom_0' ).style.display = 'block';
			$( 'kom_1' ).style.display = 'none';
			$( 'list' ).style.display = 'block';
			cat =  new Array( 'users', 'events', 'firm', 'property' );
			if( $( 'catMain' ) )
			{
				$( 'catMain' ).value = cat[nr];
			}
			if( $( 'subCat' ) )
			{
				$( 'subCat' ).value = '';
			}
			changeCategoryFilter( cat[nr] );
			getProducts( cat[nr], '', '' );
		}
		else
		{
			$( 'kom_0' ).style.display = 'none';
			$( 'kom_1' ).style.display = 'block';
			$( 'list' ).style.display = 'none';
			
			map.clearOverlays();
		}
	}
	
	function showLine( line ) 
	{
		map.clearOverlays(); 
		//var req = new Request( {url:'ajax.php', evalScripts: true} ).get( {'line': line.innerHTML} );
		if( lastTrasNum != '' )
		{
			$( lastTrasNum ).className = 'trasNum';
		}
		lastTrasNum = thisTrasNum;
		new Ajax( ajaxSendUrl_3,
				{
					method: 'get',
					data: "line="+line.innerHTML,
					onComplete: function( zmienna )
								{	
								},
					onFailure: function()
								{
								},
					evalScripts: true
				}
			).request();
	}
	
	var lastTrasNum = '';
	var thisTrasNum = '';

