var Flagi2 = Class.create({
	/**
	 * Konstruktor klasy
	 */
	initialize: function()
	{
		this._timeout = 100;
		this._timer = null;

		this._initElements();
		this._initObservers();
	},

	/**
	 * Metoda pobiera wskaźniki do elementów
	 */
	_initElements: function()
	{
		this._elements = {};
		var self = this;
		$$('#flagi2 *').each(function(element) {
			if (element.id != '') {
				var match = (new RegExp('^flagi2_(.+)')).exec(element.id);
				if (typeof match[1] != 'undefined') {
					self._elements[match[1]] = $(element);
				}
			}
		});
	},

	/**
	 * Metoda ustawia observery
	 */
	_initObservers: function()
	{
		var self = this;

		[ 'dziobek', 'current' ].each(function(element) {
			Event.observe(self._elements[element], 'mouseover', function(event) {
				self._show();
			});
			Event.observe(self._elements[element], 'mouseout', function(event) {
				self._hideTimer();
			});
		});

		Event.observe(self._elements['lista'], 'mouseover', function(event) {
			self._cancel();
		});
		Event.observe(self._elements['lista'], 'mouseout', function(event) {
			self._hideTimer();
		});
	},

	/**
	 * Metoda ukrywa listę języków
	 */
	_hide: function()
	{
		var queue = Effect.Queues.get('flagi2');
		queue.each(function(effect) { effect.cancel(); });
		
		Effect.Fade(this._elements['lista'], { duration: 0.3, queue: { scope: 'flagi2' } });
	},

	/**
	 * Metoda kolejkuje schowanie listy
	 */
	_hideTimer: function()
	{
		var self = this;
		if (!this._timer) {
			this._timer = window.setTimeout(function() {
				self._hide();
			}, this._timeout);
		}
	},

	/**
	 * Metoda pokazuje listę
	 */
	_show: function()
	{
		this._cancel();

		var queue = Effect.Queues.get('flagi2');
		queue.each(function(effect) { effect.cancel(); });
		
		Effect.Appear(this._elements['lista'], { duration: 0.2, queue: { scope: 'flagi2' } });
		//this._elements['lista'].style.display = 'block';
	},

	/**
	 * Metoda odwołuje timer chowający listę
	 */
	_cancel: function()
	{
		if (this._timer) {
			window.clearTimeout(this._timer);
			this._timer = null;
		}
	}
});

