/**
*   @version $Id$
*	@package frontend googleMaps
*	@author kamilag
*/

	var map = null;
	var mapmark = [];
	var html = [];
	var geocoder = null;
	var gmarkers = [];
	
	/**
	* 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 url - adres do odpowiedniej ikonki dla markera
	* @param string txt - informacja wyswietlana podczas dodawania punktu na mapie
	*/
	function loadGM( gmaps_x, gmaps_y, url )
	{ 
		if ( GBrowserIsCompatible() ) 
		{ 
			if( document.getElementById( "map" ) )
			{
				map = new GMap2( document.getElementById( "map" ) ); 
				if( ( gmaps_x != '' ) && ( gmaps_y != '' ) )
				{
					var point = new GLatLng( gmaps_y, gmaps_x );
					var number = 16;
				}
				else
				{
					var point = new GLatLng( 54.388298, 18.578443 );
					var number = 10;  
				}
				map.setCenter( point, number );  
				if( ( gmaps_x != '' ) && ( gmaps_y != '' ) )
				{
					html ='';
					map.addOverlay( createMarker( point, url, 1, html ) );
				}
			}
		} 
	}
	
	
	
	
	// Creates a marker at the given point
	// Clicking the marker will hide it
	function createMarker( latlng, url, type, gdata )
	{
		gicon = getIcon( url );
		if( type == 1 )
		{
			gcategory = 'text';
			var myhtml = gdata;
		}
		else
		{
			gcategory = gdata["category"];
			more = '';
			if( gdata["more"] )
			{
				more = '<a href="'+gdata["more"]+'" style="font-weight: bold;">&#187; more</a>';
			}
			var myhtml = '<h4 style="font-size: 12px; color: #5c5757; font-weight: bold; margin: 0px; padding-top: 5px;">'+gdata["title"]+'</h4>'+more;
		}
		var markers = new GMarker( latlng,  gicon );
		markers.isVisible = 0;
		markers.mycategory = gcategory;
		markers.name = myhtml;
		if( type == 0 )
		{
			markers.title = gdata["title"];
		}
		if( type != 1 )
		{
			GEvent.addListener( markers, "click", function()
										{
											markers.openInfoWindowHtml( myhtml );
										}
			);
		}
		gmarkers.push( markers );
		return markers;
	}
	
	
	
	function createStaticMarker( latlng, url, txt )
	{
		gicon = getIcon( url );
		var marker = new GMarker( latlng,  gicon );
		GEvent.addListener( marker, "click", function()
									{
										marker.openInfoWindowHtml( txt );
									}
		);
		return marker;
	}


	
	
	function loadBigGM( url, city )
	{
		if ( GBrowserIsCompatible() ) 
		{ 
			if( document.getElementById( "map" ) )
			{
				map = new GMap2( document.getElementById( "map" ) ); 
				var point = new GLatLng( 54.388298, 18.578443 );
				map.setCenter( point, 10 );  
				map.addControl( new GLargeMapControl() );
				map.addControl( new GMapTypeControl() );
				
				geocoder = new GClientGeocoder();
				if( city != '' )
				{
					setMap( city );
				}
				getProducts( 'lista_centrow_handlowych', '', 2, 1 );
			}
		} 
	}
	
	
	function loadShoppingGM( url )
	{
		if ( GBrowserIsCompatible() ) 
		{ 
			if( document.getElementById( "map" ) )
			{
				map = new GMap2( document.getElementById( "map" ) ); 
				geocoder = new GClientGeocoder();
				
				var point = new GLatLng( 54.44162184889164, 18.56011390686035 );
				map.setCenter( point, 10 ); 
				map.addControl( new GLargeMapControl() );
				map.addControl( new GMapTypeControl() );
				gmapsChangeCategory( 'lista_centrow_handlowych', '' );
				showStaticMarker();
			}
		}
	}


	// ustawienie ikonki markera
	function getIcon( url )
	{
		var blueIcon = new GIcon(G_DEFAULT_ICON);
		if( ( url != '' ) )
		{
			
			blueIcon.iconSize = new GSize( 32, 32 );
			blueIcon.iconAnchor = new GPoint( 32, 32 );
			blueIcon.shadow = ghost+'/images/icons/bg_shade.png';
			blueIcon.shadowSize = new GSize( 49, 32 );
			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, url , id, strona )
	{
		if( url == undefined )
		{
			url = ghost+'/images/icons/bg_shade.png';
		}
		
		new Ajax( ajaxSendUrl,
				{
					method: 'post',
					data: "mainGroupId="+id+"&strona="+strona+"&cat="+category,
					onComplete: function( html )
									{
										
										$( 'list' ).innerHTML = html;
										gmapsChangeCategory( category, url );
									},
					onFailure: function()
								{
								},
					evalScripts: true
				}
			).request();
	}

	function gmapsChangeCategory( category, url ) 
	{
		
		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] );
				if( url == '' )
				{
					iconUrl = data[k]["icon"];
				}
				else
				{
					iconUrl = url;
				}
				mapmark = createMarker( point, iconUrl, 0, data[k] );
				map.addOverlay( mapmark );
			}
		}
	}
  
  
    function deleteMarker() 
	{
		map.clearOverlays();
		/*for( var i=0; i<gmarkers.length; i++ ) 
		{
			gmarkers[i] = null;
		}*/
    }
   

	
	
	
	
	function showAddress( address )
	{
		if( geocoder )
		{
			geocoder.getLatLng( address, function( point )
										{
											if( !point )
											{
												alert( "Podanego punktu nie ma na mapie" );
											}
											else
											{
												map.setCenter( point, 14 );
												map.openInfoWindow( map.getCenter(), document.createTextNode( address ) );
											}
										}
			);
		}
	}
	
	
	/**
	* Opis: funkcja podswietla taby na stronie glownej
	*/
	function lightGMapTabs( id, on )
	{
		oDiv = $( id );
		if( oDiv.className != 'gmapTabOn' )
		{
			if( on == 1 )
			{
				oDiv.className = 'gmapTabOffOver';
			}
			else
			{
				oDiv.className = 'gmapTabOff';
			}
		}
	}


		/**
	* Opis: funkcja przelacza taby na stronie glownej
	*/
	function switchGMapTabs( nr, id )
	{
		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();
		}
	}
	
	function showInfo( x )
	{
		for( var i=0; i<gmarkers.length; i++ ) 
		{
			if( gmarkers[i].title == x )
			{
				gmarkers[i].Info( gmarkers[i].name );
			}
		}
	}
	
	GMarker.prototype.Info=function( category )  
	{
		this.openInfoWindow( category );
	}


	function setMap( address )
	{
		if( address == 'Region' )
		{
			address = 'Sopot';
			num = 10;
		}
		else
		{
			num = 14;
		}
		if( geocoder )
		{
			geocoder.getLatLng( address, function( point )
										{
											map.setCenter( point, num );
										}
			);
		}
	}
	
	
	function showStaticMarker()
	{
		var point = new GLatLng( 54.54802328688133, 18.506137132644653 );
		txt = '<h4 style="font-size: 12px; color: #5c5757; font-weight: bold; margin: 0px;">Stena Line</h4>Eugeniusza Kwiatkowskiego 60<br/>Gdynia<br/>tel.: 0 48 58 660 92 00';
		map.addOverlay( createStaticMarker( point, ghost+'/images/icons/prom_32x32.png', txt ) );
		var point = new GLatLng( 54.380677, 18.476262 );
		txt = '<h4 style="font-size: 12px; color: #5c5757; font-weight: bold; margin: 0px;">Port Lotniczy Gdańsk Sp. z o.o.</h4>Juliusza Słowackiego<br/>Gdańsk<br/>tel.: 0 48 58 348 11 11';
		map.addOverlay( createStaticMarker( point, ghost+'/images/icons/airport_32x32.png', txt ) );
		var point = new GLatLng( 54.405587, 18.66197 );
		txt = '<h4 style="font-size: 12px; color: #5c5757; font-weight: bold; margin: 0px;">Polska Żegluga Bałtycka SA Polferries</h4>Przemysłowa 1<br/>80-542 Gdańsk<br/>Tel.: (+48) 58 343 18 87, (+48) 58 343 02 12';
		map.addOverlay( createStaticMarker( point, ghost+'/images/icons/prom_32x32.png', txt ) );
		
	}
