var TICK;

/*


---


*/





























function getStarRotate(animation)
{
	var time = getSceneTimeFromStart();
	var time_duration = 3.5;
	if (time < time_duration) {return 0;}
	time -= time_duration;
	return 180*Math.sin(time*(0.1+animation.column*0.5+animation.row*0.5));
}

Demo.prototype.initUsFlag = function(layer, start, end)
{
	var fade_out = TICK;

	this.loader.addAnimation([
	{
		 "start": start, "duration": end
		,"image": "data/us_background.png"
		,"layer": layer
		,"color":[{},{"duration":end-fade_out},{"duration":fade_out,"a":0}]
	}]);

	layer += 10;

	this.loader.addAnimation([
	{
		 "start": start, "duration": end
		,"image": "data/us_background_blue.png"
		,"layer": layer
		,"color":[{},{"duration":end-start-fade_out},{"duration":fade_out,"a":0}]
	}]);

	var start_start_x = 43;
	for(var r1 = 0; r1 < 5; r1++) {
		for(var c1 = 0; c1 < 6; c1++) {
			this.loader.addAnimation([
			{
				 "start": start, "duration": end
				,"image": "data/star_white.png"
				,"layer": layer
				,"scale": [{"uniform2d":0.065}]
				,"position": [{"x":start_start_x+c1*85, "y":661-r1*72}]
				,"row": r1
				,"column": c1
				,"angle": [{"degreesZ":"{return getStarRotate(animation);}"}]
				,"color":[{},{"duration":end-fade_out},{"duration":fade_out,"a":0}]
			}]);
		}
	}

	for(var r2 = 0; r2 < 4; r2++) {
		for(var c2 = 0; c2 < 5; c2++) {
			this.loader.addAnimation([
			{
				 "start": start, "duration": end
				,"image": "data/star_white.png"
				,"layer": layer
				,"scale": [{"uniform2d":0.065}]
				,"position": [{"x":start_start_x+43+c2*85, "y":661-35-r2*72}]
				,"row": r2
				,"column": c2
				,"angle": [{"degreesZ":"{return getStarRotate(animation);}"}]
				,"color":[{},{"duration":end-fade_out},{"duration":fade_out,"a":0}]
			}]);
		}
	}
}

