
HELLERAU = function() {

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

  var heights = {};
  var dialog = null;
  var player = null;

  /**
  * Movie player controller
  */
  Player = function(el, url) {

    /**
    * The DOM element container
    */
    this.el = {};

    /**
    * The <a> els that append <object> or <embed> when the player loads
    */
    this.movies = [];

    /**
    * The <img> el that the <object>/<embed> replaces
    */
    this.image = {};

    /**
    * The Flowplayer instance
    */
    this.player = {};

    /**
    * Flowplayer Flash config variables
    */
    this.flash = {
      height: 344,
      src: '/assets/i/flowplayer-3.1.3.swf',
      width: 425
    };

    /**
    * Flowplayer JS config variables
    */
    this.config = {
      canvas: { 
        backgroundColor: "#e31e1e",
        backgroundGradient: 'none',
        backgroundColor: "#000"
      },
      playlist: [],
      clip: { 
        scaling: 'orig',
        onStart: function (clip) { }
      },
      plugins: {
        controls: {
          url: '/assets/i/flowplayer.controls-3.1.3.swf',
          sliderGradient: 'none',
          progressColor: '#e31e1e',
          tooltipTextColor: '#D00000',
          volumeSliderGradient: 'none',
          durationColor: '#ffffff',
          buttonColor: '#000000',
          tooltipColor: '#C9C9C9',
          bufferColor: '#C78F8E',
          borderRadius: '0',
          progressGradient: 'none',
          timeColor: '#ffffff',
          backgroundGradient: 'none',
          timeBgColor: '#555555',
          sliderColor: '#555555',
          bufferGradient: 'none',
          backgroundColor: '#ffffff',
          buttonOverColor: '#e31e1e',
          volumeSliderColor: '#D00000',
          height: 24,
          opacity: 1.0
        }
      }
    };

    if (el) {
      this.init(el, url);
    }

  };

  Player.prototype = {

    /**
    * Destroy player instance
    */
    close: function() {
      this.player.unload();
      var objs = this.el.getElementsByTagName('object');
      for (var i = 0; i < objs.length; i++) {
        objs[i].parentNode.removeChild(objs[i]);
      }
      var embeds = this.el.getElementsByTagName('embed');
      for (var i = 0; i < embeds.length; i++) {
        embeds[i].parentNode.removeChild(objs[i]);
      }
    },

    /**
    * Initialize and setup event handlers
    * @param  HTMLElement  The container for our SWF player
    * @param  str          The video URL
    */
    init: function(el, url) {
      this.el = el;
      this.config.playlist.push({url: url});
      this.player = flowplayer(this.el, this.flash, this.config);
      this.player.play(0);
    } // end Player.init()

  };

  return {
    dialog: dialog,
    init: function() {

      Cufon.replace($D.getElementsByClassName('cufon'));

      var players = $D.getElementsByClassName('player', 'div', 'wrapper')
      if(players.length > 0) {
        for(var i = 0; i < players.length; i++) {
          var img = $D.getFirstChild(players[i]);
          var p = flowplayer(players[i], 
            { 
              height: 360,
              scaling: 'orig',
              src: '/assets/i/flowplayer-3.1.3.swf',
              width: 500
            }, 
            { 
              playlist: [img.src.slice(0, -3)+'flv'] 
            }
          );
        }
      }

      $E.onAvailable('history-back', function() {
        $E.on(this, 'click', function(e) {
          $E.stopEvent(e);
          window.history.back();
        });
      });

      $E.onAvailable('qt-warning', function() {
        if(isQTInstalled()) {
          $D.setStyle(this, 'display', 'none');
        }
      });

      $E.onAvailable('house-1', function() {
        if(isQTInstalled()) {
          var house1 = new QTObject(this.src.slice(0, -3)+'mov', this.id, "500", "359");
          var id = $D.generateId(this.parentNode);
          house1.write(id);
        }
      });

      $E.onAvailable('house-2', function() {
        if(isQTInstalled()) {
          var house1 = new QTObject(this.src.slice(0, -3)+'mov', this.id, "500", "359");
          var id = $D.generateId(this.parentNode);
          house1.write(id);
        }
      });

      $E.onAvailable('house-3', function() {
        if(isQTInstalled()) {
          var house1 = new QTObject(this.src.slice(0, -3)+'mov', this.id, "500", "359");
          var id = $D.generateId(this.parentNode);
          house1.write(id);
        }
      });

      $E.onAvailable('house-4', function() {
        var house1 = new QTObject(this.src.slice(0, -3)+'mov', this.id, "500", "359");
        var id = $D.generateId(this.parentNode);
        house1.write(id);
      });

      var galleries = $D.getElementsByClassName('gallery', 'ul', 'wrapper');

      if(galleries.length > 0) {

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

        HELLERAU.dialog.hideEvent.subscribe(function(){
          if($('video-player') && HELLERAU.player) {
            HELLERAU.player.close();
          } if($('video-player')) {
            HELLERAU.swf.removeSWF('video-player');
            $('video-player').parentNode.removeChild($('video-player'));
          }
          HELLERAU.dialog.setBody('');
          HELLERAU.dialog.setFooter('');
        });

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

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

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

          $E.stopEvent(e);
          var t = $E.getTarget(e);
          if(t.tagName.toLowerCase() != 'a') {
            t = $D.getAncestorByTagName(t, 'a');
          }

          var ft = document.createElement('div');

          if(t.href.indexOf('youtube') !== -1) {
            var bd = document.createElement('div');
            var video = document.createElement('div');
            video.id = 'video-player';
            bd.appendChild(video);
          } else if(t.href.indexOf('.flv') !== -1 || t.href.indexOf('.mp4') !== -1) {
            var bd = document.createElement('div');
            var video = document.createElement('div');
            video.id = 'video-player';
            bd.appendChild(video);
          } else {
            var bd = document.createElement('div');
            var img = new Image();
            img.src = t.href;
            $E.on(img, 'load', function(e) {
              // HELLERAU.dialog.cfg.setProperty('width', (this.width+40)+'px');
              // HELLERAU.dialog.cfg.setProperty('height', (this.height+80)+'px');
              // HELLERAU.dialog.center();
            });
            if(t.title) {
              var chunks = t.title.split('--');
              var caption = document.createElement('span');
              caption.appendChild(document.createTextNode(chunks[0]));
              $D.addClass(caption, 'caption');
              var copyright = document.createElement('span');
              copyright.appendChild(document.createTextNode(chunks[1]));
              ft.appendChild(caption);
              ft.appendChild(copyright);
            }
            bd.appendChild(img);
          }

          HELLERAU.dialog.setBody(bd);
          HELLERAU.dialog.setFooter(ft);
          HELLERAU.dialog.render('wrapper');
          HELLERAU.dialog.show();

          if(t.href.indexOf('youtube') !== -1) {
            var url = 'http://www.youtube.com/v/'+t.href.substr(t.href.indexOf('=')+1);
            HELLERAU.dialog.cfg.setProperty('width', '465px');
            HELLERAU.dialog.cfg.setProperty('height', '414px');
            HELLERAU.swf.embedSWF(url, "video-player", "425", "344", "9.0.0");
            HELLERAU.dialog.center();
          } else if(t.href.indexOf('.flv') !== -1 || t.href.indexOf('.mp4') !== -1) {
            var url = t.href;
            HELLERAU.dialog.cfg.setProperty('width', '465px');
            HELLERAU.dialog.cfg.setProperty('height', '414px');
            // HELLERAU.swf.embedSWF(url, "video-player", "425", "344", "9.0.0");
            HELLERAU.player = new Player($('video-player'), url);
            HELLERAU.dialog.center();
          }

        });
      }

    }
  }

}();

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