TADR = function() {

  var $E  = YAHOO.util.Event;
  var $D  = YAHOO.util.Dom;
  var $A  = YAHOO.util.Anim;
  var $Ea = YAHOO.util.Easing;
  var $   = YAHOO.util.Dom.get;

  var swf = swfobject;
  var dialog = null;
  var els = null;

  function strpad(i,l,s,d) {
    var o = i.toString();
    if (!s) { s = '0'; }
    while (o.length < l) {
      if(d) {
        o = o+s;
      } else {
        o = s+o;
      }
    }
    return o;
  };

  return {
    dialog: dialog,
    swf: swf,
    els: els,

    gallery: function() {

      var cur = 0;
      var next = 0;

      for(var i = 0; i < TADR.els.length; i++) {
        if($D.hasClass(TADR.els[i].image, 'active')) {
          cur = i;
        } else {
          $D.setStyle(TADR.els[i].image, 'opacity', 0);
        }
      }

      if(cur != TADR.els.length - 1) {
        next = cur + 1;
      }

      var imageOut = new YAHOO.util.ColorAnim(TADR.els[cur].image, {opacity: { from: 1, to: 0 }}, .25);
      var txtOut = new YAHOO.util.ColorAnim(TADR.els[cur].txt, {opacity: { from: 1, to: 0 }}, .25);

      imageOut.onComplete.subscribe(function(e, anim) { 
        $D.removeClass(TADR.els[this[0]].image, 'active');
        $D.addClass(TADR.els[this[1]].image, 'active');
        var imageIn = new YAHOO.util.ColorAnim(TADR.els[next].image, {opacity: { from: 0, to: 1 }}, .25);
        var txtIn = new YAHOO.util.ColorAnim(TADR.els[next].txt, {opacity: { from: 0, to: 1 }}, .25);
        imageIn.animate();
        txtIn.animate();
      }, [cur, next], true);

      imageOut.animate();
      txtOut.animate();

    },

    sponsors: function() {
      var ul = $D.getFirstChild($('sponsors'));
      var li = $D.getFirstChild(ul);
      $D.setStyle(li, 'marginLeft', '-'+ul.offsetWidth-300+'px');
      var attributes = {marginLeft: { to: ul.offsetWidth}};
      var a = new YAHOO.util.Motion(li, attributes, 30);
      a.onComplete.subscribe(TADR.sponsors);
      a.animate();
    },

    init: function() {

      var bubbles = $D.getElementsByClassName('bubble');

      if(bubbles.length) {
        $E.on(bubbles, 'click', function(e) {
          var t = $E.getTarget(e);
          while(t != this) {
            var links = t.getElementsByTagName('a');
            if(links.length) {
              if (links[0].getAttribute('onclick') == null) {
                if (links[0].getAttribute('href')) {
                  document.location = links[0].getAttribute('href');
                }
              } else {
                links[0].onclick();
              }
              break;
            } else {
              t = t.parentNode;
            }
          }
        });
      }

      $E.on(document.getElementsByTagName('a'), 'click', function(e) {
        var hostname = window.location.hostname;
        hostname = hostname.replace("www.","").toLowerCase();
        var href = this.href.toLowerCase();
        if (href.indexOf("http://") != -1 
          && href.indexOf(hostname) == -1) {
          $E.stopEvent(e);
          window.open(href);
        }
      });

      // $E.onAvailable('role-switch', function() {
      //   $D.setStyle('roles', 'display', 'none');
      //   $D.setStyle(this, 'cursor', 'pointer');
      //   $E.on(this, 'click', function(e) {
      //     $E.stopEvent(e);
      //     if($D.getStyle('roles', 'display') == 'none') {
      //       $D.setStyle('roles', 'display', 'block');
      //     } else {
      //       $D.setStyle('roles', 'display', 'none');
      //     }
      //   });
      // });

      $E.onAvailable('sponsors', function() {
        TADR.sponsors();
        // var fader = setInterval(TADR.sponsors, 5); 
      });

      $E.onAvailable('roles', function() {
        var images = this.getElementsByTagName('img');
        var roles = [];
        var clones = [];
        var container = $('portrait').parentNode;
        var txtContainer = $('role').parentNode;
        if(images.length > 0) {
          container.removeChild($('portrait'));
          txtContainer.removeChild($('role'));
        }
        for (var i = 0; i < images.length; i++) {

          var role = $D.getPreviousSibling(images[i].parentNode);
          var play = $D.getFirstChild($D.getPreviousSibling(role));

          var img = document.createElement('div');
          $D.setStyle(img, 'backgroundImage', 'url('+images[i].src+')');
          $D.setStyle(img, 'backgroundRepeat', 'no-repeat');
          $D.setStyle(img, 'backgroundPosition', 'center center');
          $D.setStyle(img, 'height', '200px');
          $D.setStyle(img, 'width', '150px');
          // var img = images[i].cloneNode(true);

          var str = role.innerHTML.slice(0, 50);
          var span = document.createElement('span');
          var txt = document.createTextNode(strpad('<<'+str, 56, '<', true));
          span.appendChild(txt);

          var str2 = play.innerHTML.slice(0, 50);
          var span2 = document.createElement('span');
          var txt2 = document.createTextNode(strpad('<<'+str2, 56, '<', true));
          span2.appendChild(txt2);

          var span3 = document.createElement('span');
          span3.appendChild(span);
          span3.appendChild(span2);

          if(i > 0) {
            clones.push(img);
            clones.push(span3);
          } else {
            $D.addClass(img, 'active');
            $D.addClass(span3, 'active');
          }
          roles.push({
            image: container.appendChild(img),
            txt: txtContainer.appendChild(span3)});
        }
        $D.setStyle(clones, 'opacity', 0);
        TADR.els = roles;
        var fader = setInterval(TADR.gallery, 5000); 
      });


      $E.onAvailable('videos', function() {
        var videos = this.getElementsByTagName('a');

          // var id = $D.generateId(media[i], 'media');
          // swf.embedSWF('/assets/player.swf?myURL='+media[i].href, id, '25', '25', '9.0.0');

          TADR.dialog = new YAHOO.widget.Dialog('video',
            { 
              draggable: false,
              height : "365px",
              width : "500px",
              fixedcenter : true,
              visible : false,
              modal: true,
              close: false,
              underlay: false,
              easing: YAHOO.util.Easing.easeBothStrong
            }
          );

          TADR.dialog.hideEvent.subscribe(function(){
            TADR.swf.removeSWF('video-player');
            $('video-player').parentNode.removeChild($('video-player'));
            TADR.dialog.setBody(body);
          });

          var kl = new YAHOO.util.KeyListener(document, { keys:27 },
            { fn: TADR.dialog.hide,
              scope: TADR.dialog,
              correctScope:true });

          var hd = document.createElement('span');
          hd.appendChild(document.createTextNode('close'));
          $D.setStyle(hd, 'cursor', 'pointer');
          $E.on(hd, 'click', function() { TADR.dialog.hide(); });
          TADR.dialog.setHeader(hd);
          TADR.dialog.cfg.queueProperty('keylisteners', kl);
          
          $E.removeListener(videos, 'click');

          $E.on(videos, 'click', function(e) {

            $E.stopEvent(e);

            var bd = document.createElement('div');
            var video = document.createElement('div');

            video.id = 'video-player';
            bd.appendChild(video);
            TADR.dialog.setBody(bd);
            TADR.dialog.render('wrapper');
            TADR.dialog.show();

            if($D.hasClass(this, 'vimeo')) {
              var id = this.href.slice(this.href.lastIndexOf('/')+1);
              var url = 'http://vimeo.com/moogaloop.swf?clip_id='+id+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1';
              TADR.swf.embedSWF(url, 'video-player', '491', '276', '9.0.0');
            } else {
              var url = 'http://www.youtube.com/v/'+this.href.substr(this.href.indexOf('=')+1);
              TADR.dialog.cfg.setProperty('width', '465px');
              TADR.dialog.cfg.setProperty('height', '424px');
              TADR.swf.embedSWF(url, 'video-player', '425', '344', '9.0.0');
            }
          });

      });


      $E.onAvailable('media', function() {
        var media = this.getElementsByTagName('a');
        for (var i = 0; i < media.length; i++) {
          var id = $D.generateId(media[i], 'media');
          swf.embedSWF('/assets/player.swf?myURL='+media[i].href, id, '30', '30', '9.0.0');
        }
      });

      $E.onAvailable('rundgang', function() { 
        $D.setStyle(this, 'cursor', 'pointer');
        $D.setStyle(this.getElementsByTagName('p'), 'display', 'none');
        this.getElementsByTagName('img')[0].src = '/assets/i/splash-haus.jpg';
        $E.on(this, 'click', function() {
          TADR.swf.embedSWF("/assets/content/rundgang/rundgang.2007.swf", 'rundgang', '560', '405', '9.0.0');
        });
      });

      $E.onAvailable('gallery', function() {
        var images = $D.getChildren(this);
        $E.onAvailable('gallery-controls', function() {
          if(images.length > 1) {
            $D.setStyle('gallery-controls', 'display', 'block');
            var status = $D.getFirstChild(this);
            status.innerHTML = '1/'+images.length;
            $E.on(this, 'click', function(e) {
              var el = $E.getTarget(e);
              var images = $D.getChildren('gallery');
              var pos = 0
              for (var i = 0; i < images.length; i++) {
                if(!$D.hasClass(images[i], 'hidden')) {
                  pos = i;
                  break;
                } 
              }
              if($D.hasClass(el, 'next')) {
                if(images[pos+1]) {
                  $D.addClass(images[pos], 'hidden');
                  $D.removeClass(images[pos+1], 'hidden');
                  status.innerHTML = (pos+2)+'/'+images.length;
                }
              } else {
                if(images[pos-1]) {
                  $D.addClass(images[pos], 'hidden');
                  $D.removeClass(images[pos-1], 'hidden');
                  status.innerHTML = pos+'/'+images.length;
                }
              }
            });
          }
        });
      });

    }
  }

}();

YAHOO.util.Event.onDOMReady(TADR.init);
