var RunInterval=30*60
var TrigInterval=60
var TrigOffset=7
var TrigA1=5
var TrigA2=30
var SyncNr=3
//----
var vechS=0.8
var flyS=.7
var rangeSt=2
var tiltSt=2
var rangeAr=new Array(1000,700,500,300,200,100)
var ratiltAr=new Array(3,3,4,4,5,5)
var tiltAr=new Array(0,45,60,70,80,85)
var vechSc=new Array(.5, .6, .7, .8, .9, 1.00)
var vechIm=new Array('vi5','vi4','vi3','vi2','vi1','vi0')
var B='&nbsp;'
var resync=SyncNr
var iconOld=-1
var rangeI=rangeSt
var tiltI=tiltSt
var showTimer=pageLoad=dataLoad=run=inh=track=newLoad=IE=false
var firstPass=maplock=loadEarth=true
var Lat=Lon=rptTime=lopTime=totTime=oldTime=xx=Itv=0
var Call=Dir=Vel=Alt=Msg=Tim=Loc=Srv=''
var modloc,modore,modsca,model,modelPlacemark,startView,modfet
var timerID,Ecall,Etim,Evel,Edir,Ealt,Elat,Elon,Emsg,Esrv
var vechOlay,vechDyn,icon
var browsersup=''//'Browsers Supported By Google Maps<BR><BR>'
//browsersup+='IE 6.0+<BR>Firefox 0.8+<BR>Safari 1.2.4+<BR>Netscape 7.1+<BR>Mozilla 1.4+<BR>Opera 8.02+'
pcp = new Array()
var trkPGM='http://k6ib.com/cgi-bin/aprs/trk3ddat'
var trkSRC='about:blank'
if(self!=top)top.location.replace(self.location.href)
function setF(n,s){
	var f='<IFRAME NAME="'+n+'" SRC="'+s+'" '
	f+='BORDER="0" HEIGHT="0" WIDTH="0" '
	f+='SCROLLING="NO" MARGINWIDTH="0" MARGINHEIGHT="0" '
	f+='NORESIZE="NORESIZE" HSPACE="0" VSPACE="0" '
	f+='FRAMEBORDER="0" FRAMESPACING="0"></IFRAME>'
	return f
}

function setNewSta(){
	trksta=trksta.toUpperCase()
	clearTimeout(timerID)
	dataLoad=false
//	ge.getWindow().setVisibility(false);
	document.getElementById('Econt').style.visibility='hidden'
	document.getElementById('map').style.visibility='hidden'
	document.getElementById('map2').style.visibility='hidden'
	setClear()
	Vcall.innerHTML=B
	var url=trkPGM+'?call='+escape(trksta)
	dat.document.location.replace(url)
	Vcall.innerHTML=trksta.toUpperCase()
	oldTime=0
	newLoad=true
	loadEarth=true
	rangeI=rangeSt
	tiltI=tiltSt
	run=true
	xx=0
	oldTime=0
	iconOld=-1
	setStat('ptd','CONNECTING','bdd','')
	Vrun.disabled=true
	Vopm.innerHTML='Loading Information For <BR>APRS Station<BR><BR>'+trksta+'<BR><BR><BR><BR>PLEASE WAIT....'
	resync=SyncNr
	totTime=0
	lopTime=100000
	ge.getOptions().setFlyToSpeed(2)
	ge.getView().setAbstractView(startView)
	ge.getOptions().setFlyToSpeed(flyS)
	timerID=setTimeout("startNew()",1000)
}
function startNew(){
	clearTimeout(timerID)
	if(dataLoad){
		inh=false
		ge.getWindow().setVisibility(true)
//		document.getElementById('SetWin').style.visibility='visible'
		document.getElementById('StaWin').style.visibility='visible'
		document.getElementById('trksta').innerHTML='Tracking Station - '+trksta.toUpperCase()
//==============================================
/*
	model = ge.createModel('')
	modloc = ge.createLocation('')
	modloc.setLatitude(parseFloat(Lat))
	modloc.setLongitude(parseFloat(Lon))
	modloc.setAltitude(0)
	model.setLocation(modloc)
	var link = ge.createLink('')
	model.setLink(link)
	link.setHref('http://k6ib.com/aprs/_img/scar.dae')
	vechPlace = ge.createPlacemark('')
	modore = ge.createOrientation('')
	modore.set(parseFloat(0)+(180),0,0)
	model.setOrientation(modore)
	modsca = ge.createScale('')
	model.setScale(modsca)
	rangeI=rangeSt
	tiltI=tiltSt
	vechScale()
	vechPlace.setGeometry(model)
	ge.getFeatures().appendChild(vechPlace)
*/
//===============================================
//	modore.set(fixAngle(parseFloat(Dir)-180),0,0)
	setViewInit()
//	modfet=createFeature('')
vechPlace.setVisibility(0)

/*
	var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND)
	la.setRange(rangeAr[rangeI])
	la.setTilt(tiltAr[tiltI])
	modloc.setLatitude(parseFloat(Lat))
	modloc.setLongitude(parseFloat(Lon))
	modore.setHeading(parseFloat(Dir)+(180))
	la.setHeading(parseFloat(Dir))	
	la.setLatitude(parseFloat(Lat))
	la.setLongitude(parseFloat(Lon))
	ge.getView().setAbstractView(la)
*/
//=================================================
		timerID=setTimeout("update()",3000)
	}else{
		timerID=setTimeout("startNew()",1000)
	}
}

