//////////////////////
// Multi Ball      //
//Code By Ayoros  //
///////////////////
function part9() {
    this.vbl = 0 ;
    this.canvaspart;
    this.initdone = 0 ;
var ball_tween = new Array();	
var myobjball = new Array();
myobjball=[
        {x:-150, y:0, z: 100, color:0xab5ee6, size:60},
        {x:-100, y:30, z: 100, color:0x5e5ee6, size:60},
        {x:-50, y:60, z: 100, color:0x5ec4e6, size:60},
        {x:-00, y:90, z: 100, color:0x357fdb, size:60},
        {x:50, y:120, z: 100, color:0x5ee6a8, size:60},
        {x:100, y:150, z: 100, color:0xe6c85e, size:60},
               ];
var myobjball2 = new Array();
myobjball2=[
        {x:0, y:100, z: 100, color:0xab5ee6, size:60},
        {x:0, y:-100, z: 100, color:0x5e5ee6, size:60},
        {x:100, y:0, z: 100, color:0x5ec4e6, size:60},
        {x:-100, y:0, z: 100, color:0x357fdb, size:60},
        {x:-20, y:20, z: 100, color:0x5ee6a8, size:60},
        {x:20, y:-20, z: 100, color:0xe6c85e, size:60},
               ];
var myobjball3 = new Array();
myobjball3=[
        {x:0, y:200, z: 100, color:0xab5ee6, size:60},
        {x:0, y:-200, z: 100, color:0x5e5ee6, size:60},
        {x:200, y:0, z: 100, color:0x5ec4e6, size:60},
        {x:-200, y:0, z: 100, color:0x357fdb, size:60},
        {x:-50, y:50, z: 100, color:0x5ee6a8, size:60},
        {x:50, y:-50, z: 100, color:0xe6c85e, size:60},
               ];
var myobjball4 = new Array();
myobjball4=[
        {x:50, y:50, z: 100, color:0xab5ee6, size:60},
        {x:-50, y:-50, z: 100, color:0x5e5ee6, size:60},
        {x:-200, y:-100, z: 100, color:0x5ec4e6, size:60},
        {x:200, y:100, z: 100, color:0x357fdb, size:60},
        {x:-125, y:-150, z: 100, color:0x5ee6a8, size:60},
        {x:125, y:150, z: 100, color:0xe6c85e, size:60},
               ];
var myobjball5 = new Array();
myobjball5=[
        {x:0, y:50, z: 100, color:0xab5ee6, size:60},
        {x:0, y:100, z: 100, color:0x5e5ee6, size:60},
        {x:0, y:150, z: 100, color:0x5ec4e6, size:60},
        {x:100, y:-50, z: 100, color:0x357fdb, size:60},
        {x:-100, y:-50, z: 100, color:0x5ee6a8, size:60},
        {x:0, y:200, z: 100, color:0xe6c85e, size:60},
               ];      
var myobjball6 = new Array();
myobjball6=[
        {x:0, y:-75, z: 100, color:0xab5ee6, size:60},
        {x:200, y:-75, z: 100, color:0x5e5ee6, size:60},
        {x:-200, y:-75, z: 100, color:0x5ec4e6, size:60},
        {x:100, y:75, z: 100, color:0x357fdb, size:60},
        {x:-100, y:75, z: 100, color:0x5ee6a8, size:60},
        {x:0, y:200, z: 100, color:0xe6c85e, size:60},
            ];         

var mycanvas_ball;
var my3dball;
var mycanvas_stars;

var cheesboardcanvas;

var pos_stars =0;
var sti =0.1;

var my3d_board;

var movez=0;
var movez_stc=0;
var sy=0;
var movey=0;
var angle=1;	
var mygrad;
var mygradcolor=
	[
	{color: 'rgba(0,0,0,255)' ,  offset:0},
	{color: 'rgba(0,0,0,255)' ,  offset:0.6},
	{color: 'rgba(94,196,230,255)' ,  offset:0.7},
	{color: 'rgba(255,200,200,255)' ,  offset:1},
	];
var mygrad2;
var mygradcolor2=
	[
	{color: 'rgba(0,0,0,255)' ,  offset:0},
	{color: 'rgba(0,0,0,255)' ,  offset:0.6},
	{color: 'rgba(171,94,230,255)' ,  offset:0.7},
	{color: 'rgba(255,200,200,255)' ,  offset:1},
	];
var lol=0;

function dostars(){
    i = i % 10;
    if (j >= 6) i=0;

    i+=0.018;
    j+=0.025;
    mystarfield.centx = 300 + (pos_stars*Math.cos(j));
    mystarfield.centy = 200 + (pos_stars*Math.sin(i));

    if (pos_stars <= 200) pos_stars+=sti;
    else pos_stars-=sti;

    mystarfield.draw();
	
}
function board(){
    my3d_board.draw();
    speed_3d=4;	
    movey+=0.01;
    sy=200 * Math.sin( angle )*0.01;
    angle += (Math.PI / 50)*0.2;
    my3d_board.group.rotation.y=sy+movey;
    //Move le cheesboard
    for (var i=0;i<400;i++){	
    my3d_board.group.children[i].position.z += speed_3d-movez_stc;	
    }	
    movez+=speed_3d;  
    //test init move
    if (movez>79.5){movez_stc=movez;movez=0;} else{movez_stc=0;}
} 

    this.init = function(dest) {
        this.initdone = 1 ;

        mycanvas_ball=new canvas(600,400);
        mycanvas_stars=new canvas(600,240);
        cheesboardcanvas=new canvas(600,400);
        var compt_3d=0;
        for(var compt_3d=0;compt_3d<6;compt_3d++)
        {
          ball_tween[compt_3d]=createjs.Tween.get(myobjball[compt_3d],{loop:true})
          .wait(80)
          .to({x:myobjball2[compt_3d].x,y:myobjball2[compt_3d].y,z:myobjball2[compt_3d].z},9000,createjs.Ease.elasticInOut)
          .wait(80)
          .to({x:myobjball3[compt_3d].x,y:myobjball3[compt_3d].y,z:myobjball3[compt_3d].z},9000,createjs.Ease.elasticInOut)
          .wait(80)
        //  .to({x:myobjball4[compt_3d].x,y:myobjball4[compt_3d].y,z:myobjball4[compt_3d].z},9000,createjs.Ease.elasticInOut)
        //  .wait(80)
        //  .to({x:myobjball5[compt_3d].x,y:myobjball5[compt_3d].y,z:myobjball5[compt_3d].z},9000,createjs.Ease.elasticInOut)
        //  .wait(80)
          .to({x:myobjball6[compt_3d].x,y:myobjball6[compt_3d].y,z:myobjball6[compt_3d].z},9000,createjs.Ease.elasticInOut)
          .wait(80)
          .to({x:myobjball[compt_3d].x,y:myobjball[compt_3d].y,z:myobjball[compt_3d].z},9000,createjs.Ease.elasticInOut)
        }
  
        my3dball=new codef3D(mycanvas_ball, 900, 37, 1, 1600 );
        my3dball.vectorball_dot(myobjball);
         
         //init 3d Board
        my3d_board=new codef3D(cheesboardcanvas, 150, 80, 1, 1600 );

        var mat= new THREE.MeshBasicMaterial({color: 0x000000, shading: FlatShading, overdraw: false});
        var car = new THREE.PlaneGeometry(40,40);

        for(var j=-15;j<15;j++){
          if (j%2){
            lol=40;
          }else{
          lol=0;
        }
	 
        for(var i=-8;i<8;i++){
          var mesh = new THREE.Mesh( car , mat );
          mesh.position.x=i*80+15+lol;
          mesh.position.z=j*40+15;

          my3d_board.group.add( mesh );
        }
      }

      my3d_board.camera.position.y=80;
      my3d_board.camera.position.z=350;
      my3d_board.group.position.z=40*5-40
      mygrad=new grad(cheesboardcanvas,mygradcolor);
      mygrad2=new grad(cheesboardcanvas,mygradcolor2);

      
      //Stars
      mystarfield=new starfield3D(mycanvas_stars, 500, 2, 600,240, 600/2,240/2,'#999999', 100,0,0);

      this.initdone = 1 ;
      }

     

    this.render = function(dest) {
        if (this.initdone == 0) { this.canvaspart = new canvas(600,400) ; this.init(dest) ; }
        this.canvaspart.fill("#000000");
        mycanvas_ball.clear();
        mycanvas_stars.clear();
      
        mygrad2.drawH();
        cheesboardcanvas.contex.globalCompositeOperation='destination-out';
        board();
        cheesboardcanvas.contex.globalCompositeOperation='destination-over';
        mygrad.drawH();
        cheesboardcanvas.contex.globalCompositeOperation='source-over';
        cheesboardcanvas.draw(this.canvaspart,0,0);
        dostars();
        mycanvas_stars.draw(this.canvaspart,0,0);
        mycanvas_ball.contex.globalCompositeOperation='lighter';
    	  for(var compt_3d=0;compt_3d<6;compt_3d++){
          my3dball.group.children[compt_3d].position.x = myobjball[compt_3d].x;
          my3dball.group.children[compt_3d].position.y = myobjball[compt_3d].y;
          my3dball.group.children[compt_3d].position.z = myobjball[compt_3d].z;
          ball_tween[compt_3d].tick(33);
        }
        my3dball.group.rotation.x+=0.01;
        my3dball.group.rotation.y+=0.02;
        my3dball.group.rotation.z+=0.04;
        my3dball.draw();
        mycanvas_ball.contex.globalCompositeOperation='source-over';
        mycanvas_ball.draw(this.canvaspart,0,0);
     //   mycanvas_ball.drawPart(this.canvaspart,0,400,0,100,600,120,0.5,0,1,-0.5);
        
        
        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 PART9//
////////////
