PLASMA = function() {

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

  var paper = {};

  // Animated Project Icons:
  var links = null;
  var points = new Array();
  var attributes = new Array();
  var anims = new Array();
  var paused = null;
  var x = 0;
  var y = 0;
  var distance = 20;
  var beep = 1;

  var detect = document.createElement('audio');

  function reset(status, motion, i) {
    if(paused !== i) {
      anims[i].from.animate();
    }
  }

  function restart(status, motion, i) {
    if(paused !== i) {
      anims[i].to.animate();
    }
  }

  function pause(e, i) {
    if (detect.play) {
      var audio = new Audio("/assets/sound/beep"+beep+".mp3");
      audio.play();
      if(beep == 1) {
        beep = 2; 
      } else {
        beep = 1; 
      }
    }
    paused = i;
    anims[i].to.stop();
    anims[i].from.stop();
  }

  function start(e, i) {
    paused = null;
    anims[i].from.animate();
  }


  return {
    init : function() {
      
      $E.onAvailable('content', function() {
        if (detect.play) {
          if($D.hasClass(this, 'team')) {
            var audio = new Audio("/assets/sound/people.mp3");
            audio.loop = true;
            audio.play();
          } else if($D.hasClass(this, 'plasma')) {
            var audio = new Audio("/assets/sound/plasma.mp3");
            audio.loop = true;
            audio.play();
          }
        }
      });

      $E.onAvailable('video', function() {
        $D.setStyle('player', 'display', 'block');
        $D.addClass(this.getElementsByTagName('li')[0], 'selected');
        var imgs = this.getElementsByTagName('img');
        var links = this.getElementsByTagName('a');
        var videos = [];

        for (var i = 0; i < imgs.length; i++) {
          if(i == 0) {
            var img = new Image();
            img.src = imgs[i].src.slice(0, -5)+'2.jpg';
            $('player').appendChild(img);
          }
          videos.push(imgs[i].parentNode.href);
        }
        var fp = flowplayer('player', {
            height: 450,
            scaling: 'orig',
            src: '/assets/i/flowplayer-3.1.3.swf',
            width: 600
          }, 
          { clip: {
            autoBuffering: true
          },
          playlist: videos });

        $E.on(links, 'click', function(e) {
          $E.stopEvent(e);
          var lis = $('video').getElementsByTagName('li');
          var parent = $D.getAncestorByTagName(this, 'li');
          $D.removeClass(lis, 'selected');
          for (var i = 0; i < lis.length; i++) {
            if(lis[i] == parent) {
              $D.addClass(parent, 'selected');
              fp.play(i);
            }
          }
        });
      });

      $E.onAvailable('secondary', function() {
        var links = this.getElementsByTagName('li');
        $D.setStyle(links, 'display', 'none');

        // Info link is always there
        $D.setStyle(links[0], 'display', 'block');

        // Video is the last link - always displayed if
        // it exists
        if(links[5]) {
          $D.setStyle(links[5], 'display', 'block');
        }

        if($('sample')) {
          $D.setStyle(links[1], 'display', 'block');
        }
        if($('press')) {
          $D.setStyle(links[2], 'display', 'block');
        }
        if($('technical-rider')) {
          $D.setStyle(links[3], 'display', 'block');
        }
        if($('press-materials')) {
          $D.setStyle(links[4], 'display', 'block');
        }
        $E.on(links, 'click', function(e) {
          $E.stopEvent(e);
          var layers = ['information', 'sample', 'press', 'assets'];

          if($D.hasClass(this, 'material')) {
            window.location = $('technical-rider').href;
          } else if($D.hasClass(this, 'video')) {
            window.location = $D.getFirstChild(this).href;
          } else if($D.hasClass(this, 'information') && $('video')) {
            window.location = $D.getFirstChild(this).href;
          }

          $D.setStyle(layers, 'display', 'none');
          $D.removeClass(links, 'selected');
          $D.addClass(this, 'selected');

          if($D.hasClass(this, 'press')) {
            $D.setStyle($('press'), 'display', 'block');
          }
          if($D.hasClass(this, 'sample')) {
            $D.setStyle($('sample'), 'display', 'block');
          }
          if($D.hasClass(this, 'information')) {
            $D.setStyle($('information'), 'display', 'block');
          }

        });
      });

      $E.onAvailable('gallery', function() {
        $D.addClass(document.body, 'active');
        $E.on(['next', 'previous'], 'click', function(e) {
          $E.stopEvent(e);
          var ul = $D.getLastChild(this.parentNode.parentNode.parentNode);
          var images = ul.getElementsByTagName('li');
          for (var i = 0; i < images.length; i++) {
            if($D.hasClass(images[i], 'focus')) {
              if(this.id == 'next' && images[i+1]) {
                $D.removeClass(images[i], 'focus');
                $D.addClass(images[i+1], 'focus');
              } else if(this.id == 'next' && !images[i+1]) {
                $D.removeClass(images[i], 'focus');
                $D.addClass(images[0], 'focus');
              } else if(this.id == 'previous' && images[i-1]) {
                $D.removeClass(images[i], 'focus');
                $D.addClass(images[i-1], 'focus');
              } else if(this.id == 'previous' && !images[i-1]) {
                $D.removeClass(images[i], 'focus');
                $D.addClass(images[images.length-1], 'focus');
              }
              break;
            }
          }
        });
      });

      $E.onAvailable('home', function() {

        $D.addClass(document.body, 'active');
        $E.on(['contact', 'dates', 'sponsors'], 'click', function(e) {
          $E.stopEvent(e);
          if($D.getStyle(this.id+'-box', 'display') == 'none') {
            $D.setStyle(this.id+'-box', 'display', 'block');
          } else {
            $D.setStyle(this.id+'-box', 'display', 'none');
          }
        });

        if(YAHOO.util.Cookie.get('splash')) {
          if (detect.play) {
            var loop = new Audio("/assets/sound/home.mp3");
            loop.loop = true;
            loop.play();
          }
          $D.setStyle('home', 'display', 'block');
          return;
        }

        $D.setStyle('home', 'display', 'none');

        var r = $D.getClientRegion();
        var paper = Raphael(0, 0, r.width, r.height);
        var bg = paper.rect(0, 0, r.width, r.height).attr('fill', '#000').attr('stroke-width', '0');
        var spiral = paper.image("/assets/i/spiral.png", ((r.width/2)-(r.width/2)), ((r.height/2)-(r.width/2)), r.width, r.width);
        var head = paper.image("/assets/i/head.home.png", ((r.width/2)-1), ((r.height/2)-1), 1, 1).hide();

        if (detect.play) {
          var audio = new Audio("/assets/sound/intro.mp3");
          audio.play();
        }

        function start() {
          if (detect.play) {
            audio.pause();
          }
          head.show();
          head.toFront();
          spiral.animate({opacity: 0}, 200).remove();
          head.animate({
            width: 419,
            height: 530,
            x: (r.width/2)-204,
            y: (r.height/2)-300}, 1000);
          bg.animateWith(head, {opacity: 0}, 900, function() {
              $D.setStyle($('home'), 'opacity', 0);
              $D.setStyle($('home'), 'display', 'block');
              var a = new YAHOO.util.Anim($('home'), {opacity: {to: 1}}, .5);
              a.onComplete.subscribe(function() {
                paper.clear();
              });
              a.animate();
            });
          YAHOO.util.Cookie.set('splash', 1);
        }

        spiral.animate({rotation: -3600}, 16200, start);

        spiral.mouseover(function(event) {
          this.attr({'opacity': .5});
        });
        spiral.mouseout(function(event) {
          this.attr({'opacity': 1});
        });

        spiral.click(start);

        $E.on(window, 'resize', function(e) {
          var r = $D.getClientRegion();
          this.setSize(r.width, r.height);
          spiral.attr({
              width: r.width,
              height: r.width,
              x: ((r.width/2)-(r.width/2)), 
              y: ((r.height/2)-(r.width/2))})
          bg.attr({
              width: r.width,
              height: r.width});
          head.attr({
              x: ((r.width/2)-(head.attrs.width/2)), 
              y: ((r.height/2)-(head.attrs.height/2))})
        }, paper, true);

        // $E.on(spiral.node, 'mouseover', function(e) {
        //   this.attr('fill-opacity', .5);
        // }, spiral, 'true');

      });

      $E.onAvailable('projects', function() {

        $D.setStyle(this, 'overflow', 'visible');
        links = this.getElementsByTagName('li');

        for(var i = 0; i < links.length; i++) {

          x = $D.getX(links[i]);
          y = $D.getY(links[i]);
          if(i % 2 == 0) {
            points[i] = {
              start: [x, y],
              via: [[x - Math.floor(Math.random()*distance+1), y + Math.floor(Math.random()*distance+1), x + Math.floor(Math.random()*distance+1), y - Math.floor(Math.random()*distance+1)]],
              end: [x + distance, y + distance]
            };
          } else {
            points[i] = {
              start: [x, y],
              via: [[x + Math.floor(Math.random()*distance+1), y - Math.floor(Math.random()*distance+1), x - Math.floor(Math.random()*distance+1), y + Math.floor(Math.random()*distance+1)]],
              end: [x - distance, y - distance]
            };
          }

          attributes[i] = { 
            duration: Math.floor(Math.random()*distance+1),
            to: {
              points: { 
                to: points[i].end, 
                control: points[i].via}
            },
            from: {
              points: { 
                to: points[i].start, 
                control: points[i].via}
            }
          };

          anims[i] = {
            to: new YAHOO.util.Motion(links[i], attributes[i].to, attributes[i].duration),
            from: new YAHOO.util.Motion(links[i], attributes[i].from, attributes[i].duration)
          };
          anims[i].to.onComplete.subscribe(reset, i)
          anims[i].from.onComplete.subscribe(restart, i)
          anims[i].to.animate();
          $E.on(links[i], 'mouseover', pause, i);
          $E.on(links[i], 'mouseout', start, i);

        }
        
      });

    }
  }


}();

YAHOO.util.Event.onAvailable('home', function(e) { YAHOO.util.Dom.setStyle(this, 'display', 'none'); });
YAHOO.util.Event.onAvailable('dates-box', function(e) { YAHOO.util.Dom.setStyle(this, 'display', 'none'); });
YAHOO.util.Event.onAvailable('contact-box', function(e) { YAHOO.util.Dom.setStyle(this, 'display', 'none'); });
YAHOO.util.Event.onAvailable('sponsors-box', function(e) { YAHOO.util.Dom.setStyle(this, 'display', 'none'); });
YAHOO.util.Event.onDOMReady(PLASMA.init);
