// -------------------------------------------------------------------
// Virtual Pagination Script- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Last updated: May 31st, 2007
//
// PUBLIC: virtualpaginate(className, chunksize)
// Main Virtual Paginate Object function.
// -------------------------------------------------------------------

function virtualpaginate(className, chunksize, elementType, id, sArray){
this.elementId = id 
this.pieces = 0
this.cpspan = [] 
this.paginatediv = [] 
var elementType=(typeof elementType=="undefined")? "div" : elementType //The type of element used to divide up content into pieces. Defaults to "div"
this.pieces = sArray ;
//alert("number of marker boxes:"+this.pieces.length);
//this.pieces=virtualpaginate.collectElementbyClass2(className, elementType,sArray) //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 : (parseInt(chunksize)>0 && parseInt(chunksize) <this.pieces.length)? chunksize : this.pieces.length
this.pagecount=Math.ceil(this.pieces.length/this.chunksize) //calculate number of "pages" needed to show the divs
//alert("html element id:"+this.elementId+" pieces:"+this.pieces+" chunkSize:"+this.chunkSize+" pagecount:"+this.pagecount )
this.showpage(-1) //show no pages (aka hide all)
this.currentpage=0 //Having hidden all pages, set currently visible page to 1st page
this.showpage(this.currentpage) //Show first page
}

// -------------------------------------------------------------------
// PRIVATE: collectElementbyClass(classname)- Returns an array containing DIVs with the specified classname
// -------------------------------------------------------------------
/*
virtualpaginate.collectElementbyClass2=function(classname, element,alltags){ //Returns an array containing DIVs with specified classname
var pieces=[]
//alert("Given Array length:"+alltags.length)
for (var i=0; i<alltags.length; i++){
pieces[pieces.length]=alltags[i]
}
return pieces
}
*/

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: showpage(pagenumber)- Shows a page based on parameter passed (0=page1, 1=page2 etc)
// -------------------------------------------------------------------

virtualpaginate.prototype.showpage=function(pagenumber){
if(this.elementId =="undefined") alert("Error: couldn't find the id to inject the marker boxes!!")
var totalitems=this.pieces.length //total number of broken up divs
var showstartindex=pagenumber*this.chunksize //array index of div to start showing per pagenumber setting
var showendindex=showstartindex+this.chunksize-1 //array index of div to stop showing after per pagenumber setting
document.getElementById(this.elementId).innerHTML = "";

for (var i=0; i<totalitems; i++){
if (i>=showstartindex && i<=showendindex)
document.getElementById(this.elementId).innerHTML += this.pieces[i]
}

var anchors = null;
// invisible LAST and NEXT buttons
if(this.currentpage == this.pagecount-1) {

  for(var i=0;i<this.paginatediv.length;i++){
     anchors = this.paginatediv[i].getElementsByTagName("a");
     if(!anchors.length >0 ) alert("Error in pagination script");
     for(var j=0; j< anchors.length;j++)
     {
        if(anchors[j].className == "nav_last")
           anchors[j].style.visibility     = "hidden";
        if(anchors[j].className == "nav_next")
           anchors[j].style.visibility     = "hidden";
        if(anchors[j].className == "nav_first")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_previous")
           anchors[j].style.visibility     = "visible";
     }
  }
}
// invisible PREVIOUS and FIRST buttons
else if(this.currentpage == 0) {

  for(var i=0;i<this.paginatediv.length;i++){
     anchors = this.paginatediv[i].getElementsByTagName("a");
     if(!anchors.length >0 ) alert("Error in pagination script");
     for(var j=0; j< anchors.length;j++)
     {
        if(anchors[j].className == "nav_last")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_next")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_first")
           anchors[j].style.visibility     = "hidden";
        if(anchors[j].className == "nav_previous")
           anchors[j].style.visibility     = "hidden";
     }
  }
}
else {
  for(var i=0;i<this.paginatediv.length;i++){
     anchors = this.paginatediv[i].getElementsByTagName("a");
     if(!anchors.length >0 ) alert("Error in pagination script");
     for(var j=0; j< anchors.length;j++)
     {
        if(anchors[j].className == "nav_last")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_next")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_first")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_previous")
           anchors[j].style.visibility     = "visible";
     }
  }

}
      var maxLength = 30;
      $(".sidebar_box_title").each(function(i){
         var titleText = $(this).text();
	 if(titleText.length>=maxLength){
	    // find next space after the max length
  	    var index = titleText.indexOf(" ",maxLength);
	    // replace the existing length
	    if(index!=-1){
                  $(this).text(titleText.substring(0,index)+"...");
            }
            else{
               index = titleText.search(/\S*[-+]/);
               if(index>4)
                $(this).text(titleText.substring(0,index)+"..");
            }
	 }
      });
      maxLength = 25;
      $(".eventLoc").each(function(i){
         var titleText = $(this).text();
	 if(titleText.length>=maxLength){
	    // find next space after the max length
  	    var index = titleText.indexOf(" ",maxLength);
	    // replace the existing length
	    if(index!=-1){
                  $(this).text(titleText.substring(0,index)+"...");
            }
            else{
               //index = titleText.search(/\S^\w/); titleText.search(/\S*[-+]/);
               index = titleText.search(/\S*[-+]/);
               //$(this).text(titleText.substring(0,index)+"..	");
            }
	 }
      });


