
var Demo = function() {
}
var sippeKerroin=0;
	var bpm = 125;
	var beat = 60/bpm;
	var pattern = beat*8;
	var tick = beat/4;
var flashtexts = [
		"data/kiky.png",
		"data/chempolis.png",
		"data/yle.png",
		"data/koulutuslupaus.png",
		"data/terrafame.png",
		"data/vakuutuskuori.png"
];

var currentText = 0;
	

Demo.prototype.init = function()
{

			this.loader.addAnimation([
		{
			"start": 0, "duration":1*pattern
			,"layer": 1, "image": ["data/jumalauta.png"]
			,"scale":[{"x":1.0,"y":1.0}]
		}]);

					this.loader.addAnimation([
		{
			"start": pattern, "duration":pattern
			,"layer": 1, "image": ["data/presents.png"]
			,"scale":[{"x":1.0,"y":1.0}]
		}]);
		
					this.loader.addAnimation([
		{
			"start": 2*pattern, "duration":pattern
			,"layer": 1, "image": ["data/juha.png"]
			,"scale":[{"x":1.0,"y":1.0}]
		}]);
		
					this.loader.addAnimation([
		{
			"start": 3*pattern, "duration":pattern
			,"layer": 1, "image": ["data/white.png"]
			,"color":[{"a":55}]
			,"scale":[{"x":1.0,"y":1.0}]
		}]);
		
		
	this.loader.addAnimation([
		{
			"start": 0, "duration":42*pattern
			,"layer": 49999, "image": ["data/vignette.png"]
			,"scale":[{"x":1.0,"y":1.0}]
		}]);
	
	this.loader.addAnimation([
    {
         "start": 8*pattern, "duration":4*pattern
        ,"layer": 300, "image": ["data/suomikuntoon.png"]
		,"scale":[{"x":3.0,"y":3.0}]
     ,"shader":{
            "name":"data/shader/mirrorscroll.fs",
            "variable":[
                  {"name":"time","value":["{return getSceneTimeFromStart();}"]}
				 ,{"name":"speedX","value":[1.0]}
				 ,{"name":"speedY","value":[1.0]}
            ]
        }
		
    }
]);

	

	this.loader.addAnimation([
    {
         "start": 6*pattern, "duration":2*pattern
        ,"layer": 300, "image": ["data/suomikuntoon.png"]
		,"scale":[{"x":3.0,"y":3.0}]
     ,"shader":{
            "name":"data/shader/mirrorscroll.fs",
            "variable":[
                  {"name":"time","value":["{return getSceneTimeFromStart();}"]}
				 ,{"name":"speedX","value":[0.0]}
				 ,{"name":"speedY","value":[-2.0]}
            ]
        }
		
    }
]);

	this.loader.addAnimation([
    {
         "start": 20*pattern, "duration":4*pattern
        ,"layer": 300, "image": {"name":"data/suomikuntoon2.ogv", "video":{"speed":.9} }
		,"scale":[{"x":4.0,"y":4.0}]
			,"angle": [{"degreesZ":"{return Math.sin(getSceneTimeFromStart()*.5)*360}"}]
     ,"shader":{
            "name":"data/shader/mirrorscroll.fs",
            "variable":[
                  {"name":"time","value":["{return getSceneTimeFromStart();}"]}
				 ,{"name":"speedX","value":[1.0]}
				 ,{"name":"speedY","value":[2.0]}
	
            ]
        }
		
    }
]);

this.fullScreenNoise(0,pattern*40);


for(var i=0;i<30;i++)
{
	this.loader.addAnimation([
		{
			"start": 8*pattern+0+i*beat, "duration":1*beat
			,"layer": 500, "image": ["data/sipi.png"]
			,"beat": beat
			
			,"angle": [{"degreesZ":Math.random()*360}
			,{"duration":1*pattern,"degreesZ":Math.random()*360}]
			,"scale":[{"x":"{return (getSceneTimeFromStart()-8*pattern)-( )*(getSceneTimeFromStart()-8*pattern)*1.2}","y":"{return (getSceneTimeFromStart()-8*pattern)-((getSceneTimeFromStart())%beat)*(getSceneTimeFromStart()-8*pattern)*1.2}"}]
			,"position":[{"x":640,"y":360,"z":0}]
			}
		]);
}	
for(var i=30;i<32;i++)
{
	this.loader.addAnimation([
		{
			"start": 8*pattern+0+i*beat, "duration":1*beat
			,"layer": 500, "image": ["data/berner.png"]
			,"beat": beat
			,"scale":[{"x":"{return 5-((getSceneTimeFromStart())%beat)*5.0}","y":"{return 5-((getSceneTimeFromStart())%beat)*5.0}"}]
			,"position":[{"x":640,"y":360,"z":0}]
			}
		]);
}	
	
this.piikkiPallo(pattern*4);
this.piikkiPallo(pattern*6);


		/*
			this.loader.addAnimation([
		{
			"start": 0, "duration":42*pattern
			,"layer": 1, "image": ["data/white.png"]
			,"scale":[{"x":1.0,"y":1.0}]
		}]);
		*/
		
		/*
for (var i=0;i<10;i++)
{
	this.loader.addAnimation([
		{
			"start": 0, "duration":42*pattern
			,"layer": 500+i, "image": ["data/sipi.png"]
			,"scale":[{"x":1.0,"y":1.0}]
			//,"position":[{"x":"{return (Math.sin(getSceneTimeFromStart()*2))*320+640;}","y":"{return (Math.abs(-.5+Math.sin(getSceneTimeFromStart())))*180+360;}"}]
			,"position":[{"x":"{return sippeScrollX()}","y":"{return sippeScrollY()}"}]
			//,"position":[{"x":"{return sipiScrollX(getSceneTimeFromStart())}","y":"{return (Math.abs(Math.sin(getSceneTimeFromStart())))*360+180;}"}]
		}]);
}

*/




this.loader.addAnimation([
	{
		"start": 12*pattern, "duration": 8*pattern

	,"image": {"name":"data/suomikuntoon2.ogv", "video":{"speed":0.48825} }
	,"scale":[{"x":1.7,"y":1.7}]
		,"layer": 1
	}]);


		


	
		
this.distortion(0,pattern*38.5,0,50000,50002);

this.loader.addAnimation([
		{
			"start": 38*pattern+2*beat, "duration":4*pattern
			,"layer": 49999, "image": ["data/end.png"]
			,"scale":[{"x":1.3,"y":1.3}]
		}]);
		

		
			this.loader.addAnimation([
		{
			"start": 28*pattern, "duration":0.49*pattern
			,"layer": 1, "image": ["data/kekkonen.png"]
			,"scale":[{"x":0.6,"y":0.6}]
		}]);
		
		this.loader.addAnimation([
		{
			"start": 28.5*pattern, "duration":0.49*pattern
			,"layer": 1, "image": ["data/mannerheim.png"]
			,"scale":[{"x":0.5,"y":0.5}]
		}]);

			this.loader.addAnimation([
		{
			"start": 29*pattern, "duration":0.49*pattern
			,"layer": 1, "image": ["data/hitler.png"]
			,"scale":[{"x":0.45,"y":0.45}]
		}]);
		
		this.loader.addAnimation([
		{
			"start": 29.5*pattern, "duration":0.49*pattern
			,"layer": 1, "image": ["data/juhasipila.png"]
			,"scale":[{"x":0.85,"y":0.85}]
		}]);
		
		
//this.glow(0,pattern*38.5,50006,50010,50019);
this.hackGlow(0,pattern*38.5,50006,50008,50010);
this.brightnessContrast(0,pattern*38.5,50001,50004,50007,0.5,3.5);


}

