var TargiBox = {
	_data: Object(),
	_tabWidth: 111,
	_current: 0,
	_remote: 0,

	clickArrow: function(tab, set) {
		var div = $('targi_box_tabs');
		var left = 1 + ((- tab) * this._tabWidth);
		
		if (typeof set == 'undefined')
			set = false;

		this._current = tab;
		
		// Blokada przekroczenia zakresu.
		if (tab < 0) 
			this._current = 0;
		if (tab > this._data.targi.length - 4) 
			this._current = this._data.targi.length - 4;
		
		left = 1 + ((- this._current) * this._tabWidth);
		
		if (!set) {
			var ajaxObj = new Ajax.Request('/ajax-form,targi-current', {
				method: 'get',
				parameters: { 
					current: this._current
				},
				
				onSuccess: function(transport) {},
				onFailure: function() {}
			});

			
			new Effect.Morph(div, {
				style: 'left: ' + left + 'px',
				duration: 0.2,
				queue: {
					position: 'end', 
					scope: 'box_targi_tabs' 
				}
			});
		} else {
			div.style.left = left + 'px';
		}
	},
		
	_initElements: function() {
		$('targi_box_tabs').style.width = this._data.targi.length * this._tabWidth + 'px';
	},
	
	_initObservers: function() {
		var _this = this;
	
		Event.observe($('targi_box_arrow_left'), 'click', function(event) {
			_this.clickArrow(_this._current - 1);
		});
		
		Event.observe($('targi_box_arrow_right'), 'click', function(event) {
			_this.clickArrow(_this._current + 1);
		});
	},		
		
	init: function(data) {
		this._remote = (typeof remote != 'undefined') ? remote : 0;

		this._data = data;
		
		this._initElements();		
		this._initObservers();
		
		this.clickArrow(data.start, true);
	},
	
	highlightKategoria: function(kategoria) {
		$$('#tmo_kategorie a').each(function(obj) {
			obj.removeClassName('b');
		});

		$$('#tmo_kategorie a.tmo_kategoria_' + kategoria).each(function(obj) {
			obj.addClassName('b');
		});
	},

	loadWystawcy: function(tab, kategoria) {
		if (!Object.isElement($('tmo_wystawcy_ajax')))
			return false;

		if (typeof kategoria == 'undefined')
			kategoria = 'all';

		this.highlightKategoria(kategoria);

		Common.Loader.startAjax();
		var ajaxWystawcy = new Ajax.Request('/ajax-form,wystawcy', {
			method: 'get',
			parameters: {
				targi: tab,
				kategoria: kategoria,
				kto: 'klienci',
				header: 'Lista wystawców'
			},

			onSuccess: function(transport) {
				Common.Loader.stopAjax();
				$('tmo_wystawcy_ajax').innerHTML = transport.responseText;
			},

			onFailure: function() {
				alert(Lang.generic_error);
			}
		});
	},


	load: function(tab) {
		_this = this;
		Common.highlightTab('targi_box_tab_' + tab, 'mtb_sel');

		if (Object.isElement($('tmo_iframe_openx')))
			$('tmo_iframe_openx').src = '/iframe,openx,strona,index,targi,' + tab;

		Common.Loader.startAjax();
		var ajaxTargi = new Ajax.Request('/ajax-form,targi', {
			method: 'get',
			parameters: { 
				targi: tab,
				remote: _this._remote,
				kategoria: 'all'	// Wracamy na 'wszyscy'
			},
			
			onSuccess: function(transport) {
				Common.Loader.stopAjax();
				$('targi_box_content_container').innerHTML = transport.responseText;
			},
			
			onFailure: function() { 
				alert(Lang.generic_error);
			}
		});

		this.loadWystawcy(tab, 'all');	// Wracamy na 'wszyscy'

		if (Object.isElement($('tmo_sponsorzy'))) {
			Common.Loader.startAjax();
			var ajaxSponsorzy = new Ajax.Request('/ajax-form,sponsorzy', {
				method: 'get',
				parameters: {
					targi: tab
				},

				onSuccess: function(transport) {
					Common.Loader.stopAjax();
					$('tmo_sponsorzy').innerHTML = transport.responseText;
				},

				onFailure: function() {
					alert(Lang.generic_error);
				}
			});
		}
	}
};
