/**
 *  @version $Id: gMap.js 90 2010-05-24 08:59:11Z paulinad $
 *	@package frontend
 *	@author kamilag
 */
 
 	/**
	* Funkcja ladujaca duza mape google
	* @author paulinad
	*/
	function loadChGMDynamic()
	{
		google.load( "maps", "2", {"callback" : loadChGM } );
	}
 	
 	/**
	* Funkcja ladujaca mapke
	* @author paulinad
	*/
	function loadChGM()
	{
		if ( GBrowserIsCompatible() ) 
		{
			var mObj;
			if( mObj = $( "map" ) )
			{
				//GMarker.prototype.Info=function( category )  
				//{
				//	this.openInfoWindow( category );
				//}
				map = new GMap2( mObj ); 
				var zoomMap = 13;
				var gmaps_x = $( "hidden_gmaps_com_x" ).value;
				var gmaps_y = $( "hidden_gmaps_com_y" ).value;
				var url = '';
				var point = new GLatLng( gmaps_y, gmaps_x );
				map.setCenter( point, zoomMap );  
				//map.addControl( new GSmallMapControl() );
				//map.addControl( new GMapTypeControl() );
				
				geocoder = new GClientGeocoder();
				//alert('point 05');
				iconUrl = '';//data[k]["icon"];
				var dataTmp={"id":'aaa'};
				mapmark = createMarkerWidthIcon( point, iconUrl, dataTmp, 1 );
				map.addOverlay( mapmark );
				mObj.addEvent( 'mouseover', function(){
								resizeToWithMargins( 400, 400, -260, 0, 1 );
								//resizeToWithMargins( 400, 400, 0, 0, 1 );
							 });
				mObj.addEvent( 'mouseout', function(){
								resizeToWithMargins( 140, 140, 0, 0, 0 );
							 });
			}
		} 
	}
 	
	var gMapProvinceId = 0;
	var gMapCityId = 0;
	var gMapPage = 1;
	var gMapTypeId = 0;
	
	var gMapMarkers = [];
	
	var ifUpdateGMapCities = true;
	var ifUpdateGMapMarkers = true;
	var ifCenterProvince = true;
	var ifCenterCity = false;
	
	var gMapProvinceCentres = new Array();
	gMapProvinceCentres[0] = new Array( 18.9646484375, 52.26815737376817, 6 );
	gMapProvinceCentres[1] = new Array( 16.4916015625, 51.11041991029264, 8 );
	gMapProvinceCentres[2] = new Array( 18.568017578125, 53.08082737207479, 8 );
	gMapProvinceCentres[3] = new Array( 23.06142578125, 51.2206474303833, 8 );
	gMapProvinceCentres[4] = new Array( 15.37099609375, 52.14697334064471, 8 );
	gMapProvinceCentres[5] = new Array( 19.5677734375, 51.56341232867588, 8 );
	gMapProvinceCentres[6] = new Array( 20.391748046875, 49.79544988802771, 8 );
	gMapProvinceCentres[7] = new Array( 21.17177734375, 52.3755991766591, 8 );
	gMapProvinceCentres[8] = new Array( 18.018701171875, 50.666872321810715, 8 );
	gMapProvinceCentres[9] = new Array( 22.347314453125, 50.04302974380058, 8 );
	gMapProvinceCentres[10] = new Array( 23.072412109375, 53.34399288223421, 8 );
	gMapProvinceCentres[11] = new Array( 18.062646484375, 54.265224078605655, 8 );
	gMapProvinceCentres[12] = new Array( 18.996484375, 50.2612538275847, 8 );
	gMapProvinceCentres[13] = new Array( 20.8861328125, 50.764259357116465, 8 );
	gMapProvinceCentres[14] = new Array( 21.03994140625, 53.8460456413833, 8 );
	gMapProvinceCentres[15] = new Array( 17.34853515625, 52.335339071889386, 8 );
	gMapProvinceCentres[16] = new Array( 15.799462890625, 53.69020141273198, 8 );
	
	/**
	* Funkcja ladujaca duza mape google
	* @author paulinad
	*/
	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();
				getProducts( 'users', '', '' );
				changeCategoryFilter( 'users' );
			}
		} 
	}
	*/
	
	/**
	* Funkcja ladujaca duza mape
	* @author paulinad
	*/
	function loadBigGM()
	{
		if ( GBrowserIsCompatible() ) 
		{
			if( $( "map" ) )
			{
				GMarker.prototype.Info=function( category )  
				{
					this.openInfoWindow( category );
				}
				map = new GMap2( $( "map" ) ); 
				var point = new GLatLng( gMapProvinceCentres[0][1], gMapProvinceCentres[0][0] );
				map.setCenter( point, gMapProvinceCentres[0][2] );  
				map.addControl( new GLargeMapControl() );
				map.addControl( new GMapTypeControl() );
				
				geocoder = new GClientGeocoder();
				
				updateGMapElems();
			}
		} 
	}
	
	
	function updateGMapElems()
	{
		new Request(
				{
					method: 'post',
					url: ajaxSendUrl,
					onComplete: function()
								{	
									//if( $( 'subCat' ) )
									//{
									//	$( 'subCat' ).value = subcategory;
									//}
									//gmapsChangeCategory( category );
									//
									
									if( ifUpdateGMapMarkers )
									{
										updateGMapMarkers();
									}
									//var pom = false;
									if( ifCenterCity )
									{
										ifCenterCity = false;
										if( !centerGMapCity() )
										{
											ifCenterProvince = true;
										}
									}
									if( ifCenterProvince )
									{
										ifCenterProvince = false;
										centerGMapProvince();
									}
									if( $( 'gmapProv' ) )
									{
										$( 'gmapProv' ).value = gMapProvinceId;
									}
									if( $( 'gmapCity' ) )
									{
										$( 'gmapCity' ).value = gMapCityId;
									}
								},
					onSuccess: function( responseText, responseXML )
								{
									$( 'gMapListOuter' ).innerHTML = responseText;
								},
					onFailure: function()
								{
								},
					evalScripts: true
				}
			).send( "getElems=1&gmapType="+gMapTypeId+"&gmapProv="+gMapProvinceId+"&gmapCity="+gMapCityId+"&gmapPage="+gMapPage );
		if( ifUpdateGMapCities )
		{
			updateGMapCities();
		}
	}
	
	function deleteMarkers()
	{
		map.clearOverlays();
	}
	
	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 );
		}
	}
	
	function updateGMapCities()
	{
		new Request(
				{
					method: 'post',
					url: ajaxSendUrl,
					onComplete: function()
								{	
									//centerGMapCity();
									//ifCenterProvince = false;
								},
					onSuccess: function( responseText, responseXML )
								{
									$( 'gMapSearcher' ).innerHTML = responseText;
								},
					onFailure: function()
								{
								},
					evalScripts: true
				}
			).send( "getSearcher=1&gmapType="+gMapTypeId+"&gmapProv="+gMapProvinceId+"&gmapCity="+gMapCityId+"&gmapPage="+gMapPage );
	}
	
	function changeGMapTab( typeId )
	{
		if( !isNaN( typeId ) )
		{
			if( ( typeId >= 0 ) && ( typeId <= 4 ) )
			{
				correctGMapFilter( typeId, gMapProvinceId, gMapCityId, gMapPage );
				var set = $$( '.lvl3Menu2On' );
				if( set.length > 0 )
				{
					for( var i = 0; i < set.length; i++ )
					{
						set[i].className = 'lvl3Menu2Off';
					}
				}
				$( 'gMapTab_'+typeId ).className = 'lvl3Menu2On';
				if( !map )
				{
					loadBigGMDynamic();
				}
				else
				{
					updateGMapElems();
				}
			}
		}
	}
	
	function changeGMapProvince( provinceId )
	{
		if( !isNaN( provinceId )  )
		{
			if( ( provinceId >= 0 ) && ( provinceId <= 16 ) )
			{
				correctGMapFilter( gMapTypeId, provinceId, gMapCityId, gMapPage );
				if( !map )
				{
					loadBigGMDynamic();
				}
				else
				{
					updateGMapElems();
				}
			}
		}
	}
	
	function changeGMapCity( cityId )
	{
		if( !isNaN( cityId )  )
		{
			correctGMapFilter( gMapTypeId, gMapProvinceId, cityId, gMapPage );
			if( !map )
			{
				loadBigGMDynamic();
			}
			else
			{
				updateGMapElems();
			}
		}
	}
	
	function changeGMapPage( page )
	{
		correctGMapFilter( gMapTypeId, gMapProvinceId, gMapCityId, page );
		if( !map )
		{
			loadBigGMDynamic();
		}
		else
		{
			updateGMapElems();
		}
	}
	
	function correctGMapFilter( type, province, city, page )
	{
		var oldType = gMapTypeId;
		var oldProvince = gMapProvinceId;
		var oldCity = gMapCityId;
		var oldPage = gMapPage;
		ifUpdateGMapCities = false;
		ifUpdateGMapMarkers = false;
		ifCenterProvince = false
		if( province != oldProvince )
		{
			city = 0;
			ifUpdateGMapCities = true;
			ifCenterProvince = true;
		}
		if( ( type != oldType ) 
			|| ( province != oldProvince ) 
			|| ( city != oldCity ) )
		{
			page = 1;
			ifUpdateGMapMarkers = true;
		}
		if( ( city != oldCity ) && ( city > 0 ) )
		{
			ifCenterCity = true;
		}
		gMapTypeId = type;
		gMapProvinceId = province;
		gMapCityId = city;
		gMapPage = page;
	}
	
	
	function createMarkerWidthIcon( latlng, url, gdata, type )
	{
		var gicon = getIcon( url );
		var markers = new GMarker( latlng, gicon );
		markers.isVisible = 0;
		markers.id = gdata["id"];
		markers.myContent = '';
		if( type == 0 )
		{
			//markers.title = gdata["title"];
			
		}
		if( type != 1 )
		{
			GEvent.addListener( markers, "click", function()
										{
											gmapShowInfo( markers.id );
										}
			);
		}
		//gmarkers.push( markers );
		gMapMarkers[markers.id] = markers;
		return markers;
	}
	
	function gmapShowInfo( id )
	{
		if( gMapMarkers[id] != undefined )
		{
			if( gMapMarkers[id].myContent != '' )
			{
				gMapMarkers[id].openInfoWindowHtml( gMapMarkers[id].myContent );
			}
			else
			{
				new Request(
					{
						method: 'post',
						url: ajaxSendUrl,
						onComplete: function( zmienna )
									{	
										gMapMarkers[id].myContent = zmienna;
										gMapMarkers[id].openInfoWindowHtml( zmienna );
									},
						onFailure: function()
									{
									},
						evalScripts: true
					}
				).send( "getInfo=1&gmapElemId="+id );
			}
		}
	}
	
	// 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( 16, 32 );
			blueIcon.shadow = ghost+'/images/icons/ico_shade.png';
			blueIcon.shadowSize = new GSize( 32, 32 );
			//blueIcon.infoWindowAnchor=new GPoint(16,0);
			//blueIcon.image = url;
			blueIcon.image = ghost+'/images/icons/ico.png';
		}
		/*
		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 centerGMapProvince( provId )
	{
		var centerProvId = -1;
		if( provId == undefined )
		{
			centerProvId = gMapProvinceId;
		}
		else
		{
			if( !isNaN( provId ) )
			{
				centerProvId = provId;
			}
		}
		if( gMapProvinceCentres[centerProvId] != undefined )
		{
			//alert( 'centerProvId: ' + centerProvId + '; x: ' + gMapProvinceCentres[centerProvId][0]+ '; y:'+gMapProvinceCentres[centerProvId][1] );
			var point = new GLatLng( gMapProvinceCentres[centerProvId][1], gMapProvinceCentres[centerProvId][0] );
			map.setCenter( point, gMapProvinceCentres[centerProvId][2] );
			return true;
		}
		return false;
	}
	
	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, 10 );
			return true;
		}
		return false;
	}
	
	/**
	 * @author		unknown
	 */
	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 ) );
											}
										}
			);
		}
	}
