/* CSS background image */
function newImage(url, cssClass, title) {
  var $div = $("<div>").css("background-image", "url(" + url + ")");
  if (cssClass) $div.addClass(cssClass);
  if (title) $div.attr("title", title);
  var html = $div.wrap("<div>").parent().html();
  $div.unwrap();
  return html;
}


var IBERollingImages = Class.extend(
  {
    init : function(args) {
      this.args = $.extend({
                             rollingImages : undefined,
                             placeHolderId : undefined,
                             cssClass : ''
                           }, args);
      validateArgument(args.rollingImages, {rules:{allowUndefined:false}}, "args.rollingImages");
      validateArgument(args.placeHolderId, {rules:{allowUndefined:false}}, "args.placeHolderId");
      validateArgument(args.cssClass, {rules:{allowUndefined:false, requireString : true}}, "args.placeHolderId");

      this.rollingImages = args.rollingImages;
      this.placeHolderId = args.placeHolderId;
      this.cssClass = args.cssClass;
    },

    startSlideShow : function() {
      var that = this,
        $placeHolder = $('#' + that.placeHolderId),
        images = [],
        prevIndex = 0,
        nextIndex = 0,
        duration = 0,
        numImages = that.rollingImages.length;

      function textStyleFunctionCallback(c) {
        var r = '';
        r += 'z-index: 300;';
        r += 'position:absolute;';
        r += 'width:100%;';
        r += 'left:0px;';
        if (c) {
          if (c.indexOf('middle') >= 0) {
            r += 'top:40%;';
          } else if (c.indexOf('top') >= 0) {
            r += 'top:0;';
          } else if (c.indexOf('bottom') >= 0) {
            r += 'bottom:0;';
          }
        }
        return r;
      }

      function getTextConfigHtml(rollingImageItem) {
        var textConfigDiv = '', config = rollingImageItem.textConfig;
        if (config && config.length) {
          for (var i = 0; i < config.length; ++i) {
            textConfigDiv += '<div style="' + textStyleFunctionCallback(config[i].cssClass) + '" class="tc rc ' + config[i].cssClass + '"><div class="t ' + config[i].cssClass + '">' + config[i].displayText + '</div></div>';
          }
        }
        return textConfigDiv;
      }

      function createHtmlForRollingImageItem(rollingImageItem) {
        var img = new Image();
        img.src = rollingImageItem.imageSrc;

        return '<a href="' + rollingImageItem.href + '"' +
               (rollingImageItem.hrefId ? (' id="' + rollingImageItem.hrefId + '"') : '') +
               (!rollingImageItem.hrefId && rollingImageItem.title ? (' title="' + rollingImageItem.title + '"') : '') +
               (rollingImageItem.target ? (' target="' + rollingImageItem.target + '"') : '') +
               ' class="none absolute ri-a">' +
               getTextConfigHtml(rollingImageItem) +
               '<div class="t-im"><img src="' + rollingImageItem.imageSrc + '"' +
               (rollingImageItem.title ? (' alt="' + rollingImageItem.title + '"') : '') +
               ' class="' + that.cssClass + ' im"/>' +
               '</div></a>';
      }

      function setPrevAndNextIndex() {
        prevIndex = nextIndex;
        nextIndex = ((prevIndex + 1) > (numImages - 1)) ? 0 : (prevIndex + 1);
      }

      function slideshow() {
        setPrevAndNextIndex();
        createImgHtmlAndAppendToDom(nextIndex);
        var imageToHide = images[prevIndex], imageToShow = images[nextIndex],
          displayTimeInMilliSeconds = Math.max(2000, that.rollingImages[nextIndex].displayTimeInSeconds * 1000);
        setTimeout(function() {
          $(imageToHide).fadeOut(duration);
          $(imageToShow).fadeIn(duration, function() {
            slideshow();
          });
        }, displayTimeInMilliSeconds);
      }

      function createImgHtmlAndAppendToDom(index) {
        if (index !== undefined && index < numImages && images.length < numImages) {
          var rollingImage = that.rollingImages[index], $html = $(createHtmlForRollingImageItem(rollingImage));
          $placeHolder.append($html);
          images.push($html);
        }
      }

      function setLongestTransitionDuration() {
        for (var i = 0; i < numImages; i++) {
          duration = Math.max(duration, that.rollingImages[i].transitionTimeMilliseconds);
        }
      }

      function setup() {
        if (numImages) {
          setLongestTransitionDuration();
          createImgHtmlAndAppendToDom(0);
          $(images[0]).fadeIn('fast');
        }
        if (numImages > 1) slideshow();
      }

      setup();
    }
  });
