/*
	SCROLLAREA Version: 1.0
	2003 (c) advanware.com
	Created by: Andrey Romashkin
	Last update: 7.04.2006

	Custom attributes:
		imagesPath
		btnUpImage
		btnDownImage
		scrollImage
		sliderImage
		scrollbarBackgroundColor
		scrollStep
		wheelSensitivity
		scrollIEWrap
*/
var SA_scrollAreas = new Array();
var SA_resizeTimer = null;

//default settings
var SA_default_imagesPath = "images";
var SA_default_btnUpImage = "top-row.gif";
var SA_default_btnDownImage = "bottom-row.gif";
var SA_default_scrollImage = "";
var SA_default_sliderImage = "scrollslider.gif";
var SA_default_scrollbarBackgroundColor = "";
var SA_default_scrollStep = 5;
var SA_default_wheelSensitivity = 10;

if (window.addEventListener)
	window.addEventListener("load", initScrollbars, false);
else if (window.attachEvent)
	window.attachEvent("onload", initScrollbars);

function initScrollbars()
{
	var scrollElements = getElements("scrollable", "DIV", document, "id");
	for (var i=0; i<scrollElements.length; i++)
	{
		SA_scrollAreas[i] = new ScrollArea(i, scrollElements[i]);
	}
	initGreenBoxes();
}

function ScrollArea(index, elem) //constructor
{
	this.index = index;
	this.element = elem;
	var attr = this.element.getAttribute("imagesPath");
	this.imagesPath = attr ? attr : SA_default_imagesPath;
	attr = this.element.getAttribute("btnUpImage");
	this.btnUpImage = attr ? attr : SA_default_btnUpImage;
	attr = this.element.getAttribute("btnDownImage");
	this.btnDownImage = attr ? attr : SA_default_btnDownImage;
	attr = this.element.getAttribute("scrollImage");
	this.scrollImage = attr ? attr : SA_default_scrollImage;
	attr = this.element.getAttribute("sliderImage");
	this.sliderImage = attr ? attr : SA_default_sliderImage;
	attr = this.element.getAttribute("scrollbarBackgroundColor");
	this.scrollbarBackgroundColor = attr ? attr : SA_default_scrollbarBackgroundColor;
	attr = Number(this.element.getAttribute("scrollStep"));
	this.scrollStep = attr ? attr : SA_default_scrollStep;
	attr = Number(this.element.getAttribute("wheelSensitivity"));
	this.wheelSensitivity = attr ? attr : SA_default_wheelSensitivity;

	this.scrolling = false;
	this.scrollButtonHeight = 11;
	this.scrollbarWidth = 20;
	this.iOffsetY = 0;
	this.scrollHeight = 0;
	this.scrollContent = null;
	this.scrollbar = null;
	this.scrollup = null;
	this.scrolldown = null;
	this.scrollslider = null;
	this.scroll = null;
	this.enableScrollbar = false;
	this.scrollFactor = 1;
	this.scrollingLimit = 0;
	this.topPosition = 0;

	//functions declaration
	this.init = SA_init;
	this.scrollUp = SA_scrollUp;
	this.scrollDown = SA_scrollDown;
	this.createScrollBar = SA_createScrollBar;
	this.scrollIt = SA_scrollIt;
	this.setScrollPosition = SA_setScrollPosition;

	this.init();
}


function SA_init()
{
/*
	if (document.all && this.element.getAttribute("scrollIEWrap") == "true") //fix IE event bug
	{
		var d = document.createElement("DIV");
		this.element.parentNode.replaceChild(d, this.element);
		d.appendChild(this.element);
	}
*/
	this.scrollContent = document.createElement("DIV");
	this.scrollContent.style.position = "relative";
	this.scrollContent.style.left = "27px";
	this.scrollContent.innerHTML = this.element.innerHTML;
	this.element.innerHTML = "";
	this.element.appendChild(this.scrollContent);
	this.element.style.left = "0px";
	this.element.style.top = "19px";
	this.element.style.height = "253px";
	this.element.style.width = "298px";
	this.element.style.overflow = "hidden";
	this.element.style.display = "block";
	this.element.style.visibility = "visible";
	this.element.index = this.index;

	if (document.all)
	{
		this.element.onscroll = SA_handleOnScroll;
		this.element.onmousewheel = SA_handleMouseWheel;
		this.element.onresize = SA_handleResize;
		//element.onselectstart = handleSelectStart;
	}
	else
	{
		window.onresize = SA_handleResize; //???
	}
	this.createScrollBar();
}

