if (KIOS == undefined) {
	// singleton
	var KIOS = {};
}
function backlinkde() {
				this.text = '<- zurück ';
				this.type = 'link';
				this.write = backlink_write;
				this.form = true;
}
function backlinkhu() {
				this.text = '<- Vissza ';
				this.type = 'link';
				this.write = backlink_write;
				this.form = true;
}


function backlink_write() {
	if (! window.history) return;
	if (window.history.length == 0)return;

	this.type = this.type.toLowerCase();
	if (this.type == 'button') {
		if (this.form)
			document.write('<form>');
		document.write('<input type=button onClick="history.back(-1)" VALUE="', this.text, '"');
		if (this.otheratts) document.write(' ', this.otheratts);
		document.write('>');
		if (this.form)document.write('<\/form>');
	} else {
		document.write('<strong><a href="javascript:history.back(-1)"');
		if (this.otheratts)
			document.write(' ', this.otheratts);
		document.write('>');
		if (this.type == 'image' || this.type == 'img') {
			document.write('<img src="', this.src, '" ALT="', this.text, '"');
			if (this.width) document.write(' width=', this.width);
			if (this.height) document.write(' height=', this.height);
			if (this.otherimgatts) document.write(' ', this.otherimgatts);
			document.write(' border=0 />');
		}
		else
			document.write(this.text);
		document.write('<\/a></strong>');
	}
}



function pwd(x) {
	switch (x) {
	case 0 : document.getElementById('pass').style.backgroundPosition = "0px -18px"; break;
	case 1 : document.getElementById('pass').style.backgroundPosition = "0px 0px"; break;
	}
} 

KIOS.Changer = new Class({

	Implements: Options,

	options: {
		container: 'header-image',
		loader: 'typo3conf/ext/kios_layout/img/ajax-loader.gif',
		loaderStyle: 'position: absolute;top:40%;left:50%;',
		period: 5000,
		fadeDuration: 700
	},

	container: null,
	images: null,
	currentItem: 0,
	nextItem: 0,

	initialize: function() {
		this.loader = new Element('img', {style: this.options.loaderStyle, src: this.options.loader});
		this.container = $(this.options.container);
	},

	load: function(images) {
		this.container.grab(this.loader);
		this.images = new Asset.images(images.split(','), {onComplete: function() {this.onLoad();}.bind(this)});
	},

	onLoad: function() {
		this.loader.destroy();

		this.images.each(function(item) {
			item.setStyles({'position': 'absolute', 'top': 0, 'left': 0, 'z-index': 5});
		}, this);

		this.container.grab(new Element('img'));

		this.images[this.currentItem].setStyle('z-index', 10);
		this.images[this.currentItem].replaces(this.container.getFirst());

		this.nextItem = this.getNext();
		this.images[this.nextItem].setStyle('z-index', 5);
		this.images[this.nextItem].replaces(this.container.getLast());

		this.change.periodical(this.options.period, this);
	},

	change: function() {
		new Fx.Tween(this.images[this.currentItem], {duration: this.options.fadeDuration,
			onComplete: function() {this.onFade()}.bind(this)}).start('opacity', 1, 0);
	},

	onFade: function() {

		this.images[this.nextItem].setStyle('z-index', 10);
		this.images[this.currentItem].setStyles({'z-index': 5, 'opacity': 1});

		var aaa = this.getNext();
		this.currentItem = this.nextItem;
		this.nextItem = aaa; 

		this.images[this.nextItem].replaces(this.container.getLast());
		this.images[this.currentItem].replaces(this.container.getFirst());
	},

	getNext: function() {
		var next;
		while (1) {
			next = $random(0, this.images.length - 1);
			if (next != this.currentItem && next != this.nextItem) {
				return next;
			}
		}
	}

});

/**
 * Trieda slidera
 *
 */
