/*
	Used to display a slide show
	Title:		PhotoChanger Class
	Author:		Matt McCloskey, Kemso, LLC
	Date:		2008
*/
var PhotoChanger = new Class({
	Implements: Options,
	
	options: {
		url: '',
		tag: '',
		photo_size: 'l'
	},
	
	initialize: function(element, opts){
		if(!opts) opts = {};
		this.setOptions(opts);
		
		this.container = $(element);
		this.bannerNodes = new Array();
		this.current = -1;
	},
	
	load: function(){
		new Request.JSON({url: this.options.url, onComplete: function(banners){
			this.onLoad(banners.items);
		}.bind(this)}).post({tag:this.options.tag, photo_size:this.options.photo_size});
	},
	
	onLoad: function(json){
		var banners = json;
		banners.each(function(node, index){
			
			this.addImage(node.photo, node.option2.toInt());
			
		}.bind(this));
	
		this.start();
	},
	
	addImage: function(url, dsp_length){
		// make banner div
		var b = new Element('div', {
			'class': 'banner',
			'styles': {
				'visibility': 'hidden'
			}
		})
		.inject(this.container, 'top')
		.set('tween', {duration:1000});
		
		// make image			
		var img = new Element('img', {
			'src': url,
			'alt': '',
			'title': ''
		})
		.inject(b, 'top');
			
		// add to main array
		var obj = {
			ref: b,
			time: dsp_length*1000
		};
		this.bannerNodes.push(obj);
	},
	
	start: function(){
		this.turnBanner();
	},
	
	turnBanner: function(){
		// fade old banner
		if(this.current > -1){
			this.bannerNodes[this.current].ref.fade('out');
		}
		
		// increase and wrap
		this.current++;
		if(this.current >= this.bannerNodes.length) this.current = 0;
		
		// show current
		this.bannerNodes[this.current].ref.tween('opacity', [0,1]);
		
		// if there's more than onetime for next
		if(this.bannerNodes.length > 1) {
			(function(){ this.turnBanner(); }.bind(this)).delay(this.bannerNodes[this.current].time);
		}
	}
	
});