Demo.prototype.kuva = function(startTime, duration, pic)
{
	this.loader.addAnimation([
		{
			"start": startTime, "duration":duration
			,"layer": 65500, "image": [pic]
			,"scale":[{"x":1.5,"y":1.5}]
		}]);
}

/*
	this.loader.addAnimation([
    {
         "start": startTime, "duration":6*pattern
        ,"layer": 40000, "image": ["data/noise.png","data/noise.png"]
		,"scale":[{"x":1.0,"y":1.0}]
     ,"shader":{
            "name":"data/shader/fire.fs",
            "variable":[
                  {"name":"time","value":["{return "{return getSceneTimeFromStart();}"]}
            ]
        }
		
    }
]);
*/


Demo.prototype.distortion = function(startTime, duration, startLayer, endLayer, layer)
{
	this.loader.addAnimation ([
	{
	"start": startTime, "duration": duration
	,"layer": startLayer
	,"fbo":{"name":"fbodist","action":"begin"}
	},
	{
	"start": startTime, "duration": duration
	,"layer": endLayer,"fbo":{"name":"fbodist","action":"unbind"}
	}
	]);
	this.loader.addAnimation ([
	{
		 "start": startTime, "duration": duration
		,"image": "fbodist.color.fbo"
		,"layer": layer
		,"shader":{"name":"data/shader/distortion.fs",
			"variable":[
				 {"name":"time","value":["{return getSceneTimeFromStart();}"]}
				,{"name":"timeMultiplier","value":[20.9]}
				,{"name":"pixelSize","value":["{return Math.random()*0.02;}",0.001]}
				,{"name":"noiseWaveSpeed","value":[-3]}
				,{"name":"noiseWaveSize","value":[1]}
				,{"name":"noiseLuminance","value":[1]}
				,{"name":"noiseAlpha","value":[0.0]}
				,{"name":"colorComponentDistortionX","value":[0.02,0.02,0.02,0.02]}
				,{"name":"colorComponentDistortionY","value":[0.02,0.02,0.02,0.02]}
			]
			}
		}]);
}

