Ads = Class.create();

Ads.prototype = {

   initialize: function(container, options) {
      this.container            = $(container);
      this.setOptions(options);
      this.timer = null;
      this.array = new Array();
      this.index = 0;
      this.init = false;
      this.playing = false;
      this.paused = false;
      this.xfer = false;
      this.counter = null;

      this.buffer = new Image();

      while ( this.container.hasChildNodes() )
            this.container.removeChild(this.container.firstChild);

      var l = document.createElement('a');
      this.img = document.createElement('img');
      this.img.setAttribute("border","0");
      this.img.onmouseover = this.pause.bindAsEventListener(this);
      this.img.onmousemove = this.pause.bindAsEventListener(this);
      this.img.onmouseout = this.pauseToggle.bindAsEventListener(this);

      l.appendChild(this.img);      
      this.container.appendChild(l);
      
      if ( this.options["url"] )
          this.readXML(this.options["url"]);
   },
   
   setOptions: function(options) {
      this.options = {
         url         : null,
         delay       : 15000
      }
      Object.extend(this.options, options || {});
   },

   readXML: function(url) {
      if ( url == null ) return;
      new Ajax.Request(url, {onComplete: this.parseXML.bind(this)} );
   },
   
   parseXML: function(request) {
      var xml = request.responseXML.getElementsByTagName("images")[0];
      if ( xml.nodeType != 1 )
          return;

      var imgs = xml.getElementsByTagName("image");
      for ( var i = 0; i < imgs.length; i++ )
      {
          var img = imgs[i];
	
          // only process nodes of type element.....
          if ( img.nodeType != 1 )
              continue;

          var hash = {title: null, link: null, url: null};

          if ( img.getElementsByTagName("title")[0] )
              hash["title"] = RicoUtil.getContentAsString(img.getElementsByTagName("title")[0]);

          if ( img.getElementsByTagName("link")[0] )
              hash["link"]  = RicoUtil.getContentAsString(img.getElementsByTagName("link")[0]);

          if ( img.getElementsByTagName("url")[0] )
              hash["url"]   = RicoUtil.getContentAsString(img.getElementsByTagName("url")[0]);
			  
          this.addImage(hash["url"], hash["title"], hash["link"]);

      }
      this.index=0;

      this.start();
      this.flip();
      
   },
   
   addImage: function(u, t, l) {
      this.array.push({url: u, title: t, link: l})
   },

    start: function() {
        this.index = 0;
        this.resetTimer();
        this.play();
    },
    
    stop: function() {
        this.playing = false;
    },
    
    playToggle: function() {
        if ( !this.playing ) this.play(); else this.stop();
        if ( this.playButton ) this.playButton.src = (this.playing ?'http://pire.fiu.edu/images/stop.jpg':'http://pire.fiu.edu/images/play.jpg');
    },

    play: function() {
        this.playing = true;
        this.paused=false;
        if ( this.playButton ) this.playButton.src = 'http://pire.fiu.edu/images/stop.jpg';
    },
    
    pauseToggle: function() {
        if ( !this.playing ) return;
        if ( this.paused ) this.play(); else this.pause();
        if ( this.playButton ) this.playButton.src = (this.paused ?'http://pire.fiu.edu/images/pause.jpg':'http://pire.fiu.edu/images/stop.jpg');
    },

    pause: function() {
        if ( !this.playing || this.paused ) return;
        this.paused=true;
        if ( this.playButton ) this.playButton.src='http://pire.fiu.edu/images/pause.jpg';
    },

    resetTimer: function() {
        if ( this.timer ) clearInterval(this.timer);
        this.timer = null;
        this.timer = setInterval(this.auto.bind(this),this.options['delay']);
    },
   
   fadeIn: function() {
      new Rico.Effect.FadeTo(this.img,.99,100,20,{complete: this.setXfer.bind(this,false)});
   },
   
   setXfer: function(b) {
      this.xfer = b;
      if ( !b ) this.resetTimer();  
   },

   setCounter: function(o) {
      if ( !o || o == null ) return;
      var obj = $(o);
      if (obj) this.counter = obj;
   },

   showCount: function() {
      if ( this.counter == null || this.array.length == 0 ) return;
      while ( this.counter.hasChildNodes() ) this.counter.removeChild(this.counter.firstChild);
      this.counter.appendChild(document.createTextNode( (this.index + 1) + " of " + this.array.length ));
   },
   
   fadeOut: function() {
      if ( this.index > this.array.length-1 ) this.index = 0;
      this.setXfer(true);
      new Rico.Effect.FadeTo(this.img,0,100,20,{complete: this.flip.bind(this) } );
   },
   
   flip: function() {
      if ( this.array.length == 0 ) return;
      var p = this.img.parentNode;
      var ad = this.array[this.index];
      if ( ad["title"] )    p.title = ad["title"]; else p.title = "";
      if ( ad["link"] )      p.href = ad["link"];  else p.href  = "";
      if ( ad["url"] ) this.img.src = ad["url"];   else this.img.src = "";
      
      this.showCount();
      if ( ++this.index > this.array.length-1 ) this.index = 0;
      this.buffer.src = this.array[this.index]["url"];
      
      this.fadeIn();
   },
   
   go: function(dir) {
        if ( this.xfer ) return;
        this.index = ( Math.abs(( dir < 0 ? -1 : 1 ) * this.index + dir) % this.array.length );
        this.fadeOut();
   },
   
   forward: function() {
        this.go(0);
   },
   
   reverse: function() {
        this.go(-1);
   },
   
    playButton: function(button) {
        var b = $(button);
        b.onclick = this.playToggle.bindAsEventListener(this);
        this.playButton = b;
    },
   
    forwardButton: function(button) {
        var b = $(button);
        b.onclick = this.forward.bindAsEventListener(this);
    },

    reverseButton: function(button) {
        var b = $(button);
        b.onclick = this.reverse.bindAsEventListener(this);
    },

    auto: function() {
        if ( !this.playing || this.paused ) return;
        this.go(0);
   }

};

