function eventMousePos(e, calcScroll)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
		
	var scrollx = 0, scrolly = 0;

	if (calcScroll)
	{
		if( typeof( window.pageYOffset ) == 'number' )
		{
			//Netscape compliant
			scrollx = window.pageXOffset;
			scrolly = window.pageYOffset;
		}
		else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
		{
			//DOM compliant
			scrolly = document.body.scrollTop;
			scrollx = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
		{
			//IE6 standards compliant mode
			scrolly = document.documentElement.scrollTop;
			scrollx = document.documentElement.scrollLeft;
		}
	}
	
	return {x:posx + scrollx, y:posy+scrolly};
}

function nodePos(n)
{
	var curleft = curtop = 0;
	if (n.offsetParent)
	{
		do
		{
			curleft += n.offsetLeft;
			curtop += n.offsetTop;
		} while (n = n.offsetParent);
		return {x:curleft, y:curtop};
	}
}

function nodeSize(n)
{
	var sx, sy;
	sx = n.clientWidth;
	sy = n.clientHeight;
	return {x:sx , y:sy};

}

function eventClientMousePos(e, n)
{
	var pos = eventMousePos(e);
	var off = nodePos(n);
	
	return {x:pos.x - off.x, y:pos.y - off.y};
}

var savedPosition = 0;
var savePositions = true;

function resetPosStopSaving()
{
	savePositions = false;
	popupcu.hide();
}

function morestories()
{
	this.ms_main = document.getElementById('morestories');
	this.ms_main.scrollTop = 0;
	this.stop = false;
	
	for (var i=0; i<this.ms_main.childNodes.length; i++)
	{
		if(this.ms_main.childNodes[i].getAttribute && this.ms_main.childNodes[i].getAttribute('id')=='ms-container')
			this.content = this.ms_main.childNodes[i];
	}

	if (savePositions)
	{
		this.scrollTo = savedPosition;
		this.ms_main.scrollTop = savedPosition;
	}
	savePositions = true;
	
	// start saving if stopped previously!
	var savePosition = true;
	
	this.on_mousemove = function(e)
	{
		if (!e) var e = window.event;
		
		/*
		mode 1 with margin	
		var hscroll_pos = eventClientMousePos(e, this.ms_main).y;
		var hscroll_w = this.ms_main.clientHeight;
		var content_h = this.content.offsetHeight;
		this.ms_main.scrollTop = (hscroll_pos/hscroll_w) * content_h - (hscroll_w/2);
		*/
		
		/* mode 2 no margin */
		var margin = 15;
		
		var hscroll_pos = Math.max(0, eventClientMousePos(e, this.ms_main).y - margin);
		var hscroll_w = this.ms_main.clientHeight - (margin*2);
		var content_h = this.content.offsetHeight - hscroll_w;
		this.scrollTo = (hscroll_pos/hscroll_w) * content_h;			
	}
	
	this.stopanim = function()
	{
		this.stop = true;
	}
	
	this.anim = function()
	{
		if (this.stop)
			return;
		
		try
		{
			/*this.ms_main.scrollTop = (this.scrollTo + this.ms_main.scrollTop*2) / 3;*/
			
			// calculate movement in pixels
			var pos = this.ms_main.scrollTop;
			var new_pos = (this.scrollTo + pos*2) / 3;
			
			if (Math.abs(pos - new_pos)>10)
			{
				if (pos > new_pos)
					new_pos = pos - 10;
				else
					new_pos = pos + 10;
				
			}
			
			this.ms_main.scrollTop = new_pos;
			if (savePosition)
				savedPosition = this.ms_main.scrollTop;
		}
		catch (e)
		{
			alert(e);
		}
		
		var that = this;
		window.setTimeout(function () {that.anim()}, 50);	
		
		//this.a = this.a+1;
		//window.status = this.a;
	}
	this.anim();
	
	var event_closure = function(that_)
	{
		var that = that_;
		that.ms_main.onmousemove = function(e) { that.on_mousemove(e);};
			
	}
	event_closure(this);
}