//////////////////////////////////////////////////////////////
// cqp.slideshow module
//
// Contains class definitions in cqp.slideshow namespace
// to create SlideShow widget and helper classes.
//
// Widget displays product information in a slideshow-like
// format. x number of products are displayed in a strip
// at a single time. Clicking "left" or "right" scroll controls
// displays the next or preceding x items.
//
// Author: Demian Hess, OPD CQ Press
// 
// Initial Date: January 2008 (versions 1.1)
// 
// Modified, Added Tabbed UI, by Nampora, version 1.2
//
// Current Version: 1.2
//////////////////////////////////////////////////////////////

//************************************************************************
// Namespaces
//************************************************************************
var cqp = cqp ? cqp : {};
cqp.slideshow = {};

//************************************************************************
// Start SlideShow class definition
//************************************************************************

/**
 * SlideShow displays items in a slide show. NumberOfCells 
 * specifies how many items are displayed at a time.
 */
cqp.slideshow.SlideShow = function(
   targetId, 
   numberOfCells, 
   viewerWidth, 
   borderSize, 
   borderColor, 
   leftScrollImage, 
   rightScrollImage, 
   cellClassName, 
   lastCellClassName, 
   titleText,
   titleClassName,
   pageCounterClassName ){
               
   // Instance properties
   this.targetId = targetId;
   this.currentPage = 0;
   this.numberOfPages = 0;
   this.numberOfCells = 0;
   if (numberOfCells != undefined && ! isNaN(numberOfCells)){
      this.numberOfCells = parseInt(numberOfCells);
   }
   this.items = [];
   this.viewer = new cqp.slideshow.UI(this.numberOfCells, viewerWidth, borderSize, borderColor, leftScrollImage, rightScrollImage, cellClassName, lastCellClassName, titleText, titleClassName, pageCounterClassName); 
   
   this.cells = this.viewer.divs;
   
   // Set up event handlers
   var oThis = this;
   this.viewer.leftLink.onclick = function(){
      oThis.getNextPageToLeft();
      return false;
   }
   this.viewer.rightLink.onclick = function(){
      oThis.getNextPageToRight();
      return false;
   }     

} // cqp.slideshow.SlideShow constructor

/**
 * cqp.slideshow.SlideShow.showPage()
 *
 * Displayed the specified page of items. If page number is out
 * of range, nothing is done. Also reset the page counter.
 */
cqp.slideshow.SlideShow.prototype.showPage = function(pageNumber){
   if (isNaN(pageNumber) || pageNumber < 1 || pageNumber > this.numberOfPages || this.numberOfCells < 1){
      return;
   }

   var maxItemIndex = (this.numberOfCells * pageNumber);
   var minItemIndex = (this.numberOfCells * pageNumber) - this.numberOfCells;
   
   // Retrieve the slice of the array for this page
   var cellCounter = 0;      
   for (i = minItemIndex; i < maxItemIndex; i++){
      // Clear cell
      if (this.cells[cellCounter].innerHTML){
        this.cells[cellCounter].innerHTML = '';
      }     
      
      // Retrieve next item (if available)
      if (this.items[i] != undefined){
         this.cells[cellCounter].innerHTML = this.items[i];
      }
      
      // increment cell counter to move through the table row
      cellCounter++;
   }//for
   
   this.currentPage = pageNumber;
   this.viewer.pageCounterSpan.innerHTML = 'Page ' + this.currentPage + ' of ' + this.numberOfPages;
   
}//cqp.slideshow.SlideShow.showPage()

/**
 * cqp.slideshow.SlideShow.loadItems()
 *
 * Loads items into the view. Replaces all 
 * previous items. Caller can pass in any
 * number of items. After load is called, we
 * calculate the number of pages and display
 * the first page in the UI.
 */
cqp.slideshow.SlideShow.prototype.loadItems = function(){

   // Reinitialize
   this.items = []; // clear previous items
   this.currentPage = 0;
   this.numberOfPages = 0;
            
   // Store all the items
   for (i = 0; i < arguments.length; i++){      
      this.items[i] = arguments[i];
   }
   
   if (this.items.length == 0){
      return;
   }
   
   // Calculate page info.
   
   var temp = (this.numberOfCells == 0) ? 0 : (this.items.length / this.numberOfCells);
   if (temp == 0){
      this.numberOfPages = 0;
      this.currentPage = 0;
   }
   else if (temp < 1){
      this.numberOfPages = 1;
      this.currentPage = 1;
   }
   else{
      this.numberOfPages = Math.ceil(temp);
      this.currentPage = 1;
   }
   
   // Display items in first page
   this.showPage(1);

}//cqp.slideshow.SlideShow.loadItems()

/**
 * cqp.slideshow.SlideShow.getNextPageToLeft()
 *
 * Page left.
 */
cqp.slideshow.SlideShow.prototype.getNextPageToLeft = function(){
   
   if (this.currentPage <= 1){
      this.currentPage = this.numberOfPages;
   }
   else{
      --this.currentPage;
   }
   this.showPage(this.currentPage);

}//SlideShow.getNextPagetoLeft

/**
 * SlideShow.getNextPageToRight()
 *
 * Page right.
 */
cqp.slideshow.SlideShow.prototype.getNextPageToRight = function(){
   if (this.currentPage >= this.numberOfPages){
      this.currentPage = 1;
   }
   else{
      ++this.currentPage;
   }
   this.showPage(this.currentPage);

}//cqp.slideshow.SlideShow.getNextPageToRight

/**
 * Make the slideshow visible
 */
cqp.slideshow.SlideShow.prototype.sh