//usage: document.observe('dom:loaded', function() { new diiFader('sponsors_foo_id', { effect_duration: 0.5, loop_duration: 4000 }); });

var diiFader = Class.create({
  initialize: function(container, options) {
    this.container = $j('#'+container);
    this.effect_duration = options['effect_duration'] || 0.5;
    this.loop_duration = options['loop_duration'] || 4000;
    this.jobid = options['jobid'] || '';

    this.getimages();
    var images = $j(this.container).find('img');
    if (images.length == 1) {
    images[0].parent('div').show();
  } else {
      $j(images).each(function(i) {
        var img = new Image();
        img.src = i.src;
      });
      this.image_index = images.length - 1;
      this.loop();
    }
  },
  loop: function(){
    var divs = $j(this.container).find('div.imageContainerDIV');
    if (divs.length == 0) {
      divs = $j(this.container).find('div.imageContainerDIVSPN');
    };
    if (divs.length == 0) {
      divs = $j(this.container).find('div');
    };
    if (divs.length > 0) {
      var e = this.image_index;
      var f = e + 1;
      if (f >= divs.length) f=0;
      $j(divs[e]).queue(function(){
        $j(this).fadeOut(500);
        $j(divs[f]).fadeIn(500);
        $j(this).dequeue();
      });
      this.image_index = f;
      setTimeout(this.loop.bind(this), this.loop_duration);
    };
  },
  getimages: function() {
    var collection_name = ($j(this.container).attr('subdept') != null) ? $j(this.container).attr('subdept') : this.container.id;
    var c_url = "/_collections/" + collection_name + "/manifest.xml?r=" + Math.round(Math.random()*10000);
    if(window.location.protocol == "https:"){
      c_url = "/" + this.jobid + c_url;
    };
    new Ajax.Request(c_url, {
        method: 'get',
        asynchronous: false,
        onCreate: function() { },
        onSuccess: function(r) {
        this.container.innerHTML = '';
        var files = $A(r.responseXML.getElementsByTagName('file'));
        files.sort(function(a,b) {
          try {
            return parseInt(a.attributes.getNamedItem('seq_no').value) - parseInt(b.attributes.getNamedItem('seq_no').value);
          } catch(e) {
            return -1;
          };
        });
        files.each(function(f) {
          var filename = (f.hasChildNodes() ? f.firstChild.nodeValue : '');
          if (filename != '') {
            var hreftag = '', hrefend = '';
            if (f.attributes.getNamedItem('href') != null) {
              var target = f.attributes.getNamedItem('target').value;
              hreftag = '<a href="' + f.attributes.getNamedItem('href').value + '" target="' + target + '">';
              hrefend = '</a>';
            }
            if(window.location.protocol == "https:"){
              filename = "/" + this.jobid + filename;
            };
            var heightattr = ((f.attributes.getNamedItem('height') != null) ? 'height="' + f.attributes.getNamedItem('height').value + '" ' : ' ');
            var widthattr = ((f.attributes.getNamedItem('width') != null) ? 'width="' + f.attributes.getNamedItem('width').value + '" ' : ' ');
            this.container.insert('<div class="imageContainerDIV" style="display:none">' + hreftag + '<img src="' + filename + '" ' + 
                                  heightattr + widthattr + 'border="0"/>' + hrefend + '</div>');
          };
        }.bind(this));
        }.bind(this),
        onFailure: function(){  }
      });
  }
});


