"strict";

var WdVerticalAdjust = new Class
({

	initialize: function(el)
	{
		this.element = $(el);
		this.container = this.element.getParent();

		this.initialY = this.element.getPosition().y;
		this.elementHeight = this.element.getSize().y;
		this.min = this.element.getStyle('margin-top').toInt();

		window.addEvent('scroll', this.adjust.bind(this));

		this.element.set
		(
			'tween',
			{
				property: 'margin-top',
				duration: 'long',
				link: 'cancel',
				transition: Fx.Transitions.Sine.easeInOut
			}
		);

		this.adjust();
	},

	adjust: function()
	{
		var min = this.min;
		var e_coords = this.element.getCoordinates();
		var cCoords = this.container.getCoordinates();
		var c_size = this.container.getSize();

		var y = window.scrollY;

		y = Math.max(y, cCoords.top + this.min);
		y = Math.min(y, cCoords.bottom - this.elementHeight - (this.initialY - cCoords.top - this.min));

		this.element.get('tween').start(y - cCoords.top);
	}
});

window.addEvent
(
	'domready', function()
	{
		$$('div.article.main div.meta').each
		(
			function(el)
			{
				new WdVerticalAdjust(el);
			}
		);
	}
);
