function setObj()
	{	
	window.zLoc= {
		place:document.getElementById('place'),
		rgeo:document.getElementById('rgeo'),
		lat:document.getElementById('lat'),
		lng:document.getElementById('lng'),
		setby:document.getElementById('setby'),
		say:document.getElementById('say'),
		sayMsg:document.getElementById('autoMsg') }
	window.geocoder= new GClientGeocoder()
	}

function updateForm(lat,lng,setby,say)
	{
	autoLocationInterrupted= true
	strView(lat,lng)
	zLoc.lat.value= lat
	zLoc.lng.value= lng
	zLoc.setby.value= setby
	zLoc.sayMsg.innerHTML= say
	zLoc.say.value= say
	latlng= '(' + lat + ', ' + lng + ')'
	geocoder.getLocations(latlng, revGeo)
	if (scrpt == _Gp_searchresults)
		{
		gSearch_Bring_Results()
		document.getElementById('mapZoom').style.display= 'block'
		}
	}

function setZkatterMarkerObj()
	{
	if (document.getElementById('mtgt_unnamed_0')) window.zkatterMarkerObj= document.getElementById('mtgt_unnamed_0')
	else 
		{
		imgs= document.getElementById('zkatterMap').getElementsByTagName('img')
		for (f=0; f<imgs.length; f++) if (substr_count(imgs[f].src,'_img/marker.png')) window.zkatterMarkerObj= imgs[f]
		}
	}

function revGeo(rsp)
	{
	if (!window.zkatterMarkerObj) setZkatterMarkerObj()
	if (!rsp || rsp.Status.code != 200) {}
	else
		{
		if (rsp.Placemark[0].address != '') zLoc.rgeo.value= rsp.Placemark[0].address
		else zLoc.rgeo.value= 'Unknown'
		if (document.getElementById('search'))
			if (zLoc.rgeo.value == '' || zLoc.rgeo.value == 'Unknown') gsSearch= gSearch_Sempty_listmedia, document.getElementById('search').value= gSearch_Sempty_listmedia
			else gsSearch= zLoc.rgeo.value, document.getElementById('search').value= zLoc.rgeo.value
		else if (zkatterMarkerObj) zkatterMarkerObj.setAttribute('onmouseover','balloon.showTooltip(event,\'' + zLoc.rgeo.value + '\')')
		}
	}

function locateMe(flg)
	{
	if (flg == 1) window.fromFriends= 1
	else window.fromFriends= 0
	if (document.getElementById('searchHeaderNearby')) document.getElementById('searchHeaderNearby').innerHTML= 'your'
	setObj()
	if (!window.google || !google.gears) 
		if (!navigator.geolocation) 
			if (!LokiAPI.isInstalled()) popUpPlugin()
			else doLoki()
		else doMozilla()
	else doGears()
	}

function popUpPlugin()
	{
	if (scrpt == _Gp_friends)
		{
		nearbyNA()
		return 
		}
	if (rC('locPlugIn')) setMapByForm()
	else $.openDOMWindow({windowSourceID:'#locPlugin',borderSize:0,windowBGColor:'',windowBGImage:'_img/panel.png',height:375,width:702,windowPadding:0})
	}

function popUpPluginClose(tech)
	{
	cC('locPlugIn',1)
	$.closeDOMWindow()
	if (tech == 'loki') doLoki()
	if (tech == 'gears') location.href= 'http://gears.google.com/?action=install&message=' + encodeURIComponent('Install Google Gears and get the MAX out of Zkatter!') + '&return=' + encodeURIComponent(window.location.href)
	if (!tech) setMapByForm()
	}

function doGears()
	{
	if (document.getElementById('techtxt')) document.getElementById('techtxt').innerHTML= 'Google Gears'
	try
		{
		var gearsGeo= google.gears.factory.create('beta.geolocation')
		gearsGeo.getCurrentPosition(
			function(gearsLoc)
				{
				window.zkatterAccuracy= gearsLoc.accuracy
				setMap(gearsLoc.latitude,gearsLoc.longitude,'Google Gears','Current location set automaticaly from Google Gears.')
				}, 
			function() {setMapByForm('Google Gears didn\'t find you.')}, 
			{enableHighAccuracy:true})
		}
	catch (e) {setMapByForm('Google Gears didn\'t find you.')}
	}

