$(function(){
	window._wikiModule = $('#main_category').attr('class');
	
	_wikiHandlers['init']();
	_wikiHandlers[_wikiModule] && _wikiHandlers[_wikiModule]();
});

window._wikiModule = null;
window._wikiHandlers = {};

/**
 * Inicjalizacja menu z lewej strony
 * @return
 */
_wikiHandlers['init'] = function()
{	
	initLeftMenu();
	initHeader();
	highlightActiveLink();
    initFaqDivs();
	
	function initLeftMenu()
	{
		var menuBoxes= $('div.generated-sidebar:has(h5)');
        if (!menuBoxes.size())
        {
            return;
        }
		// dołożenie ikon rozwijania do elementów, które mają więcej niż jeden element na liście
		menuBoxes.children('h5').each(function(){
			var count = $(this).next().find('ul').children().size();
			if(count == 1 && $(this).next().find('a').text().replace(/ /g,'').toLowerCase() == $(this).text().replace(/ /g,'').toLowerCase())
			{
				$(this).wrap($('<a/>').attr({
					'href' : $(this).next().find('a').attr('href')
				}));
				$(this).next().find('a').remove();
			}
			else
			{
				$(this).addClass('toggle_plus')
			}
		});

		menuBoxes.children('h5.toggle_plus')
		.toggle(function(){
			$(this).toggleClass('toggle_plus toggle_minus')
				.next().children('ul').toggleClass('hide show');
		}, function(){
			$(this).toggleClass('toggle_plus toggle_minus')
				.next().children('ul').toggleClass('hide show');
		});
	}

	function initHeader()
	{
		$('#header ul a#lnk_'+_wikiModule).addClass('active');
		$('#header h2').text($('#header ul a#lnk_'+_wikiModule).text());
	}
	
	function highlightActiveLink()
	{	
		$('.generated-sidebar a').each(function(){
			if (window.location.href === this.href)
			{
				var wrap = $('<span/>').addClass('active');
				$(this).addClass('active');
				$(this).parents('.generated-sidebar:eq(0)').find('h5').toggleClass('toggle_plus toggle_minus')
                    .next().children('ul').toggleClass('hide show');
				if ($(this).children().size())
				{
					wrap.append($(this).children());
				}
				else
				{
					wrap.text($(this).text());
				}
				wrap.insertAfter(this);
				$(this).remove();
			}
		});
	}

    function initFaqDivs()
    {
        $('#faq dt').addClass('inactive').next().hide()
            .end().toggle(function(){
                $(this).toggleClass('active inactive');
                $(this).next().toggle();
            }, function(){
                $(this).toggleClass('active inactive');
                $(this).next().toggle();
            });
        if (window.location.hash)
        {
            $('#'+window.location.hash.substring(1)).parent().click();
        }
    }
}

_wikiHandlers['kontakt'] = function()
{
        $('#column-one').remove();
        $('#column-content').add('#content').css('width', '100%');
        return;
}

