
// Retrieve the rendered height of an element
function getObjectHeight(obj) {
    var elem = $(obj);
    var result = 0;
    if (elem.offsetHeight) {
        result = elem.offsetHeight;
    } else if (elem.clip && elem.clip.height) {
        result = elem.clip.height;
    } else if (elem.style && elem.style.pixelHeight) {
        result = elem.style.pixelHeight;
    }
    return parseInt(result);
}


/********** OVERFLOW SCROLLING ************/

function scrollarrows(arrow,action,dir){
	if (action=='active'){
		$(arrow+'_'+dir).className='koscroll_active';
		$(arrow+'_'+dir).onmousedown = function(){scrollme(arrow,dir,'start');}
	} else {
		$(arrow+'_'+dir).className='koscroll_inactive';
		$(arrow+'_'+dir).onmousedown = function(){scrollme(arrow,dir,'stop');}
	}
}


function checkflow(ID){	
	if( getObjectHeight(ID+'_hd') > getObjectHeight(ID+'_mask')) {
		scrollarrows(ID,'active','d');
		if (parseInt($(ID+'_hd').style.top) < 0){ // check if we need the up arrow
			scrollarrows(ID,'active','u');
		} else {
			scrollarrows(ID,'inactive','u');
		}
	} else {
		scrollarrows(ID,'inactive','d');
		scrollarrows(ID,'inactive','u');
	}
}

function overflow(ID){
	scrollarrows(ID,'active','d');
}


function scrollme(ID,dir,what){
	var scroller = $(ID+'_hd');
	var box = $(ID);
	if (dir == 'd'){
		moveamt =  -12;
	} else {
		moveamt = 12;
	}
	if (what == 'start'){
		scroller.moveit = true;
		scrolling(scroller,moveamt,box,ID);
	} else {
		scroller.moveit = false;
	}
}

function scrolling(scroller,moveamt,box,ID,mw){
	if(scroller == '') var scroller = $(ID+'_hd');
	if(box == '') var box = $(ID);
		
	if (scroller.moveit){
		scroller.style.top = parseInt(scroller.style.top) + moveamt + 'px';
		if (!mw) setTimeout("scrolling('',moveamt,'','"+ID+"')",50);
	}

	current = parseInt(scroller.style.top);
	boxh = parseInt(box.style.height)-40;
	scrollerh = getObjectHeight(scroller);
	if ((scrollerh + current + moveamt) < boxh){ // check if arrows should still show
		stoptop = scrollerh - boxh;// set to bottom
		scroller.style.top = '-'+stoptop+'px';
		scrollarrows(ID,'inactive','d');
		scroller.moveit = false;
	} else {
		scrollarrows(ID,'active','d');
	}
	if ((current + moveamt) < 0){ // check if arrows should still show
		scrollarrows(ID,'active','u');
	} else {
		scroller.style.top = '0px';// set to top
		scrollarrows(ID,'inactive','u');
		scroller.moveit = false;
	}
}


function init_koscroll(ID){
	if ($(ID).style.position != 'absolute') $(ID).style.position='relative';
	var content = $(ID).innerHTML;
	$(ID).innerHTML = '';
	// Create mask div
	var mask = document.createElement("div");
	mask.setAttribute('id',ID+'_mask');
	$(ID).appendChild(mask);
	$(ID+'_mask').style.height='100%';
	$(ID+'_mask').style.width='100%';
	$(ID+'_mask').style.overflow='hidden';
	$(ID+'_mask').style.position='absolute';
	$(ID+'_mask').style.margin='0px';
	$(ID+'_mask').style.left='0px';
	$(ID+'_mask').style.top='0px';
	// Create hd div
	var hd = document.createElement("div");
	hd.setAttribute('id',ID+'_hd');
	mask.appendChild(hd);
	$(ID+'_hd').style.position='absolute';
	$(ID+'_hd').style.margin='0px';
	$(ID+'_hd').style.top='0px';
	$(ID+'_hd').style.left='0px';
	hd.innerHTML = content;
	// set control arrows
	$(ID+'_d').onmousedown = function(){scrollme(ID,'d','start');}
	$(ID+'_d').onmouseup = function(){scrollme(ID,'d','stop');}
	$(ID+'_d').onclick = function(){this.blur(); return false;}
	$(ID+'_u').onmousedown = function(){scrollme(ID,'u','start');}
	$(ID+'_u').onmouseup = function(){scrollme(ID,'u','stop');}
	$(ID+'_u').onclick = function(){this.blur(); return false;}
	// attach mousewheel
	hookEvent(ID, 'mousewheel', MouseWheel);
	// and away we go...
	checkflow(ID);
}

/********** MOUSEWHEEL SCROLLING ************/

function hookEvent(element, eventName, callback)
{
	if(typeof(element) == "string")
		element = $(element);
	if(element.addEventListener) {
		element.addEventListener('DOMMouseScroll',callback, false); 
		element.addEventListener(eventName, callback, false);
	}
	else if(element.attachEvent)
		element.onmousewheel = function(){MouseWheel(element);}
}

function cancelEvent(e)
{
	 e = e ? e : window.event;
	 if(e.stopPropagation)
		e.stopPropagation();
	 if(e.preventDefault)
		e.preventDefault();
	 e.cancelBubble = true;
	 e.cancel = true;
	 e.returnValue = false;
	 return false;
}

function MouseWheel(e)
{
	if (e.id == undefined){ // ff
		id = this;
		e = e;
	} else { // ie
		id = e;
		e = window.event;
	}
	var wheelData = e.detail ? e.detail * -1 : e.wheelDelta / 40;
	//do something
	var scroller = $(id.id+'_hd');
	scroller.moveit = true;
	scrolling('',wheelData*12,id,id.id,true);
 
	return cancelEvent(e);
}


