// Set scroll position
function fnScrollSet() {
	// Check if browser supports client side cookies
	if(window.navigator.cookieEnabled == true) {
		// Store current scroll position in cookie
		document.cookie = "scrollfix=" + document.body.scrollTop + ";";
	}
}

// Scroll to specified position if position is set
function fnScrollFix() {
	var psScrollFix = "";
	var piScrollPos = 0;
	// Check if browser supports client side cookies
	if(window.navigator.cookieEnabled == true) {
		// Get cookie data
		psScrollFix	= document.cookie;
		if(psScrollFix.indexOf("scrollfix=") > -1) {
			psScrollFix	= psScrollFix.substring(psScrollFix.indexOf("scrollfix="), psScrollFix.length);
			if(psScrollFix.indexOf(";") > -1) {
				psScrollFix	= psScrollFix.substring(10, psScrollFix.indexOf(";"));
			}
			else {
				psScrollFix	= psScrollFix.substring(10, psScrollFix.length);
			}
		}
		else {
			psScrollFix = 0;
		}
		piScrollPos = parseInt(psScrollFix);
		// If scroll position is larger than 0, scroll to that postition and clear cookie
		if(piScrollPos > 0) {
			var poTimeOut = setTimeout("window.scrollBy(0, " + piScrollPos + ")", 20);
			document.cookie = "scrollfix=" + 0 + ";";
		}
	}
}

// Scroll functions
var time	= false;
var moveit	= null;
var space	= 0;
var current = "";
var fart 	= 15;
var hold_at = 0;

function fnInitScroll(psID) {
	current = psID;
	setHeight();
	if(bar_ratio()>=1) hide();
}

function getY(evt)		{if (evt.pageY) { return parseInt(evt.pageY); } else { return parseInt(event.clientY);}}
function bar_ratio() 	{return (getEl("textFrame").offsetHeight-space) / (getEl("content").offsetHeight);}
function bondary() 		{return getEl("bar").offsetHeight - getEl("page").offsetHeight;}
function setHeight() 	{getEl("page").style.height = bar_height() * bar_ratio()+"px";}
function moveBy(value)	{moveTo(parseInt(getEl("page").style.top+0)+value);}
function getHeight() 	{return parseInt(getEl("page").style.height);}
function getEl(id) 		{return document.getElementById(current+id);} 
function getTop() 		{return parseInt(getEl("page").style.top);}
function bar_height() 	{return getEl("bar").offsetHeight;}
function stopScroll()	{if (time) {clearTimeout(time);}}
function disableSelect(){return false;}
function enableSelect() {return true;}	
function hide()			{getEl("upDown").style.display="none";}

function drop(obj,evt) {
	moveit=false;
	document.onselectstart=new Function ("return true");
	document.onmousedown = enableSelect;
	moveit=false;	
}

function hold(evt,obj,prefix) {
	if (moveit) {return;}
	clienty = getY(evt);
	hold_at = clienty - parseInt(getEl("page").style.top+0) ;
	document.onselectstart=new Function ("return false");
	document.onmousedown = disableSelect;
	current = prefix;
	moveit=true;
}

function moved(evt,obj) {
	if (moveit==true) {
		Y = getY(evt);
		moveTo(Y - hold_at);
	} 
}

function moveTo(value) {
	retval = false;
	t_bondary = bondary()
	if ( value >= 0 && value <= t_bondary ) {
		retval = true;
	} else if (value > t_bondary) {
		value = t_bondary
	} else if (value < 0) {
		value = 0;
	}
	getEl("page").style.top = value + 'px' ;
	getEl("content").style.top = - value / bar_ratio() + "px";
	return retval;
}

function scrollUp(prefix) {
	if (moveit) {return;}
	if (prefix) { current = prefix;}
	time = setTimeout("scrollUp()",100);			
	moveBy(-fart*bar_ratio());
}

function scrollDown(prefix) {
	if (moveit) {return;}
	if (prefix) { current = prefix;}
	time = setTimeout('scrollDown()',100);			
	moveBy(Math.ceil(fart*bar_ratio()));
}

// utils
function getObj(name) {
  if (document.getElementById) {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all) {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  } else if (document.layers) {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}
