//usage: document.observe('dom:loaded', function() { new diiFader('sponsors_foo_id', { effect_duration: 0.5, loop_duration: 4000 }); });
// 3/17/11 jb - spurious $() changed to $j()
// 2/9/2011 jdm and cvd - added in random option for ncsl
// 1/13/11 jb - fix typo in seq 
diiFader = function(container, options) {
  this.container = $j('#'+container);
  var defaults = {
    effect_duration: 0.5,
    loop_duration: 4000,
    jobid: "",
    fx: "fade",
    maxWidth: this.container.width(),
    random: false,
    selector: "#"+container
  };
  this.options = $j.extend({}, defaults, options);
  this.getimages();
  this.imgs = $j("img", this.container);
};

diiFader.prototype = {
  preloadImages: function(){
    $j(this.imgs).each(function(i, img) {
      var newImg = new Image();
      newImg.src = img.src;
    });
  },
  checkSize: function($img) {
    // changes the width of the img to the width of the container if it is too big
    var maxWidth = this.options.maxWidth;
    var width = $img.width();
    if (width > maxWidth) {
      $img.css( {'width': maxWidth} );
    }
  },  
  start: function(){
    if (this.imgs.length == 1){
      var img = this.imgs.eq(0);
      img.parents('div').show();
      this.checkSize(img);
    }
    else{
      this.cycle();
    }
  },
  cycle: function(){
    /*  
      This function used to determine a set of divs in the following order:
        -div.imageContainerDIV
        -div.imageContainerDIVSPN'
        -div
      now it just cycles any div in the container.
    */
    var self = this;
    $j(this.options.selector).cycle({
      slideExpr: "div",
      fx: this.options.fx,
      timeout: this.options.loop_duration,
      speed: this.options.effect_duration * 1000,
      after: function(currSlideElement, nextSlideElement, options, forwardFlag){
        var $img = $j("img", currSlideElement);
        self.checkSize($img);
      },
      random: this.options.random ? 1 : 0
    });
  },
  getimages: function() {
    var collection_name = ($j(this.container).attr('subdept') != null) ? $j(this.container).attr('subdept') : this.container.id;
    var collection_url = "/_collections/" + collection_name + "/manifest.xml?r=" + Math.round(Math.random()*10000);
    if(window.location.protocol == "https:"){
      collection_url = "/" + this.options.jobid + collection_url;
    };
    var self = this;
    var seq = function(x){
      return parseInt($j(x).attr('seq_no'), 10);
    };
    var success = function(r){
      self.container.html("");
      var files = $j('file', r);
      files.sort(function(a, b) {
        try {
          return seq(a) - seq(b);
        } catch(e) {
          return -1;
        }
      });
      $j(files).each(function(i, file){
        var fileObj = {};
        fileObj.filename = $j(file).text();
        var tmpl = "<div class='imageContainerDIV' style='display: none;'> \
                      {{#href}}<a href='{{href}}' target='{{target}}' title='{{alt}}'>{{/href}}\
                        <img src='{{filename}}' {{#height}}height='{{height}}'{{/height}} {{#width}}width='{{width}}'{{/width}} border='0' alt='{{alt}}'/>\
                      {{#href}}</a>{{/href}}\
                    </div>";
        fileObj.href = $j(file).attr("href");
        fileObj.alt = $j(file).attr("alt");
        fileObj.target = $j(file).attr("target");
        if(window.location.protocol == "https:"){
          fileObj.filename = "/" + self.jobid + fileObj.filename;
        };
        fileObj.height = $j(file).attr("height");
        fileObj.width = $j(file).attr("width");
        var html = $j.mustache(tmpl, fileObj);
        self.container.append(html);
      });
      self.imgs = $j("img", self.container);
      self.preloadImages();
      self.start();
    };
    $j.ajax({
      url: collection_url,
      type: "get", 
      success: success
    });
  }
};

