if (!window.MapMover)
		MapMover = new Object();
	
	MapMover.initialise = function() {
		var moving_bit = $("map_mover");
		var sidebar = $("s_map");
		var companies = $("s_content");
		this.top = 130 ;
		//this.top = Position.cumulativeOffset(moving_bit)[1]
		this.bottom = Element.getHeight(companies) + Position.cumulativeOffset(companies)[1];
		this.left = 5;
		this.movingBitHeight = Element.getHeight(moving_bit);
		this.movingBitTop = 0;
		this.timeoutHandle = null;
		this.moveInProgress = false;
		this.padding = 0;
		moving_bit.style.top = this.top + "px";
		moving_bit.style.left = this.left + "px";
		moving_bit.style.position = "absolute";
	}
	
	MapMover.getScrollYPosition = function () {
		if (window.pageYOffset)
			return window.pageYOffset
			;
		else if (document.documentElement.scrollTop)
			return document.documentElement.scrollTop;
		else
			return document.body.scrollTop;
	}
	
	MapMover.getWindowHeight = function () {
		if (window.innerHeight)
			return window.innerHeight;
		else if (document.documentElement.clientHeight)
			return document.documentElement.clientHeight;
		else
			return document.body.clientHeight;
	}
	
	MapMover.timeoutHandler = function() {
		// Handles any events queue
		if (this.moveInProgress) {
			var newPos = MapMover.calculateNewPosition();
			if (newPos != this.movingBitTop) {
				dur = Math.abs(newPos - this.movingBitTop) / 400;
				if (dur < 0.1) dur = 0.1;
				new Effect.Move("map_mover", {y: newPos, x:0, mode: 'absolute', duration: dur});
				this.movingBitTop = newPos;
			}
			this.moveInProgress = false;
		}
		this.timeoutHandle = null;
	}
	
	MapMover.startTimeout = function() {
		if (this.timeoutHandle != null)
			window.clearTimeout(this.timeoutHandle);
		this.timeoutHandle = window.setTimeout("MapMover.timeoutHandler();", 700);
	}
	
	MapMover.queueMove = function() {
		this.moveInProgress = true;
		MapMover.startTimeout();
	}
	
	MapMover.calculateNewPosition = function() {
		//this.movingBitHeight = Element.getHeight(moving_bit);
		var windowTop = this.getScrollYPosition() + this.padding;
		var newPos = this.movingBitTop;
		
		if (this.movingBitTop < Math.max(windowTop, this.top) ||
		    (this.movingBitTop + this.movingBitHeight) > (windowTop + this.windowHeight)) {
			// Keep near the top if we can.
			newPos = Math.max(windowTop, this.top)- 170;
			// provided that we don't go below the bottom.
			if ((newPos + this.movingBitHeight) > this.bottom)
				newPos = this.bottom - this.movingBitHeight;
			if (newPos < this.top)
				newPos = this.top ;
		}
		return newPos;		
	}
	
	MapMover.resizeHandler = function() {
		this.windowHeight = this.getWindowHeight();
		this.left = Position.cumulativeOffset($("s_map"))[0];
		$("map_mover").style.left = 0+ "px";
	}
	
	MapMover.scrollHandler = function() {
		if (MapMover.calculateNewPosition() != this.movingBitTop)
			this.queueMove();
	}
	
	// Link to events.
	var old_onscroll = window.onscroll;
	var old_onresize = window.onresize;
	
	window.onscroll = function() {
		MapMover.scrollHandler();
		
		if (old_onscroll)
			old_onscroll();
	}
	
	window.onresize = function() {
		MapMover.resizeHandler();
		
		MapMover.scrollHandler();
		
		if (old_onresize)
			old_onresize();
	}

	// Run anyway when loading.	
	MapMover.initialise();
	MapMover.resizeHandler();
	MapMover.scrollHandler();