var DynamicSlideShow = Class.create({
	initialize: function(selector, container, script)
	{
		this.selectors = $$(selector);
		this.script    = script;
		this.container = $(container);
		
		var dyn = this; // because closure = new scope
		dyn.selectors.each(function(element, index) {
			element.observe('mouseover', dyn.selectorEventHandler.bind(dyn));
			element.observe('mouseout', dyn.startSlideShow.bind(dyn, 12));
		});
		dyn.container.observe('mouseover', dyn.stopSlideShow.bind(dyn));
		dyn.container.observe('mouseout', dyn.startSlideShow.bind(dyn, 12));
		
		this.getContent();
	},
	getContent: function()
	{
		var dyn = this; // in closures(?) se creaza un nou scope, si 'this' n-ar mai referi obiectul
	
		new Ajax.Request(this.script, {
			onComplete: function(req)
			{
				dyn.content = req.responseJSON;
				dyn.startSlideShow(12).bind(dyn);
			}
		});
	},
	selectorEventHandler: function(event)
	{
		event.preventDefault();
		
		this.changeContent(Event.element(event).readAttribute('count'));
		this.pe.stop();
	},
	changeContent: function(toIndex)
	{
		var	dyn	= this;
		if (!Object.isUndefined(toIndex))
			toIndex = new Number(toIndex);
		else
			toIndex = 0;
		
		new Effect.Fade(dyn.container, { duration: 0.3, afterFinish: function() {
				dyn.container.update(dyn.content[toIndex]);
				new Effect.Appear(dyn.container);
			}
		});
	},
	startSlideShow: function(seconds)
	{
		var index = 1; // pt a nu aparea tot prima stire dupa efectul de fade
		var dyn   = this;
		
		dyn.pe = new PeriodicalExecuter(function() {
			dyn.changeContent(index);
			index = (index + 1) % 6;
		}, seconds);
	},
	stopSlideShow: function()
	{
		this.pe.stop();
	}
});