jQuery(document).ready(function($) {

	// We use the timeout here to work around a Google Chrome bug
	// which gives us totally wrong offsets for the sidebar...
	window.setTimeout(function() {
		var menu = $('div.menubar');
		var $window = $(window);
		var menuCurrentlyFixed = false;

		$window.scroll(function(e) {
			if (!menuCurrentlyFixed && $window.scrollTop() > 118) {
				$('div.sm_icon').addClass('fixed');
				menu.addClass('fixed');
				menu.next('.greybar').addClass('fixed');
				$('.cut_top').addClass('fixed');
				menuCurrentlyFixed = true;
			} else if (menuCurrentlyFixed && $window.scrollTop() < 118) {
				$('div.sm_icon').removeClass('fixed');
				menu.removeClass('fixed');
				menu.next('.greybar').removeClass('fixed');
				$('.cut_top').removeClass('fixed');
				menuCurrentlyFixed = false;
			}
		});
		$('.sidebar').each(function() {
			var $this = $(this);
			var sidebarCurrentlyFixed = false;
			var absolutePositionOfSidebar = $this.offset().top;
			var offsetOfSidebarFromTopEdge = 60;

			$window.resize(function() {
				if (!sidebarCurrentlyFixed) return;
				var pageWidth = $('.center').outerWidth();
				var viewportWidth = $(document).width();
				var newLeftOffset = ((viewportWidth / 2) + (pageWidth/2) - $this.outerWidth() - 20);
				if ($.browser.msie) {
					// Hack for IE
					newLeftOffset -= 10;
				}
				$this.css('left', newLeftOffset + 'px');
			});

			$window.scroll(function(e) {
				if (!sidebarCurrentlyFixed && $window.scrollTop() > absolutePositionOfSidebar - offsetOfSidebarFromTopEdge) {
					sidebarCurrentlyFixed = true;
					$this.addClass('fixed');
					$this.css('height', $this.height() + 'px');
					$this.css('top', offsetOfSidebarFromTopEdge + 'px');
					$this.css('left', $this.offset().left + 'px');
					$this.css('position', 'fixed');
					$window.resize();
				} else if (sidebarCurrentlyFixed && $window.scrollTop() < absolutePositionOfSidebar - offsetOfSidebarFromTopEdge) {
					sidebarCurrentlyFixed = false;
					$this.removeClass('fixed');
					$this.css('height', 'auto');
					$this.css('top', '0px');
					$this.css('left', '0px');
					$this.css('position', 'relative');
				}
			});
			// Trifgger first scroll in IE
			$window.scroll();

			var $sideMenu = $this.find('ul');
			var currentlyChosenAnchor = null;

			var positionsOfSectionHeadlines = [];

			$sideMenu.find('li a').each(function() {
				var $link = $(this);
				var anchor = $link.attr('href');

				anchor = anchor.match(/#(.*)$/)[1];

				var offset = $('#' + anchor).offset().top - 100;

				positionsOfSectionHeadlines.push({
					anchor: anchor,
					offset: offset
				});

				// TODO: recalculate on resize

				$link.click(function(e) {
					$('html,body').animate({'scrollTop': offset+3});
					e.preventDefault();
				});
			});

			$window.scroll(function(e) {
				var scrollPosition = $window.scrollTop();
				var chosenAnchor = null;

				$.each(positionsOfSectionHeadlines, function(index, el) {
					if (scrollPosition > el.offset) {
						chosenAnchor = el.anchor;
					}
				});
				if (!chosenAnchor) {
					chosenAnchor = positionsOfSectionHeadlines[0].anchor;
				}
				if (currentlyChosenAnchor === chosenAnchor) return;

				currentlyChosenAnchor = chosenAnchor;

				$sideMenu.find('.active').removeClass('active');
				$sideMenu.find('a[href$="#' + chosenAnchor + '"]').parent('li').addClass('active');
			});
			$sideMenu.find('li').first().addClass('active');
		});
	}, 20);
});
