function $(el){
	return document.getElementById(el);
}
function debug(txt){
	$('debug').innerHTML = txt;
}

var scroller = null;
var inner = null;
var previewWidth = 300;
var scount = 0;
var timer = null;

function initPortfolioScroller(){
	scroller = $('portfolio-scroller');
	inner = $('portfolio-scroller-inner');
	var divs = inner.getElementsByTagName('div');
	if(divs.length%2 == 1){
		inner.appendChild(document.createElement('div'));
	}
	var w = (previewWidth * divs.length);
	inner.style.width = w + 'px';
	for(var i=0;i<divs.length;i++){
		if(i%2 == 0){
			divs[i].className = 'preview left';
		}else{
			divs[i].className = 'preview right';
		}
	}
}
function portfolioScrollNext(){
	if(!timer){
		timer = setInterval("scrollRight();",10);
	}
}
function portfolioScrollPrevious(){
	if(!timer){
		timer = setInterval("scrollLeft();",10);
	}
}

function scrollLeft(){
	portfolioScroll('left');
}

function scrollRight(){
	portfolioScroll('right');
}

function portfolioScroll(dir){
	var amt = 10;
	var totalPix = previewWidth*2;
	var cushion = 8;
	
	if((dir == 'left' && scroller.scrollLeft == 0) || (dir == 'right' && scroller.scrollLeft == (inner.offsetWidth - totalPix))){
		scount = totalPix;
	}

	if(scount < totalPix){
		if(scount >= (totalPix - (amt * cushion))){
			var remains = totalPix - scount;
			if(remains > 1){
				amt = Math.ceil(remains/cushion);
			}else{
				amt = remains;
			}
		}
		
		scount += amt;
		if(dir == 'left'){
			amt = amt*-1;
		}
		scroller.scrollLeft = scroller.scrollLeft + amt;
	}else{
		clearInterval(timer);
		timer = null;
		scount = 0;
	}
	//debug(scount);
}