// JUN 02 || EXPERIMENT ENGINE

var eC = null;
var experimentStopped=0;

var gradient = ['9B','A5','AF','B9','C3','CD','D7','E1','EB','F5','FF'];

function Bubble() {
  this.left = 0;
  this.top = 0;
  this.o = document.createElement('div');
  this.o.className = 'bubble';
  this.o.innerHTML = 'o';
  this.init();
  eC.appendChild(this.o);  
  return this;
}

Bubble.prototype.getLeft = function(newVal) { return this.o.style.left; }
Bubble.prototype.setLeft = function(newVal) { this.o.style.left = newVal; }

Bubble.prototype.getTop = function(newVal) { return this.o.style.top; }

Bubble.prototype.init = function() {
  this.setTop(8-(Math.floor(Math.random()*8)));
  this.sineValue = Math.random()*6;
  this.gradIndex = 0;
  this.fadePoint = (48 + parseInt(Math.floor(Math.random()*48))*-1);
  this.setLeft(32+(Math.sin(this.sineValue)*10));
}

Bubble.prototype.setTop = function(newVal) { 
  this.o.style.top = newVal;
  if (newVal < -this.fadePoint) {
    this.gradIndex += (Math.random()>0.75?1:0);
    if (this.gradIndex < gradient.length) {
      this.setColor(this.gradIndex);
    }
    else if (this.gradIndex >= (gradient.length+2) && !experimentStopped) {
      this.gradIndex=0;
      this.setColor(this.gradIndex);
      this.setTop(8-Math.floor(Math.random()*8));
    }
    else if (experimentStopped)
     this.o.style.visibility = 'hidden';
  }
}

Bubble.prototype.setColor = function(i) {
  this.o.style.color = '#' + gradient[i] + gradient[i] + gradient[i];
}

Bubble.prototype.doSine = function() {
  this.sineValue += (Math.floor(Math.random()*10)/100);
  if (this.sineValue > 6.2)
    this.sineValue=0;
  this.setLeft(32+(Math.sin(this.sineValue)*12));
}

Bubble.prototype.isVisible = function() {
  return (this.o.style.visibility == 'hidden')?0:1;
}

bubbles = [];

function startExperiment() {
  eC = document.getElementById('experimentContainer');
  resizeHandler();

  eC.style.visibility = 'visible';

  for (i=0; i<8; i++) {
    setTimeout("bubbles["+i+"] = new Bubble()",250*(i+1));
  }
}

var bubbleAllowed=1;

function stopExperiment() {
  if (isSix()) {
    document.body.style.cursor='wait';
    exImg.style.cursor='wait';
  }
  experimentStopped=1;
}

function bubbleAni() {
  var allHidden = 1;
  for (var i=0; i<bubbles.length; i++) {
    if (Math.random()>0.5)
      bubbles[i].setTop(parseInt(bubbles[i].getTop())-1);
    bubbles[i].doSine();
    if (bubbles[i].isVisible())
      allHidden=0;
  }
  if (bubbleAllowed && !experimentStopped)
    setTimeout("bubbleAni()",20);
  else if (experimentStopped && !allHidden)
    setTimeout("bubbleAni()",20);
  else {
    exImg.style.filter = 'blendTrans(duration=1)';
    exImg.filters.blendTrans.apply();
    exImg.src = 'image/experiment_bw.gif';
    exImg.filters.blendTrans.play();
    slideIn('features','setTimeout("init()",1000)',300,200);
  }
}

function getRndDelay() {
  return 500+parseInt(Math.random()*500);
}

var slideObj = null;
var slideAction = null;
var slideIndex = 0;
var slideWidth = 0;
var slideData = [1,2,4,6,9,12,16,20,25,30,36,42,49,56,62,68,73,78,82,86,89,92,94,96,97,98,99,100];

function slideIn(sTarget, nextAction, sWidth, sHeight) {
  if (sTarget && nextAction && sWidth && sHeight) {
    slideObj = document.getElementById(sTarget);
    slideAction = nextAction;
    slideWidth = sWidth;
    slideHeight = sHeight;
    slideIndex = 0;
  }
  slideObj.style.clip = 'rect(0px '+slideWidth*slideData[slideIndex]/100+'px ' + slideHeight + 'px 0px)';
  slideIndex++;
  if (slideIndex<slideData.length)
    setTimeout("slideIn()",20);
  else
    eval(slideAction);
}

window.onload = new Function("startExperiment();bubbleAni()");

var fullSize = 0;
var aWidth = 0;
var aHeight = 0;
var asWidth = 0;
var asHeight = 0;
var treeObjects = [];
var activeTree=-1;
var d=0;
var dir=1;
var aniRunning=0;
var percentage=0;
var origX=0;
var origY=0;
var trees = 0;
var offX=0;
var offY=0;