function SA_createScrollBar()
{
	//remove old if exists
	if (this.scrollbar != null)
	{
		this.element.removeChild(this.scrollbar);
		this.scrollbar = null;
	}

	if (this.scrollContent.offsetHeight <= this.element.offsetHeight)
		this.enableScrollbar = false;
	else if (this.element.offsetHeight > 2*this.scrollButtonHeight)
		this.enableScrollbar = true;
	else
		this.enableScrollbar = false;

	if (this.scrollContent.offsetHeight - Math.abs(this.scrollContent.offsetTop) < this.element.offsetHeight)
		this.scrollContent.style.top = 0;

	//create scrollbar if needed
	if (this.enableScrollbar)
	{
		this.scrollContent.style.width = this.element.offsetWidth - this.scrollbarWidth;

		//create scrollbar		
		this.scrollbar = document.createElement("DIV");
		this.element.appendChild(this.scrollbar);
		this.scrollbar.style.position = "absolute";
		this.scrollbar.style.top = "126px";
		this.scrollbar.style.left = "6px";

		this.scrollbar.style.height = "167px"; /* this.element.offsetHeight*/ ;
		this.scrollbar.style.width = this.scrollbarWidth + "px";

		//create scroll up button
		this.scrollup = document.createElement("DIV");
		this.scrollup.index = this.index;
		this.scrollup.onmousedown = SA_handleBtnUpMouseDown;
		this.scrollup.onmouseup = SA_handleBtnUpMouseUp;
		this.scrollup.onmouseout = SA_handleBtnUpMouseOut;
		this.scrollup.style.position = "absolute";
		this.scrollup.style.top = "0px";
		this.scrollup.style.textAlign = "center";
		this.scrollup.style.width = this.scrollbarWidth + "px";
		this.scrollup.innerHTML = '<img title="Scroll Up" alt="Scroll Up" src="' + this.imagesPath + '/' + this.btnUpImage + '" border="0"/>';
		this.scrollbar.appendChild(this.scrollup);
		
		//create scroll down button
		this.scrolldown = document.createElement("DIV");
		this.scrolldown.index = this.index;
		this.scrolldown.onmousedown = SA_handleBtnDownMouseDown;
		this.scrolldown.onmouseup = SA_handleBtnDownMouseUp;
		this.scrolldown.onmouseout = SA_handleBtnDownMouseOut;
		this.scrolldown.style.position = "absolute";
		this.scrolldown.style.textAlign = "center";
		this.scrolldown.style.top =  this.scrollbar.offsetHeight - this.scrollButtonHeight + "px";
		this.scrolldown.style.width = this.scrollbarWidth + "px";
		this.scrolldown.innerHTML = '<img title="Scroll Down" alt="Scroll Down" src="' + this.imagesPath + '/' + this.btnDownImage + '" border="0"/>';
		this.scrollbar.appendChild(this.scrolldown);

		if(document.all)
		{
			this.scrolldown.style.left = "-9px";
			this.scrollup.style.left = "-9px";
		}

		//create scroll
		this.scroll = document.createElement("DIV");
		this.scroll.index = this.index;
		this.scroll.style.position = "absolute";
		this.scroll.style.zIndex = 0;
		this.scroll.style.textAlign = "center";
		this.scroll.style.top = this.scrollButtonHeight + "px";
		this.scroll.style.left = "0px";
		
		this.scroll.style.width = "100%";
		var h = this.scrollbar.offsetHeight - 2*this.scrollButtonHeight;
		this.scroll.style.height = ((h > 0) ? h : 0) + "px";
		this.scroll.innerHTML = '';
		this.scroll.onclick = SA_handleScrollbarClick;
		this.scrollbar.appendChild(this.scroll);

		//create slider
		this.scrollslider = document.createElement("DIV");
		this.scrollslider.index = this.index;
		this.scrollslider.style.position = "absolute";
		this.scrollslider.style.zIndex = 1000;
		this.scrollslider.style.textAlign = "center";
		this.scrollslider.innerHTML = '<div id="scrollbar" style="margin:0px 0 0px 3px;width:11px;height:30px;border:1px solid #EFF0C7;"></div>';
		this.scrollbar.appendChild(this.scrollslider);
		
		this.subscrollslider = document.getElementById("scrollbar");
		
		this.subscrollslider.style.height = Math.round((this.element.offsetHeight/this.scrollContent.offsetHeight)*(this.scrollbar.offsetHeight - 2*this.scrollButtonHeight)) + "px";

		if(document.all)
		{
			this.subscrollslider.style.margin = "0px 0 0px 2px";
		}
		
		this.scrollHeight = this.scrollbar.offsetHeight - 2*this.scrollButtonHeight - this.scrollslider.offsetHeight;
		this.scrollFactor = (this.scrollContent.offsetHeight - this.element.offsetHeight)/this.scrollHeight;
		this.topPosition = getRealTop(this.element)+this.scrollbar.offsetTop;
		this.scrollbarHeight = this.scrollbar.offsetHeight - 2*this.scrollButtonHeight - this.scrollslider.offsetHeight;

		this.scrollslider.style.top = /* 1 / this.scrollFactor * Math.abs(this.scrollContent.offsetTop) +*/ this.scrollButtonHeight + "px";
		this.scrollslider.style.left = "0px";
		this.scrollslider.style.width = "100%";
		this.scrollslider.onmousedown = SA_handleSliderMouseDown;
		if (document.all)
			this.scrollslider.onmouseup = SA_handleSliderMouseUp;
	}
	else
		this.scrollContent.style.width = this.element.offsetWidth + "px";
}

