/********** KOSCROLL v 1.5 ************/

/********** 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( ($(ID).scrollHeight - $(ID).scrollTop) > parseInt($(ID).style.height) ) {
		scrollarrows(ID,'active','d');
	} else {
		scrollarrows(ID,'inactive','d');
		$(ID).moveit = false;
	}
	if ($(ID).scrollTop > 0){
		scrollarrows(ID,'active','u');
	} else {
		scrollarrows(ID,'inactive','u');
		$(ID).moveit = false;
	}
}


function scrollme(ID,dir,what){
	if (dir == 'd'){
		moveamt =  12;
	} else {
		moveamt = -12;
	}
	scrolling(moveamt,ID);
	
	if (what == 'start'){
		$(ID).moveit = true;
		scrolling(moveamt,ID);
	} else {
		$(ID).moveit = false;
	}	
	
}


function scrolling(moveamt,ID,mw){
	if ($(ID).moveit){
		$(ID).scrollTop = $(ID).scrollTop + moveamt;
		if (!mw) setTimeout("scrolling(moveamt,'"+ID+"')",50);
	}
	checkflow(ID);
}


function init_koscroll(ID){
	$(ID).style.overflow='hidden';
	// 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;
	wheelData = wheelData - wheelData - wheelData;
	//do something
	id.moveit = true;
	scrolling(wheelData*12,id.id,true);
 
	return cancelEvent(e);
}
