



function santaJsScroller(params){
	var params = params || {};

	this.rendered = false;
	this.targetElement = null;
	this.items = [];
	this.itemsContainer = null;
	this.itemsTable = null;
	this.firstIndex = 0;
	this.itemsPerPage = params.perPage ? (parseInt(params.perPage) || 1) : 1;
	this.cellSpacing = 0;
	this.hideArrowsOnMargin = params.hireArrows ? !!params.hireArrows : false;

	//public declarations
	this.leftArrow = null;
	this.rightArrow = null;
	this.setTargetElement = function(tar_el){this.targetElement = this.getParamElement(tar_el); };
	this.getTargetElement = function(){return this.targetElement; };
	this.getParamElement = function(val){
		if (typeof val == "string"){
			return document.getElementById(val) || null;
		}else if (typeof val == "object" && typeof val.nodeType != "undefined"){
			return val;
		}
		return null;
	};
	this.setTargetElement(params.target);
	return this;
};
santaJsScroller.prototype.clickScrollLeft = function(e){
	this.scrollToItem(this.firstIndex-this.itemsPerPage);
};
santaJsScroller.prototype.clickScrollRight = function(e){
	this.scrollToItem(this.firstIndex+this.itemsPerPage);
};
santaJsScroller.prototype.render = function(finishRendering){
	if (this.rendered) return false;
	if (!this.targetElement){
		throw("Invalid target element");
	}

	//arrows
	var lnks = this.targetElement.getElementsByTagName("a");
	if (lnks.length > 1){
		this.leftArrow = lnks[0];
		this.rightArrow = lnks[lnks.length - 1];
	}else throw("Invalid structure : arrows!");

	whi.events.addEvent(this.leftArrow,'click',this.clickScrollLeft.bind(this));
	whi.events.addEvent(this.rightArrow,'click',this.clickScrollRight.bind(this));
	
	try{
		//items container
		this.itemsContainer = this.targetElement.getElementsByTagName("div")[0];
		this.itemsTable = this.targetElement.getElementsByTagName("table")[0];
		this.cellSpacing = parseInt(this.itemsTable.getAttribute("cellspacing")) || 0;
	}catch(e){
		throw(e + " Invalid structure : items!");
	}

	//items
	var itemsTr = this.itemsTable.getElementsByTagName("tr")[0];
	if (itemsTr && itemsTr.childNodes){
		var nodes = itemsTr.childNodes;
		for (var i=0; i<nodes.length; i++){
			if (nodes[i].nodeType == 1){
				this.items.push(nodes[i]);
			}
		}
	}
	
	//animation
	this.scrollFx = new Fx.Morph(this.itemsTable, {duration: 500, transition: Fx.Transitions.Sine.easeOut});

	if (finishRendering) this.rendered = true;
	this.checkHideArrows();
	return true;
};
santaJsScroller.prototype.checkHideArrows = function(){
	if (this.firstIndex == 0){
		this.leftArrow.style.visibility = "hidden";
	}else{
		this.leftArrow.style.visibility = "visible";
	}
	if (this.firstIndex == this.items.length - this.itemsPerPage){
		this.rightArrow.style.visibility = "hidden";
	}else{
		this.rightArrow.style.visibility = "visible";
	}
};
santaJsScroller.prototype.scrollToItem = function(nr){
	var nr = parseInt(nr) || 0;
	if (nr >= 0 && nr < this.items.length - this.itemsPerPage + 1){
		var toItem = this.items[nr];
		var container_left = parseInt(whi.getElementLeft(this.itemsContainer)) || 0;
		var item_left = parseInt(whi.getElementLeft(toItem)) || 0;
		var old_left = parseInt(whi.getStyle(this.itemsTable, 'left')) || 0;
		var new_left = (container_left - item_left + old_left + this.cellSpacing) + "px";

		this.scrollFx.start({'left': new_left});

		this.firstIndex = nr;
		if (this.hideArrowsOnMargin){
			this.checkHideArrows();
		}
	}
};











