/*
helpers for vf by sg 2010-08-02
elements:
- runOnLoad
- scroller util
*/
// scroller globals(ahem)
var vf_textID = 'contentText';
var vf_scrollButts = 'textScrollButtons';
var vf_upID = 'scrollUpBtn';
var vf_downpID = 'scrollDownBtn';
var vf_maxTxtHeight = 393; //418-25
var vf_scrollerID;

function initScroll() {
	//alert('initScroll');
	if (!document.getElementById) return;
	var btnObj = document.getElementById(vf_scrollButts);
	var txtObj = document.getElementById(vf_textID);
	if (txtObj == null || btnObj == null) return;
	// check height
	if (txtObj.offsetHeight > vf_maxTxtHeight) {
		btnObj.style.visibility = 'visible';
		var upBtn = document.getElementById(vf_upID);
		var downBtn = document.getElementById(vf_downpID);
		upBtn.onmouseover = function(){scroll(1);};
		downBtn.onmouseover = function(){scroll(-1);};
		upBtn.onmouseout = function(){stopscroll();};
		downBtn.onmouseout = function(){stopscroll();};
	} else {
		// disable scroller
		btnObj.style.visibility = 'hidden';
	}
	//alert('initScroll '+ txtObj.offsetHeight +' '+ vf_maxTxtHeight);
	
}

function scroll(val) {
	if (!document.getElementById) return;
	txtObj = document.getElementById(vf_textID);
	var mintop = vf_maxTxtHeight - txtObj.offsetHeight;
	var maxtop = 1;
	var newtop = parseInt(txtObj.style.top) + val;
	if (newtop < mintop) newtop = mintop;
	if (newtop > maxtop) newtop = maxtop;
	
	txtObj.style.top = newtop+'px';
	vf_scrollerID = window.setTimeout("scroll("+val+");",50);
	//vf_scrollerID = window.setInterval("scroll("+val+");",30);
	//alert('scroll '+ txtObj.style.top +' '+ val +' '+ mintop +' '+ newtop);
}
function stopscroll() {
	//window.clearInterval(vf_scrollerID);
	if (vf_scrollerID) {
		window.clearTimeout(vf_scrollerID);
	}
}

// loader
function runOnLoad(func) {
	if (runOnLoad.loaded) func();
	else runOnLoad.funcs.push(func);
	alert('runOnLoad');
}
runOnLoad.funcs = ['initScroll'];
runOnLoad.loaded = false;

// func to be called once at load
runOnLoad.run = function() {
	if (runOnLoad.loaded) return;
	initScroll();
	/*
	for (var i = 0; i < runOnLoad.funcs.length; i++) {
		try { runOnLoad.funcs[i](); }
		catch(e) {}
	}
	*/
	runOnLoad.loaded = true;
	//alert('runOnLoad run');
}

// setup
if (window.addEventListener) {
	window.addEventListener("load", runOnLoad.run, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", runOnLoad.run);
} else {
	window.onload = runOnLoad.run;
}

