Event.observe(window, 'load', function() {
	
	if($('flv1') != null) placeFlash();	
	loadAccordion();
	new wallOffFame();
	new backgroundImpression()
	
	preloadImages('images/sap-productie-lijn2.jpg','images/sap-productie-lijn3.jpg','images/boost-your-bg.jpg');
	
});

function preloadImages() {
	var d=document; if(d.images){ if(!d.p) d.p=new Array();
	var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

var backgroundImpression = Class.create({
	initialize: function() {
		this.timer			= null;
		this.loader			= null;
		this.wrapper1		= $('impression');
		this.wrapper2		= $('impression2');
		this.currentCount	= 1;
		this.insertIn		= this.wrapper2;
		this.timer = setTimeout(function(){
			this.loadImage();		
		}.bind(this),2000);
	},
	loadImage: function(){
		if(this.currentCount==5) this.currentCount =1;
		new Ajax.Request('pages/impression.php',{
			parameters: 'counter='+this.currentCount,	
			onComplete: function (resp){
				this.newImage	= resp.responseText;	
				//this.newImage= new Image();
				//this.newImage.src = src;
				this.currentCount++;
				this.insertImage();
			}.bind(this)
		});
	},
	insertImage: function(){
		var images = '<img src='+this.newImage+' />';
		if(this.insertIn == $('impression2') ){
			this.wrapper2.update(images).hide();
			this.fadeIn 	= this.wrapper1;
			this.fadeOut 	= this.wrapper2;
			this.insertIn	= this.wrapper1;
		}else{
			this.wrapper1.update(images).hide();
			this.fadeOut 	= this.wrapper1;
			this.fadeIn 	= this.wrapper2;
			this.insertIn	= this.wrapper2;
		}
		
		this.loadInterval();
	},
	loadInterval: function(){
		
		this.loader 	= setTimeout(function(){
			clearInterval(this.loader);
			this.loader = null;
			this.crossFade();
		}.bind(this),2000);
		
	},
	crossFade: function(){
		
		new Effect.Parallel([
			new Effect.Fade(this.fadeIn, { sync: true}), 
			new Effect.Appear(this.fadeOut, { sync: true}) 
		],{ 
			duration: 1.0,
			afterFinish: function(){
				this.createInterval();
			}.bind(this)
		});
	},
	createInterval: function(){		
		this.timer 	= setTimeout(function(){
			clearInterval(this.timer);
			this.timer = null;
			this.loadImage();
		}.bind(this),8000);
	}
});

function placeFlash(){
	
	var params = {};
	var flashvars = {
		flvToPlay: "sap_proces.flv",
		startImage: "images/proces_start.jpg",
		hiddenGui: "false",
		autoStart: "false"
	};
	var attributes = {};
	$('flv1').update('<div id="fc1"></div>');
	swfobject.embedSWF("flash/flvPlayer.swf", "fc1", "285", "240", "7.0.0", false, flashvars, params, attributes);
	
	var params = {};
	var flashvars = {
		flvToPlay: "sapfabriek.flv",
		startImage: "images/logo_sap_film.jpg",
		hiddenGui: "false",
		autoStart: "false"
	};
	var attributes = {};
	$('flv2').update('<div id="fc2"></div>');
	
	swfobject.embedSWF("flash/flvPlayer.swf", "fc2", "285", "240", "7.0.0", false, flashvars, params, attributes);
}
function loadAccordion () {
	var elements = $$(".agenda a");
	elements.each(function(e){
		e.observe('click', accordion);
	});
}
function accordion(e) {
	Event.stop(e);
	
	var ele = Event.element(e);
	var openElement = $$('.agenda .open');
	var oE = openElement[0];
	if (!oE) {
		new Effect.BlindDown (ele.next(1), {duration:0.5});
	}
	else {
		if (!ele.hasClassName('open')) {
			oE.removeClassName('open');
			new Effect.Parallel(
				[
					new Effect.BlindUp(oE.next(1), {duration:1.0}), 
					new Effect.BlindDown(ele.next(1), {duration:0.5})
				]
			);
		}
	}
	ele.addClassName('open');
}

var wallOffFame = Class.create({
	initialize: function() {
		this.timer			= null;
		this.wrapper		= $('fameWall').down().next();
		this.itemCount		= ($$('#fameWall li').length)-1;
		this.currentFame 	= this.wrapper.down();
		this.nextFame 		= this.wrapper.down().next();
		this.currentCount	= 0;
		this.initList();
				
		this.timer = setTimeout(function(){
			this.fadeCurrent();
		}.bind(this),4000);
		
	},
	initList: function(){
		$$('#fameWall li').invoke('hide').invoke('addClassName','activated');
		$('fameWall').down().next().down().show();
	},
	fadeCurrent: function(){
		
		new Effect.Fade(this.currentFame,{
			duration:1.0,
			afterFinish: function(){
				this.showNext();				
			}.bind(this)
		});
		
	},
	showNext: function(){
		
		new Effect.Appear(this.nextFame,{
			duration:1.0,
			afterFinish: function(){
				this.currentCount++;
				this.createInterval();
			}.bind(this)
		});
		
	},
	createInterval: function(){
		
		this.timer 	= setTimeout(function(){
			clearInterval(this.timer);
			this.timer = null;
			this.resetVars();
		}.bind(this),8000);
		
	},
	resetVars: function(){
	
		if(this.currentCount == this.itemCount){
			this.currentFame 	= this.nextFame;
			this.nextFame 		= this.wrapper.down();
			this.currentCount	= -1;
		}else{
			this.currentFame 	= this.nextFame;
			this.nextFame		= this.wrapper.down().next(this.currentCount);
		}
		
		this.fadeCurrent();
		
	}
});
