function Portfolio(thumbList, nextButan, prevButan, data, initialOffset, perPage){
	var self=this;
	this.thumbList=thumbList;
	this.nextButan=nextButan;
	this.prevButan=prevButan;
	this.data=data;
	this.offset=initialOffset;
	this.perPage=perPage;
	this.isAnimating=false;
	this.normalLeftMargin=null;
	nextButan.bind("click", function(){return self.next();});
	prevButan.bind("click", function(){return self.prev();});
}

Portfolio.prototype.next=function(){
	if(this.nextButan.hasClass("disabled")) return false;
	this.scroll("left", true, function(){
		this.offset+=this.perPage;
		this.redrawItems();
		if(this.offset+this.perPage > this.data.DATA.length) this.nextButan.addClass("disabled");
		this.prevButan.removeClass("disabled");
		this.scroll("right", false);
	});
	return false;
};

Portfolio.prototype.prev=function(){
	if(this.prevButan.hasClass("disabled")) return false;
	this.scroll("right", true, function(){
		this.offset=Math.max(this.offset-this.perPage, 0);
		this.redrawItems();
		if(this.offset==0) this.prevButan.addClass("disabled")
		this.nextButan.removeClass("disabled")
		this.scroll("left", false);
	});
	return false;
};

Portfolio.prototype.redrawItems=function(){
	this.thumbList.empty();
	var endRow=Math.min(this.data.DATA.length, this.offset+this.perPage);
	var listItem,itemLink,itemImage,clientCol,imgCol,urlCol,j,i;
	for(j=0; j<this.data.COLUMNS.length; j++){
		switch(this.data.COLUMNS[j]){
			case "IMAGETHUMB":
				imgCol=j;
				break;
			case "CLIENTNAME":
				clientCol=j;
				break;
			case "WEBSITEADDRESS":
				urlCol=j;
				break;
		}
	}
	for(i=this.offset; i<endRow; i++){
		itemImage=$(document.createElement("img"));
		itemImage.attr({
			src: "/content/plugins/Portfolio/thumbs/"+this.data.DATA[i][imgCol],
			alt: this.data.DATA[i][clientCol],
			title: this.data.DATA[i][clientCol]
		});
		itemLink=$(document.createElement("a"));
		itemLink.attr({
			target: "_blank",
			href: this.data.DATA[i][urlCol]
		});
		listItem=$(document.createElement("li"));
		this.thumbList.append(listItem.append(itemLink.append(itemImage)));
	}
}

Portfolio.prototype.scroll=function(direction, scrollOut, callback){
	if(this.isAnimating) return;
	this.isAnimating=true;
	var initialMargin,newMargin;
	switch(direction){
		case "left":
			initialMargin=newMargin="-"+this.thumbList.width()+"px";
			break;
		case "right":
			initialMargin=newMargin=this.thumbList.width()+this.normalLeftMargin+"px";
			break;
	}
	if(scrollOut){
		initialMargin="";		
	}else{
		newMargin=this.normalLeftMargin+"px";
	}
	this.thumbList.css("marginLeft", initialMargin);
	if(scrollOut && this.normalLeftMargin==null) this.normalLeftMargin=parseInt(this.thumbList.css("margin-left"));
	var self=this;
	this.thumbList.animate({marginLeft: newMargin},"normal", null, function(){
		self.isAnimating=false;
		if(callback!=null) callback.apply(self, arguments);
	});
};