Demo.prototype.glow = function(startTime, duration, startLayer, endLayer, layer)
{
this.loader.addAnimation([
	//render contents rendered in layers 00000 - 00050 to FBO "fbo"
	{
		 "start": startTime, "duration":duration
		,"layer": startLayer
		,"fbo":{"name":"fboGlow","action":"begin"}
	},
	{
		 "start": startTime, "duration":duration
		,"layer": endLayer
		,"fbo":{"name":"fboGlow","action":"unbind"}
	},
	//Render "fbo" via Glow shader (first pass) to "fbo2"
	{
		 "start": startTime, "duration":duration
		,"layer": layer-4
		,"fbo":{"name":"fboGlow2","action":"begin"}
	},
	{
		"start": startTime, "duration":duration
		,"layer": layer-3
		,"fbo":{"name":"fboGlow","action":"draw"}
		,"shader":{"name":"data/shader/glow.fs","variable":[
					 {"name":"direction","value":[0,1]}
					,{"name":"alpha","value":[0.5]}
					,{"name":"spread","value":[0.0025]}
					,{"name":"intensity","value":[0.15]}
					,{"name":"samples","type":"int","value":[15]}
				]}
	},
	{
		 "start": startTime, "duration":duration
		,"layer": layer-2
		,"fbo":{"name":"fboGlow2","action":"unbind"}
	},
	//Draw the original "fbo" to screen
	{
		 "start": startTime, "duration":duration
		,"layer": layer-1
		,"fbo":{"name":"fboGlow","action":"draw"}
		,"color": [
				 {"r":255,"g":0,"b":0}
			]
	},
	//Draw "fbo2" via Glow shader (second pass) to screen
	{
			"start": startTime, "duration":duration
		,"layer": layer
		,"fbo":{"name":"fboGlow2","action":"draw"}
		,"shader":{"name":"data/shader/glow.fs","variable":[
					 {"name":"direction","value":[1,0]}
					,{"name":"alpha","value":[0.7]}
					,{"name":"spread","value":[0.0025]}
					,{"name":"intensity","value":[0.35]}
					,{"name":"samples","type":"int","value":[15]}
				]}
	}
	]);
}


Demo.prototype.brightnessContrast = function (startTime, duration, startLayer, endLayer, layer, brightness, contrast)
{
	this.loader.addAnimation ([
	{
	"start": startTime, "duration": duration
	,"layer": startLayer
	,"fbo":{"name":"fbobc","action":"begin"}
	},
	{
	"start": startTime, "duration": duration
	,"layer": endLayer,"fbo":{"name":"fbobc","action":"unbind"}
	}
	]);
	this.loader.addAnimation ([
	    {
	         "start": startTime, "duration":duration
	        ,"layer": layer, "image": ["fbobc.color.fbo"]
		 ,"initFunction":"{fboBcImage = imageLoadImage('fbobc.color.fbo'); setTextureSizeToScreenSize(fboBcImage.ptr); }"
		 ,"runFunction":"{setTextureSizeToScreenSize(fboBcImage.ptr);}"
		 ,"brightness": brightness
		 ,"contrast": contrast
		 ,"beat": beat
	     ,"shader":{
	            "name":"data/shader/brightnesscontrast.fs",
	            "variable":[
	                  {"name":"time","value":["{return getSceneTimeFromStart();}"]}
          ,{"name":"brightness","value":["{return animation.brightness-(getSceneTimeFromStart()%animation.beat)*2}"]}
					 ,{"name":"contrast","value":[contrast]}
	            ]
	        }
	    }
	]);
}

Demo.prototype.edgeGlow = function (startTime, duration, startLayer, endLayer, layer)
{
	this.loader.addAnimation ([
	{
	"start": startTime, "duration": duration
	,"layer": startLayer
	,"fbo":{"name":"fboeg","action":"begin"}
	},
	{
	"start": startTime, "duration": duration
	,"layer": endLayer,"fbo":{"name":"fboeg","action":"unbind"}
	}
	]);
	this.loader.addAnimation ([
	    {
	         "start": startTime, "duration":duration
	        ,"layer": layer, "image": ["fboeg.color.fbo"]
		 ,"initFunction":"{fboegImage = imageLoadImage('fboeg.color.fbo'); setTextureSizeToScreenSize(fboegImage.ptr); }"
		 ,"runFunction":"{setTextureSizeToScreenSize(fboegImage.ptr);}"
	     ,"shader":{
	            "name":"data/shader/edgeglow.fs",
	            "variable":[
	                  {"name":"time","value":["{return getSceneTimeFromStart();}"]}
	            ]
	        }
	    }
	]);
}