KIOS.Slider = new Class({

	Implements: Options,

	options: {
		'slow': {transition: Fx.Transitions.Sine.easeOut, duration: 350, fps: 50, onComplete: $empty},
		'accel': {transition: Fx.Transitions.Sine.easeIn, duration: 150, fps: 50, onComplete: $empty},
		'decel': {transition: Fx.Transitions.Sine.easeOut, duration: 150, fps: 50, onComplete: $empty},
		'fast': {transition: Fx.Transitions.linear, duration: 80, fps: 10, onComplete: $empty},
		positions: [-164, 0, 152, 304, 504, 656, 808],
		small: {
			div: {
				width: 152
			},
			image: {
				width: 128,
				height: 92,
				'margin-top': 48
			}
		},
		big: {
			div: {
				width: 200
			},
			image: {
				width: 160,
				height: 127,
				'margin-top': 12
			}
		}
	},

	container: null,
	window: null,
	effects: new Array(),
	items: new Array(),
	curIndex: null,
	newIndex: null,
	locked: false,
	steps: 0,

//	items: null,

	/*
	 * Z obj povybera obrazky, nazvy a linky a vytvori graficky slider pod obj
	 */
	initialize: function(obj) {

		obj = $(obj);
		if (obj) {

			// vytvorime scroller
			this.container = new Element('div', {id: 'hoedlmayr-scroller'});
			this.container.grab(new Element('a', {id: 'hs-arrowLeft', events: {click: function() {
				KIOS.myslider.newIndex = KIOS.myslider.getPrev(KIOS.myslider.curIndex);
				KIOS.myslider.steps = 0;
				KIOS.myslider.moveLeft()}}}));
			this.window = new Element('div', {id: 'hs-window'});
			this.container.grab(this.window);
			this.container.grab(new Element('a', {id: 'hs-arrowRight', events: {click: function() {
				KIOS.myslider.newIndex = KIOS.myslider.getNext(KIOS.myslider.curIndex);
				KIOS.myslider.steps = 0;
				KIOS.myslider.moveRight()}}}));

			obj.getLast().getChildren().each(function(item, index) {

				// ocislujeme si linky v menu a nastavime onclick
				item.getFirst().set('index', index);
				item.getFirst().addEvent('click', function(e) {return KIOS.myslider.itemClick(e)});

				var img = item.getFirst().getFirst();
				if (img) {

					img.removeClass('h');
					img.setStyles(this.options.small.image);
					var div = new Element('div', {'class': 'item'});
					var linkimg = new Element('a', {href: img.getParent().get('href'), title: img.get('title')});
					var linkspan = linkimg.clone();
					linkimg.grab(img);
					div.grab(linkimg);
					var span = new Element('span', {text: img.get('title'), 'class': item.getFirst().get('class')});
					linkspan.grab(span);
					div.grab(linkspan);

					this.items.push({container: div, image: img, text: span});
//					this.effects.push(new Fx.Elements([div, img]));

					// pre prvych 5 obrazkov nastavime aby sa uz zobrazovali
					if (index < 5) {
						div.setStyle('left', this.options.positions[index + 1]);
					} else {
						// ostatne schovame na koniec
						div.setStyle('left', this.options.positions.getLast());
					}
					// stredny dame vacsi
					if (index == 2) {
						div.setStyles({width: this.options.big.div.width});
						img.setStyles({width: this.options.big.image.width,
							height: this.options.big.image.height,
							'margin-top': this.options.big.image['margin-top']});
					}

					this.window.grab(div);
				}
			}, this);

			this.curIndex = 2;
			this.newIndex = 2;

		}
	},

	inject: function(obj, where) {

		if (this.container) {
			this.container.inject(obj, where);
		}
	},

	itemClick: function(e) {

//this.log('click');
		// ak este bezi animacia, nerob nic
		if (this.locked) {
//this.log('locked');
			return false;
		}

		var obj = e.target;
		this.newIndex = obj.get('index');

		// ak klikame na polozku, ktora je prave naskrolovana v strede, skok na linku
		if (this.newIndex == this.curIndex) {
			return true;
		}

		// urcime smer
		var smer;
		if (this.newIndex > this.curIndex) {
			smer = this.moveRight;
		} else {
			smer = this.moveLeft;
		}
		// ak je rozdiel dlhsi ako polovica, prehod smer
		if (Math.abs(this.newIndex - this.curIndex) > (this.items.length / 2)) {
			if (smer == this.moveRight) {
				smer = this.moveLeft;
			} else {
				smer = this.moveRight;
			}
		}

		// vynulujeme pocet krokov
		this.steps = 0;

		smer.run(null, this);

		return false;
	},

	moveLeft: function() {

		// ak sme doskrolovali, koniec
		if (this.newIndex == this.curIndex) {
			this.locked = false;
//this.log('unlock');
			return;
		}

		this.locked = true;
//this.log('lock');

		// urcime rychlost animacie
		var aaa = Math.abs(this.newIndex - this.curIndex);
		if (aaa == 1 || aaa == this.items.length - 1) {
			// ak vol predtym vacsi pocet krokov, je to spomalenie
			if (this.steps > 2) {
				aaa = 'decel';
			} else {
				aaa = 'slow';
			}
		} else {
			// este urcime pripadnu rozbehovu rychlost podla napocitaneho poctu krokov
			if (this.steps == 0) {
				aaa = 'accel';
			} else {
				aaa = 'fast';
			}
		}
		var ooo = this.options[aaa];
		ooo.onComplete = $empty;

		this.steps += 1;

		var oldIndex = this.curIndex;
		oldIndex = this.getNext(oldIndex);
		oldIndex = this.getNext(oldIndex);

		this.curIndex = this.getPrev(this.curIndex);

		// vytvorime 6 animacii
		// maly, 5. odsunie sa
		new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
			'0': {left: [this.options.positions[5], this.options.positions[6]],
				width: [this.options.small.div.width, this.options.small.div.width]
			},
			'1': {width: [this.options.small.image.width, this.options.small.image.width],
				height: [this.options.small.image.height, this.options.small.image.height],
				'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
			}
		});
		oldIndex = this.getPrev(oldIndex);

		// maly, z 4. na 5. poziciu
		new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
			'0': {left: [this.options.positions[4], this.options.positions[5]],
				width: [this.options.small.div.width, this.options.small.div.width]
			},
			'1': {width: [this.options.small.image.width, this.options.small.image.width],
				height: [this.options.small.image.height, this.options.small.image.height],
				'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
			}
		});
		oldIndex = this.getPrev(oldIndex);

		// velky z 3. na 4. poziciu, zmensi sa
		if (aaa == 'slow' || aaa == 'accel') {
			new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
				'0': {left: [this.options.positions[3], this.options.positions[4]],
					width: [this.options.big.div.width, this.options.small.div.width]
				},
				'1': {width: [this.options.big.image.width, this.options.small.image.width],
					height: [this.options.big.image.height, this.options.small.image.height],
					'margin-top': [this.options.big.image['margin-top'], this.options.small.image['margin-top']]
				}
			});
		} else {
			new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
				'0': {left: [this.options.positions[3], this.options.positions[4]],
					width: [this.options.small.div.width, this.options.small.div.width]
				},
				'1': {width: [this.options.small.image.width, this.options.small.image.width],
					height: [this.options.small.image.height, this.options.small.image.height],
					'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
				}
			});
		}
		oldIndex = this.getPrev(oldIndex);

		// maly zo 2. na 3. poziciu, zvacsi sa
		if (aaa == 'slow' || aaa == 'decel') {
			new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
				'0': {left: [this.options.positions[2], this.options.positions[3]],
					width: [this.options.small.div.width, this.options.big.div.width]
				},
				'1': {width: [this.options.small.image.width, this.options.big.image.width],
					height: [this.options.small.image.height, this.options.big.image.height],
					'margin-top': [this.options.small.image['margin-top'], this.options.big.image['margin-top']]
				}
			});
		} else {
			new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
				'0': {left: [this.options.positions[2], this.options.positions[3]],
					width: [this.options.small.div.width, this.options.small.div.width]
				},
				'1': {width: [this.options.small.image.width, this.options.small.image.width],
					height: [this.options.small.image.height, this.options.small.image.height],
					'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
				}
			});
		}
		oldIndex = this.getPrev(oldIndex);

		// maly z 1. na 2. poziciu
		new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
			'0': {left: [this.options.positions[1], this.options.positions[2]],
				width: [this.options.small.div.width, this.options.small.div.width]
			},
			'1': {width: [this.options.small.image.width, this.options.small.image.width],
				height: [this.options.small.image.height, this.options.small.image.height],
				'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
			}
		});
		oldIndex = this.getPrev(oldIndex);

		// schovany maly zo zaciatku na 1. poziciu
		// v poslednej animacii nastavime pokracovanie
		new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: function() {KIOS.myslider.moveLeft()}}).start({
			'0': {left: [this.options.positions[0], this.options.positions[1]],
				width: [this.options.small.div.width, this.options.small.div.width]
			},
			'1': {width: [this.options.small.image.width, this.options.small.image.width],
				height: [this.options.small.image.height, this.options.small.image.height],
				'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
			}
		});
	},

	moveRight: function() {

		// ak sme doskrolovali, koniec
		if (this.newIndex == this.curIndex) {
			this.locked = false;
//this.log('unlock');
			return;
		}

		this.locked = true;
//this.log('lock');

		// urcime rychlost animacie
		var aaa = Math.abs(this.newIndex - this.curIndex);
		if (aaa == 1 || aaa == this.items.length - 1) {
			// ak vol predtym vacsi pocet krokov, je to spomalenie
			if (this.steps > 2) {
				aaa = 'decel';
			} else {
				aaa = 'slow';
			}
		} else {
			// este urcime pripadnu rozbehovu rychlost podla napocitaneho poctu krokov
			if (this.steps == 0) {
				aaa = 'accel';
			} else {
				aaa = 'fast';
			}
		}
		var ooo = this.options[aaa];
		ooo.onComplete = $empty;

		this.steps += 1;

		var oldIndex = this.curIndex;
		oldIndex = this.getPrev(oldIndex);
		oldIndex = this.getPrev(oldIndex);

		this.curIndex = this.getNext(this.curIndex);

		// vytvorime 6 animacii
		// maly, 1. odsunie sa
		new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
			'0': {left: [this.options.positions[1], this.options.positions[0]],
				width: [this.options.small.div.width, this.options.small.div.width]
			},
			'1': {width: [this.options.small.image.width, this.options.small.image.width],
				height: [this.options.small.image.height, this.options.small.image.height],
				'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
			}
		});
		oldIndex = this.getNext(oldIndex);

		// maly, z 2. na 1. poziciu
		new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
				'0': {left: [this.options.positions[2], this.options.positions[1]],
				width: [this.options.small.div.width, this.options.small.div.width]
			},
			'1': {width: [this.options.small.image.width, this.options.small.image.width],
				height: [this.options.small.image.height, this.options.small.image.height],
				'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
			}
		});
		oldIndex = this.getNext(oldIndex);

		// velky z 3. na 2. poziciu, zmensi sa
		if (aaa == 'slow' || aaa == 'accel') {
			new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
				'0': {left: [this.options.positions[3], this.options.positions[2]],
					width: [this.options.big.div.width, this.options.small.div.width]
				},
				'1': {width: [this.options.big.image.width, this.options.small.image.width],
					height: [this.options.big.image.height, this.options.small.image.height],
					'margin-top': [this.options.big.image['margin-top'], this.options.small.image['margin-top']]
				}
			});
		} else {
			new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
				'0': {left: [this.options.positions[3], this.options.positions[2]],
					width: [this.options.small.div.width, this.options.small.div.width]
				},
				'1': {width: [this.options.small.image.width, this.options.small.image.width],
					height: [this.options.small.image.height, this.options.small.image.height],
					'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
				}
			});
		}
		oldIndex = this.getNext(oldIndex);

		// maly zo 4. na 3. poziciu, zvacsi sa
		if (aaa == 'slow' || aaa == 'decel') {
			new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
				'0': {left: [this.options.positions[4], this.options.positions[3]],
					width: [this.options.small.div.width, this.options.big.div.width]
				},
				'1': {width: [this.options.small.image.width, this.options.big.image.width],
					height: [this.options.small.image.height, this.options.big.image.height],
					'margin-top': [this.options.small.image['margin-top'], this.options.big.image['margin-top']]
				}
			});
		} else {
			new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
				'0': {left: [this.options.positions[4], this.options.positions[3]],
					width: [this.options.small.div.width, this.options.small.div.width]
				},
				'1': {width: [this.options.small.image.width, this.options.small.image.width],
					height: [this.options.small.image.height, this.options.small.image.height],
					'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
				}
			});
		}
		oldIndex = this.getNext(oldIndex);

		// maly z 5. na 4. poziciu
		new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: $empty}).start({
			'0': {left: [this.options.positions[5], this.options.positions[4]],
				width: [this.options.small.div.width, this.options.small.div.width]
			},
			'1': {width: [this.options.small.image.width, this.options.small.image.width],
				height: [this.options.small.image.height, this.options.small.image.height],
				'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
			}
		});
		oldIndex = this.getNext(oldIndex);

		// schovany maly z konca na 5. poziciu
		// v poslednej animacii nastavime pokracovanie
		new Fx.Elements([this.items[oldIndex].container, this.items[oldIndex].image], {transition: ooo.transition, duration: ooo.duration, onComplete: function() {KIOS.myslider.moveRight()}}).start({
			'0': {left: [this.options.positions[6], this.options.positions[5]],
				width: [this.options.small.div.width, this.options.small.div.width]
			},
			'1': {width: [this.options.small.image.width, this.options.small.image.width],
				height: [this.options.small.image.height, this.options.small.image.height],
				'margin-top': [this.options.small.image['margin-top'], this.options.small.image['margin-top']]
			}
		});
	},

	/**
	 * 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.initSearchHelp = function(obj, div) {
	obj = $(obj);
	div = $(div);
	if (obj) {
		obj.addEvent('click', function(e) {
			e.preventDefault();
			if (div.hasClass('h')) {
				div.removeClass('h');
			} else {
				div.addClass('h');
			}
		});
	}
}

/**
 * Nastavy skryty styl ak je vypnuty javascript
 */
KIOS.hideNonJS = function(obj) {
	obj = $(obj);
	obj.getElements('.hidejs').addClass('h');
//	obj.getElements('.hidejs').setStyles({position: 'absolute', top: '-10000px', left: '-10000px'});
}

window.addEvent('domready', function() {

	KIOS.hideNonJS(document.body);

	KIOS.myslider = new KIOS.Slider('homeProducts');
	KIOS.myslider.inject('homeProducts', 'bottom');

	KIOS.initSearchHelp('yatse_helplink', 'yatse_helpbox');
});
