var Klanten = new Class({
	initialize: function() {		  
		this.firstRun = true;
		this.interval = 4000;
		this.active = 0;
		this.getKlanten();
	},
    
	getKlanten: function(){		
		new Request.JSON({
			url: "includes/getKlanten.asp", 
			onComplete: function(json){
				this.items = json.results;				
				if(this.items.length>0)	this.build();	
			}.bind(this)
		}).get();	
	},		
	
	build: function(){
		this.container = $("onzeklanten");			
		this.shedule();
	},
	
	shedule: function(){
		this.preload();	
	},	
	
	preload: function(){
		var src;
		if(this.items[this.active].afbeeldingfilename1 == "" || this.items[this.active].afbeeldingfilename1 == null){
			src ="images/geenlogo.gif";
		} else {
			src = "picture.asp?kenmerk=klantsmall&volgnr="+this.items[this.active].volgnr+"&anticache=" + $random(1,99999)+"";
		}
		var img = new Element("img", {
			src: src,			
			events: {
				'load':function(){
					this.makeElement(img);
				}.bind(this)
			}
		});
	},
	
	makeElement: function(img){
		var item = this.items[this.active];
		//if(item.sponsor_url!=""){
			//var a = new Element("a",{
				//href:item.sponsor_url+"",
				//target:"_blank"
			//});
			//img.inject(a);
			//var el = a;
		//} else {
			var el = img;
		//}
		this.showImage(el,img);
	},
	
	showImage: function(el,img){
		el.setStyle('opacity',0);		
		var old = this.container.getElement("img");
		if(old){
			new Fx.Tween(old,{
				transition:Fx.Transitions.easeOut,
				onComplete:function(){
					old.dispose();
				}
			}).start("opacity",0);
		}
		el.inject(this.container);		
		new Fx.Tween(el,{transition:Fx.Transitions.easeIn}).start("opacity",1);
		this.active = this.active + 1;
		if(this.active == this.items.length) this.active = 0;		
		this.preload.delay(this.interval,this);
	}	
});
