ï»¿RCRMTopOfferPage = Class.create();
RCRMTopOfferPage.prototype =
{
    initialize: function()
    {
        this.slides = $A();
        this.currentSlide = -1;
        this.t = null;
        this.slideDuration = 5000;
        this.paused = false;
        this.imageContainer = null;
    },
    show: function(request, offers)
    {
        this.view = request.elementToUpdate;
        this.imageContainer = this.view.select("div.tsr-desc img").first();
        this.imageContainer.onclick = this.onImageClick.bind(this);
        this.imageContainer.style.cursor = "pointer";

        if (offers.numOffers > 0)
        {
            // Uppdatera vår lista
            var container = this.view.select(".tsr-list").first();

            for (var i = (offers.offers.length -1); i >= 0; i--)
            {
                var oHtml = offers.offers[i];
                new Insertion.Top(container,oHtml);
            }
            var me = this;
            var idx = 0;
            // OK, vi pillar dit ett id pa samtliga
            container.select("li").each(function(x)
                                        {
                                          if(x.className == "")
                                          {
                                            x.id = "to" + idx++;
                                            me.slides[me.slides.length] = x.id;
                                          }
                                        });
            this.autoSlide();
        }
    },
    autoSlide: function()
    {
      this.t = setTimeout(this.autoSlide.bind(this),this.slideDuration);

      if(!this.paused)
          this.nextSlide();
    },
    nextSlide: function()
    {
      var nextIdx = this.currentSlide + 1;

      if(nextIdx >= this.slides.length)
          nextIdx = 0;

      this.showSlide(nextIdx);
    },
    showSlide: function(idx)
    {
      if(idx == this.currentSlide)
          return;

      if(this.currentSlide >= 0)
      {
        var oldSlide = $(this.slides[this.currentSlide]);

        // Vi deaktiverar
        oldSlide.removeClassName("active");
      }
      this.currentSlide = idx;
      var newSlide = $(this.slides[idx]);
      var newImg = newSlide.select("img").first();
      this.imageContainer.hide();
      newSlide.addClassName("active");
      this.imageContainer.src = newImg.src;
      this.imageContainer.appear();

    },
    onImageClick: function()
    {
      var slide = $(this.slides[this.currentSlide]);
      if(slide)
      {
        var target = slide.select("a").first();

        if(target)
            document.location = target;

      }
    }
};

