/**
 * folding trees
 */
$.fn.foldingTree = function(){
	var tree = $(this);
	
	// hide subtrees initially
	tree.find('.keywordsubtree').hide();
	
	// add click handler to all items with subtree
	tree.find('li.keyword-has-children > a').click(function(){
		$(this).next().toggle(400);
		return false;
	});
}

/**
 * adjust tree height
 */
$.fn.keywordtreeHeight = function(){
	var tree = $(this).children('ul').eq(0);
	var height_avail = tree.height();
	var height_item = 20;
	var items = tree.children('li');
	var count = items.length;
	
	var margin_avail = height_avail - height_item * count;
	var margin_item  = Math.floor(margin_avail / (count - 1));

	items.css({'margin-bottom': margin_item});
	items.filter(':last').css({'margin-bottom': 0});
}

/**
 * init language names
 */
$.fn.initLanguageNames = function(){
	$(this).find('li.lc-deu_deu span').text('Deutsch');
	$(this).find('li.lc-eng_gbr span').text('English');
	$(this).find('li.lc-eng_usa span').text('English');
}

/**
 * adjust JS links
 */
$.fn.adjustJsLinks = function(){
	if($(this).length == 0) return;
	
	var jslinks = $(this).find('#mainmenu a[href^=javascript]');
	if(jslinks.length == 0) return;
	
	jslinks.each(function(){
		if($(this).attr('target') == '_top'){
			$(this).attr('target', '_self');
		}
	});
	
}

/**
 * run on document.ready
 */
$(document).ready(function(){
	$('#mainmenu').initLanguageNames();
	$('#home-categories').keywordtreeHeight();
	$('#home-categories .keywordtree').foldingTree();
	$('#wrapper.webgate').adjustJsLinks();
});
