Object.extend(Event, {
  wheel:function(event){
    var delta = 0;
    if(!event) event = window.event;
    if(event.wheelDelta){
      delta = event.wheelDelta/120;
      if(window.opera) delta = -delta;
    }
    else if(event.detail){
      delta = -event.detail/3;
    }
    return Math.round(delta); //Safari Round
  }
});


var SRPVDPCarousel = {
  carousel:null,
  pushNext:false,
  pushPrev:false,
  addReflection:false,
  addMouseScroll:false,
  needImagePatch:(/firefox/i.test(navigator.userAgent)),

  init_carousel:function(){
      //defect 22122 - JS error on VDP for owners who have this product, but product is empty. see VDPListingViewBean's setShowSmartSuggest for the reason I didn't put this in the bean... 
      if($('scroller-header')){
        SRPVDPCarousel.carousel = new Carousel('carolssaul', {animHandler:SRPVDPCarousel.circularHack,
                                           animParameters:{},
                                           buttonStateHandler:SRPVDPCarousel.buttonStateHandler,
                                           nextElementID:'next-arrow-thumber',
                                           prevElementID:'prev-arrow-thumber',
                                           size:9,
                                           numVisible:3,
                                           scrollInc:3
                                          });

        if(typeof(Reflection) == "undefined") SRPVDPCarousel.addReflection = false;

        if(SRPVDPCarousel.addMouseScroll){
          Event.observe(SRPVDPCarousel.carousel.carouselElemID, "mousewheel", SRPVDPCarousel.mouseListener, false);
          Event.observe(SRPVDPCarousel.carousel.carouselElemID, "DOMMouseScroll", SRPVDPCarousel.mouseListener, false);
        }
        if(SRPVDPCarousel.addReflection){
           SRPVDPCarousel.setupReflections();
        }
    
        SRPVDPCarousel.pushPrev = true;
        SRPVDPCarousel.circularHack(SRPVDPCarousel.carousel.carouselElemID, "after", "");
      }
  },

  setupReflections:function(){
    var items = $A($(SRPVDPCarousel.carousel.carouselList).getElementsByClassName("carousel-item"));
    items.each(function(item){$($(item).getElementsByTagName("IMG")[0]).addClassName("reflect")});
    SRPVDPCarousel.applyReflections();
  },
  
  mouseListener:function(e){
    var scroll = Event.wheel(e);
    if(scroll < 0) SRPVDPCarousel.carousel.prevScroll();
    else SRPVDPCarousel.carousel.nextScroll();
  },

  buttonStateHandler:function(button, enabled){
    if(button == "prev-arrow-thumber")
      $('prev-arrow-thumber').src = enabled ? "/img/fyc/vdp/btn-scroll-left.jpg" : "/img/fyc/vdp/btn-scroll-left.jpg"
    else
      $('next-arrow-thumber').src = enabled ? "/img/fyc/vdp/btn-scroll-right.jpg" : "/img/fyc/vdp/btn-scroll-right.jpg"

    if(button == "next-arrow-thumber" && !enabled){
      if(SRPVDPCarousel.carousel) SRPVDPCarousel.pushNext = true;
    }
    else if(button == "prev-arrow-thumber" && !enabled){
      if(SRPVDPCarousel.carousel) SRPVDPCarousel.pushPrev = true;
    }
  },

  circularHack:function(carouselID, status, direction){
    var list = $(SRPVDPCarousel.carousel.carouselList);
    if(status == "after"){
      if(SRPVDPCarousel.pushNext){
        SRPVDPCarousel.pushNext = false;

        var els = $A(list.getElementsByClassName("carousel-item"));
        for(var i=0; i<SRPVDPCarousel.carousel.currentIndex-SRPVDPCarousel.carousel.options.numVisible; i++){
          var el = $(els[i]);
          if (SRPVDPCarousel.addReflection) 
              SRPVDPCarousel.reflectionFix(el);
          list.appendChild(el.cloneNode(true));
          el.remove();
          list.setStyle({top:0, left:-1*SRPVDPCarousel.carousel.elementSize*SRPVDPCarousel.carousel.options.numVisible + "px"});
        }

        if(SRPVDPCarousel.needImagePatch) $A(list.getElementsByTagName("IMG")).invoke("getHeight");
        SRPVDPCarousel.applyReflections();
        SRPVDPCarousel.carousel.currentIndex = SRPVDPCarousel.carousel.options.numVisible;
      }
      else if(SRPVDPCarousel.pushPrev){
        SRPVDPCarousel.pushPrev = false;

        var els = $A(list.getElementsByClassName("carousel-item"));
        var moves = 0;
        for(var i=SRPVDPCarousel.carousel.options.size-1; i>=SRPVDPCarousel.carousel.currentIndex+(2*SRPVDPCarousel.carousel.options.numVisible); i--){
          var el = $(els[i]);
          if(SRPVDPCarousel.addReflection) SRPVDPCarousel.reflectionFix(el);
          var tmp = el.cloneNode(true);
          list.insertBefore(tmp, list.childNodes[0]);
          if(SRPVDPCarousel.addReflection && SRPVDPCarousel.needImagePatch){
            Reflection.add($(tmp).getElementsByClassName("reflected")[0])
          }
          el.remove();
          moves++
        }

        list.setStyle({top:0, left:-1*SRPVDPCarousel.carousel.elementSize*moves + "px"});
        SRPVDPCarousel.applyReflections();
        SRPVDPCarousel.carousel.currentIndex = moves;
      }
      var elx = list.getElementsByClassName("carousel-item");
      var asisParams = "";
      var sType = "";
      for (var eli=3;eli<6;eli++) {
        if (eli < elx.length) {
            var elxId = elx[eli].getElementsByTagName("table")[0].id;
            var elxParts = elxId.split("-");
            if (elxParts.length > 4) {
                if (eli == 3) {
                    asisParams = "?d1="+elxParts[4];
                }
                sType = elxParts[5];
//                asisParams += "c"+elxParts[2]+"="+elxParts[3]+"&d"+elxParts[2]+"="+elxParts[4];
                asisParams += "&c"+parseInt(eli-2)+"="+elxParts[3];
            }
        }
      }
      asisParams += !(sType == undefined || sType.length==0) ? "&search_type="+sType : '';
      if (asisParams != "") {
        var now = new Date();
        var asisImg = document.createElement("img");
        var asisURL = "/no_cache/ac/inventory_scroller_vdp_pmdl.asis";
        asisImg.setAttribute("src",asisURL+asisParams+"&cache_killer="+ now.getTime());
        asisImg.width = asisImg.height = 1;
//        document.body.appendChild(asisImg); // Don't need to append
      }
      SRPVDPCarousel.carousel._updateButtonStateHandler(SRPVDPCarousel.carousel.prevElementID, true);
      SRPVDPCarousel.carousel._updateButtonStateHandler(SRPVDPCarousel.carousel.nextElementID, true);
    }
  },

  reflectionFix:function(el){
    if(!SRPVDPCarousel.needImagePatch) return;
    var reflecto = $(el.getElementsByClassName("reflected")[0]);
    Reflection.remove(reflecto);
    reflecto.removeClassName("reflected").addClassName("reflect");
  },

  applyReflections:function(){
    addReflections();
  }
}
