var Home = new Class({
	initialize: function() {		  
		this.firstRun = true;
		this.interval = 2000;
		this.active = 0;
		this.getSponsors();
	},
    
	getSponsors: function(){		
		new Request.JSON({
			url: "includes/getSponsors.asp", 
			onComplete: function(json){
				this.items = json.results;				
				if(this.items.length>0)	this.build();	
				this.getHockeyNieuws();
			}.bind(this)
		}).get();	
	},
	
	getHockeyNieuws: function(){
		$('hockeynieuws').load('/includes/hockeynieuws.asp');
	},
	
	build: function(){
		this.container = new Element("div").setStyle('position','relative').inject($("sponsor"));			
		this.shedule();
	},
	
	shedule: function(){
		this.preload();	
	},	
	
	preload: function(){
		var img = new Element("img", {
			src: "picture.asp?kenmerk=sponsor&volgnr="+this.items[this.active].sponsor_volgnr+"&anticache=" + $random(1,99999)+"",			
			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);		
		el.setStyles({
			'position':'absolute',
			'top':60-(img.getCoordinates().height / 2),
			'left':0
		});	
		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);
	}	
});