var Tweets = {
  listen: function() {
    if($('tweets') == null)
      return;
    $('tweets').observe('click', function(event){Tweets.process(event);});
  },
  
  process: function(event) {
    var el = event.element();
    if (el.tagName == 'IMG' && el.className == 'edit') {
      var el = event.element();
      new Ajax.Request(el.up().href, {method: 'get'});
      event.stop();
    }
  }
}




// Group of functions to controll the display of tweets depending on moods set from the weather menu.
//
var WeatherUpdate = {

 // Listes on weather images for a click.
  listen: function() {
    $('weather').observe('click', (
      function(event) { 
        var el = event.element();
        if ((el.tagName == 'IMG' )) {

          if(el.hasClassName('active'))
            el.src = "/images/" + el.id + "op40.png";
          else
            el.src = "/images/" + el.id + ".png";

          el.toggleClassName('active');
          WeatherUpdate.updateView();
          event.stop();
        }
      })
    )        
  },
  
  updateView: function() {
    var items = WeatherUpdate.activeWeatherItems();
   $$('#tweets dl').each(function(tweet){
     if(items.indexOf(tweet.className.sub('mood_', '')) > -1)
        tweet.show()
      else
        tweet.hide();
   })
  },
  
  activeWeatherItems: function(){
    var items = []
    $$('#weather li img').each(function(img){
      if(img.hasClassName('active')) {
        items.push(img.id)
      }
    });
    return items;
  }
}


Event.observe(window, 'load', function(){ 
  if($('weather'))
    WeatherUpdate.listen();
});  