/**************************************
 *	Fadeshow
 **************************************/

var Fadeshow = new Class({
	object:				null,
	data: 				null,
	index: 				0,
	effect: 			null,
	periodicalTimer: 	null,
	
	setOptions: function(options){
		this.options = {
			interval: 5000,
			duration: 2000
		}
		Object.extend(this.options, options || {});
	},
	
	initialize: function(object, data, options){
		this.object = object;
		this.data = data;
		
		this.setOptions(options);
		if (this.options.duration > this.options.interval) { this.options.interval = this.options.duration + 2000 }
		
		this.showNew();
		if(this.data.length > 1){
			this.periodicalTimer = this.transist.periodical(this.options.interval, this);
		}
	},
	
	transist: function(){
		//Fade out
		if(window.macgecko){
			this.object.setStyle('visibility', 'hidden');
			this.showNew();
		}else{
			this.effect = new Fx.Style(this.object, 'opacity', {duration: this.options.duration / 4, onComplete: this.showNew.bind(this)});
			this.effect.start(1,0);
		}
	},
	
	showNew: function(){
		//Set new image + url
		this.setCurrentData(this.data[this.index]);
		
		//Fade in
		if(window.macgecko){
			this.object.setStyle('visibility', 'visible');
		}else{
			this.effect = new Fx.Style(this.object, 'opacity', {duration: this.options.duration});
			this.effect.start(0,1);
		}
		
		//Next index
		if(this.index == this.data.length -1) { 
			this.index = 0;
		} else { 
			this.index++;
		}
	},
	
	setCurrentData: function(data){
		// Image
		img = this.object.getElement('img');
		if(data.image){
			img.src = '/uploads/images/120sc/' + data.image;
		}else{
			img.src = '/uploads/images/120sc/no-image.gif';	
		}
		
		// Info
		this.object.getElement('div.info').innerHTML = data.description + '...';
		
		// Title
		if(data.type == 'building'){
			this.object.getElement('h4').innerHTML = data.estate + '<em>' + data.city + '</em>';
		}else{
			this.object.getElement('h4').innerHTML = data.name;	
		}
		
		// More info
		this.object.getElements('a').each(function(link){
			if(data.type == 'building'){
				link.href = '/gebouwen/' + data.quicklink + '/';
			}else{
				link.href = '/projecten/' + data.quicklink + '/';	
			}
		});
	}
})

fireOn = Window.ie ? 'load' : 'domready';
window.addEvent(fireOn, function(){
	if((navigator.appVersion.indexOf("Mac") != -1) && (window.gecko)){
		window.macgecko = true;
	}
	
	// Load fadeshow
	var spotlight
	if(spotlight = $('spotlight')){
		var data		= spotlight.getElement('input[name=fadeshowData]').value;
		new Fadeshow(spotlight, Json.evaluate(data) , {duration: 2000, interval: 8000} );
	}
});