/* JavaScript Document
 *********************/

$(document).ready(function(){

	// headings...
	$('h2, h3, h4, h5, h6').each(function(i, e) {

		// wrap default content
		$(this).wrapInner('<span class="h"></span>');


		if ($(this).attr('id')) {
			$(this).wrapInner('<a href="#'+ $(this).attr('id') +'" title="Odkaz na tuto sekci." class="anchor"></a>');
		}

		// add breadcrumbs

		var h_level = e.nodeName.substr(1,1);
		var bc = $('h1').text();

		if (h_level >= 2) {
			for (var i = 2; i <= h_level; i++) {

				if (i == h_level) {
					bc += ' &rarr; ' + $(this).text();
				}
				else {
					bc += ' &rarr; ' + $(this).prevAll('h' + i + ':first').find('.h').text();
				}
			}

			$(this).append(' <span class="bc">' + bc + '</span>');
		}

	});

	// add skip-to-top link
	$('body').attr('id', 'top').append('<a href="#top" id="to-top">&uArr; Nahoru</a>');


	// fix links to files
	$('a > img').parent().addClass('linking-image');

	// blockcode: select all
	// add button
	$('pre:has(code), code.block')
		.css('position', 'relative')
		.append('<button class="select-all">Vybrat vše</button>');

	// make button work
	var setting_blockcode_top_nodename = 'pre'; // e.g. change to 'div' if you write block code into div.myblockcode > code
	var blockcode_textarea_counter = 0;

	$('button.select-all').click(function(){

		// The button's text should not be in the textarea. Temporary hiding into its title attribute.
		$('button.select-all[title]').each(function(){
			$(this).text($(this).attr('title')).removeAttr('title');
		});
		$(this).attr('title', $(this).text()).text('');

		// Hide other textareas and show their originals
		$('.hidden-code').show();
		$('textarea.blockcode').remove();

		var parent = $(this).parent(setting_blockcode_top_nodename);
		blockcode_textarea_counter++;

		// Add textarea and hide original
		parent
			.after('<textarea class="blockcode" readonly="readonly" id="blockcode_'+ blockcode_textarea_counter +'">' + parent.text() + '</textarea>')
			.addClass('hidden-code')
			.hide();

		// Focus the new textarea and select its content
		$('#blockcode_'+blockcode_textarea_counter)
			.css('height', (parent.innerHeight() - 13) + 'px')
			.focus()
			.select();
	});
});
