/**
 * @author Peter Slagter
 * @email peter@procurios.nl
 */

document.observe("dom:loaded", function()
{	
	// Add a back (if possible) & print link under last article
	if (!$('home')) {
		if (window.history.length > 1) {
			var backLink = new Element('a', {
				'href': 'javascript:history.go(-1)',
				'title': PbLib.g('Click to go back one page in history'),
				'class': 'back'
			});
			
			var backLinkTxt = document.createTextNode(PbLib.g('Back'));
			backLink.appendChild(backLinkTxt);
			
			$('content').insert({
				'bottom': backLink
			});
		}
		
		var printLink = new Element('a', {
			'href': 'javascript:window.print()',
			'title': PbLib.g('Click to print the current page'),
			'class': 'print'
		});
		
		var printLinkTxt = document.createTextNode(PbLib.g('Print'));
		printLink.appendChild(printLinkTxt);
		
		$('content').insert({
			'bottom': printLink
		});
	}
	
	// Add main menu classes to first childs
	$$('div#nav>ul>li').each(function(element, index) {
		element.addClassName('direct-child-'+index);
	});
	
	// Add menu-title to submenu
	if ($('subnav') && $$('div#nav li.active')[0]) {
		var activeTitle = $('home') ? $('nav').down().next().down().next().down().innerHTML : $('nav').select('li.active > a')[0].innerHTML;
		activeTitle = activeTitle.replace(/&amp;/g,'&');
		var h2 = new Element('h2');
		var h2Text = document.createTextNode(activeTitle);
		h2.appendChild(h2Text);
		
		$('subnav').insert({'top':h2});
	}
	
	// Set click event on banners
	if ($('home')) {
		var banners = $$('div.banner');
		
		banners.each( function(elem) {
			
			elem.setStyle({'cursor' : 'pointer'});
			var href = elem.select('a').first().href;
				
			elem.observe('click', function(event) {
				Event.stop(event);
				window.location.href = href;
			});
			
		});
	}
});