//////////////////////
//Particles Perlin //
//Code By Ayoros  //
///////////////////

function part5() {
    this.vbl = 0 ;
    this.canvaspart;
    this.initdone = 0 ;
var grad_perlin=0;
var noise_pp=0;
var perlin=0;
var dx_perlin=0;
var dy_perlin=0;
var ang_perlin=0;
var ix_perlin=0;
var iy_perlin=0;
var sy_perlin=0;
var z_perlin=0;
var number_perlin=400;
var scale_noise=350;
var dots_noise=[];
var x0_perlin; 
var y0_perlin; 
var i1_perlin; 
var perlin_noise_i;
var q_noise=0;

    this.init = function() {
      for (i=0;i<number_perlin;i++){
        if (q_noise==0){dots_noise[i]=({	x:i, y:i, sprite:particle[0]});}
        if (q_noise==1){dots_noise[i]=({	x:i, y:i, sprite:particle[1]});}
        if (q_noise==2){dots_noise[i]=({	x:i, y:i, sprite:particle[2]});}
        if (q_noise==3){dots_noise[i]=({	x:i, y:i, sprite:particle[3]});}
        if (q_noise==4){dots_noise[i]=({	x:i, y:i, sprite:particle[4]});}
        if (q_noise==5){dots_noise[i]=({	x:i, y:i, sprite:particle[5]});}
        if (q_noise==6){dots_noise[i]=({	x:i, y:i, sprite:particle[6]});}
        if (q_noise==7){dots_noise[i]=({	x:i, y:i, sprite:particle[7]});}
        if (q_noise==8){dots_noise[i]=({	x:i, y:i, sprite:particle[8]});}
        q_noise++;
     
        if (q_noise==9){q_noise=0;}
      }

        this.initdone = 1 ;
     }

function draw_perlin(dest) {
	z_perlin+=.003;
	for(i=0;i<number_perlin;) {
		noise_pp=dots_noise[i++];
		ix_perlin=noise_algo(noise_pp.x / scale_noise + z_perlin, sy_perlin=noise_pp.y / scale_noise) * 4 + noise_pp.x;
		iy_perlin=noise_algo(noise_pp.x / scale_noise - z_perlin, sy_perlin) * 4 + noise_pp.y;
		ix_perlin+=(600/2-ix_perlin)/50;
		iy_perlin+=(400/2-iy_perlin)/50;
			
		for(j=i+1;j<number_perlin;) {
			b=dots_noise[j++];
			dx_perlin=b.x-ix_perlin;
			dy_perlin=b.y-iy_perlin;
			if(Math.sqrt(dx_perlin*dx_perlin+dy_perlin*dy_perlin)<20) {
				ang_perlin=Math.atan2(dy_perlin, dx_perlin);
				b.x+=((ix_perlin + Math.cos(ang_perlin)*20)-b.x)/4;
				b.y+=((iy_perlin + Math.sin(ang_perlin)*20)-b.y)/4;
			}
		}
		if (iy_perlin>=390){iy_perlin=390;}
		if (iy_perlin<=0){iy_perlin=0;}
	  if (ix_perlin>=590){ix_perlin=590;}
		if (ix_perlin<=0){ix_perlin=0;}
	noise_pp.sprite.draw(dest,noise_pp.x=ix_perlin,noise_pp.y=iy_perlin);
		
	}
}
	
grad_perlin = [[1,1],[-1,1],[1,-1],[-1,-1], 
             [1,0],[-1,0],[1,0],[-1,0], 
             [0,1],[0,-1],[0,1],[0,-1]];
function noise_algo(xin, yin) {
		perlin = (xin+yin)*.366;
		dx_perlin = Math.floor(xin+perlin);
		dy_perlin = Math.floor(yin+perlin);
		j_perlin = (dx_perlin+dy_perlin)*.21;
		x0_perlin = xin-(dx_perlin-j_perlin);
		y0_perlin = yin-(dy_perlin-j_perlin);
		i1_perlin=x0_perlin>y0_perlin;
		return 70 * (perlin_calc(x0_perlin,y0_perlin, (perlin_noise_i = (dx_perlin & 255) + (dy_perlin & 255)) % 12) + perlin_calc(x0_perlin - i1_perlin + .21,y0_perlin - !i1_perlin + .21,(perlin_noise_i+1) % 12) + perlin_calc(x0_perlin - .58,y0_perlin - .58,(perlin_noise_i+2) % 12)); 
};
function perlin_calc(x,y,g) {
		j_perlin = 0.5 - x*x-y*y;
		if(j_perlin<0) return 0;
		return j_perlin * j_perlin * j_perlin * (grad_perlin[g][0]*x + grad_perlin[g][1]*y);
}
   
    this.render = function(dest) {
        if (this.initdone == 0) { this.canvaspart = new canvas(600,400) ; this.init() ; }
          this.canvaspart.clear();
         // this.canvaspart.contex.globalCompositeOperation='lighter';
          draw_perlin(this.canvaspart) ;
        //  this.canvaspart.contex.globalCompositeOperation='source-over';
          this.canvaspart.draw(dest,60,88);

        if (this.vbl<100) mycanvasblack.draw(dest,60,88,1-this.vbl/100);
        if (this.vbl>intro_parts[part_running].part_duration-100) mycanvasblack.draw(dest,60,88,1-(intro_parts[part_running].part_duration-this.vbl)/100);
        this.vbl++ ;
    }
return this;
}
//////////////
//END PART5//
////////////
