/* Snow
*  ------------------
*  iio.js version 1.4 
*/

// define a new iio app
function Snow( app, settings ){

  // set the background to black
  app.set({ color: 'black' });

  // define the total number of particles
  var num = 400;

  // define the average width of the particles
  var radius_mid = 40;

  // if settings are passed and preview is active
  if( settings && settings.preview ) {
    // limit the total number of particles
    num = 50;
    // limit the average size
    radius_mid = 20;
  }

  // add new circles to the app
  for(var i=0; i<num; i++)
    app.add( new iio.Ellipse({
      // set random position above app view
      pos:[ 
        iio.random(0, app.width), 
        iio.random(-app.height, 0) 
      ],
      // set random radius relative to the average size
      radius: iio.random( radius_mid-15, radius_mid+15 ),
      // set color to white
      color: 'white',
      // set random velocity
      vel:[ 
        // set vel.x value
        iio.random(-.1, .1), 
        // set vel.y value
        iio.random(.1, .2) 
      ],
      // define fade animation with speed and callback
      fade:{
        // define rate at which the alpha value decreases
        speed: iio.random(.0001,.001),
        // return false to prevent automatic object removal
        callback: function(o){
          return false;
        }
      },
      // define a function to be run on each update
      onUpdate: function(){
        // add a random value to the velocity
        // (to make the particles sway/meander)
        this.vel.x += iio.random(-.01, .01);
      }
      // include true to prevent automatic drawing after add()
    }), true);

  // define a function to be run when the app is resized
  this.onResize = function(){

    // set bounds on each object, relative to the new size
    for(var i=0; i<app.objs.length; i++)
      app.objs[i].bounds = {
        // position the object back at the top
        // whenever it reaches a bottom limit
        bottom:{
          limit: app.height + 140, 
          callback: function(o){
            // set random x pos within app view
            o.pos.x = iio.random(0, app.width);
            // set random y pos above app
            o.pos.y = iio.random(-app.height, -100);
          }
        },
        left:{
          limit: 0,
          callback: function(o){
            // reverse the object's velocity 
            o.vel.x *= -1 
          }
        },
        right:{ 
          limit: app.width, 
          callback: function(o){ 
            // reverse the object's velocity 
            o.vel.x *= -1 
          }
        }
      }
  }; 

  // set the initial bounds
  this.onResize();
}

// start the app fullscreen
iio.start( Snow );