/*
 * Clouds
 *
 * Copyright (c) 2010 Boris Dinkevich (dinkevich.com)
 * Licensed under the GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2010-01-01 10:10:01 -0200 (Fri, 01 Jan 2010) $
 * $Rev: 0001 $
 */

function random(range) {
  return Math.floor(Math.random()*range);
};

function Cloud($container, random_placement, only_special) {
  var object = {
    x: null,
    y: null,
    speed: null,
    speed2: null,
    width: null,
    height: null,
    $item: null,
    $container: $container,

    init: function() 
    {
      this.special = (!random_placement && random(5) == 1 ? true : false);
      if (only_special && !this.special)
        return false;

      this.type   = Math.floor(Math.sqrt(random(8))) + 1;
      this.width  = parseInt($container.css('width'));
      this.height = parseInt($container.css('height'));
      this.x      = (random_placement ? random(this.width + 700) - 350 : -350);
      this.y      = random(this.height + 700) - 350;
      this.speed  = this.type * 2 * (random(2) + 1);
      this.speed2 = (4 - this.type) * 2 * (random(2) + 1);
      this.size   = (4 - this.type) / 2;

      var img ='images/cloud' + (random(5) + 1) + '.png';

      if (this.special) {
        this.speed = 20 + 3 * random(6);
        this.type  = 3;
        img = 'images/bird1.gif';
      }

      this.$item = $('<img src="' + img + '"/>')
        .css('position', 'absolute')
        .addClass('cloud')
        .addClass('cloud_type_' + this.type);

      this.$item.attr('width',  Math.floor(this.$item.attr('width')  * this.size));
      this.$item.attr('height', Math.floor(this.$item.attr('height') * this.size));
        
      $('body').prepend(this.$item);

      this.update(0, 0, true);

      return this;
    },

    update: function(offsetX, offsetY, wind) 
    {
      var move_me = (wind || this.special ? true : false);

      if (move_me)
        this.x = this.x + this.speed;

      var newX = this.x - Math.floor(offsetX / this.speed2);
      var newY = this.y - Math.floor(offsetY / this.speed2);

      this.$item.css("left", newX+"px").css("top", newY+"px");

      Console.add(this.type + ' ' + this.x + ':' + this.y);

      if (move_me && this.x >= (this.width + 350)) {
        this.$item.remove();
        return false;
      }

      return true;
    }
  }
  return object.init();
}

var Clouds = function() {
  return {
    cloudDensity: 5,
    startClouds: 60,
    speed: 60,
    clouds: new Array(),
    mouseX: 0,
    mouseY: 0,
    wind: false,

    init: function($container) 
    {
      this.$container = $container;
      this.centerX = parseInt($container.css('width'))  / 2;
      this.centerY = parseInt($container.css('height')) / 2;

      this.density = ((this.centerX*2 * this.centerY*2) / (1000 * 1000));
      this.cloudDensity = this.cloudDensity * this.density;
      this.startClouds = this.startClouds * this.density;

      $container.mousemove(function(e) {
        Clouds.mouseX = e.clientX;
        Clouds.mouseY = e.clientY;
      });

      for (var i=0;i<=this.startClouds;i++)
        this.clouds.push(Cloud(this.$container, true, false));

      setInterval(this.updateClouds, this.speed);
    },

    setWind: function(wind)
    {
      this.wind = wind;
    },

    addClouds: function()
    {
      var new_cloud = Cloud(this.$container, false, !this.wind);
      if (new_cloud)
        this.clouds.push(new_cloud);
    },

    updateClouds: function()
    {
      var that = Clouds;
      var offsetX = that.mouseX - that.centerX;
      var offsetY = that.mouseY - that.centerY;

      Console.clear();

      /* Figure out if to add more clouds */
      if (random(that.cloudDensity) == 1)
        that.addClouds();

      var i = that.clouds.length - 1;
      while (i >= 0) {
        that.updateCloud(i, offsetX, offsetY);
        i --;
      }

      Console.show();
    },

    updateCloud: function(index, offsetX, offsetY)
    {
      that = Clouds;

      if (! that.clouds[index].update(offsetX, offsetY, that.wind))
        that.clouds.splice(index, 1);
    }
  }
}();




