function init_scroll(){
	var divArray = document.getElementsByTagName("div");
	var i =0;
	
	for(i=0;i<divArray.length;i++){
		if(divArray[i].getAttribute("dir")=="scroll") new setScroller(divArray[i].id);	
	}
}

function test(){
	var box = document.getElementById("scroll_container");
	alert(scroll_container.offsetHeight);
}

function setScroller(id){
	this.box = document.getElementById(id);
	this.sizes = new Array();
	this.sizes["box"] = new Array();
	this.sizes["content"] = new Array();
	var divArray = this.box.getElementsByTagName("div");
	var i =0;
	
	for(i=0;i<divArray.length;i++){
		if(divArray[i].getAttribute("class")=="content" || divArray[i].className=="content") this.content = (divArray[i]);
		if(divArray[i].getAttribute("class")=="up" || divArray[i].className=="up") this.up = (divArray[i]);
		if(divArray[i].getAttribute("class")=="down" || divArray[i].className=="down") this.down = (divArray[i]);
	}
	
	if (this.box.innerHeight) {
		theHeight=this.box.innerHeight;
	} else if (this.box.clientHeight) {
		theHeight=this.box.clientHeight;
	}
	else{
		theHeight=id.clientHeight;
	}
	if (this.box.innerWidth) {
		theWidth=this.box.innerWidth;
	} else if (this.box.clientWidth) {
		theWidth=this.box.clientWidth;
	}
	else{
		theWidth=id.clientWidth;
	}
	
	this.sizes["box"]["width"] = theWidth-16;
	this.sizes["box"]["height"] = theHeight;
	
	if (this.content.innerHeight) {
		theHeight=this.content.innerHeight;
	} else if (this.content.clientHeight) {
		theHeight=this.content.clientHeight;
	}
	else{
		var contentId = this.content.id;
		theHeight=contentId.clientHeight;
	}
	
	this.sizes["content"]["height"] = theHeight; 
	var maxUp = this.sizes["box"]["height"]-this.sizes["content"]["height"];
	
	this.content.style.width = this.sizes["box"]["width"]+"px";
	var content = this.content.getAttribute("id");
	
	this.up.onmouseover = function(){moveup(content,maxUp)};
	this.down.onmouseover = function(){movedown(content,maxUp)};
		
	this.up.onmouseout = function(){stop()};
	this.down.onmouseout = function(){stop()};
	
	this.box.onmouseover = function(){selectDiv(id)}
	
}

function selectDiv(id){
		this.scrolldiv = id;
		var box = document.getElementById(id);
		if (box.addEventListener)
    /** DOMMouseScroll is for mozilla. */
    box.addEventListener('DOMMouseScroll', wheel, false);
		/** IE/Opera. */
		box.onmousewheel = document.onmousewheel = wheel;		
}

function getSettings(){
	return this.scrolldiv;
}

function wheel(event){
	var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta){
						this.scrolldiv = getSettings();
						var div = document.getElementById(this.scrolldiv);
						var divArray =div.getElementsByTagName("div");
						var i =0;
						for(i=0;i<divArray.length;i++){
							if(divArray[i].getAttribute("class")=="content" || divArray[i].className=="content") var content = (divArray[i]);
						}
							if (div.innerHeight) {
								theHeight=div.innerHeight;
							} else if (div.clientHeight) {
								theHeight=div.clientHeight;
							}
							else{
								theHeight=this.scrolldiv.clientHeight;
							}
							if (div.innerWidth) {
								theWidth=div.innerWidth;
							} else if (div.clientWidth) {
								theWidth=div.clientWidth;
							}
							else{
								theWidth=this.scrolldiv.clientWidth;
							}
							
							if (content.innerHeight) {
								conHeight=content.innerHeight;
							} else if (content.clientHeight) {
								conHeight=content.clientHeight;
							}
							else{
								var contentId = content.id;
								conHeight=contentId.clientHeight;
							}
							
							var maxUp = theHeight-conHeight;
							this.wheelmove = 1;

             if(delta>0){
							 moveupWheel(content.id,maxUp);
						 }
						 else if(delta<0){
							 movedownWheel(content.id,maxUp);
						 }
				}
        if (event.preventDefault)
                event.preventDefault();
				event.returnValue = false;
}

function stop(){
	clearTimeout(this.scrollTimer);
}

function moveup(content,max){
	var contentbox = document.getElementById(content);
	var currentTop = contentbox.style.top;
	currentTop = currentTop.replace(/px/,"");
	if(currentTop<0){
		currentTop++;
		contentbox.style.top = currentTop+"px";
		this.scrollTimer = setTimeout("moveup('"+content+"','"+max+"')",10);
	}
}

function moveupWheel(content,max){
	if(this.busy!==1){
	this.busy = 1;
	var contentbox = document.getElementById(content);
	var currentTop = contentbox.style.top;
	currentTop = currentTop.replace(/px/,"");
	if(currentTop<0){
		currentTop = parseInt(currentTop)+5;
		contentbox.style.top = currentTop+"px";
	}
	this.busy=0;
	}
}

function movedown(content,max){
	var contentbox = document.getElementById(content);
	var currentTop = contentbox.style.top;
	currentTop = currentTop.replace(/px/,"");
	max = parseInt(max);
	if(currentTop>max){
		currentTop--;
		contentbox.style.top = currentTop+"px";
		this.scrollTimer = setTimeout("movedown('"+content+"','"+max+"')",10);
	}
}

function movedownWheel(content,max){
	if(this.busy!==1){
	this.busy = 1;
	var contentbox = document.getElementById(content);
	var currentTop = contentbox.style.top;
	currentTop = currentTop.replace(/px/,"");
	max = parseInt(max);
	if(currentTop>max){
		currentTop = currentTop-5;
		contentbox.style.top = currentTop+"px";
	}
	this.busy=0;
	}
}

