﻿var entry1, entry2, entry3;

createMenuEntry = function(day, name, headline) {
  this.init(day, name, headline);
};

createMenuEntry.prototype = {

  links: null,
  times: null,
  headline: null,

  getlinks: function(o) {
    if (o) {
      this.links = o.query.results.a;
      this.finished();
    }
  },

  gettimes: function(o) {
    if (o) {
      this.times = o.query.results.p;
      this.finished();
    }
  },

  finished: function() {
    if (this.links && this.times) {
      var h2 = document.createElement('h2');
      h2.appendChild(document.createTextNode(this.headline));
      document.getElementById('menu').appendChild(h2);
      for (var i = 0; i < this.times.length; i++) {
        var dl = document.createElement('dl');
        dl.setAttribute('class', 'clearfix');
        var dt = document.createElement('dt');
        dt.appendChild(document.createTextNode(this.times[i]));
        var dd = document.createElement('dd');
        var a = document.createElement('a');
        a.setAttribute('href', this.links[i].href);
        a.setAttribute('onclick', 'control.showDetail(\'' + this.links[i].href + '\'); return false;');
        a.appendChild(document.createTextNode(this.links[i].content));
        dd.appendChild(a);
        dl.appendChild(dt);
        dl.appendChild(dd);
        document.getElementById('menu').appendChild(dl);
      }
    }
  },
  
  init: function(day, name, headline) {
    this.headline = headline;
    var objTransaction = YAHOO.util.Get.script('http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.schauburg.de%2Fprogramm.php%22%20and%20xpath%3D%22%2Fhtml%2Fbody%2Ftable%5Bposition()%3D' + day + '%5D%2Ftr%2Ftd%5Bposition()%3D1%5D%2Fp%22&format=json&callback=' + name + '.gettimes'); 
    var objTransaction = YAHOO.util.Get.script('http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.schauburg.de%2Fprogramm.php%22%20and%20xpath%3D%22%2Fhtml%2Fbody%2Ftable%5Bposition()%3D' + day + '%5D%2Ftr%2Ftd%5Bposition()%3D2%5D%2Fa%22&format=json&callback=' + name + '.getlinks'); 
  }

};

createMenu = function(name) {
  this.init(name);
};

createMenu.prototype = {
  
  getContent: function(content) {
    if (content.content) {
      return content.content;
    } else {
      return content;
    }
  },
  
  getdates: function(o) {
    if (o) {
      var dates = o.query.results.h5;
      entry1 = new createMenuEntry(1, 'entry1', this.getContent(dates[0]));
      entry2 = new createMenuEntry(2, 'entry2', this.getContent(dates[1]));
      entry3 = new createMenuEntry(3, 'entry3', this.getContent(dates[2]));
    }
  },
  
  init: function(name) {
    var objTransaction = YAHOO.util.Get.script('http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.schauburg.de%2Fprogramm.php%22%20and%20xpath%3D%22%2Fhtml%2Fbody%2Fh5%22&format=json&callback=' + name + '.getdates'); 
  }
  
};

var menu = new createMenu('menu');