/*
**  debian-balloon.js - Show floating ballons on a web page.
**  Copyright (C) 2010  Aurélio A. Heckert
**
**  This program is free software: you can redistribute it and/or modify
**  it under the terms of the GNU Affero General Public License as
**  published by the Free Software Foundation, either version 3 of the
**  License, or (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU Affero General Public License for more details.
**
**  You should have received a copy of the GNU Affero General Public License
**  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function DebianBalloon() {
  this.imgs = [];
  for ( i=1; i<=9; i++ ) {
    this.imgs[i-1] = "http://valessiobrito.info/projetos/debian/others/dad/b"+i+".png"
  }
  this.balloons = [];
  this.createCountdown = 0;
  if ( self.innerWidth ) {
    this.bodyWidth  = self.innerWidth;
    this.bodyHeight = self.innerHeight;
  } else {
    this.bodyWidth  = document.body.clientWidth;
    this.bodyHeight = document.body.clientHeight;
  }
  this.bodyLimtHeight = this.bodyHeight;
  this.tic();
}

DebianBalloon.prototype.tic = function() {
  try {
    this.moveAll();
    if ( this.createCountdown == 0 ) this.createBallon();
    this.createCountdown--;
  } catch(e) {
    /* alert("ERROR:\n"+e); */
  }
  var ballooner = this;
  setTimeout( function(){ ballooner.tic() }, 66 );
}

DebianBalloon.prototype.createBallon = function() {
  var b = document.createElement("img");
  b.style.position = "absolute";
  b.wind = 0;
  b.windDisplacementCount = 0;
  b.posX = Math.random() * this.bodyWidth;
  b.style.left = Math.round( b.posX ) +"px";
  b.style.top  = this.bodyLimtHeight +"px";
  b.style.opacity = 0;
  var rnd = Math.round( Math.random() * this.imgs.length );
  if ( rnd == this.imgs.length ) rnd = 0;
  b.src = this.imgs[rnd];
  document.body.appendChild( b );
  this.balloons[this.balloons.length] = b;
  this.createCountdown = 100;
}

DebianBalloon.prototype.moveAll = function() {
  for ( var b,i=0; b=this.balloons[i]; i++ ) {
    this.applyWind(b);
    var y = parseInt(b.style.top);
    var h = parseInt(b.height);
    var a = parseFloat(b.style.opacity);
    if ( y+h <= 0 ) {
      this.remove(i);
    }
    if ( a < 1 ) {
      b.style.opacity = a + 0.02;
    }
    if ( this.bodyHeight < y+h ) {
      y = this.bodyHeight - h;
      this.bodyLimtHeight = y;
    }
    y--;
    b.style.left = parseInt(b.posX)+"px";
    b.style.top  = y+"px";
  }
}

DebianBalloon.prototype.applyWind = function(balloon) {
  var x = balloon.posX;
  var w = parseInt(balloon.width);
  var wind = balloon.wind + ((Math.random()-0.5)/3);
  var wDCount = balloon.windDisplacementCount;
  if ( this.bodyWidth < x+w ) {
    x = this.bodyWidth - w;
    wind = -0.4;
  }
  if ( x < 0 ) {
    x = 0;
    wind = 0.4;
  }
  if ( wind >  2 ) wind =  2;
  if ( wind < -2 ) wind = -2;
  if ( wDCount >  50 ) wind -= 0.2;
  if ( wDCount < -50 ) wind += 0.2;
  wDCount += wind;
  x += wind;
  balloon.wind = wind;
  balloon.windDisplacementCount = wDCount;
  balloon.posX = x;
}

DebianBalloon.prototype.remove = function(pos) {
  var delB = this.balloons[pos];
  delB.parentNode.removeChild( delB );
  var newBalloonsList = [];
  for ( var i=0; i<pos; i++ ) {
    newBalloonsList[i] = this.balloons[i];
  }
  if ( pos < this.balloons.length-1 ) {
    for ( var b,i=pos+1; b=this.balloons[i]; i++ ) {
      newBalloonsList[i-1] = b;
    }
  }
  this.balloons = newBalloonsList;
}

if ( window.addEventListener ) {
  window.addEventListener( 'load', function(){ new DebianBalloon() }, false );
} else {
  window.attachEvent( 'onload', function(){ new DebianBalloon() } );
}


