if (KIOS == undefined) {
	// singleton
	var KIOS = {};
}

/**
 * Trieda slidera
 *
 */
KIOS.Slider = new Class({

	Implements: Options,

	options: {
		anim: {transition: Fx.Transitions.Sine.easeOut, duration: 1000}
	},

	container: null,
	window: null,
	effects: new Array(),
	items: new Array(),
	curIndex: 0,
	oldIndex: 0,

	locked: false,

	items: null,

	/*
	 * Z obj povybera obrazky
	 */
	initialize: function(obj) {

		obj = $(obj);
		if (obj) {

			this.container = obj.getFirst();
			this.items = this.container.getChildren();
			this.items.each(function(item, index) {

				item.set('tween', {transition: this.options.anim.transition, duration: this.options.anim.duration, onComplete: this.afterAnim.bind(this)});
				item.setStyle('z-index', 10);

			}, this);

			// hned dame nejaku nahodnu (-1 aby mohlo vratit zo vsetkych)
			this.curIndex = this.getRand(-1);
			this.items[this.curIndex].setStyle('z-index', 20);
		}
	},

	moveRand: function() {
		this.locked = true;

		this.oldIndex = this.curIndex;
		this.curIndex = this.getRand(this.curIndex);

		// novu fotku dame priehladnu a nastavime z-index nad aktivnou
		this.items[this.curIndex].setStyles({'z-index': 30, 'opacity': 0});
		// zanimujeme zviditelnenie
		this.items[this.curIndex].tween('opacity', [0, 1]);
	},

	moveNext: function() {
		this.locked = true;

		this.oldIndex = this.curIndex;
		this.curIndex = this.getNext(this.curIndex);

		// novu fotku dame priehladnu a nastavime z-index nad aktivnou
		this.items[this.curIndex].setStyles({'z-index': 30, 'opacity': 0});
		// zanimujeme zviditelnenie
		this.items[this.curIndex].tween('opacity', [0, 1]);
	},

	afterAnim: function() {
		// staru fotku vratime na zakladny z-index
		this.items[this.oldIndex].setStyles({'z-index': 10, 'opacity': 0});
		// aktualnu fotku dame na aktivny z-index
		this.items[this.curIndex].setStyle('z-index', 20);
		this.unlockAnim();
	},

	/**
	 * Vrati nahodny obrazok, ale nie rovnaky ako old
	 */
	getRand: function(old) {
		var r;
		do {
			r = Math.floor(Math.random() * this.items.length);
		} while (r == old);
		return r;
	},

	/**
	 * Vrati dalsie cislo polozky
	 */
	getNext: function(old) {
		old += 1;
		if (old >= this.items.length) {
			old = 0;
		}
		return old;
	},
	

	/**
	 * Vrati predch. cislo polozky
	 */
	getPrev: function(old) {
		old -= 1;
		if (old < 0) {
			old = this.items.length - 1;
		}
		return old;
	},

	unlockAnim: function() {
		this.locked = false;
	},
	
	log: function(text) {
		console.log(text);
	}
});


/*
KIOS.Gallery = new Class({

	Implements: Options,

	initialize: function(sel) {

		$$(sel).each(function (a) {
			a.addEvent('click', function(e) {e.preventDefault();});
			a.store('tip:title', '');
			a.store('tip:text', '<img src="' + a.get('href') + '" />');
			new Tips({
				showDelay: 200,
				hideDelay: 0
			}).attach(a);
		});
	}
});
*/

window.addEvent('domready', function() {

	KIOS.myslider = new KIOS.Slider('photos');
	KIOS.myslider.intervalID = KIOS.myslider.moveNext.periodical(3000, KIOS.myslider);
});