this.currentpage=parseInt(pagenumber)
if (this.cpspan.length>0) //if <span class="paginateinfo> element is present, update it with the most current info (ie: Page 3/4)
for(var i=0;i<this.cpspan.length;i++)
//this.cpspan[i].innerHTML='Page '+(this.currentpage+1)+'/'+this.pagecount
this.cpspan[i].innerHTML='Seite '+(this.currentpage+1)+'  von '+this.pagecount
}


/*
virtualpaginate.prototype.showpage1=function(pagenumber){
var totalitems=this.pieces.length //total number of broken up divs
var showstartindex=pagenumber*this.chunksize //array index of div to start showing per pagenumber setting
var showendindex=showstartindex+this.chunksize-1 //array index of div to stop showing after per pagenumber 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.currentpage=parseInt(pagenumber)
if (this.cpspan) //if <span class="paginateinfo> element is present, update it with the most current info (ie: Page 3/4)
this.cpspan.innerHTML='Page '+(this.currentpage+1)+'/'+this.pagecount
}
// -------------------------------------------------------------------
// 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 page info (ie: Page 1/4)
// -------------------------------------------------------------------

virtualpaginate.prototype.paginate_build_selectmenu=function(paginatedropdown, anchortext){
var instanceOfBox=this
var anchortext=anchortext || new Array()
this.selectmenupresent=1
for (var i=0; i<this.pagecount; i++){
if (typeof anchortext[i]!="undefined") //if custom anchor text for this link exists, use anchor text as each OPTION's text
paginatedropdown.options[i]=new Option(anchortext[i], i)
else //else, use auto incremented, sequential numbers
paginatedropdown.options[i]=new Option("Page "+(i+1)+" of "+this.pagecount, i)
}
paginatedropdown.selectedIndex=this.currentpage
paginatedropdown.onchange=function(){
instanceOfBox.showpage(this.selectedIndex)
}
}
*/


virtualpaginate.prototype.paginate_build_regularlinks=function(paginatelinks){
var instanceOfBox=this
for (var i=0; i<paginatelinks.length; i++){
var currentpagerel=paginatelinks[i].getAttribute("rel")
if (currentpagerel=="previous" || currentpagerel=="next" || currentpagerel=="first" || currentpagerel=="last") //screen for these "rel" values
paginatelinks[i].onclick=function(){
instanceOfBox.navigate(this.getAttribute("rel"))
return false
}
}
}

/*
virtualpaginate.prototype.paginate_build_flatview=function(flatviewcontainer, anchortext){
var instanceOfBox=this
var flatviewhtml=""
var anchortext=anchortext || new Array()
for (var i=0; i<this.pagecount; i++){
if (typeof anchortext[i]!="undefined") //if custom anchor text for this link exists
flatviewhtml+='<a href="#flatview" rel="'+i+'">'+anchortext[i]+'</a> ' //build pagination link using custom anchor text
else
flatviewhtml+='<a href="#flatview" rel="'+i+'">'+(i+1)+'</a> ' //build  pagination link using auto incremented sequential number instead
}
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.currentpage].className="" //"Unhighlight" last flatview link clicked on...
this.className="selected" //while "highlighting" currently clicked on flatview link (setting its class name to "selected"
instanceOfBox.showpage(this.getAttribute("rel"))
return false
}
}
this.flatviewlinks[this.currentpage].className="selected" //"Highlight" current page
this.flatviewpresent=true //indicate flat view links are present
}
*/


virtualpaginate.prototype.paginate_build_cpinfo=function(cpspan){
cpspan.innerHTML='Seite '+(this.currentpage+1)+'  von '+this.pagecount
this.cpspan[this.cpspan.length]=cpspan
}


function appendRule(elementType,myclass) {
   var classArray = [],s=""
   var classnameRE=new RegExp("(^|\\s+)"+myclass+"($|\\s+)", "i") //regular expression to screen for classname within element

   var allElems = document.getElementsByTagName(elementType);
   //var allElems = document.getElementsByTagName('*');
   for (var i = 0; i < allElems.length; i++) { 
	var thisElem = allElems[i]; 
//        if(thisElem.className)
//           s+="Element:"+thisElem+" Class:"+thisElem.className+"\n"
	if (thisElem.className.search(classnameRE)!=-1)
     	   classArray[classArray.length]=thisElem; 
   }
   //alert("Searching for:\nELEMENT:"+elementType+" CLASS:"+myclass+"\n"+s)
   return classArray
}

