//
// PART14
// CREDITS
//
function part14() {
    this.vbl = 0 ;
    this.canvaspart;
    this.initdone = 0 ;

    var cvsg ;

    var hmd_my3d;
    var hmd_maptex3;
    var hmd_maptex4;
    var zoom2=0;
    var minimum=0.00;
    var mysin1=0;
    var myoldsin1=0;
    var mysin2=0;
    var myoldsin2=0;

    var vbl=0;
    var my3d_spr;

    var text_info = new Array();
    text_info[0]="MUSIC BY";
    text_info[1]="505";
    text_info[2]="ALL DESIGN BY";
    text_info[3]="PANDAFOX";
    text_info[4]="MORAL SUPPORT";
    text_info[5]="MARACUJA - SINK";
    text_info[6]="THE PACMAN";

    var text_info2 = new Array();
    text_info2[0]="CODE BY";
    text_info2[1]="AYOROS";
    text_info2[2]="OPTIMIZATION CODE BY";
    text_info2[3]="TOTORMAN";
    text_info2[4]="ART BY";
    text_info2[5]="PANDAFOX";
    text_info2[6]="FAKE LENS";

    var text_info3 = new Array();
    text_info3[0]="CODE BY";
    text_info3[1]="AYOROS";
    text_info3[2]="AMIGA VERSION BY";
    text_info3[3]="STC";
    text_info3[4]="ART BY";
    text_info3[5]="PANDAFOX";
    text_info3[6]="CUBE WRITER";

    var text_info4 = new Array();
    text_info4[0]="CODE BY";
    text_info4[1]="AYOROS";
    text_info4[2]="OPTIMIZATION CODE BY";
    text_info4[3]="TOTORMAN";
    text_info4[4]="ART BY";
    text_info4[5]="PANDAFOX";
    text_info4[6]="LENS FLARE";

    var text_info5 = new Array();
    text_info5[0]="CODE BY";
    text_info5[1]="AYOROS";
    text_info5[2]="OPTIMIZATION CODE BY";
    text_info5[3]="TOTORMAN";
    text_info5[4]="ART BY";
    text_info5[5]="PANDAFOX";
    text_info5[6]="3D TUNNEL";

    var text_info6 = new Array();
    text_info6[0]="CODE BY";
    text_info6[1]="AYOROS";
    text_info6[2]="ART BY";
    text_info6[3]="PANDAFOX";
    text_info6[4]="";
    text_info6[5]="";
    text_info6[6]="BESTIOLES";

    var text_info7 = new Array();
    text_info7[0]="CODE BY";
    text_info7[1]="AYOROS";
    text_info7[2]="IDEA BY";
    text_info7[3]="ULM ON ATARI";
    text_info7[4]="ART BY";
    text_info7[5]="PANDAFOX";
    text_info7[6]="MAXI SCROLL";

    var text_info8 = new Array();
    text_info8[0]="CODE BY";
    text_info8[1]="AYOROS";
    text_info8[2]="OPTIMIZATION CODE BY";
    text_info8[3]="TOTORMAN";
    text_info8[4]="ART BY";
    text_info8[5]="PANDAFOX";
    text_info8[6]="LANDSCAPES";

    var text_info9 = new Array();
    text_info9[0]="CODE BY";
    text_info9[1]="TOTORMAN";
    text_info9[2]="ART BY";
    text_info9[3]="PANDAFOX";
    text_info9[4]="IDEA BY";
    text_info9[5]="ANDROMEDA ON AMIGA";
    text_info9[6]="EXPLOSION";

    var text_info10 = new Array();
    text_info10[0]="CODE BY";
    text_info10[1]="TOTORMAN";
    text_info10[2]="ART BY";
    text_info10[3]="PANDAFOX";
    text_info10[4]="IDEA BY";
    text_info10[5]="SINK AND AYOROS";
    text_info10[6]="3D SCROLL";

    var text_info11 = new Array();
    text_info11[0]="CODE BY";
    text_info11[1]="TOTORMAN";
    text_info11[2]="ART BY";
    text_info11[3]="PANDAFOX";
    text_info11[4]="IDEA BY";
    text_info11[5]="RED SECTOR INC.";
    text_info11[6]="RED SECTOR MAN";

    var text_info12 = new Array();
    text_info12[0]="CODE BY";
    text_info12[1]="AYOROS";
    text_info12[2]="ART BY";
    text_info12[3]="PANDAFOX";
    text_info12[4]="IDEA BY";
    text_info12[5]="ANARCHY";
    text_info12[6]="SPHERES";

    var text_info13 = new Array();
    text_info13[0]="ASCII BY";
    text_info13[1]="SKIN";
    text_info13[2]="CODE BY";
    text_info13[3]="AYOROS";
    text_info13[4]="MUSIC BY";
    text_info13[5]="505";
    text_info13[6]="ASCII ART";

    var text_info14 = new Array();
    text_info14[0]="CODE BY";
    text_info14[1]="AYOROS";
    text_info14[2]="ART BY";
    text_info14[3]="PANDAFOX";
    text_info14[4]="PACMAN IDEA BY";
    text_info14[5]="SINK";
    text_info14[6]="KALIDOSCOPE";

    var text_info15 = new Array();
    text_info15[0]="CODE BY";
    text_info15[1]="TOTORMAN";
    text_info15[2]="ART BY";
    text_info15[3]="PANDAFOX";
    text_info15[4]="        ";
    text_info15[5]="        ";
    text_info15[6]="3D GLENZ";
    
    var text_info16 = new Array();
    text_info16[0]="CODE BY";
    text_info16[1]="TOTORMAN";
    text_info16[2]="ART BY";
    text_info16[3]="PANDAFOX";
    text_info16[4]="REVISION ART BY";
    text_info16[5]="XXXX";
    text_info16[6]="3D TEXTURE";
    
    var text_info17 = new Array();
    text_info17[0]="MUSIC BY";
    text_info17[1]="SHOOTERS";
    text_info17[2]="ART BY";
    text_info17[3]="PANDAFOX";
    text_info17[4]="CODE BY";
    text_info17[5]="AYOROS, TOTORMAN";
    text_info17[6]="THIS END PART";

    var select_text_info=new Array();
    select_text_info=[text_info,text_info2,text_info3,text_info4,text_info5,text_info6,text_info7,text_info8,text_info9,text_info10,text_info11,text_info12,text_info13,text_info14,text_info15,text_info16,text_info17];

    var text_greet1 = new Array();
    text_greet1[0]="CODEF FRAMEWORK BY";
    text_greet1[1]="NONAMENO";
    text_greet1[2]="PROTRACKER PLAYER BY";
    text_greet1[3]="FIREHAWK-TDA";
    text_greet1[4]="MEMBERS OF HEMOROIDS";
    text_greet1[5]="SINK - MARACUJA";
    text_greet1[6]="";
    text_greet1[7]="SHOOTERS-STC-ANGEL";
    text_greet1[8]="";
    text_greet1[9]="AYOROS-TOTORMAN";
    text_greet1[10]="";
    text_greet1[11]="PANDAFOX-KELLY X";

    var text_greet2 = new Array();
    text_greet2[0]="GREETINGS FLY TO";
    text_greet2[1]="ALL MEMBERS OF HMD";
    text_greet2[2]="";
    text_greet2[3]="IMPACT - ZUUL - ULM";
    text_greet2[4]="";
    text_greet2[5]="NONAMENO - UNION";
    text_greet2[6]="";
    text_greet2[7]="RAZOR 1911 - TRSI";
    text_greet2[8]="";
    text_greet2[9]="MELLOWMAN - SOLO";
    text_greet2[10]="";
    text_greet2[11]="DUNE - OXYGEN ";

    var select_text_info2=new Array();
    select_text_info2=[text_greet1,text_greet2];

    var myobj_spr = new Array();
    var myobjvert_spr = new Array();
    myobjvert_spr=[];

    var image_part=img_part[0];
    var text_part=select_text_info[0];
    var increase = Math.PI / 100;
    var sy=0;
    var sx=0;
    var sz=0;
    var zx=0;
    var zy=0;
    var angle = 1;			 
    var zangle = 0;	
    var zoom=-1000;
    var xoom=0;
    var yoom=0;
    var time_part=0;
    var alpha_fade=0;
    var fade_end=0;
    var alphaimage_fade = 0 ;
    var fadeimage_end = 0 ;

    var part_cube=0;
    var mycanvas3;
    var mycanvas_tmp;
    var mycanvas3d;
    var i=0;
    var stop_part=0;
    var text_part2=select_text_info2[0];
    var to_next_part = 0 ;
    var time_part2=0;

    var mycanvas_stars;
    var mycanvas_texte;
    var mycanvas_white ;

    function init_stars(){
        for (var i=0;i<200;i++){
            sx=(Math.floor((Math.random()*720))-360)*0.01;
            sy=(Math.floor((Math.random()*720))-360)*0.01;
            sz=(Math.floor((Math.random()*720))-360)*0.01;
            myobjvert_spr.push({x:sx, y:sy, z: sz, img:0}); //color:0xffffff, size:1})	
        }
        
        my3d_spr=new codef3D(mycanvas_stars, 800, 70, 1, 1600 );
        my3d_spr.vectorball_img(myobjvert_spr, ball_part );
        my3d_spr.group.scale.x=my3d_spr.group.scale.y=my3d_spr.group.scale.z=200;
    }


    function do_greetings(dest,text_part2){
        if ( (time_part2 >= 0) && (time_part2 < 300) ) {
              if (time_part2==0) {
                mycanvas_tmp.clear();

                write_line(myfont_credits,mycanvas_tmp,0,0,text_part2[0],1,0) ;
                write_line(myfont_credits2,mycanvas_tmp,0,20,text_part2[1],1,18) ;
                write_line(myfont_credits,mycanvas_tmp,0,100,text_part2[2],1,0) ;
                write_line(myfont_credits2,mycanvas_tmp,0,120,text_part2[3],1,18) ;
                write_line(myfont_credits,mycanvas_tmp,0,200,text_part2[4],1,0) ;
                write_line(myfont_credits2,mycanvas_tmp,0,220,text_part2[5],1,18) ;
                write_line(myfont_credits,mycanvas_tmp,0,300,text_part2[6],1,0) ;
                write_line(myfont_credits2,mycanvas_tmp,0,320,text_part2[7],1,18) ;
                write_line(myfont_credits,mycanvas_tmp,0,400,text_part2[8],1,0) ;
                write_line(myfont_credits2,mycanvas_tmp,0,420,text_part2[9],1,18) ;
                write_line(myfont_credits,mycanvas_tmp,0,500,text_part2[10],1,0) ;
                write_line(myfont_credits2,mycanvas_tmp,0,520,text_part2[11],1,18) ;  
                alpha_fade = 0 ;
                fade_end = 0 ;
              }
              
              mycanvas_tmp.draw(dest,0,0,alpha_fade);
              if ( fade_end<=50 ) { alpha_fade += 0.02; }
              if ( (fade_end>=250 ) && (fade_end<300) ) { alpha_fade -= 0.02 ; }
              fade_end++ ;
        }
     
        time_part2 = ( time_part2 +1) % 300 ;
        if (time_part2 == 0) to_next_part =1 ;
    }

    function stars(){
        my3d_spr.group.rotation.x+=0.010;
        my3d_spr.group.rotation.y+=0.003;
        my3d_spr.group.rotation.z+=0.002;

        for (var i=0;i<200;i++){
            my3d_spr.group.children[i].position.x -= 0.08;
            if (my3d_spr.group.children[i].position.x < -4){
                my3d_spr.group.children[i].position.x = 4;
            }
        }

        //mycanvas.contex.globalCompositeOperation='lighter';	
        my3d_spr.draw();
        //mycanvas.contex.globalCompositeOperation='source-over';	
    }

    function do3d(dest,image_part,text_part){
        mycanvas3d.clear();

        hmd_my3d.scene.children[2].rotation.x += 0.02;
        hmd_my3d.scene.children[2].rotation.y += 0.05;
        hmd_my3d.scene.children[2].rotation.z += 0.01;

        hmd_my3d.scene.children[3].rotation.x += 0.02;
        hmd_my3d.scene.children[3].rotation.y += 0.05;
        hmd_my3d.scene.children[3].rotation.z += 0.01;

        // Planes position
        hmd_my3d.scene.children[2].position.z = zoom;
        hmd_my3d.scene.children[3].position.z = zoom;
        hmd_my3d.scene.children[2].position.x = xoom;
        hmd_my3d.scene.children[3].position.x = xoom;
        hmd_my3d.scene.children[2].position.y = yoom;
        hmd_my3d.scene.children[3].position.y = yoom;

        if   (time_part <= 150)                       { zoom += 8   ;}
        if ( (time_part >  150) && (time_part < 200) ){ zoom -= 4   ;}
        if ( (time_part >= 200) && (time_part < 220) ){ xoom -= 1.2 ;}
        if ( (time_part >= 200) && (time_part < 216) ){ yoom += 1.5 ;}
        if ( (time_part >= 200) && (time_part < 216) ){ zoom -= 0.5 ;}
        if ( (time_part >= 216) && (time_part < 236) ){ xoom += 1.2 ;}

        if ( (time_part >= 100) && ( time_part < 400 ) ) {
            if (time_part == 100) {
                mycanvas_tmp.clear();
                write_line(myfont_credits,mycanvas_tmp,0,300,text_part[0],1,0) ;
                write_line(myfont_credits2,mycanvas_tmp,0,320,text_part[1],1,18) ;
                write_line(myfont_credits,mycanvas_tmp,0,400,text_part[2],1,0) ;
                write_line(myfont_credits2,mycanvas_tmp,0,420,text_part[3],1,18) ;
                write_line(myfont_credits,mycanvas_tmp,0,500,text_part[4],1,0) ;
                write_line(myfont_credits2,mycanvas_tmp,0,520,text_part[5],1,18) ;
                fade_end = 0 ;
                alpha_fade = 0 ;
            }
           mycanvas_tmp.draw(dest,0,0,alpha_fade);

            if ( fade_end < 50 ) { alpha_fade = alpha_fade + 0.02 ; }
            if ( (fade_end >= 250) && (fade_end < 300) ) { alpha_fade = Math.max(alpha_fade - 0.02,0); }
            if ( fade_end > 299 ) { fade_end = 0 ; }
            fade_end++;
        }

        if ( (time_part >= 400 ) && ( time_part < 416 ) ) { yoom -= 1.5; }
        if ( (time_part >= 416 ) && ( time_part < 446 ) ) { zoom += 6  ; }
        if ( (time_part >= 446 ) && ( time_part < 500 ) ) { zoom -= 6  ; }
        if   (time_part >= 550 )                          { zoom = -1000 ;
                                                            xoom = 0 ; 
                                                            yoom = 0 ; 
                                                            time_part = 0 ; 
                                                            to_next_part = 1 ;
                                                            alphaimage_fade = 0 ;
                                                            }

        if ( (time_part>50) && ( time_part<=100 )) { alphaimage_fade += 0.02; }
        if ( (time_part>500 ) && (alphaimage_fade>0.02) ) { alphaimage_fade -= 0.02 ; }

        image_part.draw(mycanvas3);

        hmd_maptex3.needsUpdate=true;
        hmd_maptex4.needsUpdate=true;

        hmd_my3d.draw();

        cvsg.clear() ;
        if (text_part[6] != "") VerticalDraw(cvsg,text_part[6],myfont_credits2,8,8) ;

        if (time_part<dest.height/2) {
            cvsg.draw(dest,0,dest.height - time_part*2,alphaimage_fade) ;
        } else {
            if (time_part<750) cvsg.draw(dest,0,0,alpha_fade) ;
        }

        time_part++;
    }

    function VerticalDraw(dest,s,font,x,inc) {
        var starty = dest.height /2 - s.length * (font.tileh+inc) /2 ;
        for (var i=0; i<s.length; i++) {
            font.drawTile( dest, get_letter(s[i]), x , starty + i*(font.tileh+inc)  , 1 );
        }
    }

    function font_space(c,spa) {
        var result = 0 ;
        var choice=spa;
        switch(c)
        {
        case '!' : result = 12 ; break;
        case '(' : result = 12 ; break;
        case ')' : result = 12 ; break;
        case '@' : result = 12 ; break;
        case '"' : result = 16 ; break;
        case '\'': result = 8 ; break;
        case '/': result = 16 ; break;
        case '<': result = 20 ; break;
        case ',' : result = 8 ; break;
        case '.' : result = 8 ; break;
        case ':' : result = 8; break; 
     
        default: result = (20 + choice) ; break;
        }  

        return result;
    }

    function line_width(s,spa) {
        var result = 0 ;
        for (var i=0, c; c=s[i]; i++)
            result += font_space(c,spa) ;
        return result ;
    }

    function get_letter(c) {
        return ( (c.charCodeAt(0)-32) );
    }

    function write_line(font, dest,x,y,s,alpha,spa) {
        var decale = 0 ;
       
        for (var j=0, c; c=s[j]; j++) {
            font.drawTile( dest, get_letter(c), 720/2 - line_width(s,spa)/2 + x+decale  , y  , alpha );
            decale += font_space(c,spa) ;
        }
    }

    this.init = function(dest) {
        mycanvas_white = new canvas(dest.width, dest.height) ;
        mycanvas_white.fill("#ffffff") ;

        mycanvas_texte=new canvas(720,576);

        mycanvas_tmp=new canvas(720,576);
        mycanvas3=new canvas(302,204);
        mycanvas3d=new canvas(720,576);
        mycanvas_stars=new canvas(720,576);

        hmd_maptex3 = new Texture(mycanvas3.canvas);
        hmd_maptex4 = new Texture(mycanvas3.canvas);

        hmd_my3d=new codef3D(mycanvas3d, 200, 25, 1,1000 );

        var plane = new THREE.PlaneGeometry(50, 30, 5, 5 );
        var materialReflection = new THREE.MeshBasicMaterial( { map: hmd_maptex3, overdraw: true } );
        mesh = new THREE.Mesh( plane, materialReflection );
        hmd_my3d.scene.add(mesh) ;

        var materialReflectionrev = new THREE.MeshBasicMaterial( { map: hmd_maptex4, overdraw: true } );
        mesh = new THREE.Mesh( plane, materialReflectionrev );
        mesh.rotation.z = Math.PI ;
        mesh.scale.z = -1 ;
        mesh.scale.x = -1 ;
        mesh.scale.y = -1 ;
        hmd_my3d.scene.add(mesh) ;

        init_stars();

        players4 = new Audio();
        players4.autobuffer=true;
        players4.loop = true;
        players4.src = (basepathsnd + 'nepasdiffuser.ogg');
        players4.autoplay=true; 

        myfont_credits.initTile(16,16,32);
        myfont_credits2.initTile(32,32,32);
        cvsg = new canvas(48,576) ;

        this.initdone = 1 ;
        end_total=1;
    }

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

        this.canvaspart.fill('#161b1e');

        mycanvas_stars.clear();
        stars();
        mycanvas_stars.draw(this.canvaspart,0,0);

        if (this.vbl > 200) {
            if (stop_part==0){
                if (to_next_part == 1){
                    to_next_part = 0 ;
                    i++;
                    if (i==select_text_info.length) { i=0; stop_part=1; }
                    image_part = img_part[i];
                    text_part = select_text_info[i];
                }
                if (i>-1) {
                    do3d( this.canvaspart,image_part,text_part);
                    mycanvas3d.draw(this.canvaspart,0,0,alphaimage_fade); 
                }
            }
            else {
                if (to_next_part == 1){
                    to_next_part = 0 ;
                    i++;
                    if (i==select_text_info2.length) { i=0; stop_part=0; }

                    text_part2 = select_text_info2[i];
                }
                if (stop_part == 1) do_greetings( this.canvaspart,text_part2);
            }
        }
        this.canvaspart.draw(dest,0,0);
   
        if (this.vbl<200) mycanvas_white.draw(dest,0,0,1-this.vbl/200);

        this.vbl++ ;
    }

    return this;
}

///////////////
//END PART14//
/////////////
