model
{
#### first difference model re-parameterized from earlier bbsBayes versions
#### changed to better match the priors and parameterizations in Link and Sauer 2020
#### also corrects an error in the previous versions where n2 was calculated incorrectly (missing the intercept term)
	for( k in 1 : ncounts )
	{
		Eloglambda[k] <- obs[strat[k],obser[k]] + eta*firstyr[k] + yeareffect[strat[k],year[k]]
	 	loglambda[k] ~ dnorm(Eloglambda[k], taunoise)
	 	log(lambda[k]) <- loglambda[k]
		count[k] ~ dpois(lambda[k])

	}




	taunoise ~ dgamma(0.001,0.001)
	sdnoise <- 1 / pow(taunoise, 0.5)
	tauobs ~ dgamma(0.001,0.001)
	sdobs <- 1 / pow(tauobs, 0.5)
	eta ~ dnorm( 0.0,0.01)
	STRATA ~ dnorm( 0.0,0.01)
	taustrata ~ dgamma(0.001,0.0001)
	sdstrata <- 1/pow(taustrata,0.5)



	#---------------------------------------------------------#



	#----------------------------------#
	#### stratum effects  ######
	for( i in 1 : nstrata )
	{
		#### observer effects  ######
		for( o in 1 : nobservers[i] )
		{
			obs[i,o] ~ dnorm( 0.0,tauobs)
		}


		#### end observer effects  ######


		### stratum-level priors

		strata[i] ~ dnorm(STRATA,taustrata)

		#### stratum specific year effects  ######

		yeareffect[i,fixedyear] <- strata[i]

		for( t in ymin : (fixedyear-1) )
		{
			yeareffect[i,t] ~ dnorm(yeareffect[i,t+1],tauyear[i])
		}
				for( t in (fixedyear+1) :  ymax)
		{
			yeareffect[i,t] ~ dnorm(yeareffect[i,t-1],tauyear[i])
		}


		tauyear[i] ~ dgamma(0.001,0.001)




		#-------------------------------------------------#
	}



	#### summary statistics  ######
	sdn_ret <- 0.5*sdnoise*sdnoise
	sdobs_ret <- 0.5*sdobs*sdobs

	for( i in 1 : nstrata )
	{
		for( t in ymin : ymax )
		{
			for(o in 1 : nobservers[i])
			{
				no[i,t,o] <- exp(yeareffect[i,t] + obs[i,o] + sdn_ret)
			}

			mn[i,t] <- mean(no[i,t,1 : nobservers[i]])
			n[i,t] <- nonzeroweight[i]*(mn[i,t])
				n2[i,t] <- nonzeroweight[i]*exp(yeareffect[i,t] + sdobs_ret + sdn_ret) #n2 is an alternative approach to calculating n
		}
	}

	#-------------------------------------------------#
}
