var Specjalne = Class.create({

	initialize: function(idList, options, visible, hidden, action, delay, sync) {
		
		this.index = 0;
		this.buffer = {};
		this.running = true;
		this.idList = idList;
		this.visible = visible;
		this.hidden = hidden;
		this.options = options;
		this.transition = this.transitionOpacity;

		this.action = typeof(action) != 'undefined' ? action : '/specjalna';
		this.delay = 7.0 + (typeof(delay) != 'undefined' ? delay : 0);

		var _this = this;
		if (typeof sync != 'undefined') {
			this.sync = sync;
			Event.observe(_this.sync['element'], ':transition', function() {
				_this.transition();
			});
			Event.observe(_this.sync['element'], ':run', function() {
				_this.run();
			});
		} else {
			this.sychronize = false;
		}

		this.effect = null;
		this.pulsate = null;
		this.observing = true;
		
		if (this.idList.length <= 1) {
			this.stop();
		} else {
			this.initObservers();
		}
	},


	initObservers: function()
	{
		var _this = this;

		var up = this.visible.up('div').down('.specjalne_up');
		up.observe('click', function() {
			if (_this.observing) {
				up.style.opacity = '1.0';
				try {
					this.pulsate.cancel();
				} catch (e) {};
				_this.pulsate = Effect.Pulsate(up, { pulses: 1, duration: 0.2, from: 0.8 });
				_this.index--;
				if (_this.running)
					_this.index--;
				if (_this.index < 0)
					_this.index = _this.idList.length - 1;
				_this.stop();
				_this.load();
			}
		});

		var down = this.visible.up('div').down('.specjalne_down');
		down.observe('click', function() {
			if (_this.observing) {
				down.style.opacity = '1.0';
				try {
					this.pulsate.cancel();
				} catch (e) {};
				_this.pulsate = Effect.Pulsate(down, { pulses: 1, duration: 0.2, from: 0.8 });
				if (!_this.running) {
					_this.index++;
					if (_this.index >= _this.idList.length)
						_this.index = 0;
				}
				_this.stop();
				_this.load();
			}
		});
	},
	

	stop: function()
	{
			this.running = false;
			try {
				this.effect.cancel();
			} catch (e) {};
			if (this.sync) {
				this.sync['instances']--;
				this.sync = false;
			}
			this.hidden.style.opacity = "0.0";
			this.visible.style.opacity = "1.0";
	},


	run: function()
	{
		if (!this.running) {
			return;
		}

		this.index++;
		if (this.index >= this.idList.length)
			this.index = 0;

		this.load();
	},


	load: function() {
		var id = this.idList[this.index];

		if (typeof this.buffer[id] != "undefined") {
			if (this.running) {
				this.hidden.update(this.buffer[id]);
				this.transition();
			} else {
				this.visible.update(this.buffer[id]);
			}
		} else {
			var _this = this;
			this.observing = false;
			var ajaxObj = new Ajax.Request(this.action,
			{
				method: 'post',
				parameters: { id: id, options: Object.toJSON(_this.options) },
				onSuccess: function(transport) {
					_this.buffer[id] = transport.responseText;
					if (_this.running) {
						_this.hidden.update(transport.responseText);
						if (_this.sync) {
							_this.sync['counter']++;
							if (_this.sync['counter'] == _this.sync['instances']) {
								_this.sync['counter'] = 0;
								Event.fire(_this.sync['element'], ':transition');
							}
						} else {
							_this.transition();
						}
					} else {
						_this.visible.update(transport.responseText);
					}
					_this.observing = true;
				},
				onFailure: function() {
					//alert(Lang.generic_error);
				}
			});
		}

	},

	transitionSimple: function() {
		if (!this.running) {
			return;
		}

		// pusta pętla dla uzyskania kolejki i timeoutu
		var _this = this;
		this.effect = new Effect.Opacity(this.visible, {
			delay: _this.delay,
			from: 1.0,
			to: 1.0,
			duration: 0.0,
			afterFinish: function() {
				_this.visible.style.display = 'none';
				_this.hidden.style.display = 'block';
				var temp = _this.hidden;
				_this.hidden = _this.visible;
				_this.visible = temp;
				if (_this.running && _this.sync) {
					_this.sync['counter']++;
					if (_this.sync['counter'] == _this.sync['instances']) {
						_this.sync['counter'] = 0;
						Event.fire(_this.sync['element'], ':run');
					}
				} else {
					_this.run();
				}
			}
		});
	},

	transitionOpacity: function() {
		if (!this.running) {
			return;
		}

		var _this = this;
		this.effect = new Effect.Opacity(this.visible, {
			delay: _this.delay,
			from: 1.0,
			to: 0.0,
			duration: 1.5,
			afterFinish: function() {
				_this.hidden.setStyle({zIndex: 2}); 
				_this.visible.setStyle({zIndex: 1}); 
				_this.visible.setOpacity(1.0);
				var temp = _this.hidden;
				_this.hidden = _this.visible;
				_this.visible = temp;
				if (_this.running && _this.sync) {
					_this.sync['counter']++;
					if (_this.sync['counter'] == _this.sync['instances']) {
						_this.sync['counter'] = 0;
						Event.fire(_this.sync['element'], ':run');
					}
				} else {
					_this.run();
				}
			}
		});
	}

});
