var fotowissel = new Class({

	initialize: function(list) {
		this.list = $(list);		
		this.firstRun = true;
		this.interval = 10000;
		this.active = 0;
		this.getItems();
	},
	
	getItems: function(){
		var req1 = new Request.JSON({			
			onComplete: function(response){				
				this.setArray(response);
			}.bind(this),
			url:"includes/getHome.asp",
			method:'get'
		}).send();		
	},
	
	setArray: function(data){
		if(data!=null){
			this.items = data.results;
			this.shuf(this.items);
			if(this.items.length>0){
				if(this.list!=null)
					this.build();
			}
		}	
	},
	
	 shuf: function ( myArray ) {
	  var i = myArray.length;
	  if ( i == 0 ) return false;
	  while ( --i ) {
		 var j = Math.floor( Math.random() * ( i + 1 ) );
		 var tempi = myArray[i];
		 var tempj = myArray[j];
		 myArray[i] = tempj;
		 myArray[j] = tempi;
	   }
	},

	build: function(){
		this.container = $('foto_home');
		this.container.set('morph');
		this.container.setStyle('opacity',0);
		this.textcontainer = $('fototekst_home');
		this.textcontainer.set('morph');
		this.textcontainer.setStyle('opacity',0);
		this.shedule();
	},
	
	shedule: function(){
		var firstItem = this.items[0];
		if(this.firstRun==true){
			this.fadeIn(firstItem);
		}
		this.elSched = this.run.periodical(
			this.interval,
			this,
			this.items
		);
		
		listEvents = this.list.$events;
		if(!listEvents)
			this.setMouseEvents();
		else if((!listEvents.mouseenter) && (!listEvents.mouseleave))
			this.setMouseEvents();
	},
	
	setMouseEvents: function(){  
		this.container.addEvents({
			'mouseenter': this.pause.bind(this),
			'mouseleave': this.resume.bind(this)
		});
	},
	
	pause: function(){
		//this.elSched = $clear(this.elSched);
		var size = this.container.getScrollSize().y;
		if(size > this.height){			
			//this.container.morph({'height': size});
		}
	},
	
	resume: function(){		
		//this.shedule();
		//this.container.morph({'height': this.height});
	},	
	
	run: function(){
		if(this.items[1]){
			this.active = this.active + 1;
			if(this.active == this.items.length){
				this.active = 0;
			}
			var activeItem = this.items[this.active];
			this.container.set('tween', {
				duration: 'long',
				onComplete: function(e) {
					e.empty();
					var src = activeItem.src;					
					var img = new Element('img').set("src","picture.asp?kenmerk=home&volgnr="+src).inject(e);
					e.set('tween', { 
						duration: 'long',
						onComplete: Class.empty
					});
					img.addEvent('load',function(){
						e.tween('opacity',1);
					});
				}
			},this);
			this.container.tween('opacity',0);
			/*
			this.textcontainer.set('tween', {
				duration: 'long',
				onComplete: function(e) {					
					var cat = activeItem.cat;
					var titel = activeItem.titel;
					var req3 = new Request({			
						onComplete: function(response){
							var text = response;
							e.set('html',"<h2 style='text-transform:uppercase'>"+titel+"</h2>"+text);
							e.set('tween', { 
								duration: 'long',
								onComplete: Class.empty
							});
							e.tween('opacity',1);
						}.bind(e),
						url:"includes/getHomeTekst.asp",
						method:'get'
					}).send('categorie='+cat+'');					
				}
			},this);
			this.textcontainer.tween('opacity',0);
			*/
		}
	},
	
	fadeIn: function(item){				
		var src = item.src;		
		var titel = item.titel;
		var cat = item.cat;
		var req2 = new Request({			
			onComplete: function(response){
				var text = response;
				var img = new Element('img').set("src","picture.asp?kenmerk=home&volgnr="+src).inject($("foto_home"));
				$('fototekst_home').set('html',text);
				img.addEvent('load',function(){
					new Fx.Tween(this.container, {duration: 'long'}).start('opacity',1);
					new Fx.Tween(this.textcontainer, {duration: 'long'}).start('opacity',1);
				}.bind(this));		
				this.firstRun = false;
			}.bind(this),
			url:"includes/getHomeTekst.asp",
			method:'get'
		}).send('categorie='+cat+'');
		
	}
});

fotowissel.implement(new Events);