function doMozilla()
	{
	if (document.getElementById('techtxt')) document.getElementById('techtxt').innerHTML= 'Mozilla'
	navigator.geolocation.getCurrentPosition(
		function(position) 
			{
			window.zkatterAccuracy= position.coords.accuracy
			setMap(position.coords.latitude,position.coords.longitude,'Mozilla','Current location set automaticaly from Mozilla.') 
			},
		function(error) {setMapByForm('Mozilla didn\'t find you.')})
	}

function doLoki()
	{
	if (document.getElementById('techtxt')) document.getElementById('techtxt').innerHTML= 'Loki'
	var loki= new LokiAPI()
	loki.onSuccess= function(lokiLoc) 
		{
		window.zkatterAccuracy= lokiLoc.accuracy
		setMap(lokiLoc.latitude, lokiLoc.longitude,'Skyhook','Current location set automaticaly from Loki.')
		}
	loki.onFailure= function(error) {setMapByForm('Loki didn\'t find you.')}
	loki.setKey('beta')
	loki.requestLocation(true, loki.NO_STREET_ADDRESS_LOOKUP)
	}

function setMapByForm(error,obj)
	{
	if (scrpt == _Gp_friends)
		{
		nearbyNA()
		return
		}
	if (scrpt == _Gp_addzkattering) noZkatterAllowed('Your location can\'t be found!')
	if (obj) if (obj.parentNode.firstChild.innerHTML == 'Zkatter') setObj()
	if (error && autoLocationInterrupted) return false
	if (error == 'Locating process interrupted.') autoLocationInterrupted= true
	if (error) zLoc.say.value= error + ' ' + zLoc.say.value
	setMap(zLoc.lat.value, zLoc.lng.value, zLoc.setby.value, zLoc.say.value)
	zLoc.say.value= ''
	}

function searchMap(searchText)
	{
	if (geocoder)
		{
		geocoder.getLatLng(searchText, function(newLoc)
			{
			if (!newLoc) {} 
			else
				{
				msg= ''
				if (!autoLocationInterrupted)
					{
					autoLocationInterrupted= true
					msg= 'Locating process interrupted. '
					}
				setMap(newLoc.lat(),newLoc.lng(),'Google Maps geocoding',msg + 'Current location set from Google Maps geocoding.')
				}
			})
		}
	}

function setMap(lat,lng,setby,say)
	{
	if (window.zkatterAccuracy < 501 && suid) simple_ajax('_ajax/?actlat=' + lat + '&actlng=' + lng)
	if (scrpt == _Gp_friends)
		{
		simple_ajax(_Gp_jfriends_page + '?page=1&latitude=' + lat + '&longitude=' + lng, 'friendSearchNearby')
		updateForm(lat,lng,setby,say)
		return
		}
	if (scrpt == _Gp_addzkattering)
		if (!window.zkatterAccuracy || window.zkatterAccuracy > 500) noZkatterAllowed('Your location can\'t be found!')
	if (autoLocationInterrupted)
		if (setby == 'Skyhook' || setby == 'Mozilla' || setby == 'Google Gears') return false
	document.getElementById('loader').style.display= 'none'
	if (mapActive)
		{
		document.getElementById('searchHeaderNearby').innerHTML= 'this'
		searchBalloonClose()
		circleOverlayRm()
		zMap.checkResize()
		if (setby == 'Zkattering') zMap.panTo(new GLatLng(lat,lng))
		else zMap.setCenter(new GLatLng(lat,lng)) 
		zMapMrk.setPoint(new GLatLng(lat,lng))
		circleOverlay()
		}
	else
		if (GBrowserIsCompatible()) 
			{
			document.getElementById('autoMsg').innerHTML= say
			var zIcon= new GIcon()
				zIcon.image= '_img/marker.png'
				zIcon.iconSize= new GSize(58,66)
				zIcon.iconAnchor= new GPoint(0,66)
				zIcon.infoWindowAnchor= new GPoint(31,2)
			if (scrpt == _Gp_addzkattering) draggable= false
			else draggable= true
			var zMarker= {icon:zIcon,draggable:draggable}
			window.zMapMrk= new GMarker(new GLatLng(lat,lng),zMarker)
				zMapMrk.enableDragging()
			var zMapStr= new GStreetviewOverlay()
			window.zMap= new GMap2(document.getElementById('zkatterMap'))
				zMap.addControl(new GLargeMapControl())
				zMap.addControl(new GMapTypeControl())
				zMap.setCenter(new GLatLng(lat, lng), 17)
				zMap.enableScrollWheelZoom()
				//zMap.addOverlay(zMapStr)
				zMap.addOverlay(zMapMrk)
				zMap.enableScrollWheelZoom()
			GEvent.addListener(zMapMrk, 'dragend', function(newLoc)
				{
				document.getElementById('searchHeaderNearby').innerHTML= 'this'
				searchBalloonClose()
				circleOverlayRm()
				zMap.panTo(new GLatLng(newLoc.lat(), newLoc.lng()))
				updateForm(newLoc.lat(),newLoc.lng(),'Manual','Current location set manually.')
				})			
			circleOverlay()
			mapActive= true
			}
	updateForm(lat,lng,setby,say)
	}

