window.addEvent('domready', function() {
	$ifel = function(el) { return $chk($(el)); };

	// Базовый класс. Можно расширить при помощи implement()
	var TextMuncher = new Class({
		victim: false,

		// Селекторы для заголовков и блоков
		headEl: 'h2',
		blockEl: 'div.tab',

		// Куда переносить заголовки и блоки соответственно
		// По умолчанию блоки реинжектируются в victim
		targetH: false,
		targetB: false,

		// Выполнять ли дефолтные setStyle('display', 'none/block')
		directStyle: true,

		headers: [],
		blocks: [],

		current: false,

		initialize: function(v) {
			$chk(v) && (this.victim = $(v));

			if (this.victim.getChildren(this.headEl).length < 1) return;

			this.targetH || (this.targetH = this.victim);
			this.targetB || (this.targetB = this.victim);

			this.onInitialized();
			this.process();
		},

		process: function() {
			this.victim.addClass('tmEnabled');

			this.headers = this.victim.getChildren(this.headEl);
			this.blocks = this.victim.getChildren(this.blockEl);

			this.headers.each(function(v, k) { v.addEvent('click', function() {
				v.addClass('active');

				if (this.current) {
					if (this.current ==  this.blocks[k]) return;
					this.directStyle && this.current.setStyle('display', '');
					this.headers[this.blocks.indexOf(this.current)].removeClass('active');
					this.onHidingCurrent();
				}

				this.current = this.blocks[k];

				this.directStyle && this.current.setStyle('display', 'block');

				this.onHClick();
			}.bind(this))}.bind(this));

			this.onBeforeInjection();

			this.headers.inject(this.targetH);
			this.blocks.inject(this.targetB);

			this.headers[0].fireEvent('click');
		},

		//events
		onInitialized: function() {},
		onHClick: function() {},
		onBeforeInjection: function() {},
		onHidingCurrent: function() {}
	});

	$ifel('thegoogle') && new TextMuncher('thegoogle');

	if ($ifel('informator')) {
		TextMuncher.implement({
			targetB: $('iiright'),
			targetH: $('iileft')
		});

		new TextMuncher('informator');
	}
});