// scrollAttitude
//
// Copyright (c) 2008 NuAttitude Ltd www.nuattitude.ee
// All rights reserved.
// 
// Redistribution and use of this effect in source form, with or without modification,
// are permitted provided that the following conditions are met:
// 
// * USE OF SOURCE ON COMMERCIAL (FOR-PROFIT) WEBSITE REQUIRES ONE-TIME LICENSE FEE PER DOMAIN.
//   Reasonably priced! Email: info@nuattitude.ee for licensing instructions. Thanks!
//
// * Redistribution of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
// * Redistribution of source code and derived works cannot be sold without specific
//   written prior permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

var dragBar = null;
var scrollTimer;

function moveScrollBar (bar, pos)
{
	bar.style.top = pos + 'px';
}
function adjustContent (bar, pos)
{
	var contentHolder = document.getElementById(bar.id.substring(0, bar.id.indexOf('_bar')));
	var barHolder = document.getElementById('scroll_bar');
	bigHeight = contentHolder.scrollHeight - contentHolder.clientHeight;
	smallHeight = barHolder.clientHeight - bar.clientHeight;
	
	contentHolder.scrollTop = (bigHeight * pos)/smallHeight;
	
	if (0 == contentHolder.scrollTop) return false;
	return true;
}
function sync (dragBar, _e)
{
	var ev = _e ? _e : window.event;
	var _mo = mouseXY(ev);
	
	
	var _el = getPosition(dragBar.parentNode);
	
	barPosition = _mo.y - _el.y - dragBar.clientHeight / 2;
	
	if (0 <= barPosition && (dragBar.parentNode.clientHeight - dragBar.clientHeight) >= barPosition)
	{
		if (adjustContent(dragBar, barPosition)) moveScrollBar(dragBar, barPosition);
		else moveScrollBar(dragBar, 0);
	}

	startDrag(dragBar);
}
function startDrag(bar)
{
	dragBar = bar;
	var mainBlock = document.getElementById('main_block');
	mainBlock.onselectstart = function() 
	{
		return false;
	}
	mainBlock.style.userSelect = 'none';
	mainBlock.style.MozUserSelect = 'none';
	mainBlock.unselectable = "on";
}
function stopScrollDiv()
{
	clearTimeout(scrollTimer);
	var mainBlock = document.getElementById('main_block');
	mainBlock.onselectstart = function() {};
	mainBlock.style.userSelect = 'text';
	mainBlock.style.MozUserSelect = 'text';
	document.getElementById('main_block').unselectable = "off";
}
function mouseXY (e)
{
	if (e.pageX || e.pageY) return {x:e.pageX, y:e.pageY};
	
	return {x:e.clientX + document.documentElement.scrollLeft - document.documentElement.clientLeft, y:e.clientY + document.documentElement.scrollTop - document.documentElement.clientTop};
}
function elemPos (e)
{
	var left = 0;
	var top  = 0;
	
	if (0 == e.offsetHeight) e = e.firstChild;
	
	while (e.offsetParent)
	{
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}
	
	left += e.offsetLeft;
	top  += e.offsetTop;
	
	return {x:left, y:top};
}
function getPosition(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		var curleft = obj.offsetLeft;
		var curtop = obj.offsetTop;
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft - obj.scrollLeft;
			curtop += obj.offsetTop - obj.scrollTop;
		}
	}
	return {x:curleft, y:curtop};
}
document.onmousemove = function (_e)
{
	if (dragBar)
	{
		try
		{
			sync(dragBar, _e);
		}
		catch (e) { }
	}
}
document.onmouseup = function ()
{
	if (dragBar) dragBar = null;

	stopScrollDiv();
}