﻿var scroll;
var state;
var animate;
var scrollAnim, topPos;
var Stations;

function scrollLegend(elem, topPos){
	if ((document.body.scrollTop != state && !animate) || (scroll.offsetTop != (document.body.scrollTop + 30) && !animate)) {
		animate = true;
		if (elem.style.top){
			elem.style.top = elem.offsetTop + "px";
		} else {
			elem.style.top = topPos + "px";
		}
		elem.style.position = "absolute";
		
		if (document.body.scrollTop + 30 < topPos){
			to = topPos;
		} else if (document.body.scrollTop + 30 > Stations.offsetTop + Stations.offsetHeight - 200){
			to = Stations.offsetTop + Stations.offsetHeight - 200;
		} else {
			to = document.body.scrollTop + 30;
		}
			
		scrollAnim = new Tween(elem, "top", Math.easeOutCirc, elem.offsetTop, to, 20);
		scrollAnim.onMotionFinished = function(){
			animate = false;
		}
	}
	state = document.body.scrollTop;
}

$(document).ready(function(){
	scroll = document.getElementById("years_menu");
	state = document.body.scrollTop;
	animate = false;

	window.onload = function() {
		topPos = scroll.offsetTop;
		var getpos = scroll;
		while(getpos = getpos.offsetParent){
			topPos += getpos.offsetTop;
		}
		setInterval("scrollLegend(scroll, topPos)", 500);
	}
	Stations = document.getElementById("Stations");
	//ToggleLegend();
});