//////////////////////////
//3D SCROLL            //
////////////////////////

function dragonball()
{
	this.nbstars = 8; // number of stars
	this.ff=730;
	this.nf=700;

	this.my3d = new Array();
	this.my3d2 = new Array();
	
	this.u=0;
	this.v=0;
	this.sina=1;
	this.cosa=1;

	this.width = 720;
	this.height = 586;
	this.bound = false;

	this.kolor=0xD94345;
	this.obj=[ 
		{p1:1, p2:2, p3:3, params:new MeshBasicMaterial({ color: this.kolor})},
		{p1:3, p2:4, p3:5, params:new MeshBasicMaterial({ color: this.kolor})},
		{p1:5, p2:6, p3:7, params:new MeshBasicMaterial({ color: this.kolor})},
		{p1:7, p2:8, p3:9, params:new MeshBasicMaterial({ color: this.kolor})},
		{p1:9, p2:0, p3:1, params:new MeshBasicMaterial({ color: this.kolor})},		
			{p1:1, p2:3, p3:10, params:new MeshBasicMaterial({ color: this.kolor})},
			{p1:3, p2:5, p3:10, params:new MeshBasicMaterial({ color: this.kolor})},
			{p1:5, p2:7, p3:10, params:new MeshBasicMaterial({ color: this.kolor})},
			{p1:7, p2:9, p3:10, params:new MeshBasicMaterial({ color: this.kolor})},
			{p1:10, p2:9, p3:1, params:new MeshBasicMaterial({ color: this.kolor})},		
      	];
      	this.obj2=[ 
		{p1:1, p2:3, p3:2, params:new MeshBasicMaterial({ color: this.kolor})},
		{p1:3, p2:5, p3:4, params:new MeshBasicMaterial({ color: this.kolor})},
		{p1:5, p2:7, p3:6, params:new MeshBasicMaterial({ color: this.kolor})},
		{p1:7, p2:9, p3:8, params:new MeshBasicMaterial({ color: this.kolor})},
		{p1:9, p2:1, p3:0, params:new MeshBasicMaterial({ color: this.kolor})},		
			{p1:1, p2:10, p3:3, params:new MeshBasicMaterial({ color: this.kolor})},
			{p1:3, p2:10, p3:5, params:new MeshBasicMaterial({ color: this.kolor})},
			{p1:5, p2:10, p3:7, params:new MeshBasicMaterial({ color: this.kolor})},
			{p1:7, p2:10, p3:9, params:new MeshBasicMaterial({ color: this.kolor})},
			{p1:10, p2:1, p3:9, params:new MeshBasicMaterial({ color: this.kolor})},		
      	];
	this.objvert=[
		{x:212, y:0, z: this.nf},
		{x: 65.45084971003, y:47.55282581318, 		z: this.ff},
		{x: 65.51160277054, y:201.6239814479, 		z: this.nf},
		{x:-25, y:76.94208843168, 			z: this.ff},
		{x:-171.5116027705, y: 124.6104735024, 		z: this.nf},
		{x:-80.9016994200, y:0, 			z: this.ff},
		{x:-171.5116027705, y:-124.6104735024, 		z: this.nf},
		{x:-25, y:-76.94208843168, 			z: this.ff},
		{x:65.51160277054, y:-201.6239814479, 		z: this.nf},
		{x:65.45084971003, y:-47.5528258131, 		z: this.ff},
		{x:0, y:0, 					z: this.nf},
       ];

		
	//internal
	function mycircle(context,x,y,R,color)
	{
		context.fillStyle=color;
		context.beginPath(); 
		context.arc( x, y, R, 0, Math.PI*2, true );
		context.closePath();
		context.fill();
	}

	this.init = function(nbStars, aWidth, aHeight, ballColor, backColor)
	{
		this.width = aWidth;
		this.height =  aHeight;
		this.bcolor = ballColor;
		this.backcolor = backColor;
		
		this.mycanvas3d=new canvas(this.width,this.height);
		this.mycanvas3d2=new canvas(this.width,this.height);
		this.mycanvas3d.setmidhandle();
		this.mycanvas3d2.setmidhandle();	
		this.mycanvasb=new canvas(this.width,this.height);
	
		this.nbstars = nbStars;
		for (var i=0; i<this.nbstars; i++) 
		{
			// function codef3D(dst, camZ, fov, near, far){
			this.my3d[i]=new codef3D(this.mycanvas3d, 2600, 40, 10, 4800 );
			this.my3d2[i]=new codef3D(this.mycanvas3d2, 2600, 40, 10, 4800 );
			
			this.my3d[i].faces(this.objvert,this.obj, false, true );	
			this.my3d2[i].faces(this.objvert,this.obj2, false, true );	
			
			this.my3d[i].group.rotation.y=i*(360/this.nbstars)*Math.PI/180;	
			this.my3d2[i].group.rotation.y=i*(360/this.nbstars)*Math.PI/180;		
		}		
	}
	
	this.draw = function(aCanvas,x,y)
	{
		this.mycanvas3d.clear();
		this.mycanvas3d2.clear();

		mycircle(this.mycanvasb.contex,this.width/2,this.height/2,this.mycanvasb.width/2-30, this.bcolor);
		
		for (var i=0; i<this.nbstars; i++) {
			this.my3d[i].group.rotation.x+=0.01;
			this.my3d[i].group.rotation.y+=0.06;
			this.my3d[i].group.rotation.z+=0.00;	
			this.my3d[i].draw();
			this.my3d2[i].group.rotation.x+=0.01;
			this.my3d2[i].group.rotation.y+=0.06;
			this.my3d2[i].group.rotation.z+=0.00;	
			this.my3d2[i].draw();
		}
		
		if(this.bound == true)
			this.bce=-Math.abs(Math.sin(this.v+6))*90+(this.height*0.8);
		else
			this.bce=this.height/2;		

		this.sina=Math.sin(this.u)/2;
		this.cosa=Math.sin(this.u+10)/2;
			
		this.mycanvasb.draw(this.mycanvas3d2,0,0,0.5);
		
		this.mycanvas3d2.draw(aCanvas,x+this.width/2,y+this.bce,1,0,this.sina/3+1,this.cosa/3+1);
		this.mycanvas3d.draw(aCanvas,x+this.width/2,y+this.bce,1,0,this.sina/3+1,this.cosa/3+1);

		this.v+=.05;
		this.u+=0.2;
	}
	
}

