////////////////////
//PLASMA         //
//////////////////
function part18() {
    this.vbl = 0 ;
    this.canvaspart;
    this.initdone = 0 ;

    var pos4=0 ;
    var pos3=0 ;
    var pos2=0 ;
    var pos1=0 ;
    var index=0;
    var x=0;  
    var colors = [] ;
    var aSin = [] ;
    var imageData ;
    var frameBuffer ;
    var fBuff8 ;
    var imgData32 ;

    function color() {
        this.r ;
        this.g ;
        this.b ;
        this.a ;

        return this;
    }

    function dist(a, b, c, d) { return Math.sqrt((a - c) * (a - c) + (b - d) * (b - d)) ; }

    this.init = function(dest) {
        /*create sin */
        for (var i = 0; i < 512; i++)
        {
          rad =  (i * 0.703125) * 0.0174532; /* 360 / 512 * degree to rad, 360 degrees spread over 512 values to be able to use AND 512-1 instead of using modulo 360*/
          aSin[i] = Math.sin(rad) ; /*using fixed point math with 1024 as base*/
        }

        /* create palette */
        for (var i=0; i<256; i++) colors[i] = new color() ;

        for (var i = 0; i < 256; ++i)
        {
          colors[i].r = ~~(128.0 + 128 * Math.sin(3.1415 * i / 32));
          colors[i].g = ~~(128.0 + 128 * Math.sin(3.1415 * i / 64));
          colors[i].b = ~~(128.0 + 128 * Math.sin(3.1415 * i / 128));
        } 

        imageData = dest.contex.getImageData(0, 0, dest.width, dest.height);
        frameBuffer = imageData.data;
        fBuff = new ArrayBuffer(frameBuffer.length);
        fBuff8 = new Uint8ClampedArray(fBuff);
        imgData32 = new Uint32Array(fBuff);

        this.initdone = 1 ;
    }

    this.render = function(dest) {
        if (this.initdone == 0) { this.canvaspart = new canvas(600,400) ; this.init(this.canvaspart) ; }

        //this.canvaspart.clear();

        var tpos4 = pos4;
        var tpos3 = pos3;

        var k=0 ;

        for (var i = 0; i < this.canvaspart.height; ++i)
        {
            var tpos1 = pos1 + 3;
            var tpos2 = pos2 + 4;
              
            tpos3 &= 511;
            tpos4 &= 511;
              
            for (var j = 0; j < this.canvaspart.width; ++j)
            {
                tpos1 &= 511;
                tpos2 &= 511;
                  
                index = ~~( ( 128.0 + 128.0 * aSin[(tpos1+i)&511] + 128.0 + 128.0 * aSin[(2*tpos2)&511] + 128.0 + 128.0 * aSin[(tpos3)] + 128.0 + 128.0 * aSin[(tpos4+j)&511] )  / 4 );
                index = index % 256 ;

                imgData32[k++] = (0xff000000) | (colors[index].b<<16) | (colors[index].g<<8) | (colors[index].r);

                tpos1 += 3; 
                tpos2 += 4; 
            }
              
            tpos4 += 3;
            tpos3 += 2;
        }

        frameBuffer.set(fBuff8);
        this.canvaspart.contex.putImageData(imageData, 0, 0, 0, 0, this.canvaspart.width, this.canvaspart.height);
               
          /* move plasma */
          
        pos1 +=10;
        pos3 +=8;

        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 PART18//
/////////////