function init(){
	google.earth.createInstance("map3d", apiLoad, apiFail)
}
function setScale(e){
	var sc=getRadio(e)
	if(sc>=0){
		sc=1+(sc*.7)
//		modsca.set(6*sc,7*sc,6*sc)
		modsca.set(6*sc,5*sc,6*sc)
	}
}

function vechScale(){
	var sf=rangeAr[rangeI]
	var sc=(sf/100)* vechSc[rangeI]
//	modsca.set(6*sc,7*sc,6*sc)
	modsca.set(6*sc,5*sc,6*sc)
}

function getRadio(ro){
	if(!ro)return -1
	var rl = ro.length
	if(rl==undefined)
		if(ro.checked)
			return ro.value
		else
			return -1
		for(vari=0;i<rl;i++){
			if(ro[i].checked)
				return ro[i].value
		}
	return -1
}

function setLock(c){
	if(c)maplock=true
	else maplock=false
	setView()
}

function setRange(c){
	if(c){
		if(rangeI<rangeAr.length-1){
			rangeI++;setView()
		}
	}
	else{
		if(rangeI>0){
			rangeI--;setView()
		}
	}
}
function setTilt(c){
	if(c){
		if(tiltI<tiltAr.length-1){
			tiltI++;setView()
		}
	}
	else{
		if(tiltI>0){
			tiltI--;setView()
		}
	}
}
function setView(){
	vechScale()
	if(ratiltAr[rangeI]<tiltI)
		tiltI=ratiltAr[rangeI]
	ge.getOptions().setFlyToSpeed(4.9)
	var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND)
	la.setRange(rangeAr[rangeI])
	la.setTilt(tiltAr[tiltI])
	modloc.setLatitude(parseFloat(Lat))
	modloc.setLongitude(parseFloat(Lon))
	if(maplock){
		document.getElementById('Bvech').style.background='#0A0'
		document.getElementById('Bnorth').style.background='#258'
		la.setHeading(parseFloat(Dir))	
	}else
	{
		document.getElementById('Bvech').style.background='#258'
		document.getElementById('Bnorth').style.background='#0A0'
		la.setHeading(0)
	}
	la.setLatitude(parseFloat(Lat))
	la.setLongitude(parseFloat(Lon))
	ge.getView().setAbstractView(la)
	ge.getOptions().setFlyToSpeed(flyS)
}

