// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

Event.observe(window, 'load', function() {
  	onLoad();
});



// Dies Funktion wird beim Laden der Seite aufgerufen.

onLoad = function()
{
  new Ajax.Request('/feeds/fm4_sciencebusters.json',
    {
      method:'get',
      onSuccess: function(transport){
        var json = transport.responseText.evalJSON(true);
        displayRSS(json);
      },
      onFailure: function(){ alert('Something went wrong...') }
    });
}

displayRSS = function()
{

  oWeekDays = ["So","Mo","Di","Mi","Do","Fr","Sa"]
  oRss = arguments[0];
  evenodd = ["even", "odd"];

  var i = 0;
  var mp3_players = [];
  oRss.each(function(item)
  {
    var li_episode = new Element('li', { 'class': 'episode ' + evenodd[i % 2] });
    // mp3_players[i] = new Mp3Player(div_episode,item["urls"][0]);
    var a_episode = new Element('a', { 'class': 'podcast', href: item["urls"][0] }).update(item["title"]);
    li_episode.insert(a_episode);
    var pub_date = new Date(item["date_published"]);
    li_episode.insert(" (" + pub_date.getDate() + "." + (pub_date.getMonth() + 1) + "." + pub_date.getFullYear() +")");

    $("playlist").insert({bottom:li_episode});
    i++;
  });
  
}


/**** mp3player object ***/

Mp3Player = Class.create({

  initialize: function(target, url) {
    this.target = $(target);
    this.url = url;
    this.state = "0"; // 0 = stop, 1 = play, 2 = pause -> state machine, wechselt beim spielen nur mehr zwischen 1 und 2.

    this.a_play_link_image = new Element("img", {'src': "/images/icons/control_play_blue.png", 'alt': "play", 'title': "play"});
    this.a_pause_link_image = new Element("img", {'src': "/images/icons/control_pause_blue.png", 'alt': "pause", 'title': "play"});
    
    /* 
    
    Das ist der Link, der das Abspielen der mp3 steuert. 
    
    */
    this.a_play_episode = new Element('a', { 'class': 'play_link', href: "#", onclick: 'return false;'});
    this.a_play_episode.insert(this.a_play_link_image);
    
    this.a_play_episode.observe('dblclick', this.play_state_machine_double_click.bindAsEventListener(this));
    this.a_play_episode.observe('click', this.play_state_machine_click.bindAsEventListener(this));
     
    this.target.insert(this.a_play_episode);
    this.target.insert(" ");
  },

  play_state_machine_click: function ()
  {
    switch(this.state * 1)
    {
      case 0: this.play(); this.state = 1; break;
      case 1: this.pause(); this.state=2; break;
      case 2: this.resume(); this.state=1; break;
    }
  },

  play_state_machine_double_click: function ()
  {
    switch(this.state * 1)
    {
      default: 
        this.stop(); 
        this.state = 0;
        break;
    }
  },
  
  play: function() {
    // soundManager.stopAll();
    soundManager.play(this.url,this.url);
    this.a_play_episode.update("");
    this.a_play_episode.insert(this.a_pause_link_image);
    
  },

  pause: function() {
    soundManager.pause(this.url);
    this.a_play_episode.update("");
    this.a_play_episode.insert(this.a_play_link_image);

    this.a_play
  },

  resume: function() {
    soundManager.resume(this.url);
    this.a_play_episode.update("");
    this.a_play_episode.insert(this.a_pause_link_image);
  },

  stop: function() {
    soundManager.stop(this.url);
    this.a_play_episode.update("");
    this.a_play_episode.insert(this.a_play_link_image);
  }

});
