/********************************
	Scrolling content
	Author: Todor Stamatov, Webfactory Ltd.

********************************/

SCROLL = new Object();
SCROLL.otherOnload = (typeof(window.onload) == "function" ? window.onload : function(){});
window.onload =  function(){ SCROLL.init(); SCROLL.otherOnload(); }

SCROLL.$ = function (ID) { return document.getElementById(ID); }
SCROLL.scrollRate = 8;	 // initial rate of scrolling
SCROLL.scrollTick = 20;	 // keeps track of long we've scrolled so we can slow it down accordingly
SCROLL.MIN_LIST_HEIGHT = 300;
SCROLL.disabled = true;

SCROLL.setParams = function(nContainer, lContainer, upArrows, downArrows) {
  with(SCROLL) {
    SCROLL.upArrowArr = [];
    SCROLL.downArrowArr = [];
  	for(var i=0; i<upArrows.length; i++){
  	    var uArr = $(upArrows[i]);
  	    if (!uArr){
		    alert("Ivalid scroll parameters");
		    return false;
  	    }
  	    SCROLL.upArrowArr[SCROLL.upArrowArr.length] = uArr;
  	}
  	for(var i=0; i<downArrows.length; i++){
  	    var dArr = $(downArrows[i]);
  	    if (!dArr){
		    alert("Ivalid scroll parameters");
		    return false;
  	    }
  	    SCROLL.downArrowArr[SCROLL.downArrowArr.length] = dArr;
  	}

	SCROLL.lContainer = $(lContainer);
	SCROLL.nContainer = $(nContainer) 
	if (!SCROLL.lContainer || !SCROLL.nContainer){
	    alert("Ivalid scroll parameters");
	    return false;
	}
	SCROLL.disabled = false;
  }
}

SCROLL.init = function() {
 with(SCROLL) {
    if (disabled) return false;
	if(!document.getElementById) return false; // bail out if this is an older browser
	
	SCROLL.zInterval = null;
	SCROLL.currentTop = 0;

	// apply onclick behaviors to the up arrow, down arrow and expansion control elements
    for(var i=0; i<upArrowArr.length; i++){
		upArrowArr[i].onmouseover = function(){ SCROLL.scrollRateC = SCROLL.scrollRate; SCROLL.scrollObjects(1); }
		upArrowArr[i].onmouseout = function() {
			clearInterval(SCROLL.zInterval); 
			SCROLL.zInterval = null;
	    }
	}
	for(var i=0; i<downArrowArr.length; i++){
		downArrowArr[i].onmouseover = function(){ SCROLL.scrollRateC = SCROLL.scrollRate; SCROLL.scrollObjects(0); }
		downArrowArr[i].onmouseout = function() {
			clearInterval(SCROLL.zInterval); 
			SCROLL.zInterval = null;
	    }
    }
	
	SCROLL.expander = $("changeSize");
	if (expander){
		expander.onclick = function(){ if (!SCROLL.isExpanded) SCROLL.isExpanded = true; SCROLL.changeListSize(); }
	}

	nContainer.style.height = MIN_LIST_HEIGHT + "px";
	
	// set height
	SCROLL.contentHeight = lContainer.offsetHeight - MIN_LIST_HEIGHT;
	SCROLL.scrollRateC = scrollRate;

	if (SCROLL.MIN_LIST_HEIGHT <= lContainer.offsetHeight) {
		
		for(var i=0; i<upArrowArr.length; i++){		
			upArrowArr[i].style.display = "block";
		}
		for(var i=0; i<downArrowArr.length; i++){
			downArrowArr[i].style.display = "block";
		}	
	}

 }
}

SCROLL.scrollObjects = function(dir) {
	with(SCROLL) {
	if (zInterval) return; // already scrolling.
	if (typeof(isExpanded) != 'undefined') return; // list is expanded. no need to scroll.
	if ((!dir && currentTop<=-SCROLL.contentHeight) || (dir && currentTop==0)){
		
		return; // dont scroll up if we're at the top or down if at the bottom of the list
	}
	direction = dir;
	zInterval = setInterval("SCROLL.animate()", 20);
  }
}

SCROLL.animate = function () {
  with(SCROLL) {
	// increment or decrement currentTop based on direction
	if (!direction) {
		currentTop -= scrollRateC;
	} else {
		currentTop += scrollRateC;
	}
	
	// slow down scroll
	if ((currentTop > -scrollTick && currentTop < 0 && scrollRateC > 1 && direction == 1) || 
	    (currentTop < scrollTick - contentHeight + 8 && scrollRateC > 1 && direction == 0)) {
		scrollRateC-- ;
	}
	
	if (currentTop >= 0 || currentTop <= -SCROLL.contentHeight + 8) {
		clearInterval(zInterval);
		zInterval = null;
	    scrollRateC = scrollRate;
	    if (currentTop>=0) {
	        currentTop = 0;
	        lContainer.style.top = currentTop + "px";
	    }
	    if (currentTop <= -contentHeight){
	        currentTop = -contentHeight + 8;
	        lContainer.style.top = currentTop + "px";
	    }
		return;
	}
	
	lContainer.style.top = currentTop+"px";
	
	/*
	if (scrollRate<=REBOUND) {
		// scroll is finished. clear the interval and reset vars for the next scroll
		clearInterval(zInterval);
		zInterval = null;
		scrollRate = 6;
	}
	*/
  }
 
}

