var spPaginatorClassesLoaded = Array();

function spPaginatorHandleEvent(spElement)
  {
    spClassName = spElement.parentNode.parentNode.parentNode.id;
    spLoadedClass = false;
    // check id class is already loaded
    for (var key in spPaginatorClassesLoaded)
      {
        if (key == spClassName)
          {
            spLoadedClass = key;
            break;
          }
      }
    // if class not loaded create new instance
    if (!spLoadedClass)
      {
        spPaginatorClassesLoaded[spClassName] = new spPaginator(spClassName,1);
        spLoadedClass = spClassName;
      }
    // handle click event
    if (spElement.className == "spNext")
       spPaginatorClassesLoaded[spLoadedClass].showNext(spElement);
    else
       spPaginatorClassesLoaded[spLoadedClass].showPrev(spElement);
  }  // END FUNCTION







function spPaginatorHandleEventIndex(spElement, myindex)
  {
    spClassName = spElement.parentNode.parentNode.parentNode.id;
    spLoadedClass = false;
    // check id class is already loaded
    for (var key in spPaginatorClassesLoaded)
      {
        if (key == spClassName)
          {
            spLoadedClass = key;
            break;
          }
      }
    // if class not loaded create new instance
    if (!spLoadedClass)
      {
        spPaginatorClassesLoaded[spClassName] = new spPaginator(spClassName,1);
        spLoadedClass = spClassName;
      }
    // handle click event
    if (spElement.className == "spNext")
      {
        spPaginatorClassesLoaded[spLoadedClass].showIndex(spElement, myindex);
      }
  }  // END FUNCTION




var spPaginator = function(paginator_id, index)
  {
// get container
    var container = document.getElementById(paginator_id);
// get pages in the current paginator
    var pagesTmp = container.getElementsByTagName('DIV');
    this.pages = [];
// get pages and hide them all
    for (key=0;key<pagesTmp.length;key++)
      {
        if (pagesTmp[key].className == 'spPage')
          {
            var page = pagesTmp[key];
            this.pages.push(page);
            page.style.display = 'none';
          }  // END IF
      }  // END FOR
    this.next = true;
    this.prev = false;
// make sure default tab-id is valid
    this.index = index;
    this.index--;
    if ((this.index < 0) || (this.index >= this.pages.length))
    this.index = 0;
// show default tab
    this.currentPage = this.pages[this.index];
    this.currentPage.style.display = 'block';
  }  // END FUNCTION





spPaginator.prototype = {
showIndex: function(element, myindex)
  {
// remember old index and calculate the new one
    if (this.index <= this.pages.length - 1)
      {
        var oldIndex = myindex;
        this.index = myindex;
        if (this.index >= this.pages.length)  {  this.index = 0;  }
// get new page
        var newPage = this.pages[this.index];
// hide old page
        this.currentPage.style.display = 'none';
// show new page
        this.currentPage = newPage;
        this.currentPage.style.display = 'block';
      }  // END IF
  },  // END 1. FUNCTION


showNext: function(element)
  {
// remember old index and calculate the new one
    if (this.index <= this.pages.length - 1)
      {
        var oldIndex = this.index++;
        if (this.index >= this.pages.length)  {  this.index = 0;  }
// get new page
        var newPage = this.pages[this.index];
// hide old page
        this.currentPage.style.display = 'none';
// show new page
        this.currentPage = newPage;
        this.currentPage.style.display = 'block';
// disable/enable next/prev button
//
// if (this.prev == false) {
// element.parentNode.parentNode.childNodes[0].childNodes[0].childNodes[0].style.display = "block";
// this.prev = true;
// }
// if (this.index == this.pages.length -1) {
// element.childNodes[0].style.display = "none";
// this.next = false;
// }

      }  // END IF
  },  // END 1. FUNCTION



showPrev: function(element)
  {
// remember old index and calculate the new one
    if (this.index > 0)
      {
        var oldIndex = this.index--;
        if (this.index < 0)  this.index = this.pages.length - 1;
// get new page
        var newPage = this.pages[this.index];
// hide old page
        this.currentPage.style.display = 'none';
// show new page
        this.currentPage = newPage;
        this.currentPage.style.display = 'block';
// disable/enable next/prev button
        if (this.next == false)
          {
            element.parentNode.parentNode.childNodes[2].childNodes[0].childNodes[0].style.display = "block";
            this.next = true;
          }  // END IF
        if (this.index == 0)
          {
            element.childNodes[0].style.display = "none";
            this.prev = false;
          }  // END IF
      }  // END IF
  }  // END FUNCTION
}  // END prototype