var mD=0;

var mOffY=0;
var mOldY=0;

var preImg1 = new Array();
var preImg2 = new Array();

for (var i=0; i<8; i++) {
  preImg1[i] = new Image();
  preImg1[i].src = 'image/ani/a'+i+'.gif';
  preImg2[i] = new Image();
  preImg2[i].src = 'image/ani/s'+i+'.gif';
}

function mmH() {
  var percentage = (event.clientY-(fullSize/2))/(fullSize-((arrow.height+arrowShadow.height)/2));
  percentage = parseInt(percentage*100);
  if (percentage>100) percentage = 100; else if (percentage <= 2) percentage = 2;

  oldY = arrow.style.top;

  arrow.style.left = event.clientX - (arrow.width/2);
  arrow.style.top = event.clientY - (arrow.height/2);
  arrowShadow.style.left = parseInt(arrow.style.left) - (8*0.01*percentage);
  arrowShadow.style.top = parseInt(arrow.style.top) + parseInt(arrow.height) - (32*0.01*percentage);
  arrow.width = aWidth * percentage/100;
  arrow.height = aHeight * percentage/100;
  arrowShadow.width = asWidth * percentage/100;
  arrowShadow.height = asHeight * percentage/100;
  if (activeTree>=0) {
    treeObjects[activeTree].setLeft(parseInt(arrowShadow.style.left)+offX);
    treeObjects[activeTree].setTop(parseInt(arrowShadow.style.top));
    treeObjects[activeTree].setScale();
  }
  origX = arrow.style.left;
  origY = arrow.style.top;

  if (activeTree>=0) {

    if (treeObjects[activeTree].getType() == 'sound') {
      // calculate position on screen, audio etc.
      var pan = parseInt(Math.ceil(parseInt(treeObjects[activeTree].getLeft()))/(document.body.clientWidth-100)*100);
      pan = -100 + (pan*2);
      if (pan<-100) pan = -100;
      if (pan>100) pan = 100;
  
      var tmpPan = String(Math.abs(pan));
      var tmp = '';

      for (var i=0; i<tmpPan.length-1; i++) { // parse out flash label lower
        tmp = tmp + tmpPan.charAt(i);
      }  
      tmp = tmp + '0';
 
      playFrame(treeObjects[activeTree].id,'p'+(pan<0?'_':'')+tmp);

      tmp = parseInt(treeObjects[activeTree].getScale());
      if (tmp<5) tmp=0;
      if (tmp>100) tmp=100;

      if (tmp%5==0) {
        playFrame(treeObjects[activeTree].id,'v'+parseInt(treeObjects[activeTree].getScale()));
      }
    }
  }
  return false;
}

function mdH() {
  mD=1;
  mOffY = event.clientY;
  dir=1;
  if(!aniRunning)ani();
  activeTree=noOverlap(arrow.style.left,arrow.style.top,arrow.width,arrow.height);
  return false;
}

function ani() {
/*
  if (!aniRunning) aniRunning=1;
  if (d<0) d=0; else if (d>9) d=9;
  arrow.src='image/ani/a'+d+'.gif';
  arrowShadow.src = 'image/ani/s'+d+'.gif';
  d += dir;
  if ((d==0 && dir==-1) || (d==9 && dir==1)) aniRunning=0;

  if ((d<9 && dir==1) || (d>0 && dir==-1)) {
    setTimeout("ani()",20);
  }

  else if (d==0 && dir==-1) {
    setTimeout('arrow.src="image/arrow_white.gif";arrowShadow.src="image/arrow_shadow.gif"',20);
  }

  else if (d==9 && dir==1) {
    // arrowAni();
  }
*/
}

var treeMax = 32;
var treeCounter = 0;

function isSix() {
  var n = navigator.appVersion;
  var v1 = n.indexOf('MSIE');
  var fuc = n.substr(v1,n.length-v1).indexOf(';');
  fuc = n.substr(v1+4,fuc-4);
  return (parseFloat(fuc) >= 6?1:0);
}

function init() {
  if (isSix()) {
    document.body.style.cursor="url('blank.cur')";
    exImg.style.cursor='hand';
  }
  fullSize = document.body.clientHeight/1.5;
  aWidth = arrow.width/2;
  aHeight = arrow.height/2;
  asWidth = arrowShadow.width/2;
  asHeight = arrowShadow.height/2;
  document.onmousedown=mdH;
  document.onmousemove=mmH;
  document.onmouseup=new Function("mD=0;dir=-1;d--;dropTree();if(!aniRunning)ani();");

  addTree();

/*
  for (var i=0; i<32; i++) {
    setTimeout("treeObjects[treeObjects.length] = new Tree('','tree'+treeObjects.length)",parseInt(Math.random()*3000));
  }
*/

}

