﻿var FlopHandler = Class.create();
FlopHandler.prototype = {
	
	initialize : function (activator, inner, target) {
		
		if (!activator || !target)
			throw new Error('Ошибка инициализации FolpHandler');
		
		this.activator 	= $(activator);
		this.inner		= $(inner);
		this.target		= $(target);
		this.visible	= false;
		
		this.addBehavior();
	},
		
	addBehavior : function () {
		Element.hide(this.target);
		Element.observe(this.activator, 'click', this.clickHandler.bindAsEventListener(this));
		
		if (this.inner)
			Element.observe(this.inner, 'click', this.clickHandler.bindAsEventListener(this));
	},
		
	clickHandler : function (e) {
		this.toggle();
		e.stop();
	},
	
	toggle : function () {
		if (this.visible) {
			this.target.hide();
			
			if (this.inner)
				this.activator.show();
		} else {
			this.target.show();
			
			if (this.inner)
				this.activator.hide();
		}
		
		this.visible = !this.visible;
	}
}



var BibleFlop = Class.create();
BibleFlop.prototype = {
	
	initialize : function (bible) {

		// const
		this.HIDDEN_CSS_CLASS = 'hidden';
		
		// private members
		this.activator	= null;
		this.fields		= [];
		this.shown 		= false;
	
	
		// ctor
		
		for (var i = 0; i < bible.childNodes.length; i++)
		{
			var node = $(bible.childNodes[i]);
			if (node.tagName) {
				if (node.tagName.toLowerCase() == 'dt') {
					if (this.activator != null)
						throw new Error('Некорректная структура группы');
					
					this.activator = node;
				} else if (node.tagName.toLowerCase() == 'dd') {
					this.fields.push(node);
				} else
					throw new Error('Некорректная структура группы ('+node.tagName+')');
			}
		}
		
		this.addBehavior();	
	},
	
		
	// private methods

	addBehavior : function () {
		if (this.activator.observe)
			this.activator.observe('click', this.clickHandler.bindAsEventListener(this));
	},
	
	clickHandler : function (e) {
		this.showFields();
		e.stop();
	},
	
	showFields : function () {
		if (this.shown) return;
		
		for (var i = 0; i < this.fields.length; i++)
			this.fields[i].removeClassName(this.HIDDEN_CSS_CLASS);
		
		this.activator.innerHTML = this.activator.childNodes[0].innerHTML;
		this.shown = true;
		window.setTimeout('fixCellar()', 100);
	}
	
}
