function initTabs() {
	new Slidemenu('slidemenu', {
		animHandler: animHandler,
		animParameters: { duration: 0.5 },
		buttonStateHandler: buttonStateHandler,
		size: 0,
		nextElementID: 'next-arrow',
		prevElementID: 'prev-arrow'
	});
}

function buttonStateHandler(button, enabled) {
	//wylaczenie/wlaczenie przyciskow
	if (button == 'prev-arrow')  {
		enabled ? $('prev-arrow').removeClassName('disabletab') : $('prev-arrow').addClassName('disabletab');
	}
	else {
		enabled ? $('next-arrow').removeClassName('disabletab') : $('next-arrow').addClassName('disabletab');
	}
}

function animHandler(SlidemenuID, status, direction) {
	var navi_box = $(SlidemenuID).childNodes[1];
	if (status == 'before') {
		Effect.Fade(navi_box, { to: 0.5, duration: 0.3 })
	}
	if (status == 'after') {
		Effect.Fade(navi_box, { to: 1, duration: 0.3 })
	}
}

var Slidemenu = Class.create({
	initialize: function(SlidemenuElemID) {
		this.SlidemenuElemID = SlidemenuElemID;
		this.options = Object.extend({
			numVisible:         0,
			scrollInc:          1,
			animParameters:     {},
			buttonStateHandler: null,
			animHandler:        null,
			queue:              "slidemenu",
			size:               0,
			prevElementID:      "prev-arrow",
			nextElementID:      "next-arrow",
			url:                null
		}, arguments[1] || {});

		this.initDone = false;
		this.animRunning = "none";
		this.requestIsRunning = false;
		this.animAfterFinish = this.options.animParameters.afterFinish;

		Object.extend(this.options.animParameters, { afterFinish: this._animDone.bind(this), queue: { position: 'end', scope: this.options.queue }});

		this.prevScroll = this._prevScroll.bindAsEventListener(this);
		this.nextScroll = this._nextScroll.bindAsEventListener(this);
		this.onComplete = this._onComplete.bindAsEventListener(this);
		this.onFailure  = this._onFailure.bindAsEventListener(this);

		Event.observe(this.options.prevElementID, "click", this.prevScroll);
		Event.observe(this.options.nextElementID, "click", this.nextScroll);

		buttons = 0;
		if ($('tab-next').visible())
			buttons++;
		if ($('tab-prev').visible())
			buttons++;

		$('navigator-box-ul').style.width = $('navigator-box').getDimensions().width - buttons * '40' + 'px'; //-100 - button home 40 + buttony 2*30

//		this.SlidemenuList = document.getElementsByClassName('tabs')[0];
		this.SlidemenuList = $$('.tabs')[0];
		this.options.size = $(this.SlidemenuList.getElementsByTagName('li')).length;

		tabact = 0;
		tabnr = 0;
		tabblock = 0;
		tabmax = Element.getDimensions($('navigator-box')).width - 100; //minus (tab-navi + home)
		$A($('navigator-tabs').childNodes).each(function(child) {
			tabact += Element.getDimensions(child).width + 2;
			if ((tabact < tabmax) && (tabblock == 0)) {
				tabnr++;
			}
			else {
				tabblock = 1;
			}
		});
		if (tabnr > 1) {
			this.options.numVisible = tabnr-1;
		}
		this._init();
	},

 	destroy: function() {
		Event.stopObserving(this.options.prevElementID, "click", this.prevScroll);
		Event.stopObserving(this.options.nextElementID, "click", this.nextScroll);
	},

	scrollTo: function(newStart) {
		var old_inc = this.options.scrollInc;
		this.ignoreNoMoreElements = true;

		if (newStart > this.currentIndex) {
			this.options.scrollInc = newStart - this.currentIndex;
			this._nextScroll(this);
		} else {
			this.options.scrollInc = this.currentIndex - newStart;
			this._prevScroll(this);
		}
		this.options.scrollInc = old_inc;
	},

	_init: function() {
		this.currentIndex = 0;
		this._getLiElementSize();
		this._updateButtonStateHandler(this.options.prevElementID, false);
		this._updateButtonStateHandler(this.options.nextElementID, this.options.size > this.options.numVisible);
	},

	_prevScroll: function(event) {
		if (this.animRunning != "none" || this.currentIndex == 0)
			return;

		var inc = this.options.scrollInc;

		if (this.currentIndex - inc < 0) {
			inc = this.currentIndex;
		}

		this._scroll(inc);
		return false;
	},

	_nextScroll: function(event) {
		if (this.animRunning != "none")
			return false;

		if (this.currentIndex + this.options.numVisible + this.options.scrollInc <= this.options.size) {
				this._scroll(-this.options.scrollInc);
		}
		else {
			this.nbInCache = this.options.size - (this.currentIndex + this.options.numVisible);
			if (this.options.url && this.noMoreElements == false) {
				this._request(this.currentIndex + this.options.numVisible + this.nbInCache, this.options.scrollInc - this.nbInCache);
			}
			else {
				if (this.nbInCache > 0) {
					this._scroll(-this.nbInCache);
				}
			}
		}
		return false;
	},

	_request: function(start, nb) {
		if (this.options.url && ! this.requestIsRunning) {
			this.requestIsRunning = true;
			var params = "start=" + start + "&nb=" + nb;
			new Ajax.Request(this.options.url, { parameters: params, onComplete: this.onComplete, onFailure: this.onFailure });
		}
	},

	_onComplete: function(originalRequest) {
		this.requestIsRunning = false;
		this.SlidemenuList.innerHTML += originalRequest.responseText;
		var size = this.options.size;
		this.options.size = this.SlidemenuList.getElementsByTagName("li").length;
		var inc = this.options.size - size;

		if (this.initDone == false) {
			this._getLiElementSize()
			this.currentIndex = 0;
			this.initDone = true;
			this._updateButtonStateHandler(this.options.prevElementID, false);
			this._updateButtonStateHandler(this.options.nextElementID, this.options.size == this.options.numVisible);
			this.noMoreElements = this.options.size < this.options.numVisible
		}
		else {
			if (!this.ignoreNoMoreElements) {
				this.noMoreElements = inc != this.options.scrollInc;
			}
			else {
				this.ignoreNoMoreElements = false;
			}
			if (inc > 0) {
				this._scroll(-inc, this.noMoreElements)
			}
			else {
				if (this.nbInCache >0) {
					this._scroll(-this.nbInCache, true);
				}
				this._updateButtonStateHandler(this.options.nextElementID, false);
			}
		}
	},

	_onFailure: function(originalRequest) {
		this.requestIsRunning = false;
	},

	_animDone: function(event){
		if (this.options.animHandler) {
			this.options.animHandler(this.SlidemenuElemID, "after", this.animRunning);
		}
		this.animRunning = "none";
		if (this.animAfterFinish) {
			this.animAfterFinish(event);
		}
	},

	_updateButtonStateHandler: function(button, state) {
		if (this.options.buttonStateHandler) {
			this.options.buttonStateHandler(button, state);
		}
	},

	_scroll: function(delta, forceDisableNext) {
		this.animRunning = delta > 0 ? "prev" : "next";

		tab = Element.getDimensions($('navigator-box'));
		tabmax = tab.width - 100; //minus (tab-navi + home)
		tabact = 0;
		tabnr = 0;
		tabblock = 0;
		$A($('navigator-tabs').childNodes).each(function(child) {
			tabact += Element.getDimensions(child).width + 2;
				if (child.visible()) { //to nie sprawdza sie niestety
				if ((tabact < tabmax) && (tabblock == 0)) {
					tabnr++;
				}
				else {
					tabblock = 1;
				}
			}
		});

		if (tabnr > 0) {
			this.options.numVisible = tabnr - 1; //obejscie problemu z visible przy wyliczaniu rozmiaru
		}
		if (this.options.animHandler) {
			this.options.animHandler(this.SlidemenuElemID, "before", this.animRunning);
		}

		new Effect.MoveBy(this.SlidemenuList, 0, delta * this.elementSize, this.options.animParameters);
		this.currentIndex -= delta;

		this._updateButtonStateHandler(this.options.prevElementID, this.currentIndex != 0);
		enable = (this.currentIndex + this.options.numVisible < this.options.size); //wyliczanie

		this._updateButtonStateHandler(this.options.nextElementID, (forceDisableNext ? false : enable));
	},

	_getLiElementSize: function() {
		var li = $(this.SlidemenuList.getElementsByTagName("li")[0]);
		if (li) {
			this.elementSize = li.getDimensions().width + parseFloat(li.getStyle("margin-left")) + parseFloat(li.getStyle("margin-right"));
		}
	}
});

