/*
	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 = "/wp-content/themes/grasp/images";
var SA_default_btnUpImage = "arrow-up.gif";
var SA_default_btnDownImage = "arrow-down.gif";

var SA_default_scrollButtonHeight = 30;
var SA_default_scrollbarWidth = 30;

var SA_default_scrollbarPosition = "right:0px;bottom:0px;"
var SA_default_scrollbarHeight = "602px";
var SA_default_scrollbarBackgroundColor = "";

var SA_default_scrollStep = 5;
var SA_default_wheelSensitivity = 10;
//==========================================

var SA_scrollAreas = new Array();
var SA_resizeTimer = null;

//+++++++++++++++++++
var big_size = 626;
var mega_size = 1480;
//+++++++++++++++++++

if (window.addEventListener)
	window.addEventListener("load", initScrollbars, false);
else if (window.attachEvent)
	window.attachEvent("onload", initScrollbars);

function initScrollbars()
{
	var myFrameText = document.getElementById("side");
	var myFrame = document.getElementById("main");
	if (myFrameText && myFrame)
	{
		if (myFrameText.offsetHeight > big_size)
		{
			myFrame.className = "large";
			myFrameText.className += " scrollable";
			if (myFrameText.offsetHeight > mega_size)
			{
				initScrollbars();
			}
		}	
	}
	
	var idx = 0;
	var divs = document.getElementsByTagName("div");
	myFrame.style.visibility = "visible";
	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]);
	}
}

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.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 = "absolute";
	//this.scrollContent.style.backgroundColor = "yellow";
	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;
	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 = 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.onmouseover = SA_handleBtnUpMouseOver;
		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.onmouseover = SA_handleBtnDownMouseOver;
		this.scrolldown.style.position = "absolute";
		this.scrolldown.style.textAlign = "center";
		this.scrolldown.style.bottom = "0px";
		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;
	//this.firstChild.src = SA_default_imagesPath + '/' + SA_default_btnUpImage;
}

function SA_handleBtnUpMouseOver()
{
	//this.firstChild.src = SA_default_imagesPath + '/' + SA_default_btnUpImage.replace(".gif", "-h.gif");
}

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;
	//this.firstChild.src = SA_default_imagesPath + '/' + SA_default_btnDownImage;
}

function SA_handleBtnDownMouseOver()
{
	//this.firstChild.src = SA_default_imagesPath + '/' + SA_default_btnDownImage.replace(".gif", "-h.gif");
}

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");
}
