function Vyjizdec(_id, _cl, _cont, _p) {
	var ref = this,
	opened = false,
	contHeight = 0,
	cl = _cl,
	cont = _cont,
	posunovac = _p,
	mo = new SJEL.Morph();

	this.id = _id;
	this.shifted = false;

	SJEL.SStyle(cont, {display: "block"});
//	contHeight = parseInt(SJEL.GStyle(cont, "height"));
	contHeight = 76;
	SJEL.SStyle(cont, {display: "none"});

	this.Close = function() {
		if (opened) {
			mo.Init(cont, {height: "0px"}, 280);
			mo.OnMorphFinished(function(){SJEL.SStyle(cont, {display: "none"});});
			mo.Morph();
			opened = false;
		}
	}

	cl.onclick = function() {
		if (!opened) {
			SJEL.SStyle(cont, {display: "block",overflow: "hidden", height: "0px"});
			mo.Init(cont, {height: contHeight + "px"}, 280);
			mo.OnMorphFinished(function(){});
			mo.Morph();

			if ((ref.id > 0) && !ref.shifted) {
				//alert('ted');
				if (ref.id == posunovac.elel-1)
					posunovac.SetLast(true);
				posunovac.Shift(1);
				ref.shifted = true;
			}

			opened = true;
		} else
			ref.Close();

		return false;
	}
}

/**
 * _p - tlacitko 'predchozi'
 * _n - tlacitko 'dalsi'
 * _wm - obalovaci elemnt
 * _w - obalovaci element kategorii
 * _e - elementy - decka _w (trida)
 * _m - max polozek
 * _exp - tlacitko 'zobrazit vsechny kategorie'
 * _imp - tlacitko 'zobrazit sbalit kategorie'
 */
function Posunovac(_p, _n, _wm, _w, _e, _m, _exp, _imp) {
	var ref = this,
		p = SJEL.$(_p),
		n = SJEL.$(_n),
		wm = SJEL.$(_wm),
		origWmH = wm.offsetHeight,
		w = SJEL.$(_w),
		exp = SJEL.$(_exp),
		imp = SJEL.$(_imp),
		e = SJEL.$A(w, "class", _e), h = 0, el = e.length,
		max = _m,
		woh = SJEL.CE("div"),
		mo = new SJEL.Morph("easyout"),
		csh = 0, sh = 0,  // posunuti
		last = false,
		expanded = false;

	this.elel = el;
	this.act = "active";
	this.wal = "more_cats";
	this.nal = "next_active";
	this.pal = "prev_active";
	this.shif = 0;

	this.linkos = (p.id == "prev_city");

	if (el > 0) {
		//h = parseInt(SJEL.GStyle(e[0], "height"));
		if (this.linkos)
			h = 17;
		else
			h = (SJEL.ie ? 115/*100*/ : 115);  // NA PEVNO bo IEmfb !!
	} else
		return;

	this.SetLast = function(_l) {
		last = _l;
	};

	// _d - smer posunuti + krok, _m - morph
	this.Shift = function(_d, _m) {
		var p = csh, mor = _m||false;

		p += _d;
		this.shif = p;
		if (p <= 0) {
			p = 0;
			PrevActive(false);
			if (el > max)
				NextActive(true);
		} else if (p > el - max - 1) {
			p = el - max;
			NextActive(false);
			if (p > 0)
				PrevActive(true);
		} else {
			PrevActive(true);
			NextActive(true);
		}

		if ((p == csh) && !last)
			return;

		//alert(h);
		if (!last)
			sh += (csh - p) * h;
		else {
			var sign = (_d > 0) ? -1 : 1;
			sh += sign * h;
		}

		csh = p;
		//sh -= 2;
		//alert(sh);
		if (!mor) {
			mo.Stop();
			mo.Init(w, {marginTop: sh + "px"}, 450);
			mo.Morph();
		} else
			SJEL.SStyle(w, {marginTop: sh + "px"});
	}


	var vyj = new Array();
	var as = null, els = null;
	for (var i = 0; i < el; i++) {
		as = SJEL.$A(e[i], "class", "moreinf");
		els = SJEL.$A(e[i], "class", "spl_rest");
		if ((as.length > 0) && (els.length > 0)) {
			vyj.push(new Vyjizdec(i, as[0], els[0], this));
		}
	}

	SJEL.AddClass(wm, this.wal);
	//SJEL.SStyle(woh, {overflow: "hidden", height: max * h + 10 + "px"});
	SJEL.SStyle(woh, {overflow: "hidden", height: max * h + "px"});
	w.parentNode.insertBefore(woh, w);
	woh.appendChild(w);

	if (el < max) {
		SJEL.SStyle(n, {display: "none"});
		SJEL.SStyle(p, {display: "none"});
		return;
	} else
		SJEL.AddClass(n, ref.nal);

	this.CloseInfo = function() {
		for (var i = 0; i < vyj.length; i++) {
			vyj[i].Close();
			vyj[i].shifted = false;
		}
	}

	this.SetShifted = function(_id) {
		for (var i = 0; i < vyj.length; i++)
			if (vyj[i].id == _id) {
				vyj[i].shifted = true;
				//alert(_id + " " + vyj[i].shifted);
				break;
			}
	}

	SJEL.AddEvent(p, "click", function() {
		ref.Shift(-max);
		ref.CloseInfo();
		ref.SetShifted(ref.shif);
		ref.SetLast(false);
	});

	SJEL.AddEvent(n, "click", function() {
		if (ref.shif >= (el - 1))
			return;

		ref.Shift(max);
		ref.CloseInfo();
		ref.SetShifted(ref.shif);
		//ref.SetShifted(p);
	});

	if (exp != null) {
		SJEL.SStyle(exp, {display: "block"});
		SJEL.AddEvent(exp, "click", function() {
			Expand();
		});
	}

	if (imp != null) {
		SJEL.AddEvent(imp, "click", function() {
			Implode();
		});
	}

	for (var i = 0; i < el; i++)
		if (SJEL.ClassExists(e[i], this.act)) {
			this.Shift(i - Math.floor(max / 2), true);
			break;
		}

	function NextActive(_b) {
		if (_b) {
			if (!SJEL.ClassExists(n, ref.nal))
				SJEL.AddClass(n, ref.nal);
		} else
			SJEL.RemoveClass(n, ref.nal);
	}

	function PrevActive(_b) {
		if (_b) {
			if (!SJEL.ClassExists(p, ref.pal))
				SJEL.AddClass(p, ref.pal);
		} else
			SJEL.RemoveClass(p, ref.pal);
	}

	function Expand() {
		SJEL.SStyle(p, {display: "none"});
		SJEL.SStyle(n, {display: "none"});
		SJEL.SStyle(exp, {display: "none"});
		SJEL.SStyle(imp, {display: "block"});
		SJEL.SStyle(woh, {height: origWmH - 20 + "px"});
		SJEL.SStyle(w, {marginTop: 0});
	}

	function Implode() {
		SJEL.SStyle(p, {display: "block"});
		SJEL.SStyle(n, {display: "block"});
		SJEL.SStyle(exp, {display: "block"});
		SJEL.SStyle(imp, {display: "none"});
		SJEL.SStyle(woh, {height: max * h + "px"});
		SJEL.SStyle(w, {marginTop: sh});
	}
}
