/*
	SCROLLAREA Version: 1.1 light
	Created by: Andrew Romashkin
	Last update: 7.04.2006

	Custom attributes:
		imagesPath
		btnUpImage
		btnDownImage
		scrollImage
		sliderImage
		scrollbarBackgroundColor
		scrollStep
		wheelSensitivity
		scrollIEWrap
*/

//==========================================
// SETTINGS
//==========================================
var SA_default_imagesPath = "images";
var SA_default_btnUpImage = "arrow-up.gif";
var SA_default_btnDownImage = "arrow-down.gif";

var SA_default_scrollButtonHeight = 14;
var SA_default_scrollbarWidth = 18;

var SA_default_scrollbarPosition = "right:0px;bottom:0px;"
var SA_default_scrollbarHeight = "352px"; // 253px, 352px, 382
var SA_default_scrollbarBackgroundColor = "";

var SA_default_scrollStep = 10;
var SA_default_wheelSensitivity = 10;
//==========================================

var SA_scrollAreas = new Array();
var SA_resizeTimer = null;

if (window.addEventListener)
	window.addEventListener("load", initScrollbars, false);
else if (window.attachEvent)
	window.attachEvent("onload", initScrollbars);

function initScrollbars()
{
	var idx = 0;
	var divs = document.getElementsByTagName("div");
	for (var i = 0; i < divs.length; i++)
	{
		if (divs[i].className.indexOf("scrollable") != -1)
			SA_scrollAreas[SA_scrollAreas.length] = new ScrollArea(idx++, divs[i]);
	}
	//check for ...?elementId query string
	if (document.location.search)
	{
		var node = document.getElementById(document.location.search.substring(1));
		if (node)
		{
			var sa = node.parentNode;
			while (sa && sa.className.indexOf("scrollable") == -1)
				sa = sa.parentNode;
			if (sa.className.indexOf("scrollable") != -1)
			{
				var c = SA_scrollAreas[sa.index]
				if (c.enableScrollbar)
					c.scrollTo(node);
			}
		}
	}
}

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("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 = SA_default_scrollButtonHeight;
	this.scrollbarWidth = SA_default_scrollbarWidth;
	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.scrollTo = SA_scrollTo;

	this.init();
}


function SA_init()
{
	this.scrollContent = document.createElement("div");
	this.scrollContent.style.position = "absolute";
	this.scrollContent.innerHTML = this.element.innerHTML;
	this.element.innerHTML = "";
	this.element.appendChild(this.scrollContent);
	this.element.style.position = "relative"; //should be absolute in case of absolute origin
	this.element.style.display = "block";
	this.element.index = this.index;

	if (document.all)
	{
		this.element.onscroll = SA_handleOnScroll;
		this.element.onmousewheel = SA_handleMouseWheel;
	}
	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;
		var scrollarea = document.getElementById("scrollable");
		if ( scrollarea ) scrollarea.style.background = "none";
	}
	else if (this.element.offsetHeight > 2*this.scrollButtonHeight)
		this.enableScrollbar = true;
	else
		this.enableScrollbar = false;

	//alert(this.enableScrollbar)
	
	if (this.scrollContent.offsetHeight - Math.abs(this.scrollContent.offsetTop) < this.element.offsetHeight)
		this.scrollContent.style.top = "0px";

	//create scrollbar if needed
	if (this.enableScrollbar)
	{	
		//this.scrollContent.style.width = "100%";
		this.scrollContent.style.width = this.element.offsetWidth - this.scrollbarWidth + "px";
		this.element.style.overflow = "hidden";

		//create scrollbar
		this.scrollbar = document.createElement("div");
		this.element.appendChild(this.scrollbar);
		this.scrollbar.style.cssText = SA_default_scrollbarPosition;
		this.scrollbar.style.position = "absolute";
		this.scrollbar.style.height = SA_default_scrollbarHeight;
		this.scrollbar.style.width = this.scrollbarWidth + "px";
		this.scrollbar.style.backgroundColor = this.scrollbarBackgroundColor;

		//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.textAlign = "center";
		this.scrollup.style.width = this.scrollbarWidth + "px";
		this.scrollup.innerHTML = '<img 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 = "30px"; // creates space between scroll arrows
		this.scrolldown.style.width = this.scrollbarWidth + "px";
		this.scrolldown.innerHTML = '<img src="' + this.imagesPath + '/' + this.btnDownImage + '" border="0"/>';
		this.scrollbar.appendChild(this.scrolldown);
	}
	else
		this.scrollContent.style.width = this.element.offsetWidth + "px";
}

function SA_handleBtnUpMouseDown(evt)
{
	var sa = SA_scrollAreas[this.index];
	sa.scrolling = true;
	sa.scrollUp();
	if (evt && evt.preventDefault) //disable default behavior in Mac FF
	{
		evt.preventDefault();
		evt.stopPropagation();
	}
}

function SA_handleBtnUpMouseUp()
{
	SA_scrollAreas[this.index].scrolling = false;
}

function SA_handleBtnUpMouseOut()
{
	SA_scrollAreas[this.index].scrolling = false;
}

function SA_handleBtnDownMouseDown(evt)
{
	var sa = SA_scrollAreas[this.index];
	sa.scrolling = true;
	sa.scrollDown();
	if (evt && evt.preventDefault) //disable default behavior in Mac FF
	{
		evt.preventDefault();
		evt.stopPropagation();
	}
}

function SA_handleBtnDownMouseUp()
{
	SA_scrollAreas[this.index].scrolling = false;
}

function SA_handleBtnDownMouseOut()
{
	SA_scrollAreas[this.index].scrolling = false;
}


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";
	else
	{
		this.scrollContent.style.top = "0px";
		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";
	if (this.scrollContent.offsetTop <= -(this.scrollContent.offsetHeight - this.element.offsetHeight))
	{
		this.scrollContent.style.top = -(this.scrollContent.offsetHeight - this.element.offsetHeight) + "px";
		return;
	}
	setTimeout("SA_Ext_scrollDown(" + this.index + ")", 30);
}

function SA_Ext_scrollDown(index)
{
	SA_scrollAreas[index].scrollDown();
}

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_handleOnScroll()
{
	event.srcElement.doScroll("pageUp");
}

function SA_scrollTo(node)
{
	var offset = getAbsTop(node) - getAbsTop(this.element);
	if ((this.scrollContent.offsetHeight - offset) > this.element.offsetHeight)
		this.scrollContent.style.top = -offset + "px";
	else
		this.scrollContent.style.top = -(this.scrollContent.offsetHeight - this.element.offsetHeight) + "px";
}

function getAbsTop(elem)
{
	if (elem.offsetParent && elem.offsetParent.nodeName == "HTML")
		return (elem.offsetTop - elem.scrollTop);
	else if (elem.offsetParent)
		return (elem.offsetTop - elem.scrollTop + getAbsTop(elem.offsetParent));
	else
		return 0;
}