//===========================
function setViewInit(){
	vechScale()
	if(ratiltAr[rangeI]<tiltI)
		tiltI=ratiltAr[rangeI]
//	ge.getOptions().setFlyToSpeed(4.9)
	var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND)
//	la.setRange(rangeAr[rangeI-1])

	la.setRange(rangeAr[0])
	la.setTilt(tiltAr[tiltI])
	modloc.setLatitude(parseFloat(Lat))
	modloc.setLongitude(parseFloat(Lon))

	if(maplock){
		document.getElementById('Bvech').style.background='#0A0'
		document.getElementById('Bnorth').style.background='#258'
		la.setHeading(parseFloat(Dir))	
	}else
	{
		document.getElementById('Bvech').style.background='#258'
		document.getElementById('Bnorth').style.background='#0A0'
		la.setHeading(0)
	}
	la.setLatitude(parseFloat(Lat))
	la.setLongitude(parseFloat(Lon))
	ge.getView().setAbstractView(la)
//	ge.getOptions().setFlyToSpeed(flyS)
}
//============================================================


function fixAngle(a){
	while(a<-180){a+=360}
	while(a>180){a-=360}
	return a
}

function start(){
	pageLoad=true
	Vopm=document.getElementById("opm")
	Vcall=document.getElementById("call")
	Vtim=document.getElementById("tim")
	Vvel=document.getElementById("vel")
	Vdir=document.getElementById("dir")
	Valt=document.getElementById("alt")
	Vlat=document.getElementById("lat")
	Vlon=document.getElementById("lon")
	Vmsg=document.getElementById("msg")
	Vaux=document.getElementById("aux")
	Vloc=document.getElementById("loc")
	Vctl=document.getElementById("ctl")
	Vctb=document.getElementById("ctb")
	Vcps=document.getElementById("cps")
	Vrun=document.getElementById("run")
	Vsrv=document.getElementById("srv")
	if(!setup()){
		clearTimeout(timerID)	
		Vopm.innerHTML="Your Browser Is Not Compatible<BR>With The Google Map Program<BR><BR>"+browsersup
		setStat('wtd','BROWSER','bdd','')
		Vrun.disabled=true
		inh=true
	}	
	else{
		run=true
		timerID=setTimeout("update()",2000)
		load()
		setNewPoint(Lat,Lon,-1)
		setClear()
	}
}

function apiLoad(object){
	ge=object;
	ge.getWindow().setVisibility(true)
	ge.getOptions().setMouseNavigationEnabled(true)  
	ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, false)
	ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, false)
	startView = ge.createLookAt('')
	startView = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND)
	startView.setHeading(0)
	ge.getOptions().setFlyToSpeed(flyS); //0-5 100=instant
	ge.getOptions().setAtmosphereVisibility(1)


//==============================================
	model = ge.createModel('')
	modloc = ge.createLocation('')
	modloc.setLatitude(parseFloat(Lat))
	modloc.setLongitude(parseFloat(Lon))
	modloc.setAltitude(0)
	model.setLocation(modloc)
	var link = ge.createLink('')
	model.setLink(link)
	link.setHref('http://k6ib.com/aprs/_img/scar.dae')
	vechPlace = ge.createPlacemark('')
	modore = ge.createOrientation('')
	modore.set(parseFloat(0)+(180),0,0)
	model.setOrientation(modore)
	modsca = ge.createScale('')
	model.setScale(modsca)
	rangeI=rangeSt
	tiltI=tiltSt
	vechScale()
	vechPlace.setGeometry(model)
	ge.getFeatures().appendChild(vechPlace)
//===============================================


//	document.getElementById('SetWin').style.visibility='visible'
//	document.getElementById('StaWin').style.visibility='visible'
	setStat('ptd','READY','bdd','')
	setNewSta()
}

function apiFail(object){
	Vopm.innerHTML="Unable To Load Google Earth Plugin"
}