function circleOverlayRm()
	{
	if (window.mapCirlceOverlay)
		{
		zMap.removeOverlay(zCircle)
		window.mapCirlceOverlay= false
		}
	}

function circleOverlay()
	{
	if (window.zkatterAccuracy)
		{
		window.zCircle= new CircleOverlay(zMap.getCenter(), (window.zkatterAccuracy*1000)/1609344, '#336699', 1, 1, '#336699', 0.25)
		zMap.addOverlay(zCircle)
		window.mapCirlceOverlay= true
		window.zkatterAccuracy= 0
		}
	}

function strView(lat,lng)
	{
	window.strViewObj= document.getElementById('zkatterPano')
	document.getElementById('sva').style.visibility= 'hidden'
	document.getElementById('sva').style.display= 'inline'
	setTimeout("document.getElementById('sva').style.visibility= 'visible'",1000)
	var latlng= new GLatLng(lat,lng)
	panOpt= {latlng:latlng}
	strViewObj.innerHTML= ''
	window.bigPano= new GStreetviewPanorama(strViewObj,panOpt)
	GEvent.addListener(bigPano,'error',function()
		{
		document.getElementById('sva').style.display= 'none'
		if (document.getElementById('zkatterPanoCnt').style.display == 'block') strViewEnable()
		})
	}

function strViewCrean()
	{
	if (strViewObj.innerHTML != '') strViewObj.firstChild.style.display= 'block'
	bigPanos= strViewObj.getElementsByTagName('object')
	for (f=0; f<bigPanos.length; f++) if (f) bigPanos[f].parentNode.removeChild(bigPanos[f])	
	}

function strViewEnable()
	{
	if (document.getElementById('zkatterPanoCnt').style.display == 'block')
		{	
		document.getElementById('zkatterPanoCnt').style.display= 'none'
		document.getElementById('svai').src= 'images/unchecked.png'
		}
	else
		{
		if (document.getElementById('zkatterPano').style.height)
			document.getElementById('zkatterPano').firstChild.height= document.getElementById('zkatterPano').style.height.substr(0,3)
		document.getElementById('zkatterPanoCnt').style.display= 'block'
		document.getElementById('svai').src= 'images/checked.png'
		}
	}

function noZkatterAllowed(reason)
	{
	if (!admincheck)
		{
		document.getElementById('errorReason').innerHTML= reason
		document.getElementById('withCam').style.display= 'none'
		document.getElementById('withoutCam').style.display= 'block'
		}
	return false
	}

function expandMap(flg)
	{
	if (flg && document.getElementById('zkatterMap').style.height=='600px') return
	if (document.getElementById('zkatterMap').style.height=='600px')
		{
		document.getElementById('mapZoom').src= 'images/mapZoomIn.png'
		$('#mapArea').animate({'height':'302px'},'slow')
		$('#zkatterMapCnt').animate({'height':'300px'},'slow', function()
			{
			document.getElementById('zkatterMap').style.height= '300px'
			zMap.checkResize()
			zMap.setCenter(new GLatLng(zLoc.lat.value,zLoc.lng.value))
			})
		if (document.getElementById('zkatterPanoCnt').style.display == 'block')
			{
			$('#zkatterPanoCnt').animate({'height':'300px'},'slow', function()
				{
				document.getElementById('zkatterPano').style.height= '300px'
				document.getElementById('zkatterPano').firstChild.height= 300
				})
			}
		else
			{
			document.getElementById('zkatterPano').style.height= '300px'
			document.getElementById('zkatterPanoCnt').style.height= '300px'
			}
		}
	else
		{
		document.getElementById('mapZoom').src= 'images/mapZoomOut.png'
		document.getElementById('zkatterMap').style.height= '600px'
		zMap.checkResize()
		zMap.panTo(new GLatLng(zLoc.lat.value,zLoc.lng.value))
		document.getElementById('zkatterPano').style.height= '600px'
		$('#mapArea').animate({'height':'602px'},'slow')
		$('#zkatterMapCnt').animate({'height':'600px'},'slow', function(){ $.scrollTo('#autoMsg','slow') })
		if (document.getElementById('zkatterPanoCnt').style.display == 'block')
			{
			document.getElementById('zkatterPano').firstChild.height= 600
			$('#zkatterPanoCnt').animate({'height':'600px'},'slow')
			}
		else document.getElementById('zkatterPanoCnt').style.height= '600px'
		}
	}

