;(function($){
  $(function(){
    
    var hc,wc;
    var msie = $.browser.msie;
    var resize = function() {
      hc = $(window).height()-5;
      wc = Math.max($(window).width(),800)-5;
      $("#canvas").attr("width",wc).attr("height",hc);
      if(msie)
        G_vmlCanvasManager.initElement($("#canvas")[0]);
      VMLon = false;
      for(var i=0;i<numParticelle;i++) {
        part[i].rifx = Math.random()*wc;
        part[i].rify = hc/2;
      }
      
    }
    $(window).resize(resize);
    resize();
    var c = $("#canvas")[0].getContext("2d");
    var offset = $("#canvas").offset();
    var mouseX, mouseY;
    var VMLon = false;
    
    $(document).mousemove(function(e){
      mouseX = e.pageX-offset.top;
      mouseY = e.pageY-offset.left;
    });
    
    var Particella = function(x, y, radius, col) {
      this.dx = 0;
      this.dy = 0;
      this.dradius = 0;
      this.rifx = 0;
      this.rify = 0; 
      this.vel = 30;
      this.vibracounter = 0;
      
      this.x = x;
      this.y = y;
      this.radius = radius;
      this.radius2 = radius*2;
      this.col = col;
      this.state = 0;
    };
    
    Particella.prototype = {
      setRif: function(x, y) {
        this.rifx = x;
        this.rify = y;
      },
      draw: function() {
        if(!msie || !VMLon) {
          c.fillStyle = this.col;
          c.beginPath();
          c.arc(this.x,this.y,this.radius,0,2*Math.PI,true);
          c.fill();
          if(msie)
            this.el = $("shape:last")[0];          
        } else if(msie) {
          //sposto 
          var aRadius = this.radius*10;
          var xStart = this.x + aRadius - 5;
          var yStart = this.y - 5;
      
          var xEnd = this.x + aRadius - 5;
          var yEnd = this.y - 5;
      
          var p = {x:10*this.x-5,y:10*this.y-5};
          var pStart = {x:10*xStart-5,y:10*yStart-5};
          var pEnd = {x:10*xEnd-5,y:10*yEnd-5};

          var m = Math.round;
          var str = [' at ',
                       m(p.x - aRadius), ',',
                       m(p.y - aRadius), ' ',
                       m(p.x + aRadius), ',',
                       m(p.y + aRadius), ' ',
                       m(pStart.x), ',', m(pStart.y), ' ',
                       m(pEnd.x), ',', m(pEnd.y)];
          this.el.path = str.join('');
        }
      },
      update: function() {
        if(Math.abs(mouseX-this.x)<20 && Math.abs(mouseY-this.y)<20) {
          if(this.state==0) {
            this.state = 1;
            this.dx = this.rifx+Math.random()*200-100;
            this.dy = Math.random()*hc;
            this.vel = 30;
          } else if(this.state==1) {
            this.vibracounter = Math.round(Math.random()*20+10);
          }    
        }
        switch(this.state) {
          case 0:
            this.vibraLinea();
            break;
          case 1:
            this.vibraTutto();
            break;      
        }
        this.x += (this.dx-this.x)/this.vel;
        this.y += (this.dy-this.y)/this.vel;
      },
      vibraLinea: function() {
        if(this.vibracounter==0) {
          this.vel = 20;
          this.dx = this.rifx+Math.random()*20-10;
          this.dy = this.rify+Math.random()*20-10;
          this.vibracounter = Math.round(Math.random()*5+5);    
        }
        this.vibracounter--;
      },
      vibraTutto: function() {
        if(Math.abs(this.dx-this.x)<5 && Math.abs(this.dy-this.y)<5){
          this.state = 0;
          return;
        }
      }
      
    };
    
    var iPhone = navigator.userAgent.search(/iPhone|iPod/)!=-1;
    var numParticelle = (msie || iPhone)?50:100;
    var colori = ["#00FFFF","#FFFF00","#FF00FF"];
    var numColori = colori.length;
    var part = new Array(numParticelle);
    
    var creaParticelle = function() {
      var x,y = hc/2;
      for(var i=0;i<numParticelle;i++) {
        x = Math.random()*wc;
        part[i] = new Particella(x,y, Math.random()*5.5+0.5, colori[Math.round(Math.random()*(numColori-1))]);
        part[i].setRif(x,y);
      }
    }
    
    var setup = function() {
      creaParticelle();
      c.clearRect(0, 0, wc, hc);
      c.fillStyle = "#000000"
      c.fillRect(0, 0, wc, hc);
    }
    
    var draw = function() { 
      if(!msie) {
        c.clearRect(0, 0, wc, hc);
        c.fillStyle = "#000000"
        c.fillRect(0, 0, wc, hc);
      }
      for(var i=0;i<numParticelle;i++) {
          part[i].update();
          part[i].draw();
      }
      VMLon = true;
      window.setTimeout(draw,0);
    }
    
    setup();
    window.setTimeout(draw,0);
    var spostaLinea = function() {
      var h = Math.random()*hc;
      for(var i=0;i<numParticelle;i++) {
        part[i].rifx = Math.random()*wc;
        part[i].rify = h;
      }
    }
    window.setInterval(spostaLinea,10000);
  }); 
})(jQuery)

