/*
 * scroller-0.0.1.js
 * - Brendan O'Connor
 */

function scroller(elem, step){
	if(typeof(step) == 'undefined'){
		step = 50;
	}
	// create a continer to wrap around the contents
	var wr = $(document.createElement('div'));
	wr.attr('id','wrap');
	
	// create the display window
	wr_in = $(document.createElement('div'));
	wr_in.attr('id','wr_in');
	// wrap it around the #wrap
	wr_in.wrapInner(wr);
	
	// wrap the contents and modify the CSS
	// var lists = $('#lists');
	elem.wrapInner(wr_in);
	elem.css({ position: 'relative', overflow: 'hidden'});
	
	// get the height of the element - this will become the window height
	var h = elem.height();
	
	// make some space for the buttons - 20px height each.
	h -= 40;
	// re-select the wrappers; they get lost.
	wr_in = $('#wr_in');
	wr = $('#wrap');
	
	if(wr.height() < elem.height()) return;
	
	// set the height of the window to the height of the content
	wr_in.height(h);
	
	h = 0 - wr.height();	// set a negative height for the scroll
	h += wr_in.height();	// compensate for the window height
	
	// Create the buttons.
	var btnUp = $(document.createElement('span'));
	btnUp.addClass('btn_up');
	btnUp.bind('click', function(){
		var pos = wr.position().top;
		if(pos >= 0) return;
		pos += step;
		if(pos >= 0) pos = 0;
		wr.animate({top: pos+'px'}, 200);
	});
	
	var btnDown = $(document.createElement('span'));
	btnDown.addClass('btn_down');
	btnDown.bind('click', function(){
		var pos = wr.position().top;
		if(pos <= h) return;
		pos -= step;
		if(pos <= h) pos = h;
		wr.animate({top: pos+'px'}, 200);
	});
	
	// add the buttons.
	elem.prepend(btnUp);
	elem.append(btnDown);
	
	return true;
}