function update(){
	clearTimeout(timerID)
	timerID=setTimeout("update()",1000)
	rptTime++
	lopTime++
	totTime++
	if(totTime>RunInterval){
		clearTimeout(timerID)	
		setStat('wtd','TIMED OUT','brn','RESTART')
		run = false
		track=false
		dataLoad=false
		setClear()
	}
	if(xx){
		dataLoad=false
		if(xx == 1){
			resync--
			if(resync<0){
				clearTimeout(timerID)
				setClear()
				run=false
				track=false
				dataLoad=false
				setStat('wtd','NO CONNECT','brn','RESTART')
				setClear()
				Vopm.innerHTML="COULD NOT CONNECT TO<BR>APRS SERVER"
			}
			else{
				Vctb.className="ptd"	
				Vctl.innerHTML="RELINKING"
				lopTime=1000000
			}
		}
		if(xx == 2){
			clearTimeout(timerID)	
			track=false
			inh=true
			run=false
			setStat('wtd','CGI ERROR','bdd','')
			Vrun.disabled=true
			Vopm.innerHTML="CGI FORMAT ERROR<BR>COULD NOT PROCESS REQUEST"
		}
		if(xx == 3){
			clearTimeout(timerID)	
			track=false
			run=false
			track=false
			dataLoad=false
			setStat('wtd','NO CONNECT','brn','RESTART')
			Vopm.innerHTML="COULD NOT CONNECT TO<BR>APRS GATEWAY"
		}
		if(xx == 4){
			clearTimeout(timerID)	
			track=false
			Vopm.innerHTML="APRS DATA FOR STATION<BR><BR>"+Call+"<BR><BR>NOT FOUND"
			setStat('wtd','NOT FOUND','bdd','')
			Vrun.disabled=true
			inh=true
		}
	}
	if(dataLoad){
		dataLoad=false
		resync=SyncNr
		if(firstPass){
			firstPass=false	
			load()
		}
		if(newLoad){
			newLoad=false
			document.getElementById('Econt').style.visibility='visible'
			document.getElementById('map').style.visibility='visible'


//========================================================================================
//			document.getElementById('map2').style.visibility='visible'
		}
		setStat('qtd','TRACKING','bgn','RUNNING')
		Vrun.disabled=false
		track=true
		lopTime=Itv-(Math.floor(Itv/TrigInterval)*TrigInterval)
		rptTime=Itv
		if(Vsrv)Vsrv.innerHTML='SERVER: '+Srv
		if(oldTime != Tim){
			oldTime=Tim
			if(Vel == 0){
				Vvel.innerHTML='STOPPED'
				Vvel.className="ovtd"		
				Vdir.innerHTML='-&nbsp;-&nbsp;-'
				Vdir.className="ovtd"
			}else{
				Vvel.innerHTML=Vel+' MPH'
				Vvel.className="vtd"		
				Vdir.innerHTML=Dir+' Deg'
				Vdir.className="vtd"
			}
			Vcall.innerHTML=Call
			Vtim.innerHTML=cvTime(rptTime)
			Valt.innerHTML=Alt+' Feet'
			var LatF=Lat
			var LonF=Lon
			if(Lat>0)LatF+=' N'
			else LatF=Math.abs(Lat)+' S'
			if(Lon>0)LonF+=' E'
			else LonF=Math.abs(Lon)+' W'
			Vlat.innerHTML=LatF
			Vlon.innerHTML=LonF
			if(Vmsg)Vmsg.innerHTML=Msg.substr(0,70)
			setIcon()
			if(ge){
				var la=ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND)
				if((loadEarth)&&(ge)){
					loadEarth=false
					la.setRange(rangeAr[rangeI])
					la.setTilt(tiltAr[tiltI])
					vechScale()
				}
				var cra=la.getRange()
				if(cra>rangeAr[0]){
					rangeI=0		
					la.setRange(rangeAr[rangeI])
				}
				if(cra<rangeAr[rangeAr.length-1]){
					rangeI=rangeAr.length-1		
					la.setRange(rangeAr[rangeI])
				}
				la.setRange(rangeAr[rangeI])
				la.setTilt(tiltAr[tiltI])
				modloc.setLatitude(parseFloat(Lat))
				modloc.setLongitude(parseFloat(Lon))
				if(maplock){
					modore.set(fixAngle(parseFloat(Dir)-180),0,0)
					la.setHeading(parseFloat(Dir))	
				}else{
					modore.set(fixAngle(parseFloat(Dir)-180),0,0)
				}
				la.setLatitude(parseFloat(Lat))
				la.setLongitude(parseFloat(Lon))
				ge.getView().setAbstractView(la)

				vechPlace.setVisibility(1)

			}
			if(Vloc)Vloc.innerHTML=Loc
		}
	}
	if (lopTime>=TrigInterval+TrigOffset){
		dat.document.location.reload()
		lopTime=0
	}
	if((! firstPass)&&(track)){
		if(rptTime<(TrigInterval*TrigA1)+TrigOffset+10)
			Vtim.className="gtd"		
		else if(rptTime<TrigInterval*TrigA2+TrigOffset)
			Vtim.className="otd"		
		else
				Vtim.className="rtd"
		Vtim.innerHTML=cvTime(rptTime)
	}
}

