// $Id: scroller.js 3329 2007-07-13 14:08:53Z seva $

//
// Listmania Scroller
//
if (typeof(lmScroller) != 'function') {
	lmScroller = function (elm_id, quantity, direction, speed_factor, move_delay, pause_delay, width, height, show_quantity)
	{
		this.obj_name = elm_id;
		
		this.quantity = quantity;
		this.show_quantity = show_quantity;
		this.do_pause = (show_quantity == 1) ? 'true' : 'false';
		
		this.direction = direction || lm_scroller_directions[0];
	
		speed_factor = parseInt(speed_factor) || 0;
		this.speed_slow = (1 * (1 + speed_factor/100)) || 1;
		this.speed_normal = (((this.do_pause == 'true') ? 2 : 1) * (1 + speed_factor/100)) || 1;
		this.speed_fast = (4 * (1 + speed_factor/100)) || 1;
	
		this.timeOut_move = parseInt(move_delay);		//microseconds
		this.timeOut_pause = parseInt(pause_delay);		//microseconds
	
		this.p_level_fast = 80;	// percents
		this.p_level_slow = 95;	// percents
		
		this.height = -height;
		this.width = -width;
		
		this.cur_item = -1;
		
		this.scroll_objects = [];

		this.start_movelist();
	};
	
	lmScroller.prototype = {
		start_movelist: function ()
		{
			var elm_list = document.getElementById(this.obj_name);

			for (var i in elm_list.childNodes) {	// prepare array of moving objects
				if (elm_list.childNodes[i].nodeType == 1 && elm_list.childNodes[i].tagName != 'SCRIPT') {
					elm_list.childNodes[i].parent_scroller = this;
					elm_list.childNodes[i].onmouseover = this._stop_scrolling;
					elm_list.childNodes[i].onmouseout = this._start_scrolling;
					this.scroll_objects[this.scroll_objects.length] = elm_list.childNodes[i];
				}
			}
			this.movelist();
		},
		movelist: function ()
		{
			if (this.check_position() && this.cur_item >= 0) {	// moving
				var do_move = true;
				if (this.stop_scrolling != 'Y') {
					elm = this.scroll_objects[this.cur_item];
					if (this.direction == lm_scroller_directions['U'] || this.direction == lm_scroller_directions['D']) {
						elm.style.top = parseInt(this.offset_top) + "px";
					} else if (this.direction == lm_scroller_directions['L'] || this.direction == lm_scroller_directions['R']) {
						elm.style.left = parseInt(this.offset_left) + "px";
					}
					do_move = this.next_position();
				}
				this.timer = setTimeout('scrollers_list["' + this.obj_name + '"].movelist()', (do_move ? this.timeOut_move : this.timeOut_pause));
			} else {				// change object
				this.reset_position();
	
				var _cur_item = this.cur_item + 1;	// next object
				if (_cur_item >= this.scroll_objects.length) {
					_cur_item = 0;
				}
				elm = this.scroll_objects[_cur_item];
				elm.style.position = "relative";
				if (this.direction == lm_scroller_directions['U'] || this.direction == lm_scroller_directions['D']) {
					elm.style.top = parseInt(this.offset_top) + "px";
				} else if (this.direction == lm_scroller_directions['L'] || this.direction == lm_scroller_directions['R']) {
					elm.style.left = parseInt(this.offset_left) + "px";
				}
				elm.style.display = '';
				if (this.cur_item >= 0) {			// hide previous object
					elm = this.scroll_objects[this.cur_item];
					elm.style.display = 'none';
				}
				this.cur_item = _cur_item;
				var _timeOut = (this.do_pause == 'true') ? this.timeOut_pause : parseInt(this.timeOut_move / 2);
				this.timer = setTimeout('scrollers_list["' + this.obj_name + '"].movelist()', _timeOut);
				
				if (this.direction == lm_scroller_directions['U'] || this.direction == lm_scroller_directions['D']) {
					//var h = parseInt(this.scroll_objects[this.cur_item].offsetHeight/ 2) || (this.scroll_objects[this.cur_item].scrollHeight ? -this.height : parseInt(-this.height/this.show_quantity*this.quantity));
					var h = parseInt(this.scroll_objects[this.cur_item].offsetHeight/2) || parseInt(-this.height/this.show_quantity*this.quantity);
					
					this.height = - h;
					this.calculate_levels(h);
				} else if (this.direction == lm_scroller_directions['L'] || this.direction == lm_scroller_directions['R']) {
					var w = parseInt(this.scroll_objects[this.cur_item].offsetWidth/ 2) || -this.width;
					
					this.width = - w;
					this.calculate_levels(w);
				}
	
				this.reset_position();
			}
		},
		stoptimer: function ()
		{
			clearTimeout(this.timer);
		},
		check_position: function ()
		{
			if (this.direction == lm_scroller_directions['U']) {
				return (this.offset_top > this.height) ? true : false;
			} else if (this.direction == lm_scroller_directions['D']) {
				return (this.offset_top <= 0) ? true : false;
			} else if (this.direction == lm_scroller_directions['L']) {
				return (this.offset_left > this.width) ? true : false;
			} else if (this.direction == lm_scroller_directions['R']) {
				return (this.offset_left <= 0) ? true : false;
			}
			return false;
		},
		next_position: function ()
		{
			if (this.direction == lm_scroller_directions['U'] || this.direction == lm_scroller_directions['D']) {
				var length = 'height';
				var position = 'offset_top';
			} else if (this.direction == lm_scroller_directions['L'] || this.direction == lm_scroller_directions['R']) {
				var length = 'width';
				var position = 'offset_left';
			}
			if (this.direction == lm_scroller_directions['U'] || this.direction == lm_scroller_directions['L']) {
				var offset = this[position] % (this[length]/this.quantity);
				if (offset < this.level_fast && offset >= this.level_slow || this.do_pause != 'true') {
					this[position] = this[position] - this.speed_normal;
				}
				if (offset < this.level_slow && this.do_pause == 'true') {
					this[position] = this[position] - this.speed_slow;
				}
				if (offset >= this.level_fast && this.do_pause == 'true') {
					this[position] = this[position] - this.speed_fast;
				}
			} else if (this.direction == lm_scroller_directions['D'] || this.direction == lm_scroller_directions['R']) {
				var offset = this[position] % (this[length]/this.quantity);
				if (offset > this.level_fast && offset <= this.level_slow || this.do_pause != 'true') {
					this[position] = this[position] + this.speed_normal;
				}
				if (offset > this.level_slow && this.do_pause == 'true') {
					this[position] = this[position] + this.speed_slow;
				}
				if (offset <= this.level_fast && this.do_pause == 'true') {
					this[position] = this[position] + this.speed_fast;
				}
			}
			return (offset || this.do_pause != 'true') ? true : false;
		},
		reset_position: function ()
		{
			if (this.direction == lm_scroller_directions['U']) {
				this.offset_top = this.speed_slow;
			} else if (this.direction == lm_scroller_directions['D']) {
				this.offset_top = this.height;
			} else if (this.direction == lm_scroller_directions['L']) {
				this.offset_left = this.speed_slow;
			} else if (this.direction == lm_scroller_directions['R']) {
				this.offset_left = this.width;
			}
		},
		calculate_levels: function (length)
		{
			length = length / this.quantity;
			if (this.direction == lm_scroller_directions['U'] || this.direction == lm_scroller_directions['L']) {
				this.level_fast = -(length * this.p_level_fast / 100)
				this.level_slow = -(length * this.p_level_slow / 100)
			} else if (this.direction == lm_scroller_directions['D'] || this.direction == lm_scroller_directions['R']) {
				this.level_fast = -length + (length * this.p_level_fast / 100)
				this.level_slow = -length + (length * this.p_level_slow / 100)
			}
		},
		_stop_scrolling: function(e)
		{
			this.parent_scroller.stop_scrolling = 'Y';
		},
		_start_scrolling: function(e)
		{
			this.parent_scroller.stop_scrolling = 'N';
		}
	};
}
//
// Listmania Scroller
//
