/**
 * created by: Pavel Spacil, licence: LGPL, v. 0.9.17
 * modified by: 
 */ 
function HorizontalScroller (_el, _cl) {
	var ref = this;
	this.el = SJEL.$(_el);
	if (_cl != undefined)
		this.es = SJEL.$A(this.el, "class", _cl);
	else
		this.es = SJEL.$T(this.el, "div");
	
	this.esl = this.es.length;
	// nejsou zadne polozky ke skrolovani -> koncime
	if (this.esl == 0)
		return;
	
	var i = 0;
	this.mo = new SJEL.Morph("easyboth");
	this.w = parseInt(SJEL.GStyle(this.es[0], "width"));  // sirka 1 bloku
	this.h = parseInt(SJEL.GStyle(this.es[0], "height"));
	this.ew = parseInt(SJEL.GStyle(this.el, "width"));  // sirka obalovaciho bloku
	this.eh = parseInt(SJEL.GStyle(this.el, "height"));
	this.c = 0;  //
	this.pos = 0;  // index aktualni polozky
	this.max = (this.esl * this.w);  // sirka vsech bloku

	// sirka vsech prvku je mensi nez sirka obalovaciho elementu -> koncime, skrolovani neni zapotrebi
	if (this.max < this.ew)
		return;
	
	// obalovaci div this.wr
	this.wr = SJEL.CE("div");
	SJEL.SStyle(this.wr, {position: "absolute", top: "0px", left: "0px", width: this.max+"px"});
	for (i = 0; i < this.esl; i++) {  // presuneme polozky do this.wr
		SJEL.SStyle(this.es[i], {position: "absolute", left: (i * this.w)+"px"});
		this.wr.appendChild(this.es[i]);
	}
	this.el.appendChild(this.wr);

	/** LISTA S TLACITKAMA **/
	this.dots = new Array();
	var he = this.h;
	he = this.h + 25;
	/* vytvorime tlacitka, bude to vypadat nasledovne:
		<div class="hs_buts_bar">
			<div class="hs_arrow_right">&nbsp;</div>
			<div class="hs_arrow_left">&nbsp;</div>
			<div class="hs_dot [hs_dot_active]">&nbsp;</div>
			...
		</div>
	*/
	var bar = SJEL.CE("div"); SJEL.AddClass(bar, "hs_buts_bar");
	//var aleft = SJEL.CE("div"); SJEL.AddClass(aleft, "hs_arrow_left");
	//var aright = SJEL.CE("div"); SJEL.AddClass(aright, "hs_arrow_right");
	SJEL.SStyle(bar, {position: "absolute", top: this.h+"px", width: this.ew+"px"});

/*	SJEL.AddEvent(aleft, "click", function () {
		ref.GoTo(ref.pos - 1);
	});
	
	SJEL.AddEvent(aright, "click", function () {
		ref.GoTo(ref.pos + 1);
	});*/
	
	/*bar.appendChild(aright);
	bar.appendChild(aleft);*/
	for (i = this.esl - 1; i >= 0; i--) {
		var dot = SJEL.CE("div"); SJEL.AddClass(dot, "hs_dot");
		if (i == 0)
			SJEL.AddClass(dot, "hs_dot_active");
		/*SJEL.AddEvent(dot, "mouseover", function (_e) {
			var th = SJEL.$ET(_e);
			SJEL.AddClass(th, "hs_dot_active");
		});
		SJEL.AddEvent(dot, "mouseout", function (_e) {
			var th = SJEL.$ET(_e);
			SJEL.RemoveClass(th, "hs_dot_active");
		});*/
		dot.i = i;
		SJEL.AddEvent(dot, "click", function (_e) {
			var th = SJEL.$ET(_e);
			ref.GoTo(th.i);
		});
		this.dots[i] = dot;
		bar.appendChild(dot);
	}
	
	this.el.appendChild(bar);
	/** END LISTA S TLACITKAMA **/

	SJEL.SStyle(this.el, {position: "relative", overflow: "hidden", height: he+"px"});
	
	// jde na polozku cislo _p
	this.GoTo = function (_p) {
		if (_p == this.pos)
			return;
			
		SJEL.RemoveClass(this.dots[this.pos], "hs_dot_active");
	
		var n = Math.abs(_p - this.pos);
		
		if (_p < 0) {
			this.c = -this.max;
			this.pos = this.esl - 1;
		} else if (_p > this.esl - 1) {
			this.c = 0;
			this.pos = 0;
		} else if (_p > this.pos) {
			this.c -= n * this.w;
			this.pos += n;
		} else if (_p < this.pos) {
			this.c += n * this.w;
			this.pos -= n;
		}
	
		SJEL.AddClass(this.dots[this.pos], "hs_dot_active");
	
		this.mo.Init(this.wr, {left: this.c+"px"}, 650);
		this.mo.Morph();
	}
}
