<<echo=FALSE,results='hide', message=FALSE>>=
suppressPackageStartupMessages(library(RgoogleMaps))
suppressPackageStartupMessages(library(adegenet))
suppressPackageStartupMessages(library(ade4))
suppressPackageStartupMessages(library(pegas))
suppressPackageStartupMessages(library(genetics))
suppressPackageStartupMessages(library(gap))
suppressPackageStartupMessages(library(lattice))
suppressPackageStartupMessages(library(calibrate))
suppressPackageStartupMessages(library(xtable))
suppressPackageStartupMessages(library(plyr))
suppressPackageStartupMessages(library(mmod))
suppressPackageStartupMessages(library(dismo))
suppressPackageStartupMessages(library(R.utils))
suppressPackageStartupMessages(library(knitr))
suppressPackageStartupMessages(library(reshape))
suppressPackageStartupMessages(library(gdistance))
suppressPackageStartupMessages(library(vegan))
suppressPackageStartupMessages(library(sp))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(raster))
suppressPackageStartupMessages(library(GGally))
suppressPackageStartupMessages(library(hierfstat))
suppressPackageStartupMessages(library(data.table))
options(width = 80)

#global functions and parameters....
brtab <- function(mat,capt,rown, width=9)
{

n.entry<- dim(t(mat))[2]
tab <- t(mat)
pta <- matrix(NA,ncol=min(width,n.entry), nrow=2*ceiling(n.entry/width))
for (i in 1:n.entry)
{
pta[2*(floor((i-1)/width)+1),((i-1) %% width)+1] <- tab[i]
pta[2*(floor((i-1)/width)+1)-1,((i-1) %% width)+1] <- colnames(tab)[i]
}


pta <- cbind(rep(rown,ceiling(n.entry/width)),pta)


print(xtable(pta,  cap=capt), add.to.row=list(list(seq(1,2*ceiling(n.entry/width),2)),"\\rowcolor[gray]{0.9} "), include.rownames=FALSE, include.colnames=FALSE, hline.after=c(0,nrow(pta)))
}

#long table break
breaktab <- function(tab, cap_first, cap_cont)
{
num_tables<-ceiling(dim(tab)[2]/8)
for(i in 1:num_tables){
  if(i==1) {
    tempcap<-cap_first
  } else if (i>1) {
    tempcap<-cap_cont
  }
  firstcol<-1+(8*(i-1))
  lastcol<-i*8
  if(lastcol>dim(tab)[2]) lastcol<-dim(tab)[2]
  tempdata2<-as.matrix(tab[,firstcol:lastcol])
  if((lastcol-firstcol)==0) colnames(tempdata2)<-colnames(tab)[dim(tab)[2]]
  tab2<-xtable(tempdata2,cap=tempcap, digits=3)
  print(tab2,include.rownames=TRUE, ,add.to.row=list(list(seq(1,nrow(tab2),2)),"\\rowcolor[gray]{0.9} ")) 
}
}


#initial calculations
sumcats<-summary(cats)
samplocnum<-sumcats$n.by.pop
filename<-cats@other$filename
allresults<-list()


#figure parameters....
fig.path <- paste(cats@other$filename,"-",sep="")
dev <- c('pdf', 'png','svg')
dpi <- 300
@
