/*
/        BAGGER 
/
/ Code: the_JinX / deFEEST
/  Music: Cinder / deFEEST
/
*/

var audio = document.createElement('audio'),
sourceMp3 = document.createElement('source');
sourceMp3.setAttribute('src', 'aud/episch-slecht.mp3');
audio.appendChild(sourceMp3);

Dancer.setOptions({
    flashJS  : 'js/lib/soundmanager2.js',
    flashSWF : 'js/lib/soundmanager2.swf'
});

var dancer = new Dancer();

Dancer.isSupported() || loaded();
  !dancer.isLoaded() ? dancer.bind( 'loaded', loaded ) : loaded();

function loaded () {
  var
  loading = document.getElementById( 'loading' ),
  anchor  = document.createElement('A'),
  supported = Dancer.isSupported(),
  p;

  anchor.appendChild( document.createTextNode( supported ? 'Spelen!' : 'Close' ) );
  anchor.setAttribute( 'href', '#' );
  loading.innerHTML = '';
  loading.appendChild( anchor );

  if ( !supported ) {
	  p = document.createElement('P');
	  p.appendChild( document.createTextNode( 'Your browser does not currently support either Web Audio API or Audio Data API. The audio may play, but the visualizers will not move to the music; check out the latest Chrome or Firefox browsers!' ) );
	  loading.appendChild( p );
  }

  anchor.addEventListener( 'click', function () {
		  dancer.play();
		  document.getElementById('loading').style.display = 'none';
  }, false );

 }

var deFEEST = document.getElementById("deFEEST");
var back = document.getElementById('back');
var lean = document.getElementById('lean');
var creds = document.getElementById('creds');
var titles = document.getElementById('titles');
var lampies = document.getElementById('lampies');
var text = document.getElementById('text');
var meuk = document.getElementById('meuk');
var weird = document.getElementById('weird');

var kick = dancer.createKick({
	onKick: function (mag) {
		console.log(dancer.getTime()+' '+mag);
		deFEEST.style.color="yellow";
		if (mag > .5) {
			titles.style.display = 'none';
		}
	},
	offKick: function (mag) {
		deFEEST.style.color="blue";
		if (mag < .1) {
			titles.style.display = 'block';
		}
	}
});

kick.on();

dancer.onceAt(0, function() {
	titles.style.display = 'block';
	deFEEST.className = 'show';
}).between(5, 6.7, function() {
	deFEEST.style.marginTop = 100+(this.getFrequency(400)*2000)+'px';
}).onceAt(9, function() {
	back.className = 'wat';
}).between(6.7, 60, function() {
	deFEEST.style.marginTop = 100+(this.getFrequency(400)*10000)+'px';
}).onceAt(2, function() {
	meuk.style.display = 'block';
}).onceAt(8, function() {
	meuk.style.display = 'none';
	weird.style.display = 'none';
}).between(2, 8, function() {
	weird.style.opacity = this.getFrequency(400)*100;
	meuk.style.opacity = this.getFrequency(200)*100;
}).onceAt(22, function() {
	titles.style.display = 'none';
	creds.style.display = 'block';
}).between(20, 100, function() {
	creds.style.opacity = this.getFrequency(200)*100;
	creds.style.left = 400+(this.getFrequency(300)*10000)+'px';
}).onceAt(32, function() {
	document.getElementById('container').style.display = 'block';
}).onceAt(64, function() {
	lean.style.display = 'block';
	back.className = 'up';
}).onceAt(43, function() {
	lampies.style.display = 'none';
}).onceAt(42, function() {
	creds.style.display = 'none';
}).onceAt(74, function() {
	back.className = '';
}).onceAt(94, function() {
	weird.style.display = 'none';
}).onceAt(84, function() {
	weird.style.display = 'block';
	back.className = 'wat';
}).onceAt(42, function() {
	text.style.display = 'block';
}).onceAt(60, function() {
	weird.style.display = 'block';
	text.style.display = 'none';
	lampies.style.display = 'none';
}).between(42, 60, function() {
	text.style.left = 600-(this.getFrequency(350)*3000)+'px';
}).between(84, 94, function() {
	weird.style.opacity = this.getFrequency(400)*100;
}).between(64, 90, function() {
	titles.style.paddingTop = this.getFrequency(500)*1000;
	lean.style.opacity = this.getFrequency(300)*100;
}).between(22, 43, function() {
	lampies.style.opacity = this.getFrequency(200)*30;
}).onceAt(72, function() {
	document.getElementById('container').style.display = 'none';
}).onceAt(90, function() {
	weird.style.display = 'none';
	lean.style.display = 'none';
	back.className = 'up';
}).onceAt(108, function() {
	deFEEST.style.display = 'none';
	document.getElementById('thx').style.display = 'block';
}).after(108, function() {
	document.getElementById("thx").style.marginTop = 50+(this.getFrequency(400)*10000)+'px';
	document.getElementById("thx").style.paddingLeft = this.getFrequency(200, 400)*10000+'px';
}).onceAt(108, function() {
	kick.off();
}).onceAt(112, function() {
	titles.style.display = 'none';
}).load(audio); // And finally, lets pass in our Audio element to load

