//////////////////////////
//3D TRANSFORM         //
////////////////////////
function part17() {
    this.vbl = 0 ;
    this.canvaspart;
    this.initdone = 0 ;

    // 3D Objects
    var my3dball;

    var tweenpoint;

    var mycanvas_stars;
    var mystarfield ;

    var col1 = 0xCC73C4 ;
    var col2 = 0xd5b8fc ;

    var myobjball=[ 
            {p1:0, p2:1, p3:4, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:1, p2:2, p3:4, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
            {p1:2, p2:3, p3:4, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:0, p2:3, p3:4, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
            {p1:5, p2:6, p3:9, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:6, p2:7, p3:9, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
            {p1:7, p2:8, p3:9, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:5, p2:8, p3:9, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
            {p1:0, p2:3, p3:12, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:0, p2:5, p3:12, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
            {p1:5, p2:8, p3:12, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:3, p2:8, p3:12, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
            {p1:1, p2:2, p3:13, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:1, p2:6, p3:13, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
            {p1:6, p2:7, p3:13, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:2, p2:7, p3:13, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
            {p1:3, p2:8, p3:10, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:7, p2:8, p3:10, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
            {p1:2, p2:7, p3:10, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:2, p2:3, p3:10, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
            {p1:0, p2:5, p3:11, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:5, p2:6, p3:11, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
            {p1:1, p2:6, p3:11, params:new MeshBasicMaterial({ color: col1, opacity:0.7})},
            {p1:0, p2:1, p3:11, params:new MeshBasicMaterial({ color: col2, opacity:0.7})},
           ];
      
    var object_ball = [
        {x:	-10,y: -10,z:-10}, 
        {x:	-10,y:  10,z:-10}, 
        {x:	 10,y:  10,z:-10}, 
        {x:	 10,y: -10,z:-10}, 
        {x:	  0,y:   0,z:-18}, 
        {x:	-10,y: -10,z: 10}, 
        {x:	-10,y:  10,z: 10}, 
        {x:	 10,y:  10,z: 10}, 
        {x:	 10,y: -10,z: 10}, 
        {x:	  0,y:   0,z: 18}, 
        {x:	 18,y:   0,z:  0}, 
        {x:	-18,y:   0,z:  0}, 
        {x:	  0,y: -18,z:  0}, 
        {x:	  0,y:  18,z:  0}, 
    ];

    var object_cube = [
        {x:	-10,y:-10,z:-10},
        {x:	-10,y: 10,z:-10},
        {x:  10,y: 10,z:-10},
        {x:	 10,y:-10,z:-10},
        {x:	  0,y:  0,z:-10},
        {x:	-10,y:-10,z: 10},
        {x:	-10,y: 10,z: 10},
        {x:	 10,y: 10,z: 10},
        {x:	 10,y:-10,z: 10},
        {x:	  0,y:  0,z: 10},
        {x:	 10,y:  0,z:  0},
        {x:	-10,y:  0,z:  0},
        {x:	  0,y:-10,z:  0},
        {x:	  0,y: 10,z:  0},
    ];

    var object_sams = [
      {x:	-10,y:-2,z:-10},
      {x:	-10,y:2,z:-10},
      {x:	10,y:2,z:-10},
      {x:	10,y:-2,z:-10},
      {x:	0,y:0,z:-12},
      {x:	-10,y:-2,z:10},
      {x:	-10,y:2,z:10},
      {x:	10,y:2,z:10},
      {x:	10,y:-2,z:10},
      {x:	0,y:0,z:12},
      {x:	12,y:0,z:0},
      {x:	-12,y:0,z:0},
      {x:	0,y:-2,z:0},
      {x:	0,y:2,z:0},
    ];

    var object_lozange = [
        {x:	-10,y:-4,z:-10},
        {x:	-10,y:4,z:-10},
        {x:	10,y:4,z:-10},
        {x:	10,y:-4,z:-10},
        {x:	0,y:0,z:-10},
        {x:	-10,y:-4,z:10},
        {x:	-10,y:4,z:10},
        {x:	10,y:4,z:10},
        {x:	10,y:-4,z:10},
        {x:	0,y:0,z:10},
        {x:	10,y:0,z:0},
        {x:	-10,y:0,z:0},
        {x:	0,y:-20,z:0},
        {x:	0,y:20,z:0},
    ];

    var object_pyra = [
        {x:	-8,y:-8,z:-8},
        {x:	-16,y:8,z:-16},
        {x:	16,y:8,z:-16},
        {x:	8,y:-8,z:-8},
        {x:	0,y:0,z:-12},
        {x:	-8,y:-8,z:8},
        {x:	-16,y:8,z:16},
        {x:	16,y:8,z:16},
        {x:	8,y:-8,z:8},
        {x:	0,y:0,z:12},
        {x:	12,y:0,z:0},
        {x:-12,y:0,z:0},
        {x:0,y:-24,z:0},
        {x:0,y:8,z:0},
    ];

    var object_obelix = [	
        {x:	-9,y:-16,z:-9},
        {x:	-8,y:8,z:-8},
        {x:	8,y:8,z:-8},
        {x:	9,y:-16,z:-9},
        {x:	0,y:-4,z:-9},
        {x:	-9,y:-16,z:9},
        {x:	-8,y:8,z:8},
        {x:	8,y:8,z:8},
        {x:	9,y:-16,z:9},
        {x:	0,y:-4,z:9},
        {x:	9,y:-4,z:0},
        {x:	-9,y:-4,z:0},
        {x:	0,y:-30,z:0},
        {x:	0,y:8,z:0	},
    ];

    var object_sship = [		
        {x:	-2,y:-16,z:-2},
        {x:	-8,y:8,z:-4},
        {x:	8,y:8,z:-4},
        {x:	2,y:-16,z:-2},
        {x:	0,y:7,z:-4},
        {x:	-2,y:-16,z:2},
        {x:	-8,y:8,z:4},
        {x:	8,y:8,z:4},
        {x:	2,y:-16,z:2},
        {x:	0,y:7,z:4},
        {x:	18,y:6,z:0},
        {x:	-18,y:6,z:0},
        {x:	0,y:-20,z:0},
        {x:	0,y:8,z:0},
    ];

var myobjects = [ object_ball, object_cube, object_sams, object_lozange, object_pyra, object_obelix, object_sship] ;
var tcomplete = 0 ;
var vbl2 = 0 ;

    function tweencomplete() {
        tcomplete = 1 ;
    }

    function tweenstarting() {
        tcomplete = 0 ;
    }

    // 3D object transformations
    function inittween(){
        tweenpoint = new Array() ;
        for( var i=0; i < myobjects[0].length; i++ ) {
            tweenpoint[i] = createjs.Tween.get(myobjects[0][i],{loop:true}) ;
            for (var j=1; j<myobjects.length; j++) {
                if ( j==1 ) { tweenpoint[i].wait(5000) ; tweenpoint[i].call(tweenstarting) ;}
                else { tweenpoint[i].wait(175) ; }
                tweenpoint[i].to({ x : myobjects[j][i].x, y : myobjects[j][i].y, z : myobjects[j][i].z }, 5000, createjs.Ease.elasticOut) ;
            }
            tweenpoint[i].wait(175) ;
            tweenpoint[i].to({ x : myobjects[0][i].x, y : myobjects[0][i].y, z : myobjects[0][i].z }, 5000, createjs.Ease.elasticOut) ;
            tweenpoint[i].call( tweencomplete ) ;
        }
    }

    this.init = function(dest) {
        mycanvas_glenz = new canvas(dest.width, dest.height) ;
        mycanvas_stars = new canvas(dest.width, dest.height) ;
        
        inittween() ;
        my3dball = new codef3D(mycanvas_glenz, 60, 45, 1, 1600 );
        my3dball.faces(object_ball,myobjball, true, false );

        my3dball.camera.position.z = 15 ;

        // function starfield3D(           dest,    nb, speed, w,        h,            centx,       centy,      color, ratio, offsetx, offsety)
        mystarfield = new starfield3D(mycanvas_stars, 500, 4, dest.width,dest.height, dest.width/2,dest.height/2,'#999999', 200,    0,     0);

        this.initdone = 1 ;
    }

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

        this.canvaspart.clear();

        mycanvas_glenz.clear();
        for(var compt_3d=0;compt_3d<14;compt_3d++){
            my3dball.group.children[0].geometry.vertices[compt_3d] = object_ball[compt_3d];
        if ((vbl2>200) && ( tcomplete == 0 )) {
                tweenpoint[compt_3d].tick(33);
            }
        }

        my3dball.group.rotation.x+=0.03;
        my3dball.group.rotation.y+=0.04;
        my3dball.group.rotation.z+=0.02;

        my3dball.draw();

        mycanvas_stars.clear() ;
        mystarfield.draw() ;

        mycanvas_stars.draw(this.canvaspart) ; 
        mycanvas_glenz.draw(this.canvaspart) ;

        if ( vbl2 > 200) {
            createjs.Tween.tick(15);
            if (tcomplete ==0) if (my3dball.camera.position.z < 60) my3dball.camera.position.z ++ ;
        }

        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);
        }

        if (tcomplete == 1) {
            if (my3dball.camera.position.z > 15) {
                my3dball.camera.position.z -- ;
            }
            if (my3dball.camera.position.z == 15) { vbl2 = 0 ; tcomplete = 0 ; }
        }

        vbl2 ++ ;

        this.vbl++ ;
    }

    return this ;
}

///////////////
//END PART17//
/////////////