// -------------------------------------------------------------------
// PRIVATE: buildpagination()- Create pagination interface by calling one or more of the paginate_build_() functions
// -------------------------------------------------------------------

virtualpaginate.prototype.buildpagination=function(divId){

var instanceOfBox=this
var allspans=null
var paginatediv = []

paginatediv = appendRule('div',divId)
// select all the elements with given classname
//paginatediv = $("."+divId);

//if user has set to display all pieces at once, no point in creating pagination div
if (this.chunksize==this.pieces.length){ 
   for(var i=0;i<paginatediv.length;i++)
      paginatediv[i].style.display="none"
//paginatediv.style.display="none"
   return
}

for(var i=0;i<paginatediv.length;i++)
   if(paginatediv[i].style.display=="none")
      paginatediv[i].style.display="block"

/*
$("."+divId).each(function(){
    if( $(this).css("display") == "none" )
          $(this).css("display","block");

    $(this).find("a").click(function(){
       this.navigate($(this).getAttribute("rel"))
       return false
       
    });

    $(this span["className==paginateinfo"]).each(function(){

           this.paginate_build_cpinfo(allspans[j]);
       
    });
     

});



this.paginatediv=paginatediv


*/


for(var i=0;i<paginatediv.length;i++)
   if (paginatediv[i].getElementsByTagName("a").length>0) //if there are links defined in div
      this.paginate_build_regularlinks(paginatediv[i].getElementsByTagName("a"))

//Look for span tags within passed div

var classnameRE=new RegExp("(^|\\s+)paginateinfo($|\\s+)", "i") //regular expression to screen for classname within element
for(var i=0;i<paginatediv.length;i++){
   allspans=paginatediv[i].getElementsByTagName("span") 

// allspans=paginatediv.getElementsByTagName("span") 
   for (var j=0; j<allspans.length; j++)
      if (allspans[j].className.search(classnameRE)!=-1)
         this.paginate_build_cpinfo(allspans[j]);
 }

this.paginatediv=paginatediv
var anchors = null;
// invisible LAST and NEXT buttons
if(this.currentpage == this.pagecount-1) {

  for(var i=0;i<this.paginatediv.length;i++){
     anchors = this.paginatediv[i].getElementsByTagName("a");
     if(!anchors.length >0 ) alert("Error in pagination script");
     for(var j=0; j< anchors.length;j++)
     {
        if(anchors[j].className == "nav_last")
           anchors[j].style.visibility     = "hidden";
        if(anchors[j].className == "nav_next")
           anchors[j].style.visibility     = "hidden";
        if(anchors[j].className == "nav_first")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_previous")
           anchors[j].style.visibility     = "visible";
     }
  }
}
// invisible PREVIOUS and FIRST buttons
else if(this.currentpage == 0) {

  for(var i=0;i<this.paginatediv.length;i++){
     anchors = this.paginatediv[i].getElementsByTagName("a");
     if(!anchors.length >0 ) alert("Error in pagination script");
     for(var j=0; j< anchors.length;j++)
     {
        if(anchors[j].className == "nav_last")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_next")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_first")
           anchors[j].style.visibility     = "hidden";
        if(anchors[j].className == "nav_previous")
           anchors[j].style.visibility     = "hidden";
     }
  }
}
else {
  for(var i=0;i<this.paginatediv.length;i++){
     anchors = this.paginatediv[i].getElementsByTagName("a");
     if(!anchors.length >0 ) alert("Error in pagination script");
     for(var j=0; j< anchors.length;j++)
     {
        if(anchors[j].className == "nav_last")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_next")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_first")
           anchors[j].style.visibility     = "visible";
        if(anchors[j].className == "nav_previous")
           anchors[j].style.visibility     = "visible";
     }
  }

}


}

// -------------------------------------------------------------------
// PRIVATE: navigate(keyword)- Calls this.showpage() with the currentpage property preset based on entered keyword
// -------------------------------------------------------------------

virtualpaginate.prototype.navigate=function(keyword){

if (keyword=="previous")
   //this.currentpage=(this.currentpage>0)? this.currentpage-1 : (this.currentpage==0)? this.pagecount-1 : 0
      this.currentpage=(this.currentpage>0)? this.currentpage-1 :  0
else if (keyword=="next")
//this.currentpage=(this.currentpage<this.pagecount-1)? this.currentpage+1 : 0
//when last page is reached do not jump to first page
   this.currentpage=(this.currentpage<this.pagecount-1) && this.currentpage>-1 ? this.currentpage+1 : this.currentpage;
else if (keyword=="first")
   this.currentpage=0
else if (keyword=="last")
   //this.currentpage=this.pieces.length-1
   this.currentpage=this.pagecount-1

this.showpage(this.currentpage)

}

virtualpaginate.navButtonStatus = function(){





}