
/*
 * Прокручивание блока
 * TimeZero <http://www.timezero.ru/>
 * 2007-12-24
 * Автор: Дмитрий Шкинёв <berkel@timezero.ru>
 */

var SliderAnimation = function(containerId){

	this._mainContainer = $(containerId);
	this._container = $(containerId + "-container");
	this._arr = $(containerId + "-arr");

	this.init();
}

SliderAnimation.prototype = {

	init : function(){

		var eThis = this;

		if (eThis._container.getElementsByTagName("DIV").length == 0)
			return false;

		eThis.tremblingInit();

		cmnAdd_event(eThis._arr, "click", function(){eThis.scrollingInit();});
		eThis.autoScrollingTimer = setInterval(function(){eThis.autoScrolling();},5000);
	},

	tremblingInit : function(){

		var eThis = this;

		eThis.tempDefaultPositionInterval = 4;
		eThis.tempPositionInterval = eThis.tempDefaultPositionInterval;

		if (!(eThis.tremblingInitIntervalId || eThis.scrollingInitIntervalId)){

			eThis.containerDefaultPosition = parseInt(eThis._container.style.top) || 0;
			eThis.tremblingInitIntervalId = setInterval(function(){eThis.trembling();}, 70);
		}
	},

	trembling : function(){

		var eThis = this;

		var y = eThis.containerDefaultPosition;

		if (eThis.tempPositionInterval < 0){

			eThis.tempPositionInterval = Math.abs(eThis.tempPositionInterval);
			eThis.tempPositionInterval--;
			y = eThis.containerDefaultPosition - eThis.tempPositionInterval;

		} else if (eThis.tempPositionInterval > 0){

			eThis.tempPositionInterval = -eThis.tempPositionInterval;
			eThis.tempPositionInterval++;
			y = eThis.containerDefaultPosition - eThis.tempPositionInterval;

		} else {

			clearInterval(eThis.tremblingInitIntervalId);
			eThis.tremblingInitIntervalId = null;
			eThis.tempPositionInterval = eThis.tempDefaultPositionInterval;
		}

		eThis._container.style.top = (y) + "px";
	},

	scrollingInit : function(){

		var eThis = this;

		if (!(eThis.tremblingInitIntervalId || eThis.scrollingInitIntervalId)){

			eThis.containerDefaultPosition = parseInt(eThis._container.style.top) || 0;

			eThis.scrollingInitIntervalId = setInterval(function(){eThis.scrolling();}, 10);
		}
	},

	scrolling : function(){
		
		var eThis = this;
		
		clearInterval(eThis.autoScrollingTimer);
		eThis.autoScrollingTimer = null;

		var step = 7;

		var y = parseInt(eThis._container.style.top) || 0;

		var oFirstItem = eThis._container.getElementsByTagName("DIV")[0];

		var needY = eThis.containerDefaultPosition - oFirstItem.offsetHeight;
		
		if (y <= needY){

			if (Math.abs(needY) > eThis._mainContainer.offsetHeight){

				eThis._container.style.paddingTop = "0";
				eThis._container.style.top = "0";

			} else {

				eThis._container.style.paddingTop = Math.abs(needY) + "px";
				eThis._container.style.top = (needY) + "px";
			}

			eThis._container.appendChild(oFirstItem);

			clearInterval(eThis.scrollingInitIntervalId);
			eThis.scrollingInitIntervalId = null;

			eThis.tremblingInit();

		} else {

			eThis._container.style.top = (y - step) + "px";
		}
		
		eThis.autoScrollingTimer = setInterval(function(){eThis.autoScrolling();},5000)
	},
	
	autoScrolling: function(){
		var eThis = this;
		clearInterval(eThis.autoScrollingTimer);
		eThis.autoScrollingTimer = null;
		eThis.scrollingInit();
		eThis.autoScrollingTimer = setInterval(function(){eThis.scrollingInit();},5000)
	}
}