model
{
	###### Link-Sauer 2011 model

#### counts and overdispersion effects

	for( k in 1 : ncounts )
	{
		Eloglambda[k] <- strata[strat[k]] + beta[strat[k]] * (year[k] - fixedyear) + 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)
	mulogtauyeareffect ~ dnorm(0.0,0.5)
	taulogtauyeareffect ~ dgamma(2,0.2)
	BETA ~ dnorm(0,0.01)
  taubeta ~ dgamma(1,0.0001) #
	### informative taubeta, parameterized so that:
	### mean of taubeta = ~10000
	### the mean of sdbeta =~ 0.02
	### there is <5% prob of sdbeta > 0.04 (i.e., there is a small, but non-zero probability that the SD of the stratum beta
	### terms is large enough to to encompass trends that vary from -8%/year to 8%/year with a continental trend = 0)
	### this informative prior should only be useful when there are few strata

	sdbeta <- 1/pow(taubeta,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  ######

		beta[i] ~ dnorm(BETA,taubeta)
		strata[i] ~ dnorm(STRATA,taustrata)


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



	for( t in ymin : ymax )
		{
			yeareffect[i,t] ~ dnorm(0,tauyeareffect[i])
		}
		log(tauyeareffect[i]) <- logtauyeareffect[i]
			logtauyeareffect[i] ~ dnorm(mulogtauyeareffect,taulogtauyeareffect)
		sdyeareffect[i] <- 1/pow(tauyeareffect[i],0.5)




	}# end s strata loop



	#### hierarchical trend effects  ######





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

	for( i in 1 : nstrata )
	{
		 sdyeareffect_ret[i] <- 0.5*sdyeareffect[i]*sdyeareffect[i]

		for( t in ymin : ymax )
		{
			for(o in 1 : nobservers[i])
			{
				no[i,t,o] <- exp(strata[i] + beta[i]*(t-fixedyear) + yeareffect[i,t] + obs[i,o]+sdn_ret)
				noslope[i,t,o] <- exp(strata[i] + beta[i]*(t-fixedyear) + sdyeareffect_ret[i] + obs[i,o]+sdn_ret)

			}

			mn[i,t] <- mean(no[i,t,1 : nobservers[i]])
			mnslope[i,t] <- mean(noslope[i,t,1 : nobservers[i]])
			n[i,t] <- nonzeroweight[i]*(mn[i,t])
			nslope[i,t] <- nonzeroweight[i]*(mnslope[i,t])

			n2[i,t] <- nonzeroweight[i]*exp(strata[i] + beta[i]*(t-fixedyear) + yeareffect[i,t] + sdn_ret + sdobs_ret)
			nslope2[i,t] <- nonzeroweight[i]*exp(strata[i] + beta[i]*(t-fixedyear) + sdyeareffect_ret[i] + sdn_ret + sdobs_ret)
		}
	}

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