// JavaScript Document

var SimpleFadeshow = new Class({

  Implements: [Options, Events],
	
	options: {
		slide: 'div.slide',
		start: 0,
		fxOff: 0,
		fxOn: 1,
		fxDuration: 500,
		showNextAfter: 5000
	},
	
	initialize: function(element, options){
		
		// main container
		this.container = document.id(element);

		this.setOptions(options);
		
		// slides
		this.slides = this.container.getElements(this.options.slide);
		
		this.count = this.slides.length;

    if (this.count < 2) return null;

		this.build();
		
		// initialize settings
		this.current = this.options.start;
		
		this.slideFx = new Array();
		
		this.slides.each(function(slide, index){
		  var fx = new Fx.Tween(slide, {property: 'opacity', duration: this.options.fxDuration, link: 'cancel' });
			index == this.options.start ? fx.set(this.options.fxOn) : fx.set(this.options.fxOff);
			this.slideFx[index] = fx;
		}, this);
		
		this.progress.each(function(item, index){
		  item.addEvent('click', function(event){
		    event.preventDefault();
				if (this.current != index){
  				$clear(this.repeater);
					this.show(index);
				}
		  }.bind(this));
		}, this);
		
		this.container.addEvents({
			'mouseleave': function(){ this.repeater = this.show.periodical(this.options.showNextAfter, this);	}.bind(this),
			'mouseenter': function(){ $clear(this.repeater);	}.bind(this)
		});
		
		this.progress[this.current].addClass('active');
		
		// all loaded
		this.container.addClass('ready');
		
		this.repeater = this.show.periodical(this.options.showNextAfter, this);

	},
	
	// builds progressbar
	build: function(){
		
		var items = '';
		for (var i = 0; i < this.count; i++){
			items += '<span></span>';
		}
		
		var progressContainer = new Element('div', {
		  'class': 'slide-progress',
			'html': items
		}).inject(this.container);
		
		this.progress = progressContainer.getElements('span');
		
	},
	
	// shows slide
	show: function(index){
		
		if (index == undefined) index = (this.current+1)%this.count
		this.slideFx[this.current].start(this.options.fxOff);
		this.slideFx[index].start(this.options.fxOn);
		
		this.progress[this.current].removeClass('active');
		this.progress[index].addClass('active');
		this.current = index;

	}


});

var AVCR = {};

AVCR.Press = {
	
	init: function(){
		if (document.id('topimg-block')) new SimpleFadeshow('topimg-block', { slide: 'div.slide-item' });
	}
	
};



window.addEvent('domready', function(){

  AVCR.Press.init();

});
