// -------------------------------------------------------------------
// Virtual Pagination Script- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Last updated: Dec 11th, 2006
//
// PUBLIC: virtualpaginate(className, chunksize)
// Main Virtual Paginate Object function.
// -------------------------------------------------------------------

function virtualpaginate(className, chunksize, elementType){
var elementType=(typeof elementType=="undefined")? "div" : elementType //The type of element used to divide up content into pieces. Defaults to "div"
this.pieces=virtualpaginate.collectElementbyClass(className, elementType) //get total number of divs matching class name
//Set this.chunksize: 1 if "chunksize" param is undefined, "chunksize" if it's less than total pieces available, or simply total pieces avail (show all)
this.chunksize=(typeof chunksize=="undefined")? 1 : (chunksize>0 && chunksize <this.pieces.length)? chunksize : this.pieces.length
this.Lapcount=Math.ceil(this.pieces.length/this.chunksize) //calculate number of "Laps" needed to show the divs
this.showLap(-1) //show no Laps (aka hide all)
this.currentLap=0 //Having hidden all Laps, set currently visible Lap to 1st Lap
this.showLap(this.currentLap) //Show first Lap
}

// -------------------------------------------------------------------
// PRIVATE: collectElementbyClass(classname)- Returns an array containing DIVs with the specified classname
// -------------------------------------------------------------------

virtualpaginate.collectElementbyClass=function(classname, element){ //Returns an array containing DIVs with specified classname
var classnameRE=new RegExp("(^|\\s+)"+classname+"($|\\s+)", "i") //regular expression to screen for classname within element
var pieces=[]
var alltags=document.getElementsByTagName(element)
for (var i=0; i<alltags.length; i++){
if (typeof alltags[i].className=="string" && alltags[i].className.search(classnameRE)!=-1)
pieces[pieces.length]=alltags[i]
}
return pieces
}

// -------------------------------------------------------------------
// PUBLIC: showLap(Lapnumber)- Shows a Lap based on parameter passed (0=Lap1, 1=Lap2 etc)
// -------------------------------------------------------------------

virtualpaginate.prototype.showLap=function(Lapnumber){
var totalitems=this.pieces.length //total number of broken up divs
var showstartindex=Lapnumber*this.chunksize //array index of div to start showing per Lapnumber setting
var showendindex=showstartindex+this.chunksize-1 //array index of div to stop showing after per Lapnumber setting
for (var i=0; i<totalitems; i++){
if (i>=showstartindex && i<=showendindex)
this.pieces[i].style.display="block"
else
this.pieces[i].style.display="none"
}
this.currentLap=parseInt(Lapnumber)
if (this.cpspan) //if <span class="paginateinfo> element is present, update it with the most current info (ie: Lap 3/4)
this.cpspan.innerHTML='Lap '+(this.currentLap+1)+'/'+this.Lapcount
}

// -------------------------------------------------------------------
// PRIVATE: paginate_build_() methods- Various methods to create pagination interfaces
// paginate_build_selectmenu(paginatedropdown)- Accepts an empty SELECT element and turns it into pagination menu
// paginate_build_regularlinks(paginatelinks)- Accepts a collection of links and screens out/ creates pagination out of ones with specific "rel" attr
// paginate_build_flatview(flatviewcontainer)- Accepts <span class="flatview"> element and replaces it with sequential pagination links
// paginate_build_cpinfo(cpspan)- Accepts <span class="paginateinfo"> element and displays current Lap info (ie: Lap 1/4)
// -------------------------------------------------------------------

virtualpaginate.prototype.paginate_build_selectmenu=function(paginatedropdown){
var instanceOfBox=this
this.selectmenupresent=1
for (var i=0; i<this.Lapcount; i++)
paginatedropdown.options[i]=new Option("Lap "+(i+1)+" of "+this.Lapcount, i)
paginatedropdown.selectedIndex=this.currentLap
paginatedropdown.onchange=function(){
instanceOfBox.showLap(this.selectedIndex)
}
}

