var PhotoChanger = new Class({
	Implements: Options,
	options: {
	},
	initialize: function(){
		this.banners = $('banners').getElements('.banner');
		this.bannerLength = this.banners.length;
		this.currentBanner = -1;
		this.playing = 1;
		
		// Start
		 this.goToBanner(0); 
		
		$('player-back').addEvent('click', function(e){
		   e.stop();
		   this.previous();
		}.bind(this));
		$('player-forward').addEvent('click', function(e){
		   e.stop();
		   this.next();
		}.bind(this));
	},
	
	goToBanner: function(key){
		var banner = this.banners[key];
		banner.setStyle('opacity', 0);
		banner.fade('in');
		this.currentBanner = key;
		this.setPhotoInfo(this.currentBanner);
		this.timeOutId = this.next.bind(this).delay(10000);
		//if($type(this.lastPhoto) != false && this.lastPhoto != key) this.photos[this.lastPhoto].fade('out');
		
		
			},
	next: function(){
		this.timeOutId = $clear(this.timeOutId);
		var banner = this.banners[this.currentBanner];
		banner.fade('out');
		if(this.currentBanner == this.bannerLength-1) {
			key = 0;
		}
		else {
			key = this.currentBanner + 1;
		}
		 this.goToBanner(key);
		},
	
	previous: function(){
		this.timeOutId = $clear(this.timeOutId);
		var banner = this.banners[this.currentBanner];
		banner.fade('out');
		if(this.currentBanner == 0) {
			key = this.bannerLength - 1;
		}
		else {
			key = this.currentBanner - 1;
		}
		 this.goToBanner(key);
		}/*
,
		
	pause: function(){
		if(this.playing == 1) {
			this.timeOutId = $clear(this.timeOutId);
		}
		else {
			this.goToBanner(this.currentPhoto);
		}

		}
*/,
		
	
	setPhotoInfo: function(key){
		var title = '';
		var url = '';
		var banner = this.banners[this.currentBanner];
		if(banner.getElement('.meta')){
		
			if(banner.getElement('.meta .title')){
				title = banner.getElement('.meta .title').get('html');
			}
			if(banner.getElement('.meta .url')){
				url = banner.getElement('.meta .url').get('html');
			}
			
			$('banner-info').getElement('.title').set('html', title);
			$('banner-info').getElement('.url').set('href', url);
			if(url){
				$('banner-info').getElement('.url').setStyle('display', 'block');
			}else{
				$('banner-info').getElement('.url').setStyle('display', 'none');
			}
			
			
		}
	}
});