function resetMap()
	{
	document.getElementById('mapZoom').src= 'images/mapZoomIn.png'
	document.getElementById('zkatterMap').style.height= '300px'
	document.getElementById('zkatterPano').style.height= '300px'
	document.getElementById('zkatterMapCnt').style.height= '300px'
	document.getElementById('zkatterPanoCnt').style.height= '300px'
	document.getElementById('zkatterPanoCnt').style.display= 'none'
	}

function zkatter2mapOFF(zid)
	{
	simple_ajax('_ajax/?z=' + zid + '&zTab=z_map','zkatter2mapDone',0,[zid,my])
	}

function zkatter2map(zid,lat,lng)
	{
	bskn= 'red'
	$.scrollTo('#autoMsg','slow')
	expandMap(1)
	setMap(lat,lng,'Zkattering','Current location set by selected Zkattering.')
	window.searchBalloonActive= true
	window.zMapMrk.openExtInfoWindow(zMap,'custom_info_window_' + bskn,'',{beakOffset:3,ajaxUrl:'_ajax/searchballon.php?z=' + zid})
	GEvent.addListener(window.zMapMrk,'click',function()
		{
		if (zLoc.setby.value == 'Zkattering')
			{
			expandMap(1)
			window.zMapMrk.openExtInfoWindow(zMap,'custom_info_window_' + bskn,'',{beakOffset:3,ajaxUrl:'_ajax/searchballon.php?z=' + zid}) 
			}
		})
	GEvent.addDomListener(zMap,'extinfowindowclose', function(){zMap.panTo(new GLatLng(zLoc.lat.value,zLoc.lng.value))})
	GEvent.addDomListener(zMap,'extinfowindowupdate',function()
		{
		tabs= new Array(document.getElementById('tab0'),document.getElementById('tab1'))
		tabContentsArray= new Array(tabs.length)
		for (i=0; i<2; i++)
			{
			tabContentsArray[i]= document.getElementById('tab' + i + '_content')
			if (i > 0) hide(tabContentsArray[i])
			tabs[i].setAttribute('name', i.toString())
			if (i == 1) initStreetView(lat,lng)
			GEvent.addDomListener(tabs[i],'click',function()
				{
				tabIndex= this.getAttribute('name')
				for (tabContentIndex=0; tabContentIndex<2; tabContentIndex++)
					{
					if (tabContentIndex == tabIndex) show(tabContentsArray[tabContentIndex])
					else hide(tabContentsArray[tabContentIndex])
					}
				})
			}
		})
	}

function searchBalloonClose()
	{
	if (window.searchBalloonActive)
		{
		zMap.closeExtInfoWindow()
		zMap.panTo(new GLatLng(zLoc.lat.value,zLoc.lng.value))
		}
	}

function initStreetView(lat,lng)
	{
	document.getElementById('searchBalloonPano').innerHTML= ''
	searchBalloonPanoObj= new GStreetviewPanorama(document.getElementById('searchBalloonPano'))
	place= new GLatLng(lat,lng)
	myPOV= {yaw:370.64659986187695,pitch:-20}
	searchBalloonPanoObj.setLocationAndPOV(place, myPOV)
	GEvent.addListener(searchBalloonPanoObj,'error',function(){document.getElementById('tab1').style.display='none'})
	setTimeout("document.getElementById('tab1').style.visibility= 'visible'",1000)
	}

function dist(lat,lng)
	{
	glatlng1= new GLatLng(lat,lng)
	glatlng2= new GLatLng(zMap.getCenter().lat(),zMap.getCenter().lng())
	miledistance= glatlng1.distanceFrom(glatlng2,3959).toFixed(1)
	kmdistance= (miledistance*1.609344).toFixed(1);
	return kmdistance * 1000
	}