/**
 * Свиток
 *
 */
Mitsuden.Scroll = new Class({

	Implements: [Options],

	/**
	 * Опции
	 * @var {Hash}
	 */
	options:
	{
		/**
		 * Высота фрейма
		 * @var {int}
		 */
		'frameHeight': 120,
		
		/**
		 * Количество фреймов
		 * @var {int}
		 */
		'framesCount': 3,
		
		/**
		 * Время задержки (в мс)
		 * @var {int}
		 */
		'refreshTime': 5000,
		
		/**
		 * Контейнер
		 * @var {String|Element}
		 */
		'elContainer': null,
		
		/**
		 * Текст
		 * @var {String|Element}
		 */
		'elText': null,
		
		/**
		 * Кнопка отображения/скрытия блока
		 * @var {String|Element}
		 */
		'elShowHide': null,
		
		/**
		 * Кнопка обновления
		 * @var {String|Element}
		 */
		'elResresh': null,
		
		/**
		 * Имя куки
		 * @var {String}
		 */
		'cookieName': 'mitsuden-scroll'
	},
	
	/**
	 * Состояние
	 * @var {Bool}
	 */
	_state: true,
	
	/**
	 * Текущий кадр
	 * @var {int}
	 */
	_currentFrame: 0,
	
	/**
	 * Таймер
	 * @var {int}
	 */
	_timer: null,

	/**
	 * Конструктор
	 * @param {Hash} options
	 */
	initialize: function(options)
	{
		this.setOptions(options);

		// Загрузка элементов из DOM
		this.options.elContainer = $(this.options.elContainer);
		this.options.elText = $(this.options.elText);
		this.options.elShowHide = $(this.options.elShowHide);
		this.options.elRefresh = $(this.options.elRefresh);

		// Скрытие элементов управления
		this._hideControls();

		// Назначение обработчиков событий
		this._setEventHandlers();

		// Загрузка состояния
		this._state = this._loadState();
		if (!this._state)
			this.hide();

		// Таймер
		this._setTimer();
	},
	
	/**
	 * Раскрытие
	 */
	expand: function()
	{
		if (!this._state)
			var fx = new Fx.Tween(
				this.options.elContainer,
				{
					property: 'left'
				}
			).start(530, 0)
			 .chain(function(){ this._state = true; }.bind(this));
		
		// Сохранение состояния
		Cookie.dispose(this.options.cookieName);
	},
	
	/**
	 * Сворачивание
	 */
	collapse: function()
	{
		if (this._state)
			var fx = new Fx.Tween(
				this.options.elContainer,
				{
					property: 'left',
					transition: Fx.Transitions.Elastic.easeOut
				}
			).start(0, 530)
			 .chain(function(){ this._state = false; }.bind(this));
		
		// Сохранение состояния
		Cookie.write(this.options.cookieName, '0');
	},
	
	/**
	 * Немедленное скрытие
	 */
	hide: function()
	{
		_state = false;
		this.options.elContainer.setStyle('left', '530px');
	},
	
	/**
	 * Обновление кадра
	 * @return {Fx}
	 */
	refresh: function()
	{
		// Смена кадра
		this._currentFrame++;
		if (this._currentFrame == this.options.framesCount)
			this._currentFrame = 0;

		// Анимация
		var fx = new Fx.Tween(this.options.elText, { property: 'opacity' }).start(1, 0).chain
		(
			// Смена кадра и отображение
			function() {
				this.options.elText.setStyle('background-position', '0 ' + Number(-this._currentFrame * this.options.frameHeight) + 'px');
				fx.start(0, 1);
			}.bind(this)
		);
		
		return fx;
	},
	
	/**
	 * Назначение обработчиков событий
	 */
	_setEventHandlers: function()
	{
		// Скрытие/отображение элементов управления
		this.options.elContainer.addEvent('mouseover', this._showControls.bind(this));
		this.options.elContainer.addEvent('mouseleave', this._hideControls.bind(this));

		// Обновление
		this.options.elRefresh.addEvent('click', function(e){
			$clear(this._timer);
			this.refresh().chain(this._setTimer.bind(this));
			e.stop();
		}.bind(this));

		// Скрытие/отображение
		this.options.elShowHide.addEvent('click', function(e){
			if (this._state)
				this.collapse();
			else
				this.expand();
			e.stop();
		}.bind(this));
	},

	/**
	 * Отображение элементов управления
	 */
	_showControls: function()
	{
		this.options.elRefresh.removeClass('hidden');
		this.options.elShowHide.removeClass('hidden');
	},
	
	/**
	 * Скрытие элементов управления
	 */
	_hideControls: function()
	{
		this.options.elRefresh.addClass('hidden');
		this.options.elShowHide.addClass('hidden');
	},
	
	/**
	 * Загрузка состояния
	 * @return {Bool}
	 */
	_loadState: function()
	{
		return Cookie.read(this.options.cookieName) === null;
	},
	
	/**
	 * Установка таймера
	 */
	_setTimer: function()
	{
		this._timer = this.refresh.periodical(this.options.refreshTime, this);
	}
});

