/*
	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: 'banner',
		dsp_length: 5
	},
	
	initialize: function(element, opts){
		if(!opts) opts = {};
		this.setOptions(opts);
		
		this.container = $(element);
		this.bannerNodes = new Array();
		this.current = -1;
		this.timerID = false;
	},
	
	load: function(){
		new Request.JSON({url: this.options.url, encoding: 'utf-8', onComplete: function(banners){
			this.onLoad(banners);
		}.bind(this)}).post({tag:this.options.tag, photosize:this.options.photo_size});
	},
	
	onLoad: function(json){
		var banners = json.items;
		banners.each(function(node, index){
			
			if(node.display_length){
				if(typeof(node.display_length) == "string") node.display_length = node.display_length.toInt();
			}else{
				node.display_length = false;
			}
			
			this.addImage(node.photo, 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': {
				'visibility': 'hidden'
			}
		})
		.inject(this.container, 'top')
		.set('tween', {duration:1000});
		
		// make image			
		var img = new Element('img', {
			'src': 'http://photos.osmek.com/get/'+url+'.'+this.options.photo_size,
			'alt': '',
			'title': ''
		})
		.inject(b, 'top');
			
		// 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();
	},
	
	turnBanner: function(){
		this.goto(this.current+1);
	},
	
	next: function(){
		if(this.current + 1 > this.bannerNodes.length)
		{
			this.goto(0);
		}
		else
		{
			this.goto(this.current+1);
		}
	},
	
	prev: function(){
		if(this.current - 1 < 0)
		{
			this.goto(this.bannerNodes.length-1);
		}
		else
		{
			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;
		
		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);
		}
	},
	
	transition: function(old_banner, new_banner){
		if(old_banner){
			old_banner.fade('out');
		}
		new_banner.tween('opacity', [0,1]);
		this.fireEvent('transition', [old_banner, new_banner]);

	}
	
});