Demo.prototype.init = function()
{
	TICK = timerGetBeatInSeconds();

	var layer = 10;
	this.initUsFlag(layer, 0, 30);

	var rainbow_start = TICK*34;
	var rainbow = [
		 {"r":0xed,"g":0x32,"b":0x37,"start":rainbow_start+TICK*1}
		,{"r":0xf5,"g":0x86,"b":0x34,"start":rainbow_start+TICK*1}
		,{"r":0xff,"g":0xf2,"b":0x12,"start":rainbow_start+TICK*2}
		,{"r":0x00,"g":0xa8,"b":0x59,"start":rainbow_start+TICK*2}
		,{"r":0x00,"g":0xaf,"b":0xef,"start":rainbow_start+TICK*3}
		,{"r":0x3e,"g":0x40,"b":0x95,"start":rainbow_start+TICK*3}
		,{"r":0x81,"g":0x5a,"b":0xa4,"start":rainbow_start+TICK*3}
	];

	var us = [
		 {"r":0xd5,"g":0x2b,"b":0x1e}
		,{"r":0xff,"g":0xff,"b":0xff}
		,{"r":0x00,"g":0x38,"b":0xa8}
	]

	var paraguay = [
		 {"r":0xb2,"g":0x22,"b":0x34}
		,{"r":0xff,"g":0xff,"b":0xff}
		,{"r":0x3c,"g":0x3b,"b":0x6e}
	]

	for(var i = 0; i < rainbow.length; i++) {
		var color = rainbow[i];
		var scale_y = 1.89;
		this.loader.addAnimation([
		{
			 "start": color.start, "duration": 30
			,"image": "data/white_stripe.png"
			,"layer": layer
			,"color": [color]
			,"scale": [{"y":scale_y}]
			,"position": [
				 {"y":675-52/2.-51*i*scale_y,"x":1280+1280/2.}
				,{"duration":TICK,"x":1280/2}
			]
		}]);
	
	}

	layer = 200;

	var paraguay_flag_start = TICK*50;
	var logo_fade_in = TICK*5;
	this.loader.addAnimation([
	{
		 "start": paraguay_flag_start, "duration": 100
		,"image": "data/paraguay_logo.png"
		,"layer": layer
		,"color": [{"a":0},{"duration":logo_fade_in,"a":255}]
		,"scale":[{"uniform2d":5},{"duration":logo_fade_in,"uniform2d":0.25}]
	}]);

	paraguay_flag_start += logo_fade_in + TICK*20;
	for(var i = 0; i < paraguay.length; i++) {
		var color = paraguay[i];
		var scale_y = 4.42;
		this.loader.addAnimation([
		{
			 "start": paraguay_flag_start+i/2, "duration": 30
			,"image": "data/white_stripe.png"
			,"layer": layer-1
			,"color": [color]
			,"scale": [{"y":scale_y}]
			,"position": [{"y":609-52/2.-51*i*scale_y,"x":1280+1280/2.}
				,{"duration":logo_fade_in,"x":1280/2}
			]
		}]);
	}

	var america_first_start = paraguay_flag_start + TICK*15;
	layer = 300;
	this.loader.addAnimation([
	{
		 "start": america_first_start, "duration": 300
		,"image": ["data/america_first_paraguay_second.png","data/america_first_jumalauta_second.png"]
		,"layer": layer
		,"angle": [{},{"duration":TICK*10,"degreesZ":360*20}]
		,"scale": [{"uniform2d":5},{"duration":TICK*5,"uniform2d":0.55}]
		,"color": [{"a":0},{"duration":TICK*5,"a":255}]
		,"shader":{"name":"data/shader/distortion.fs",
			"variable":[
				 {"name":"time","value":["{return getSceneTimeFromStart();}"]}
				,{"name":"timeMultiplier","value":[0.0002]}
				,{"name":"pixelSize","value":[0.035,0.035]}
				,{"name":"noiseWaveSpeed","value":[-3]}
				,{"name":"noiseWaveSize","value":[5]}
				,{"name":"noiseLuminance","value":[1]}
				,{"name":"texture2","type":"int","value":[1]}
				,{"name":"noiseAlpha","value":[0.2]}
				,{"name":"colorComponentDistortionX","value":[0.02,0.02,0.02,0.02]}
				,{"name":"colorComponentDistortionY","value":[0.02,0.02,0.02,0.02]}
			]
		}
	}]);

	this.loader.addAnimation([
	{
		 "start": america_first_start+TICK*20, "duration": 30
		,"image": "data/trump.png"
		,"layer": layer
		,"scale":[{"uniform2d":0.7}]
		,"position":[{"y":720/2*0.5,"x":100}]
		,"color": [{"a":0},{"duration":TICK*2,"a":255}]
	}]);

	//this.preInitRadar(52,20);

	var textScale = 3;
	var scroller = "THE DEMOCRATIC PEOPLE'S REPUBLIC OF JUMALAUTA SENDS BIRTHDAY GREETINGS TO RIMINA OF THE GLORIOUS SOCIALIST PEOPLE'S DEMOCRATIC REPUBLIC OF PARAGUAY - LET THE GLORIOUS SUN RISE ABOVE ALL OUR NATIONS AND MAKE THEM GREAT AGAIN";
	this.loader.addAnimation([
	{
		 "start": 5, "duration": 38
		,"layer": 201, "text":{"name":"arialbd.ttf","string":scroller}
		//,"layer": 100, "text":{"name":"arialbd.ttf","string":scroller}
		,"scale": [
			  {"uniform2d":textScale}
		]
		,"color": [
			  {"r":0,"g":0,"b":0}
		]
		,"position":[
			{"x":scroller.length*25*textScale,"y":getScreenHeight()/2.0}
			,{"duration":30,"x":-scroller.length*25*textScale-getScreenWidth()*3}
		]
	}]);


	layer = 250;
	this.loader.addAnimation([
	{
		 "start": 0, "duration": 300
		,"layer": layer
		,"passToFbo":{"name":"fbo", "beginLayer":0, "endLayer":layer}
	}]);

	var fade = TICK*2;
	this.loader.addAnimation([
	{
		 "start": 0, "duration": 300
		,"image": "fbo.color.fbo"
		,"layer": layer
		,"shader":{"name":"data/shader/wavy.fs",
			"variable":[
			    {"name":"time","value":["{return getSceneTimeFromStart();}"]}
			]
		}
	}]);


	layer = 500;
	this.loader.addAnimation([
    {
         "start": 0, "duration":300
        ,"layer": layer, "image": ["data/empty.png"]
				,"color":[{"a":0}]
		,"shader":{"name":"data/shader/noise.fs",
            "variable":[
                  {"name":"time","value":["{return getSceneTimeFromStart();}"]},
				  {"name":"alphaBlending","value":[0.05]}
            ]
        }
    }]);
	this.loader.addAnimation([
	{
		 "start": 0, "duration": 300
		,"image": "data/vignette.png"
		,"layer": layer
	}]);

}
