
var toc = {
	toc: [],
	cols: [],
	options: {
		cols: 3,
		switched: false,
		firstOpen: false
	},
	init: function() {
		jQuery.extend(true, this.options, objFromRel('#indexedContent'));
		if (!$('#simpleMenu').length)
			$('#indexedContent').before('<div id="simpleMenu"/>');
		var tocContainer = $('#simpleMenu');
		$(tocContainer).append('<table class="site_index col' + this.options.cols + '"><tbody><tr></tr></tbody></table>');
		for (i = 0; i<this.options.cols; i++) {
			this.cols[i] = $('<td class="vt"/>').appendTo('#simpleMenu table.site_index tr:first');
		}
		$('#indexedContent h1[id], #indexedContent h2[id]').each(function() {
			var obj = {
				html: $(this).html(),
				id: $(this).attr('id'),
				nodeName: $(this).prop('nodeName').toLowerCase(),
				href: null
			};
			toc.toc.push(obj);
		});
		$('#simpleMenu a').each(function() {
			var obj = {
					html: $(this).html(),
					id: null,
					nodeName: null,
					href: $(this).attr('href'),
					classes: ($(this).attr('class')) ? $(this).attr('class') : ''
			};
			toc.toc.push(obj);
			$(this).remove();
		});
		var itemPerColumn = this.toc.length/this.options.cols;
		for (var header=0; header<this.toc.length; header++) {
			if (this.toc[header].href) {
				$('<a href="' + this.toc[header].href + '" class="block ' + this.toc[header].classes + '"></a>')
					.appendTo(this.cols[Math.floor(header/itemPerColumn)])
					.html(this.toc[header].html)
			} else {
				var h1Classes = (this.toc[header].nodeName == 'h1')?'link marker ':'';
				$('<a href="#' + this.toc[header].id + '" class="' + h1Classes + 'block"></a>')
					.appendTo(this.cols[Math.floor(header/itemPerColumn)])
					.html(this.toc[header].html)
					.click(function() {
						toc.scrollToAnchor($(this).attr('href').split('#')[1]);
						return false;
					})
			}
		}
		this.initMoreInfo();
		if (this.options.switched) {
			var anchor = document.URL.split('#')[1];
			if (anchor && $('#'+anchor))
				this.scrollToAnchor(anchor);
			else
				$('div.help_module').hide();
		}
		if (this.options.firstOpen) {
			$('#' + this.toc[0].id).parents('div.help_module').show();
		}
	},
	initMoreInfo: function() {
		$('#indexedContent .moreInfo').each(function() {
			$(this).hide();
			$('<a href="#" class="link linkAdd">' + i18n.iface.button.more + '</a>')
				.insertAfter(this)
				.click(function() {
					toc.moreInfoClick(this);
					return false;
				})
				.data('oppened', false);
		});
	},
	moreInfoClick: function(el) {
		if ($(el).data('oppened'))
			$(el)
				.text(i18n.iface.button.more)
				.data('oppened', false)
				.prev()
				.hide('blind', {queue: false});
		else
			$(el)
				.text(i18n.iface.button.less)
				.data('oppened', true)
				.prev()
				.show('blind', {queue: false});
	},
	scrollToAnchor: function(anchor) {
		if (this.options.switched) {
			$('div.help_module').hide();
			$('#' + anchor).parents('div.help_module').show();
		}
		$('html,body').animate({
			scrollTop: $('#' + anchor).offset().top
		});
	}
};

$(function() {
	toc.init();
});