Demo.prototype.piikkiPallo = function (startTime)
{
		for(var i=0;i<4;i++)
		{
		this.pomppuObu(startTime+i*beat,200, 0, 0, "data/spikeball.obj");
		}
		
		for(var i=0;i<4;i++)
		{
		this.pomppuObu(4*beat+startTime+i*beat,180, -200, 0, "data/triplatorus.obj");
		this.pomppuObu(4*beat+startTime+i*beat,180, 200, 0, "data/triplatorus.obj");
		}
		
		for(var i=0;i<4;i++)
		{
		this.pomppuObu(8*beat+startTime+i*beat,200, -200, -200, "data/spikeball.obj");
		this.pomppuObu(8*beat+startTime+i*beat,200, 200, -200, "data/spikeball.obj");
		this.pomppuObu(8*beat+startTime+i*beat,200, -200, 200, "data/spikeball.obj");
		this.pomppuObu(8*beat+startTime+i*beat,200, 200, 200, "data/spikeball.obj");
		}
		for(var i=0;i<4;i++)
		{
		this.pomppuObu(12*beat+startTime+i*beat,150, -150, -150, "data/triplatorus.obj");
		this.pomppuObu(12*beat+startTime+i*beat,150, 150, -150, "data/spikeball.obj");
		this.pomppuObu(12*beat+startTime+i*beat,150, -150, 150, "data/spikeball.obj");
		this.pomppuObu(12*beat+startTime+i*beat,150, 150, 150, "data/triplatorus.obj");
		this.pomppuObu(12*beat+startTime+i*beat,150, -450, -150, "data/spikeball.obj");
		this.pomppuObu(12*beat+startTime+i*beat,150, 450, -150, "data/triplatorus.obj");
		this.pomppuObu(12*beat+startTime+i*beat,150, -450, 150, "data/triplatorus.obj");
		this.pomppuObu(12*beat+startTime+i*beat,150, 450, 150, "data/spikeball.obj");
		}
}

Demo.prototype.pomppuObu = function(startTime, scale, positionX, positionY, obu)
{
			this.loader.addAnimation([
					{
					"start": startTime, "duration": 3*tick, "object":obu
					,"layer": 20000
					,"position": [{"z":-900,"y":positionY,"x":positionX}]
					,"angle": [{"degreesX":Math.random()*720-360,"degreesY":Math.random()*720-360,"degreesZ":"{return (Math.sin(getSceneTimeFromStart()*.5))*360}"}]

					,"scale": [
							{"x":scale,"y":scale,"z":scale}
							, {"duration":beat,"x":scale*.25,"y":scale*.25,"z":scale*.25}
						]
					}]);
}

Demo.prototype.fullScreenNoise = function (sceneStartTime, duration)
{
	this.loader.addAnimation([
    {
         "start": sceneStartTime, "duration":duration
        ,"layer": 49995, "image": ["data/empty.png"]
     ,"shader":{
            "name":"data/shader/noise.fs",
            "variable":[
                  {"name":"time","value":["{return getSceneTimeFromStart();}"]},
				  {"name":"alphaBlending","value":[0.15]}
            ]
        }
    }
]);
}

Demo.prototype.hackGlow = function (startTime, duration, startLayer, endLayer, layer)
{
	this.loader.addAnimation ([
	{
	"start": startTime, "duration": duration
	,"layer": startLayer
	,"fbo":{"name":"fbohg","action":"begin"}
	},
	{
	"start": startTime, "duration": duration
	,"layer": endLayer,"fbo":{"name":"fbohg","action":"unbind"}
	}
	]);
	this.loader.addAnimation ([
	    {
	         "start": startTime, "duration":duration
	        ,"layer": layer, "image": ["fbohg.color.fbo"]
		 ,"initFunction":"{fbohgImage = imageLoadImage('fbohg.color.fbo'); setTextureSizeToScreenSize(fbohgImage.ptr); }"
		 ,"runFunction":"{setTextureSizeToScreenSize(fbohgImage.ptr);}"
	     ,"shader":{
	            "name":"data/shader/hackglow.fs",
	            "variable":[
	                  {"name":"time","value":["{return getSceneTimeFromStart();}"]}
	            ]
	        }
	    }
	]);
}



function sippeScrollX()
{
sippeKerroin++;
if(sippeKerroin>11)sippeKerroin=0;
print(sippeKerroin);
return ((sippeKerroin*.090909090909091+(.7*getSceneTimeFromStart()))%1.1*(1280));

}
function sippeScrollY()
{
return (Math.abs(Math.sin(sippeKerroin+getSceneTimeFromStart())))*360+180;
}