/*
	Used to display a slide show
	Title:		PhotoChanger Class
	Author:		Matt McCloskey, Kemso, LLC
	Date:		2008
*/
var PhotoChanger = new Class({
	Implements: [Options, Events],
	
	options: {
		url: '',
		tag: '',
		photo_size: 'l',
		imageClass: 'photo',
		dsp_length: 7
	},
	
	initialize: function(element, opts){
		this.setOptions(opts);
		
		this.container = $(element);
		this.bannerNodes = new Array();
		this.current = -1;
		this.timerID = false;
	},
	
	load: function(url){
		if( ! $type(url)) url = this.options.url;
		new Request.JSON({url: url, onComplete: function(banners){
			this.onLoad(banners);
		}.bind(this)}).post({tag:this.options.tag});
		
		return this;
	},
	
	onLoad: function(json){
		var banners = json.items || json;
		banners.each(function(node, index){
			
			if(node.display_length){
				node.display_length = node.display_length.toInt();
			}else{
				node.display_length = false;
			}
			
			this.addImage('http://photos.osmek.com/get/'+node.photo+'.'+this.options.photo_size+'.jpg', node.display_length, node);
			
		}.bind(this));
		
		this.fireEvent("load", [banners.length]);
		
		this.start();
	},
	
	addImage: function(url, dsp_length, info){
		
		// make banner div
		var b = new Element('div', {'class': this.options.imageClass, 'styles': {'position': 'absolute', 'visibility': 'hidden' }});
		b.inject(this.container, 'top');
		b.set('tween', {duration:1000});
		
		// make image			
		var img = new Element('img', {
			'src': url,
			'alt': '',
			'title': ''
		})
		.inject(b, 'top');
		
		
		
		if(info.link){
			img.setStyle('cursor', 'pointer').addEvent('click', function(e){
				window.location = info.link;
			});
		}
			
		// add to main array
		if( ! dsp_length){
			dsp_length = this.options.dsp_length;
		}
		var obj = {
			ref: b,
			time: dsp_length*1000,
			info: info
		};
		this.bannerNodes.push(obj);
	},
	
	start: function(){
		this.turnBanner();
		
		return this;
	},
	
	turnBanner: function(){
		this.goto(this.current+1);
	},
	
	goto: function(index){
	
		// clear previous timeout
		if(this.timerID) window.clearTimeout(this.timerID);
		
		if(index >= this.bannerNodes.length) index = 0;
		if(index < 0) index = this.bannerNodes.length - 1;
		
		var last_index = false;
		var last_banner = {info:false, ref:false};
		
		if(this.current > -1){
			last_index = this.current;
			last_banner = this.bannerNodes[last_index];
		}
		
		this.current = index;
		
		var new_index = index;
		var new_banner = this.bannerNodes[new_index];
		
		this.transition(last_banner.ref, new_banner.ref);
		
		// fire change
		this.fireEvent('change', [new_banner.info, new_index, last_banner.info, last_index]);
		
		// if there's more than one, time for next
		if(this.bannerNodes.length > 1) {
			this.timerID = window.setTimeout(this.turnBanner.bind(this), new_banner.time);
		}
		
		return this;
	},
	
	transition: function(old_banner, new_banner){
		if(old_banner){
			old_banner.fade('out');
		}
		new_banner.setStyles({'opacity': 0, 'visibility': 'visible'}).tween('opacity', [0,1]);
	},
	
	
	next: function(){
		this.goto(this.current+1);
		
		return this;
	},
	
	prev: function(){
		this.goto(this.current-1);
		
		return this;
	}
	
});