function SA_handleBtnUpMouseDown()
{
	var sa = SA_scrollAreas[this.index];
	sa.scrolling = true;
	sa.scrollUp();
}

function SA_handleBtnUpMouseUp()
{
	SA_scrollAreas[this.index].scrolling = false;
}

function SA_handleBtnUpMouseOut()
{
	SA_scrollAreas[this.index].scrolling = false;
}

function SA_handleBtnDownMouseDown()
{
	var sa = SA_scrollAreas[this.index];
	sa.scrolling = true;
	sa.scrollDown();
}

function SA_handleBtnDownMouseUp()
{
	SA_scrollAreas[this.index].scrolling = false;
}

function SA_handleBtnDownMouseOut()
{
	SA_scrollAreas[this.index].scrolling = false;
}

function SA_scrollIt()
{
	this.scrollContent.style.top = -this.scrollFactor * ((this.scrollslider.offsetTop + this.scrollslider.offsetHeight/2) - this.scrollButtonHeight - this.scrollslider.offsetHeight/2) + "px";
}

function SA_scrollUp()
{
	if (this.scrollingLimit > 0)
	{
		this.scrollingLimit--;
		if (this.scrollingLimit == 0) this.scrolling = false;
	}
	if (!this.scrolling) return;
	if (this.scrollContent.offsetTop + this.scrollStep < 0)
	{
		this.scrollContent.style.top = this.scrollContent.offsetTop + this.scrollStep + "px";
		this.scrollslider.style.top = 1 / this.scrollFactor * Math.abs(this.scrollContent.offsetTop) + this.scrollButtonHeight + "px";
	}
	else
	{
		this.scrollContent.style.top = "0px";
		this.scrollslider.style.top = this.scrollButtonHeight + "px";
		return;
	}
	setTimeout("SA_Ext_scrollUp(" + this.index + ")", 30);
}

function SA_Ext_scrollUp(index)
{
	SA_scrollAreas[index].scrollUp();
}

function SA_scrollDown()
{
	if (this.scrollingLimit > 0)
	{
		this.scrollingLimit--;
		if (this.scrollingLimit == 0) this.scrolling = false;
	}
	if (!this.scrolling) return;


	this.scrollContent.style.top = this.scrollContent.offsetTop - this.scrollStep + "px";
	this.scrollslider.style.top =  1 / this.scrollFactor * Math.abs(this.scrollContent.offsetTop) + this.scrollButtonHeight + "px";

	if (this.scrollContent.offsetTop <= -(this.scrollContent.offsetHeight - this.element.offsetHeight))
	{
		this.scrollContent.style.top = -(this.scrollContent.offsetHeight - this.element.offsetHeight) + "px";
		this.scrollslider.style.top = this.scrollbar.offsetHeight - this.scrollButtonHeight - this.scrollslider.offsetHeight + "px";
		return;
	}

	setTimeout("SA_Ext_scrollDown(" + this.index + ")", 30);
}

function SA_Ext_scrollDown(index)
{
	SA_scrollAreas[index].scrollDown();
}

function SA_handleMouseMove(evt)
{
	var sa = SA_scrollAreas[(document.all ? this.index : document.documentElement.scrollAreaIndex)];
	var posy = 0;
	if (!evt) var evt = window.event;
	
	if (evt.pageY)
		posy = sa.scrollbar.offsetTop + evt.pageY;
	else if (evt.clientY)
		posy = evt.clientY + document.documentElement.scrollTop;

	var iNewY = posy - sa.iOffsetY - sa.topPosition;
		iNewY += sa.scrollbar.offsetTop;
	if (iNewY < sa.scrollButtonHeight)
		iNewY = sa.scrollButtonHeight;
	if (iNewY > (sa.scrollbar.offsetHeight - sa.scrollButtonHeight) - sa.scrollslider.offsetHeight)
		iNewY = (sa.scrollbar.offsetHeight - sa.scrollButtonHeight) - sa.scrollslider.offsetHeight;
	sa.scrollslider.style.top = iNewY + "px";
	sa.scrollIt();
}

