var jscroller = new Object;

jscroller.init = function(){
	if (jscroller.viewportObject) return true; // no need to reinit
	
	jscroller.viewportObject = document.getElementById("cnt-scroller");
	if (!jscroller.viewportObject) return false;
	
	jscroller.carrierObject = document.getElementById("cnt-scroller-carrier");
	if (!jscroller.carrierObject) return false;
	
	jscroller.frames = new Array();
	var nl = jscroller.carrierObject.childNodes;
	for (var i = 0; i < nl.length; i++){
		if (typeof(nl[i]) != "object" || !(/(^|\s+)cnt-scroller-frame(\s+|$)/.test(nl[i].className))) continue;
		jscroller.frames.push(nl[i]);
	}
	
	jscroller.scrollAmount = 0;
	jscroller.scrollDeparture = 0;
	jscroller.scrollTarget = 0;
	jscroller.scrollTimerId = 0;
	jscroller.activeFrame = 0;
};

jscroller.tick = function(){
	if (jscroller.scrollAmount == jscroller.scrollTarget){
		jscroller.dropAnimation();
	};
	var diff = (jscroller.scrollTarget - jscroller.scrollAmount);
	var vector = (jscroller.scrollTarget > jscroller.scrollDeparture)?1:-1;
	jscroller.scrollAmount += (diff * .1 + vector * 2);
	if ((jscroller.scrollAmount - jscroller.scrollTarget) * vector > 0){
		jscroller.scrollAmount = jscroller.scrollTarget;
	}
/*
	var shownIndex = parseInt(jscroller.scrollAmount / 100);
	jscroller.setOpacity(jscroller.frames[shownIndex], (100 - jscroller.scrollAmount%100) / 100);
	jscroller.setOpacity(jscroller.frames[shownIndex + 1], jscroller.scrollAmount%100 / 100);
/* */
	
	jscroller.carrierObject.style.left = -jscroller.scrollAmount*1.2 + "%";
};


jscroller.setOpacity = function(obj, value){
	obj.style.opacity = value;
	obj.style.display = (value > 0)?"":"none"
}


jscroller.dropAnimation = function(){
	if (jscroller.scrollTimerId){
		clearInterval(jscroller.scrollTimerId);
	};
	jscroller.scrollTimerId = 0;
}

jscroller.startAnimation = function(){
	if (!jscroller.scrollTimerId){
		jscroller.scrollTimerId = setInterval(jscroller.tick, 40);
	};
}

jscroller.scrollTo = function(scrollTarget){
	jscroller.scrollTarget = scrollTarget;
	jscroller.scrollDeparture = jscroller.scrollAmount;
	jscroller.startAnimation();
}

jscroller.go = function(to){
	jscroller.init();
	switch (typeof(to)){
		case "string":
			switch (to){
				case "next":
					if (jscroller.activeFrame < jscroller.frames.length - 1){
						jscroller.activeFrame++;
					} else {
						return false;
					}
				break;
				case "prev":
					if (jscroller.activeFrame > 0){
						jscroller.activeFrame--;
					} else {
						return false;
					}
				break;
			};
		break;
		case "number":
			to = parseInt(to);
			if (to < 0 && to >=jscroller.frames.length){
				return false;
			} else {
				jscroller.activeFrame = to;
			};
		break;
		default:
			return false;
	};
	var newClass = jscroller.viewportObject.className.replace(/(^|\s+)(js-first|js-last)(\s+|$)/g, "");
	if (jscroller.activeFrame == 0){
		newClass += " js-first";
	};
	if (jscroller.activeFrame == jscroller.frames.length - 1){
		newClass += " js-last";
	};
	jscroller.viewportObject.className = newClass;
	
	jscroller.scrollTo(jscroller.activeFrame * 100);
}