virtualpaginate.prototype.paginate_build_regularlinks=function(paginatelinks){
var instanceOfBox=this
for (var i=0; i<paginatelinks.length; i++){
var currentLaprel=paginatelinks[i].getAttribute("rel")
if (currentLaprel=="previous" || currentLaprel=="next" || currentLaprel=="first" || currentLaprel=="last") //screen for these "rel" values
paginatelinks[i].onclick=function(){
instanceOfBox.navigate(this.getAttribute("rel"))
return false
}
}
}

virtualpaginate.prototype.paginate_build_flatview=function(flatviewcontainer){
var instanceOfBox=this
var flatviewhtml=""
for (var i=0; i<this.Lapcount; i++)
flatviewhtml+='<a href="#flatview" rel="'+i+'">'+(i+1)+'</a> ' //build sequential pagination links
flatviewcontainer.innerHTML=flatviewhtml
this.flatviewlinks=flatviewcontainer.getElementsByTagName("a")
for (var i=0; i<this.flatviewlinks.length; i++){
this.flatviewlinks[i].onclick=function(){
instanceOfBox.flatviewlinks[instanceOfBox.currentLap].className="" //"Unhighlight" last flatview link clicked on...
this.className="selected" //while "highlighting" currently clicked on flatview link (setting its class name to "selected"
instanceOfBox.showLap(this.getAttribute("rel"))
return false
}
}
this.flatviewlinks[this.currentLap].className="selected" //"Highlight" current Lap
this.flatviewpresent=true //indicate flat view links are present
}

virtualpaginate.prototype.paginate_build_cpinfo=function(cpspan){
this.cpspan=cpspan
cpspan.innerHTML='Lap '+(this.currentLap+1)+'/'+this.Lapcount
}


// -------------------------------------------------------------------
// PRIVATE: buildpagination()- Create pagination interface by calling one or more of the paginate_build_() functions
// -------------------------------------------------------------------

virtualpaginate.prototype.buildpagination=function(divid){
var instanceOfBox=this
var paginatediv=document.getElementById(divid)
if (this.chunksize==this.pieces.length){ //if user has set to display all pieces at once, no point in creating pagination div
paginatediv.style.display="none"
return
}
var paginationcode=paginatediv.innerHTML //Get user defined, "unprocessed" HTML within paginate div
if (paginatediv.getElementsByTagName("select").length>0) //if there's a select menu in div
this.paginate_build_selectmenu(paginatediv.getElementsByTagName("select")[0])
if (paginatediv.getElementsByTagName("a").length>0) //if there are links defined in div
this.paginate_build_regularlinks(paginatediv.getElementsByTagName("a"))
var allspans=paginatediv.getElementsByTagName("span") //Look for span tags within passed div
for (var i=0; i<allspans.length; i++){
if (allspans[i].className=="flatview")
this.paginate_build_flatview(allspans[i])
else if (allspans[i].className=="paginateinfo")
this.paginate_build_cpinfo(allspans[i])
}
this.paginatediv=paginatediv
}

// -------------------------------------------------------------------
// PRIVATE: navigate(keyword)- Calls this.showLap() with the currentLap property preset based on entered keyword
// -------------------------------------------------------------------

virtualpaginate.prototype.navigate=function(keyword){
if (this.flatviewpresent)
this.flatviewlinks[this.currentLap].className="" //"Unhighlight" previous Lap (before this.currentLap increments)
if (keyword=="previous")
this.currentLap=(this.currentLap>0)? this.currentLap-1 : (this.currentLap==0)? this.Lapcount-1 : 0
else if (keyword=="next")
this.currentLap=(this.currentLap<this.Lapcount-1)? this.currentLap+1 : 0
else if (keyword=="first")
this.currentLap=0
else if (keyword=="last")
this.currentLap=this.pieces.length-1
this.showLap(this.currentLap)
if (this.selectmenupresent)
this.paginatediv.getElementsByTagName("select")[0].selectedIndex=this.currentLap
if (this.flatviewpresent)
this.flatviewlinks[this.currentLap].className="selected" //"Highlight" current Lap
}