function SA_handleSliderMouseDown(evt)
{
	var sa = SA_scrollAreas[this.index];
	if (document.all)
	{
		sa.iOffsetY = event.offsetY;
		sa.scrollslider.setCapture();
		sa.scrollslider.onmousemove = SA_handleMouseMove;
	}
	else
	{
		sa.iOffsetY = evt.layerY;
		document.documentElement.scrollAreaIndex = sa.index;
		document.documentElement.addEventListener("mousemove", SA_handleMouseMove, true);
		document.documentElement.addEventListener("mouseup", SA_handleSliderMouseUp, true);
	}
}

function SA_handleSliderMouseUp()
{
	if (document.all)
	{
		var sa = SA_scrollAreas[this.index];
		sa.scrollslider.onmousemove = null;
		sa.scrollslider.releaseCapture();
		sa.scrollIt();
	}
	else
	{
		var sa = SA_scrollAreas[document.documentElement.scrollAreaIndex];
		document.documentElement.removeEventListener("mousemove", SA_handleMouseMove, true);
		document.documentElement.removeEventListener("mouseup", SA_handleSliderMouseUp, true);
		sa.scrollIt();
	}
}

function SA_handleResize()
{
	if (SA_resizeTimer != null)
	{
		clearTimeout(SA_resizeTimer);
		SA_resizeTimer = null;
	}
	SA_resizeTimer = setTimeout("SA_performResizeEvent()", 100);
}

function SA_performResizeEvent()
{
	for (var i=0; i<SA_scrollAreas.length; i++)
		SA_scrollAreas[i].createScrollBar();
}

function SA_handleMouseWheel()
{
	var sa = SA_scrollAreas[this.index];
	if (sa.scrollbar == null) return;
	sa.scrolling = true;
	sa.scrollingLimit = sa.wheelSensitivity;
	if (event.wheelDelta > 0)
		sa.scrollUp();
	else
		sa.scrollDown();
}

function SA_handleSelectStart()
{
	event.returnValue = false;
}

function SA_handleScrollbarClick(evt)
{
	var sa = SA_scrollAreas[this.index];
	var offsetY = (document.all ? event.offsetY : evt.layerY);
	if (offsetY < (sa.scrollButtonHeight + sa.scrollslider.offsetHeight/2))
		sa.scrollslider.style.top = sa.scrollButtonHeight + "px";
	else if (offsetY > (sa.element.offsetHeight - sa.scrollButtonHeight - sa.scrollslider.offsetHeight - sa.scrollslider.offsetHeight/2))
		sa.scrollslider.style.top = sa.element.offsetHeight - sa.scrollButtonHeight - sa.scrollslider.offsetHeight + "px";
	else
		sa.scrollslider.style.top = offsetY + sa.scrollButtonHeight - sa.scrollslider.offsetHeight/2 + "px";
	sa.scrollIt();
}

function SA_handleOnScroll()
{
	event.srcElement.doScroll("pageUp");
}

function SA_setScrollPosition(percentage)
{
	if (percentage >= 0 && scrollbar != null)
	{
		scrollslider.style.top = scrollButtonHeight + scrollHeight/100 * percentage + "px";
		scrollIt();
	}
}

//--- common functions ----
function getElements(attrValue, tagName, ownerNode, attrName) //get Elements By Attribute Name
{
	if (!tagName) tagName = "*";
	if (!ownerNode) ownerNode = document;
	if (!attrName) attrName = "name";
	var result = [];
	var nl = ownerNode.getElementsByTagName(tagName);
	for (var i=0; i<nl.length; i++)
	{
		if (nl.item(i).getAttribute(attrName) == attrValue)
			result.push(nl.item(i));
	}
	return result;
}

function getRealTop(elem)
{
	var root = "BODY";
	if(document.all)
	{
		root = "HTML";
	}
	//alert(elem.parentNode.tagName);
	if (elem.parentNode.tagName == root)
		return elem.offsetTop;
	else
		return (elem.offsetTop + getRealTop(elem.parentNode));
}




function initGreenBoxes()
{
	var divs = document.getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++)
	{
		if (divs[i].className.indexOf(" nogreen") != -1)
		{
			divs[i].onmouseover = function ()
			{				
				this.className = this.className.replace(" nogreen", " green");
				return false;
			}

			divs[i].onmouseout = function ()
			{				
				this.className = this.className.replace(" green", " nogreen");
				return false;
			}
		}
	}

}

/*
if (window.addEventListener)
	window.addEventListener("load", initGreenBoxes, false);
else if (window.attachEvent)
	window.attachEvent("onload", initGreenBoxes);
*/