var NewsTicker = Class.create({
  initialize: function(newsHeadlines, newsTeasers) {
    this.headlines = newsHeadlines;
    this.teasers = newsTeasers;
    this.pointer = 0;
    this.sliding = false;
    this.open = false;
    this.mouseState = 'off';
    this.queueUp = false;
    this.queueDn = false;
    
    this.headlines.first().appear({duration:0.5});
    this.pe = new PeriodicalExecuter(this.changeStory.bind(this, 1, true), 5);
    this.setNav();
    this.setListeners();
  },
  
  changeStory: function(p, fade) {
    fade |= 0;
    if (fade) {
      this.headlines[this.pointer].fade({duration:0.5});
    } else {
      this.headlines[this.pointer].hide();
      this.teasers[this.pointer].hide();
    }
    this.pointer += p;
    if (this.pointer == this.headlines.length) { this.pointer = 0; } else if (this.pointer == -1) { this.pointer = this.headlines.length - 1; };
    if (fade) {
      this.headlines[this.pointer].appear({duration:0.5});
    } else {
      this.headlines[this.pointer].show();
      this.teasers[this.pointer].show();
    }
  },
  
  setNav: function() {
    $('next').observe('click', this.navClick.bindAsEventListener(this, 1));
    $('prev').observe('click', this.navClick.bindAsEventListener(this, -1));
  },
  
  navClick: function(e, i) {
    e.stop();
    this.pe.stop();
    this.changeStory(i);
  },
  
  setListeners: function() {
    $('headlines').observe('mouseover', this.queueSlideDown.bind(this)).observe('mouseout', this.queueSlideUp.bind(this));
    $('teasers').select('div').invoke('observe', 'mouseout', this.queueSlideUp.bind(this));
  },
  
  queueSlideDown: function(e) {
    if (this.mouseState == 'off') {
      this.mouseState = 'over';
      if (this.mouseStateCheck) this.mouseStateCheck.stop();
      this.mouseStateCheck = new PeriodicalExecuter(this.slideDown.bind(this, e), .33);
    };
  },
  
  queueSlideUp: function(e) {
    var relTar = e.relatedTarget || e.toElement;
    if (relTar) {
      if ((Event.element(e) == e.findElement()) && !relTar.up('#headlines') && relTar != $('headlines')) {
        this.mouseState = 'off';
        if (this.mouseStateCheck) this.mouseStateCheck.stop();
        this.mouseStateCheck = new PeriodicalExecuter(this.slideUp.bind(this, e), .33);
      };
    };
  },
  
  slideDown: function(e) {
    this.pe.stop();
    this.mouseStateCheck.stop();
    if (!this.sliding && !this.open && this.mouseState == 'over') {
      this.sliding = true;
      Effect.SlideDown(this.teasers[this.pointer].identify(), {duration: 0.4, afterFinish: function(e){
        this.sliding = false;
        this.open = true;
        if (this.queueUp) {
          this.queueUp = false;
          this.slideUp(e);
        };
      }.bind(this)});
    } else if (this.sliding) {
      this.queueDn = true;
    };
  },
  
  slideUp: function(e) {
    this.mouseStateCheck.stop();
    if (!this.sliding && this.open && this.mouseState == 'off') {
      this.sliding = true;
      Effect.SlideUp(this.teasers[this.pointer].identify(), {duration: 0.4, afterFinish: function(e){
        this.sliding = false;
        this.open = false;
        if (this.queueDn) {
          this.queueDn = false;
          this.slideDown(e);
        };
      }.bind(this)});
    } else if (this.sliding) {
      this.queueUp = true;
    };
  }
  
});

google.load("feeds", "1");
function initialize() {
  var feed = new google.feeds.Feed("http://news.tn.gov/rss.xml");
  feed.load(function(result) {
    if (!result.error) {
      for (var i = 0; i < result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
        $('open').insert({before: '<h3><a href="'+entry.link+'">'+entry.title.stripTags().truncate(80)+'</a></h3>'});
        var cont = (new Element('div')).insert(entry.content);
        var scrubbedStory = new Element('div');
        cont.select("p").last().remove();
        cont.childElements().each(function(el) {
          var s = el.innerHTML.stripTags();
          if (s.gsub('&nbsp;', ' ').blank()) {
            el.remove();
          } else {
            scrubbedStory.insert('<p>'+s+'</p>');
          }
        });
        var ce = scrubbedStory.childElements();
        if (ce.length) {
          ce.first().addClassName('first');
          ce.last().addClassName('last');
        } else {
          scrubbedStory.insert('<p class="first last">There is no summary available for this story. Please click the link below to view the complete story.</p>');
        };
        scrubbedStory.insert({bottom:'<a href="'+entry.link+'">Read Complete Story...</a>'});
        var cont = (new Element('div')).update(scrubbedStory);
        $('teasers').insert(cont);
      }
      new NewsTicker($('headlines').select('h3').invoke('hide'), $('teasers').childElements().invoke('hide'));
    }
  });
}
google.setOnLoadCallback(initialize);

