Events = Class.create();

Events.prototype = {

    initialize: function(c, options) {
        this.eventContainer = $(c);
        if ( !this.eventContainer ) return;

        this.data = $A();
        this.eventElements = $A();
        this.index = 0;

        this.tout = null;
        this.timer = null;
        this.playing = false;
        this.paused = false;
        
        this.eventContainer.onmouseover = this.pause.bindAsEventListener(this);
        this.eventContainer.onmousemove = this.pause.bindAsEventListener(this);
        this.eventContainer.onmouseout  = this.pauseToggle.bindAsEventListener(this);
        this.eventContainer.style.overflow='hidden';        

        this.setOptions(options);
                
        if ( this.options['url'] ) 
            this.readXML(this.options['url']);

    },

    setOptions: function(options) {
        this.options = {
            'delay': 5000,
            'speed': 1
        }
        Object.extend(this.options, options || {});
    },
    
    readXML: function(url) {
        if ( url == null ) return;
            new Ajax.Request(url, {onComplete: this.parseXML.bind(this)} );
    },
    
    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);
    },

    parseEvents: function(xml) {
    
      var events = xml.getElementsByTagName("item");
      if ( events.length )
        this.data = $A();
      for ( var i = 0; i < events.length; i++ )
      {
          var evnt = events[i];
	
          // only process nodes of type element.....
          if ( evnt.nodeType != 1 )
              continue;

          var hash = $H();
          var title = null,time = null,loc = null,txt = null,url = null, item = null;
          
          item = evnt.getElementsByTagName("title")[0];
          if ( item ) title = RicoUtil.getContentAsString(item); else continue;
          item = evnt.getElementsByTagName("location")[0];
          if ( item ) loc = RicoUtil.getContentAsString(item);
          item = evnt.getElementsByTagName("time")[0];
          if ( item ) time = RicoUtil.getContentAsString(item);
          item = evnt.getElementsByTagName("description")[0];
          if ( item ) txt = RicoUtil.getContentAsString(item);
          item = evnt.getElementsByTagName("link")[0];
          if ( item ) url = RicoUtil.getContentAsString(item);

          this.addEvent(title, time, loc, txt, url);
      }
    },
    
    parseXML: function(request) {
      var xml = request.responseXML.getElementsByTagName("channel")[0];
      if ( xml.nodeType != 1 )
          return;

      this.parseEvents(xml);
      this.start();
   },

    addEvent: function (title, time, loc, txt, url) {
        var hash = $H();
        hash["title"]=title;
        hash["time"]=time;
        hash["location"]=loc;
        hash["txt"]=txt;
        hash["url"]=url;       
        this.data.push(hash);      
    },
    
    resetTimer: function() {
        if ( this.timer ) clearInterval(this.timer);
        this.timer = null;
        this.timer = setInterval(this.scroll.bind(this),this.options['delay']);
    },

    start: function() {
        this.index = 0;
        this.reset();
        this.resetTimer();
    },
    
    stop: function() {
        this.playing = false;
    },
    
    playToggle: function() {
        if ( !this.playing ) this.play(); else this.stop();
        if ( this.playButton ) this.playButton.src = (this.playing ?'2008/images/stop.jpg':'2008/images/play.jpg');
    },

    play: function() {
        this.playing = true;
        this.paused=false;
        if ( this.playButton ) this.playButton.src = '2008/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 ?'2008/images/pause.jpg':'2008/images/stop.jpg');
    },

    pause: function() {
        if ( !this.playing || this.paused ) return;
        this.paused=true;
        if ( this.playButton ) this.playButton.src='2008/images/pause.jpg';
    },
        
    reset: function() {
        this.stop();
        if ( this.scroller )
            while ( this.scroller.firstChild ) this.scroller.removeChild(this.scroller.firstChild);
        else {
            this.scroller = document.createElement('div');
            this.scroller.style.position='relative';
            this.scroller.className = 'scroller';
            this.eventContainer.appendChild(this.scroller);
        }
        
        this.eventElements = $A();
        this.data.each(function(d)
            {
                var span = document.createElement('div');
                span.id = 'title';
                var txt = document.createElement('div');
                txt.id = 'desc';
                
                var front = span;
                span.appendChild(document.createTextNode(d['title']));

                if ( d['url'] ) {
                    var link = document.createElement('a');
                    link.href = d['url'];
                    link.appendChild(span);
                    this.scroller.appendChild(link);                
                    front = link;
                } else
                    this.scroller.appendChild(span);

                this.eventElements.push(front);
                txt.appendChild(document.createTextNode(d['txt']));
                this.scroller.appendChild(txt);        
            }.bind(this));
            this.resetTimer();
//            this.play();
    },
    
    go: function(direction) {
//        this.stop();
        this.index += direction;
        if ( this.index < 0 ) this.index = this.eventElements.length - 1;
        this.index = ( this.index % this.eventElements.length );
//        this.index = ( this.index >= 0 ? (this.index >= this.eventElements.length ? 0 : this.index) : (this.data.length-1));

        var offset = this.eventElements[this.index].offsetTop;

        if ( document.all )
            if ( Math.abs(this.scroller.offsetTop) > 10 )
                offset += Math.abs(this.scroller.offsetTop);

        if ( offset < 10 && offset >= 0 ) offset = -1;
        this.resetTimer();
        new Rico.Effect.Position(this.scroller,null, -offset, 200, 10);
    },

    scroll: function() {
        if ( !this.playing || this.paused ) return;
        this.forward();
    },

    forward: function() {
        this.go(1);
    },
    
    getTop: function() {
        var top = this.scroller.offsetTop;
        if ( isNaN(top) ) top = 0;
        return top;
    },
    
    reverse: function() {
        this.go(-1);
    }
            
};
