jQuery.noConflict();
jQuery(function(){
	var $ = jQuery;
	// create topic navigation for wiki
	if( $('.retro-wiki') && $('h1,h2,h3,h4,h5,h6,h7','.retro-wiki').length>2){
		// check for titles
		var container = $('.retro-wiki .markup.content')//.css('position','relative')
			, _titles = $('h1,h2,h3,h4,h5,h6,h7',container)
			, curLevel = 0
			, curList  = null
			, topPos = 150;
			;
		container.prepend('<a name="topnav"></a>');
		container = $('<div id="topicNavigation"><div>Quick Navigation</div><div></div></div>').prependTo(container);
		container.css({
				position: 'absolute'
			, background: '#eee'
			, border: 'solid silver 1px'
			, width: '250px'
			, left: '990px' 
			, top: topPos
			, opacity: '.8'
		});
		container.find('div:first').css({
				fontWeight:'bold'
			, textAlign: 'center'
			, cursor:'pointer'
		}).click(function(){ $(this).next('div').toggle();}).click();

		container = container.find('div:last');
		// create anchors for each of this headers
		_titles.each(function(){
			var h = $(this)
				, label=h.text()
				, name=label.replace(/[\s_]+/g,'_').replace(/[^a-z0-9-_]+/ig,'').replace(/(^_+|_+$)/g,'')
				, level=parseInt(h.attr('tagName').replace(/^h/i,''),10)
				, i = 0
				;
			h.before('<a name="'+name+'" href="#topnav" style="float:right"> &uArr; </a>');
			
			if( level > curLevel){
				curList = $('<ul></ul>').appendTo(curList?$('li:last',curList):container).css({padding:0,margin:'0 0 0 .8em',listStylePosition:"inside"});
			}else if( level < curLevel){
				for( i=level;i<curLevel;i++){
					curList = curList.closest('ul');
				}
			}
			curList.append('<li><a href="#'+name+'">'+label+'</a></li>');
			curLevel = level;
		});
		container = container.parent();
		$(window).scroll(function(){
			var top = $(window).scrollTop();			
			container.css('top',Math.max(topPos,top+30));
		});
		// check for anchor to go
		if( window.location.href.match(/#/)){
			$('#topicNavigation div:first').click();
			//$(window).scrollTop($('a[name='+window.location.href.replace(/^.*#/,'')+']'));
		}
	}
});

