var canvas = document.getElementById('demo');
var canvas2 = document.createElement('canvas');
var canvase = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var ctx2 = canvas2.getContext('2d');
var ctxe = canvase.getContext('2d');
var logo;
var music;
var a,n,p,w,h,x,y;
var texty=0,logoy=0;
var t = 0;
var mb;
var inc;
var logoalpha = 0,bgalpha=1;
var msglength;
var kd;
var msg = document.getElementById("text").value.split('\n\n');
for(var n=0;n<msg.length;n++){
	msg[n] = msg[n].split('\n');
}

window.requestAnimFrame = function(){return(window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(callback){window.setTimeout(callback,1000/60);});}();

// original entry point
/*
window.onload = function(){
		init();
		draw();
		setTimeout(function(){
		makeText(p);
		setInterval('loop()',1000/30);
		setInterval('updateEffect()',1000/30);
		},1000);
}
*/

// new entry point
function startDemo () {
	init();
	draw();
	setTimeout(function(){
		makeText(p);
		setInterval('loop()',1000/30);
		setInterval('updateEffect()',1000/30);
	},1000);
}

var changesong = function(){
	musicpause();
	music=music2;
	musicplay();
}

function init(){
	document.title = "Loading...";
	p = 0;
	w = canvas.width;
	h = canvas.height;
	canvas2.width = w;
	canvas2.height = h;

	canvase.width = 256;
	canvase.height = 192;

	logo = new Image();
	logo.src = 'gfx/logo1.png';
	var loaded = false;
	while(loaded){
		logo.onload = function(){loaded = true};
	}

	logo2 = new Image();
	logo2.src = 'gfx/text.png';
	var loaded = false;
	while(loaded){
		logo2.onload = function(){loaded = true};
	}

	music = new Audio('sfx/music.ogg');
	music.addEventListener('ended',function(){music.play()});
	loaded = false;
	while(loaded){
		music.onload = function(){loaded = true};
	}

	music2 = new Audio('sfx/music1.ogg');
	music2.addEventListener('ended',function(){music.play()});
	loaded = false;
	while(loaded){
		music2.onload = function(){loaded = true};
	}

	canvas.onmousedown = function(e){
		kd = false;
		mb = e.which;
		if(mb == 1){if(music.muted)music.muted = false;else{music.muted = true;}}
	}
	canvas.onmouseup = function(e){
		kd = false;
		mb = 0;
	}
	document.body.onkeydown = function(e){
		switch(e.which){
			case 49:
				document.title = '1';
				break;
			case 50:
				document.title = '2';
				break;
			case 51:
				document.title = '3';
				break;
			case 83:
				if(mb&0x1 && kd){
					eval(atob('Y2hhbmdlc29uZygpOw=='));
				}
		}
		if(e.which == 68){
			kd = true;
		}
	}
	
	music.play();
	logoy = 576-208;
	texty = 576;
	bgalpha = 0;
	inc = 0.05;
	loop = delayloop;
	draw = draw_lightninglogo;
	document.title = "Datastorm excusetro!";
}
function delayloop(){
	t += inc;
	if(t>=1){
		t = 0;
		loop = in_lightning;
		inc = 0.05;
	}
}
function in_lightning(){
	t += inc;
	logoalpha = t;
	if(t>=1){
		logoalpha = 1;
		t = 0;
		inc = 0.1;
		loop = delayloop2;
	};
}
function delayloop2(){
	t += inc;
	if(t>=1){
		t = 0;
		inc = 125/6000;
		loop = out_lightning;
		makeText(p);
	}
}
function out_lightning(){
	bgalpha = t;
	t += inc;
	if(t>=1){
		bgalpha = 1;
		t = 0;
		inc = 125/6000;
		loop = moveup_logotext;
		draw = draw_logotext;
	};
}
function up_logotext(){
	texty = 64+4*Math.sin(2*Math.PI*t);
	t += inc;
	if(t>=1){
		t = 0;
		inc = 125/6000*2;
		loop = movedown_logotext;
	};
}

function movedown_logotext(){
	texty = 64+576*(-2*t*t*t+3*t*t);
	t += inc;
	if(t>=1){
		t = 0;
		inc = 1;
		loop = down_logotext;
	};
}
function down_logotext(){
	texty = 576;
	t += inc;
	if(t>=1){
		t = 0;
		inc = 125/6000*2;
		p = (p+1)%msglength;
		makeText(p);
		loop = moveup_logotext;
	};
}
function moveup_logotext(){
	texty = 64+576-576*(-2*t*t*t+3*t*t);
	t += inc;
	if(t>=1){
		t = 0;
		inc = 125/6000/4;
		loop = up_logotext;
	};
}

function updateEffect(){
	var x,dx,y,dy,c1,c2,c,p;
	var date16 = new Date()/16000;
	var date1 = date16*16000/1000;
	var date65 = date16*16000/650;
	effectBuffer = ctxe.createImageData(256,192);
	effectData = effectBuffer.data;
	
	for(y=0;y<192*1024;y+=1024){
		dy=.015625*y/1024;
		c1 = 0.5*(Math.sin(dy+date16)+4*Math.sin(dy-date1));
		for(x=0;x<256*4;x+=4){
			dx=.015625*x/4;
			p=y+x;
			c2 = Math.sin(dx+date65)+4*Math.sin(dx+dy+date16);
			c = Math.sin(c1+c2);
			effectData[p++] = 197.640625+45.640625*c;
			effectData[p++] = 15.96875+3.96875*c;
			effectData[p++] = 207.34375-45.65625*c;
			effectData[p] = 255;
		}
	}
	ctxe.putImageData(effectBuffer,0,0);
}
function musicplay(){
	music.play();
}

function musicpause(){
	music.pause();
	logo = logo2;
	logoy = 0;
}
function makeText(p){
	msglength = msg.length;
	var puttexty = 8;
	var textwidth;
	var hw = w/2;
	ctx2.save();
	ctx2.clearRect(0,0,canvas2.width,canvas2.height);
	ctx2.font = '56px Frankfurter';
	ctx2.fillStyle = 'rgba(192,127,144,0.2)';
	ctx2.strokeStyle = '#fff';
	ctx2.textBaseline = 'top';
	ctx2.lineWidth = 3;
	for(n=0;n<msg[p].length;n++){
		twh = 0.5*ctx2.measureText(msg[p][n]).width;
		ctx2.fillText(msg[p][n],hw-twh,puttexty);
		ctx2.strokeText(msg[p][n],hw-twh,puttexty);
		puttexty+=56;
	}
	ctx2.restore();
}

function draw_lightninglogo(){
	requestAnimFrame(draw);
	ctx.save();
	ctx.clearRect(0,0,w,h);
	ctx.globalAlpha = bgalpha;
	ctx.drawImage(canvase,0,0,1024,576);
	ctx.globalAlpha = logoalpha;
	ctx.drawImage(logo,0,logoy);
	ctx.restore();
}

function draw_logotext(){
	requestAnimFrame(draw);
	ctx.save();
	ctx.drawImage(canvase,0,0,1025,577);
	ctx.drawImage(canvas2,0,texty);
	ctx.drawImage(logo,0,logoy);
	ctx.restore();
}