///////////////////////////////
//3D SCROLL + Dragon Ball   //
//IDEA Ayoros (just scroll)//
//FINAL CODE BY TOTORMAN  //
///////////////////////////
function part13() {
    this.vbl = 0 ;
    this.canvaspart;
    this.initdone = 0 ;
    
    this.ininit ;

    var mydragonball = new dragonball() ;
    var yy = 0 ;

    var mystarfield;
    var mystarsparams=[
            {nb:150, speedy:0, speedx:2.7, color:'#555555', size:1},
            {nb:75, speedy:0, speedx:3.3, color:'#777777', size:1},
            {nb:35, speedy:0, speedx:6.7, color:'#999999', size:2},
                  ]; 

var mytext="IF I SAY REVENGE OF BABBNAASEN?. YOU THINK AMIGAAAAAAAA !!!   ";

var color_cube=0;
var mycanvastmp;
var buffer ;
var my3d ;

    function particleRender(context) {
        var mycolor = parseInt( context.fillStyle.slice(1, context.fillStyle.length) ) ;
        context.drawImage( particle[mycolor].img,0,0) ;
    }

    this.initobjects = function() {
        // parse a line and put into 3D
        var myobjvert = new Array() ;
        var foundpixel = 0 ;
        for (var xx=0; xx<mycanvastmp.width; xx++) {
                var p = mycanvastmp.contex.getImageData(xx, yy, 1, 1).data;
                if (p[0]!=0) {
                    var material = new THREE.ParticleCanvasMaterial ( { color : color_cube, program : particleRender } ) ;
                    var Particle = new THREE.Particle( material );
                    Particle.position.x = xx;
                    Particle.position.y = yy;
                    Particle.position.z = 0;
                    my3d.group.add( Particle );
                    color_cube = ( color_cube + 1 ) % particle.length ;
                }
        }
        yy ++ ;
    }

    this.init = function() {

        if (yy == 0) {
            this.canvaspart = new canvas(600,400) ;
            this.ininit = 1 ;

            font_cube.initTile(8,8,32) ;
            mycanvastmp=new canvas(font_cube.tilew * mytext.length,font_cube.tileh);
            mycanvastmp.fill("#000000") ;

            font_cube.print(mycanvastmp, mytext, 0, 0) ;

            mystarfield=new starfield2D_dot(this.canvaspart,mystarsparams);

            buffer = mycanvastmp.contex.getImageData(0,0,mycanvastmp.width, mycanvastmp.height) ;

            my3d = new codef3D(this.canvaspart, 1200, 18, 1, 6000 );

            mydragonball.init(5, 300, 300, "#775555", "#aa8888") ;
        }

        this.initobjects() ;

        if (yy >= mycanvastmp.height) {
            my3d.camera.lookAt( {x:0, y:0, z:0} ) ;
            my3d.camera.position.x = -800 ;
            my3d.group.rotation.x = 0 ;
            my3d.group.rotation.y = 0 ;
            my3d.group.rotation.z = 0 ;
            my3d.group.scale = { x:16, y:16, z:16 } ;
            this.ininit = 0 ;
            this.initdone = 1 ;
        }
    }


    this.render = function(dest) {
        if (this.initdone == 0) { this.init() ; }

        this.canvaspart.fill("#000000");

        mystarfield.draw();

        mydragonball.draw(this.canvaspart,150,140-Math.abs(130 * Math.sin(mydragonball.u/4)) ) ;

        if (this.ininit == 0 ) {
            my3d.draw() ;
            my3d.camera.position.x += 12 ;
            my3d.group.rotation.x -= 0.05 ;
            my3d.group.position.y = 100*Math.cos( my3d.group.rotation.x * 1.8) ;
            my3d.camera.position.z = 2500 + 1000 * Math.sin (my3d.group.rotation.x/2) ;
            my3d.camera.rotation.z = 0.8 * Math.sin(my3d.group.rotation.x) ;

            if ( my3d.camera.position.x > 140 * mytext.length ) my3d.camera.position.x = -800 ;
        }

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