var CPicSmoothSlider = new Class({
	active: 0,
	playStatus: false,
	animTimer: null,
	topLayerNum: 1,
	fading: false,
	initialize: function(){
		if (!$chk($('nyitoPicSlideContainer'))) return;
		var preloadImages = Asset.images($('nyitoPicSliderController').getElements('A[class*=num]').get('rel'));
		$('nyitoPicSliderController').getElements('A[class*=num]').addEvent('click', this.controllerClick.bind(this));
		$('nyitoPicSliderController').getElement('A[class*=startstop]').addEvent('click', this.startStopClick.bind(this));
		this.startStopClick();
	},
	controllerClick: function(e){
		e.stop();
		if (this.playStatus) this.startStopClick();
		this.setActive(parseInt(e.target.get('text')) - 1);
	},
	startStopClick: function(e){
		if ($chk(e)) e.stop();
		this.playStatus = !this.playStatus;
		var playbtn = $('nyitoPicSliderController').getElement('A[class*=startstop]');
		if (this.playStatus){
			playbtn.removeClass('play').addClass('stop');
			this.animTimer = this.animate.periodical(5000, this);
		}else{
			playbtn.removeClass('stop').addClass('play');
			$clear(this.animTimer);
		}
	},
	animate: function(){
		this.active+=1;
		if (this.active == 3) this.active = 0;
		this.setActive(this.active);
	},
	setActive: function(id){
		if (this.fading) return;
		// gomb kikapcs
		var buttons = $('nyitoPicSliderController').getElements('A[class*=num]');
		buttons.removeClass('act');
		
		// kep beallitasa
		var picfile = buttons[id].get('rel');
		var divtop = $('nyitoPicSlideContainer').getElement('DIV[class=picSliderPicContBg'+this.topLayerNum+']');
		var divbottom = $('nyitoPicSlideContainer').getElement('DIV[class=picSliderPicContBg'+(this.topLayerNum == 1 ? 2 : 1)+']');
		divbottom.setStyle('background-image', "url("+picfile+")");
		this.crossFade(divtop, divbottom);
		
		// aktiv gomb beallitasa
		buttons[id].addClass('act');
		if ($type($('nyitoPicSlideURL')) == 'element') $('nyitoPicSlideURL').set('href', buttons[id].get('rev'));
		this.active = id;
	},
	crossFade: function(divtop, divbottom){
		if (this.fading) return;
		this.fading = true;
		
		// alsot lathatova tesszuk
		divbottom.setStyle('opacity', 1);
		
		// a felsot szep lassan lehalvanyitjuk
		divtop.get('tween', {
			property: 'opacity', 
			duration: 'long',
			onComplete: function(){
				divtop.setStyle('z-index', '1');
				divbottom.setStyle('z-index', '2');
				this.topLayerNum = (this.topLayerNum == 1 ? 2 : 1);
				this.fading = false;
			}.bind(this)
		}).start(0);
	}
});

document.addEvent('domready', function(){
	new CPicSmoothSlider();
});
