/**
* Author: Alex Anzelm
* Company: BUZZfrineds
* Date: 4 feb 2007
*/

MenuScroll=function(elementName, menuScroll, scrollName)
{
	this.objectElement = document.getElementById(elementName);
	this.objectMenu = document.getElementById(menuScroll);
	this.objectScroll = document.getElementById(scrollName);
	
	this.objectTopScroll = null;
	this.objectBottomScroll = null;
	
	this.ScrollTimer = 0;
	
	this.offsetScroll = 10;
	
	this._CheckScroll();
}

MenuScroll.prototype._CheckScroll=function()
{
	if (this.objectScroll.offsetHeight > this.objectMenu.offsetHeight)
	{
		this.objectScroll.style.top = '0px';
		this._CreateElements();
	}
	else
		return;
}

MenuScroll.prototype._CreateElements=function()
{
	divTop = document.createElement('DIV');
	divTop.className = 'top';
	
	hrefTop = document.createElement('A');
	hrefTop.innerHTML = 'вверх';
	hrefTop.href = 'javascript://';
	hrefTop.thisElement = this;
	
	divTop.appendChild(hrefTop);
	
	divBottom = document.createElement('DIV');
	divBottom.className = 'bttm';
	
	hrefBottom = document.createElement('A');
	hrefBottom.innerHTML = 'вниз';
	hrefBottom.href = 'javascript://';
	hrefBottom.thisElement = this;
	
	divBottom.appendChild(hrefBottom);
	
	this.objectTopScroll = hrefTop;
	this.objectBottomScroll = hrefBottom;
	
	this.objectElement.insertBefore(divTop, this.objectMenu);
	
	divElements = this.objectElement.getElementsByTagName('DIV');
	if (divElements.length > 3)
	{
		if (5 == divElements.length)
			this.objectElement.insertBefore(divBottom, divElements[divElements.length - 2]);
		else
			this.objectElement.insertBefore(divBottom, divElements[divElements.length - 1]);
	}
	else
		this.objectElement.appendChild(divBottom);
	
	this._CreateEvents();
	this._SetActiveOffset();
}

MenuScroll.prototype._CreateEvents=function()
{
	this.objectTopScroll.onmousedown = this.ScrollUp;
	this.objectTopScroll.onmouseup = this.objectTopScroll.onmouseout = this.EndScroll;
	
	this.objectBottomScroll.onmousedown = this.ScrollDown;
	this.objectBottomScroll.onmouseup = this.objectBottomScroll.onmouseout = this.EndScroll;
}

MenuScroll.prototype._SetActiveOffset=function()
{
	linkElements = this.objectScroll.getElementsByTagName('A');
	
	offsetTop = 0;
	
	for (i = 0; i < linkElements.length; i++)
	{
		if ((-1) != parseInt(linkElements[i].className.indexOf('act')))
		{
			if ((this.objectScroll.offsetHeight - offsetTop) > this.objectMenu.offsetHeight)
				this.objectScroll.style.top = -offsetTop + 'px';
			else
				this.objectScroll.style.top = (-this.objectScroll.offsetHeight + this.objectMenu.offsetHeight) + 'px';
			
			break;
		}
		else
		{
			offsetTop += linkElements[i].offsetHeight;
		}
	}
}

MenuScroll.prototype.ScrollUp=function()
{
	if (this.thisElement)
	{
		if (0 > parseInt(this.thisElement.objectScroll.style.top))
		{
			this.thisElement._Move();
			this.thisElement.ScrollTimer = setTimeout("objMenuScroll.ScrollUp()", 10);
		}
	}
	else
	{
		if (0 > parseInt(this.objectScroll.style.top))
		{
			this._Move();
			this.ScrollTimer = setTimeout("objMenuScroll.ScrollUp()", 10);
		}
	}
}

MenuScroll.prototype.ScrollDown=function()
{
	if (this.thisElement)
	{
		if ((this.thisElement.objectScroll.offsetHeight + parseInt(this.thisElement.objectScroll.style.top)) > this.thisElement.objectMenu.offsetHeight)
		{
			this.thisElement._Move(-1);
			this.thisElement.ScrollTimer = setTimeout("objMenuScroll.ScrollDown()", 10);
		}
	}
	else
	{
		if ((this.objectScroll.offsetHeight + parseInt(this.objectScroll.style.top)) > this.objectMenu.offsetHeight)
		{
			this._Move(-1);
			this.ScrollTimer = setTimeout("objMenuScroll.ScrollDown()", 10);
		}
	}
}

MenuScroll.prototype.EndScroll=function()
{
	if (this.thisElement.ScrollTimer)
		clearTimeout(this.thisElement.ScrollTimer);
	else
		clearTimeout(this.ScrollTimer);
}

MenuScroll.prototype._Move=function(offset)
{
	if (isNaN(offset))
		offset = 1;
	this.objectScroll.style.top = (parseInt(this.objectScroll.style.top) + offset*this.offsetScroll) + 'px';
}