function addTree() {
  treeObjects[treeObjects.length] = new Tree('','tree'+treeObjects.length);
  treeObjects[treeObjects.length] = new Tree('','tree'+treeObjects.length);
  if (treeObjects.length<32) {
    setTimeout("addTree()",parseInt(Math.random()*500));
  }
  else {
    treeObjects[treeObjects.length] = new Tree('sound','sPerc');
    treeObjects[treeObjects.length] = new Tree('sound','sHighEnd');
    treeObjects[treeObjects.length] = new Tree('sound','sBass');
    playSound('sBass');
    playSound('sHighEnd');
    playSound('sPerc');
  }
}
    

function cancelEvent() {
  window.event.cancelBubble=true;
}

function noOverlap(newX, newY, width, height) {
  var isOK=-1;
  if (!treeObjects.length) {
    return isOK;
  }
  newX = parseInt(newX);
  newY = parseInt(newY);
  for (var i=0; i<treeObjects.length; i++) {
    offX = Math.abs(newX-treeObjects[i].getLeft());
    offY = Math.abs(newY-treeObjects[i].getTop());
    if (offX<width && offY<height) {
      isOK=i;
      break;
    }
  }
  return isOK;
}

function Tree(oType,oID,noDelay) { // Tree constructor
  this.left = 0;
  this.top = 0; 
  var newX = 0;
  var newY = 0;
  var isOK = 1;
  this.o = document.createElement('img');
  this.o.style.position = 'absolute';
  this.id = oID;
  this.o.id = oID;

  if (!oType) {
    this.type = 'tree';
    this.o.src = 'image/tree.gif';
    this.aWidth = 60;
    this.aHeight = 62;
  }
  else {
    this.type = oType;
    this.o.src = 'image/sound.gif';
    this.aWidth = 64;
    this.aHeight = 63;
  }

  do { // loop while overlap condition exists
    newX = parseInt(Math.random()*(document.body.clientWidth-this.aWidth));
    newY = document.body.clientHeight - this.aHeight - (parseInt(Math.random()*((document.body.clientHeight*0.54))));
    with (this.o.style) {
      left = newX;
      top = newY;
    }
    this.left = newX;
    this.top = newY;
    this.setScale();
    isOK = (noOverlap(newX, newY, this.o.width, this.o.height));
  } while (isOK!=-1);

  trees++;

  arrow.style.zIndex = trees+1; // stack above all trees
  this.setLeft(this.left);
  this.setTop(this.top);
  document.body.appendChild(this.o);

  this.o.style.visibility = 'visible';

  return this;
}

Tree.prototype.setLeft = function(newLeft) {
  this.o.style.left = parseInt(newLeft);
}

Tree.prototype.setTop = function(newTop) {
  this.o.style.top = parseInt(newTop);
}

Tree.prototype.setWidth = function(newWidth) {
  this.o.width = parseInt(newWidth);
}

Tree.prototype.setHeight = function(newHeight) {
  this.o.height = parseInt(newHeight);
}

Tree.prototype.getLeft = function() {
  return parseInt(this.o.style.left);
}

Tree.prototype.getTop = function() {
  return parseInt(this.o.style.top);
}

Tree.prototype.getType = function() {
  return this.type;
}

Tree.prototype.getScale = function() {
  return ((parseInt(this.o.style.top)-((fullSize)/2))/(fullSize-75-(this.o.height/2)))*100;
}

Tree.prototype.setScale = function() {
  this.scale = ((parseInt(this.o.style.top)-((fullSize)/2))/(fullSize-75-(this.o.height/2)))*100;
  if (this.scale >= 100) this.scale = 100; else if (this.scale < 2) this.scale = 2;

  with (this.o) {
    width = this.aWidth * this.scale/100;
    height = this.aHeight * this.scale/100;
  }
}

function dropTree() {
  activeTree=-1;
}

function playSound(audioTarget) {
  if (audio.ReadyState == 4) {
    audio.TGotoLabel("/" + audioTarget, "start")
    audio.TPlay("/" + audioTarget)
  }
}

function playFrame(audioTarget, label) {
  if (audio.ReadyState == 4) {
    audio.TGotoLabel("/"+audioTarget,label);
  }
}

function resizeHandler() {
  experimentContainer.style.left = parseInt(document.body.clientWidth/2)-130;
  experimentContainer.style.top = parseInt(document.body.clientHeight*0.65)-48;
}

window.onresize = resizeHandler;
window.onerror = Function("return false");