function restart(){
	if(! inh){
		if(run){
			run=false
			track=false
			dataLoad=false
			clearTimeout(timerID)		
			setStat('wtd','HALTED','brn','RESTART')
			setClear()
		}else{
			run=true
			xx=0
			oldTime=0
			cpsOld=-1
			iconOld=-1
			setStat('ptd','CONNECTING','bgn','RUNNING')
			Vopm.innerHTML="CONNECTING...."
			resync=SyncNr
			totTime=0
			lopTime=100000
			timerID=setTimeout("update()",100)
		}
	}
}

function setStat(cc,cv,bc,bv){
	Vctb.className=cc	
	Vctl.innerHTML=cv	
	Vrun.className=bc
	Vrun.value=bv
}

function setClear(){
	Vtim.innerHTML=B
	Vvel.innerHTML=B
	Vdir.innerHTML=B
	Valt.innerHTML=B
	Vlat.innerHTML=B
	Vlon.innerHTML=B
	if(Vmsg)Vmsg.innerHTML=B
	if(Vloc)Vloc.innerHTML=B
}

function setIcon(){
	var i=eval(Dir)+11.25
	if(i>=360)i-=360
	i=Math.floor(i/22.5)
	if(i>15)i=0
	if(i != iconOld){
		iconOld=i
		setNewPoint(Lat,Lon,i)
	}
	else{
		setNewPoint(Lat,Lon,-1)
	}
}

function cvTime(t){
	var ret=''
	var dy=Math.floor(t / 86400)
	t=t-(dy*86400)
	var hr=Math.floor(t/3600)
	t=t-(hr*3600)
	var mn=Math.floor(t/60)
	var se=t-(mn*60)
	if(dy!=0)ret=dy+"- "
	if((hr!=0)||(ret!='')){
		if(hr<10)hr="0"+hr
		ret+=hr+":"
	}
	if(mn<10)mn="0"+mn
	if(se<10)se="0"+se		
	ret+=mn+":"+se
	return ret
}	

function noEnter(){return !(window.event && window.event.keyCode == 13)}
function exitPgm(){
	document.getElementById('Econt').style.visibility='hidden'
	document.getElementById('map').style.visibility='hidden'
	document.getElementById('map2').style.visibility='hidden'
	document.getElementById('infowin').style.visibility='hidden'
	
	ge.getOptions().setFlyToSpeed(100)
	ge.getView().setAbstractView(startView)
	GUnload()
}
google.load("earth", "1")
var ge = null;

