NetR = typeof NetR === 'undefined' ? {} : NetR;

//
// Accordion
//
NetR.Accordion = function (el, options) {
	var t = this;

	this.options = $.extend({}, NetR.Accordion.defaultOptions, options || {});
	this.element = el;

	this.modules = this.element.children('div.m').map(function () {
		return new NetR.Accordion.Module($(this), t);
	});
};
NetR.Accordion.defaultOptions = {
	selectedClass: 'sel'
};
NetR.Accordion.prototype = {};

NetR.Accordion.Module = function (el, accordion) {
	var t = this;

	this.accordion = accordion;
	this.container = el;
	this.header = el.children('div.m-h');
	this.content = el.children('div.m-c');

	this.content.height(this.content.height());

	if (this.container.hasClass(this.accordion.options.selectedClass)) {
		this.accordion.activeModule = this;
	} else {
		this.content.hide();
	}

	this.header.children('h2, h3').wrapInner(function () {
		var a = $('<a>', {
			href: '#',
			click: function (e) {
				e.preventDefault();
				if (t != t.accordion.activeModule) {
					if (t.accordion.activeModule) {
						t.accordion.activeModule.hide();
					}
					t.show();
					t.accordion.activeModule = t;
				} else {
					t.hide();
					t.accordion.activeModule = null;
				}
			}
		});
		return a;
	});
};
NetR.Accordion.Module.prototype = {
	show: function () {
		var t = this;
		t.container.addClass(this.accordion.options.selectedClass);
		this.content.slideDown('fast');
	},
	hide: function () {
		var t = this;
		t.container.removeClass(this.accordion.options.selectedClass);
		this.content.slideUp('fast');
	}
};
