
/**
 * Implementation of boxProductsVisited JS product images scroller
 */

var btv_RecentlyVisitedScroller = Class.create();

btv_RecentlyVisitedScroller.prototype = {

  initialize: function(count, images, links, actions, alwaysDisplayScrollers) {
    this.alwaysDisplayScrollers = alwaysDisplayScrollers;
    this.images                 = images;
    this.links                  = links;
    this.actions                = actions;
    this.count                  = count;
    this.baseId                 = 'recentlyVisitedProduct_';
    this.spans                  = new Array();
    this.imgs                   = new Array();
    this.anchors                = new Array();
    this.productForms           = new Array();
    this.scrollIndex            = 0;
    this.prevButton             = $('recentlyVisitedPrevious');
    this.nextButton             = $('recentlyVisitedNext');

    var elements;
    var thumb;
    var thumbImg
    var thumbs;
    var thumbImgs;
    var anchors;
    var productForms;

    for (var i=0; i<this.count; i++) {
      elements = $(this.baseId + i);
      if (elements != undefined) {
        anchors = elements.getElementsBySelector('a');

        if (anchors != undefined && anchors.length > 0) {
          this.anchors.push(anchors[0]);
        }
        
        productForms = elements.getElementsBySelector('form');
        
        if (productForms != undefined && productForms.length > 0) {
          this.productForms.push(productForms[0]);
        }

        thumbs = elements.getElementsBySelector('span');
        if ((thumbs != undefined) && (thumbs.length > 0)) {
          for (var z = 0; z < thumbs.length; z++) {
            if ((thumbs[z].style.backgroundImage != '') && (thumbs[z].style.backgroundImage.indexOf(this.images[i]) > -1)) {
              this.spans.push(thumbs[z]);
              break;
            }
          }

          thumbImgs = elements.getElementsBySelector('img');
          for (var z = 0; z < thumbImgs.length; z++) {
            if ((thumbImgs[z].style.backgroundImage != '') && (thumbImgs[z].src.indexOf(this.images[i]) > -1)) {
              this.imgs.push(thumbImgs[0]);
              break;
            }
          }
        }
      }
    }

    /**
     * Set event at previous button
     */
    Event.observe(this.prevButton, 'click', (function(event){
      this.scrollLeft();
      Event.stop(event);
    }).bindAsEventListener(this) );

    /**
     * Set event at next button
     */
    Event.observe(this.nextButton, 'click', (function(event){
      this.scrollRight();
      Event.stop(event);
    }).bindAsEventListener(this) );

    /**
     * Check displaying previous button
     */
    if (this.alwaysDisplayScrollers) {
      if ($(this.prevButton).hasClassName('button')) {
        $(this.prevButton).removeClassName('button');
      }
    } else {
      this.prevButton.style.display = 'none';
    }

    /**
     * Check displaying next button
     */
    if (this.images.length <= this.count) {
      if (this.alwaysDisplayScrollers) {
        if ($(this.nextButton).hasClassName('button')) {
          $(this.nextButton).removeClassName('button');
        }
      } else {
        this.nextButton.style.display = 'none';
      }
    }
  },

  /**
   * scrollLeft
   *
   * function to scroll product images left
   */
  scrollLeft: function() {
    if (this.scrollIndex > 0) {
      this.scrollIndex--;
      this.showImage();
    }
  },
  /**
   * scrollRight
   *
   * function to scroll product images right
   */
  scrollRight: function() {
    if (this.scrollIndex < (this.images.length - this.count)) {
      this.scrollIndex++;
      this.showImage();
    }
  },

  /**
   * showImage
   *
   * function to set images at selected elements on page
   */
  showImage: function() {
    var formInnerHTML;
    var formHTML;
    var elements;
    var productForms;
    for (var i=0; i<this.count; i++) {
      if (this.spans[i] != undefined) {
        this.spans[i].style.backgroundImage = 'url(' + this.images[i + this.scrollIndex] + ')';
      }
      if (this.imgs[i] != undefined) {
        this.imgs[i].src = this.images[i + this.scrollIndex];
      }
      if (this.anchors[i] != undefined) {
        this.anchors[i].href = this.links[i + this.scrollIndex];
      }
      if (this.productForms[i] != undefined) {
        formInnerHTML = this.productForms[i].innerHTML;
        formHTML = '<form ' + this.actions[i + this.scrollIndex] + ' >' + formInnerHTML + '</form>';
        this.productForms[i].replace(formHTML);
        elements = $(this.baseId + i);
        if (elements != undefined) {
          productForms = elements.getElementsBySelector('form');
        
          if (productForms != undefined && productForms.length > 0) {
            this.productForms[i] = productForms[0];
          } else {
            this.productForms[i] = undefined;
          }
        } else {
          this.productForms[i] = undefined;
        }
      }
    }
    if (this.scrollIndex > 0) {
      if (this.alwaysDisplayScrollers) {
        if (!$(this.prevButton).hasClassName('button')) {
          $(this.prevButton).addClassName('button');
        }
      } else {
        this.prevButton.style.display = '';
      }
    } else {
      if (this.alwaysDisplayScrollers) {
        if ($(this.prevButton).hasClassName('button')) {
          $(this.prevButton).removeClassName('button');
        }
      } else {
        this.prevButton.style.display = 'none';
      }
    }
    if (this.scrollIndex == (this.images.length  - this.count)) {
      if (this.alwaysDisplayScrollers) {
        if ($(this.nextButton).hasClassName('button')) {
          $(this.nextButton).removeClassName('button');
        }
      } else {
        this.nextButton.style.display = 'none';
      }
    } else {
      if (this.alwaysDisplayScrollers) {
        if (!$(this.nextButton).hasClassName('button')) {
          $(this.nextButton).addClassName('button');
        }
      } else {
        this.nextButton.style.display = '';
      }
    }
  }
}
