function Scroller() {
	this.frame_width = 0;
	this.time = 0;
	this.amount = 0;
	this.counter = 0;
	this.lcounter = 0;
	this.items = [];
	this.items_num = 0;
	this.mode = "slideshow";//scroll
	this.DHTML = false;
	this.container_id = '';
	this.container = '';
	this.clipTop = 0;
	this.clipLeft = 0;
	this.clipRight = 0;
	this.clipBottom = 0;
	this.lefter = 0;
	this.init_left = 0;
	this.container_width = 0;
	this.window_width = 0;
	this.ctime = 0;
	this.visible_items_num = 0;
	this.item_prefix = "";
	this.current_item_id = 0;
	this.current_item_index = 0;
	this.all_divs = [];
	this.all_divs_pointer = 0;
	this.all_divs_prev_pointer = 0;
	this.scroll_frames_num = 0;
	// Callback handlers
	this.onStop = null;
	this.onChanged = null;
	this.onHighlightShow = null;
	this.onHighlightHide = null;
	this.onClick = null;
	this.center_position = 0;
}

Scroller.prototype = {
	init: function() {
		this.DHTML = (document.getElementById || document.all || document.layers)
		if (!this.DHTML) return;
		this.getContainer();
		if (!this.container) {
			return;
		}
		// Get index of selected item
		for(i = 0; i < this.items.length; i++) {
			if (this.current_item_id == this.items[i]) {
				this.current_item_index = i;
			}
		}
		// Index of place where selected item shows
		this.center_position = parseInt(this.visible_items_num/2);
		// initial items number
		this.items_num = this.items.length;
		this.copyElements();
		// Get initial values for clip and div
		drift = (this.visible_items_num + this.current_item_index - this.center_position)*this.frame_width;
		this.moveContainerX(-drift);
		this.all_divs_pointer = this.visible_items_num + this.current_item_index;
		// Set onclick function
		if (this.onClick != null) {
			for(i = 0; i < this.all_divs.length; i++) {
				element = document.getElementById(this.all_divs[i]);
				if (element != null) {
					element.onclick = this.onClick;
				}
			}
		}
		
		// Get container left margin
		this.clipRight = this.window_width + this.clipLeft;
		this.moveClipX(drift);

		if (document.layers) {
			this.container.style.clip.top = this.clipTop;
			this.container.style.clip.left = this.clipLeft;
			this.container.style.clip.right = this.clipRight;
			this.container.style.clip.bottom = this.clipBottom;
		} else if (document.getElementById || document.all) {
			var clipstring = 'rect(' + this.clipTop + 'px,' + this.clipRight + 'px,';
			clipstring += this.clipBottom + 'px,' + this.clipLeft + 'px)';
			this.container.style.clip = clipstring;
		}

		this.init_left = this.lefter;
		this.container_width = this.container.offsetWidth;
		if (this.onHighlightShow != null) {
			this.onHighlightShow(this.getCurrentElement());
		}
	},
	
	moveClipX: function(value) {
		this.clipLeft += value;
		this.clipRight += value;
	},
	
	moveClipY: function(value) {
		this.clipTop += value;
		this.clipBottom += value;
	},
	
	moveContainerX: function(value) {
		this.lefter += value;
		if (document.getElementById || document.all) {
			this.container.style.left = this.lefter + 'px';
		} else if (document.layers) {
			this.container.style.left = this.lefter;
		}
	},
	
	moveContainerY: function(value) {
		if (document.getElementById || document.all) {
			this.container.style.top = this.lefter + value + 'px';
		} else if (document.layers) {
			this.container.style.top = this.lefter + value;
		}
	},

	copyElements: function() {
		var j = 0;
		if (this.items.length > this.visible_items_num) {
			html = '<table border="0" cellpadding="0" cellspacing="0">';
			html += '<tr>';

			for(i = this.items.length - this.visible_items_num; i < this.items.length; i++) {
				html += this.copyElement(this.items[i], "_1", j);
				j++
			}
			for(i = 0; i < this.items.length; i++) {
				html += this.copyElement(this.items[i], "", j);
				j++;
			}
			for(i = 0; i < this.visible_items_num; i++) {
				html += this.copyElement(this.items[i], "_2", j);
				j++;
			}
			html += '</tr>';
			html += '</table>';
			this.container.innerHTML = html;
		}
	},
	
	copyElement: function(id, postfix, j) {
		element = document.getElementById(this.item_prefix + id);
		
		new_html = "";
		if (element != null) {
			new_id = element.id + postfix;
			new_html += '<td>';
			new_html += '<div id ="' + new_id + '" class="' + element.className + '">';
			new_html += element.innerHTML;
			new_html += '</div>';
			new_html += '</td>';
			this.all_divs[j] = new_id;
			
			return new_html;
		}
		return "";
	},
	
	
	getContainer: function() {
		name = this.container_id;
		if (document.getElementById) {
			this.container = document.getElementById(name);
		} else if (document.all) {
			this.container = document.all[name];
			this.container.style = document.all[name].style;
		} else if (document.layers) {
			this.container = document.layers[name];
			this.container.style = document.layers[name];
		}		
	},
	
	stopScroll: function() {
		this.lcounter = 0;
		this.counter = 0;

		this.all_divs_prev_pointer = this.all_divs_pointer;
		
		if (this.amount >= 0) {
			this.current_item_index += this.scroll_frames_num;
			if (this.current_item_index > this.items_num) {
				this.current_item_index -= this.items_num;
			}
			this.all_divs_pointer += this.scroll_frames_num;
		} else {
			this.current_item_index -= this.scroll_frames_num;
			if (this.current_item_index < 0) {//this.items_num) {
				this.current_item_index += this.items_num;
			}
			this.all_divs_pointer -= this.scroll_frames_num;
		}

		if (this.current_item_index > (this.items_num - 1)) {
			this.current_item_index = 0;
		} else if (this.current_item_index < 0) {
			this.current_item_index = this.items_num - 1;
		}
		this.current_item_id = this.items[this.current_item_index];
		
		if (this.onStop != null) {
			this.onStop();
		}

		if (this.onHighlightHide != null) {
			this.onHighlightHide(this.getPrevElement());
		}
		if (this.onHighlightShow != null) {
			this.onHighlightShow(this.getCurrentElement());
		}
		
		if (this.ctime) {
			clearTimeout(this.ctime);
		}
	},
	
	scroll: function() {
		if (!this.DHTML) return;

		// Stop scrolling
		if (this.lcounter >= this.counter) {
			this.stopScroll();
			return;
		}

		this.clipRight += this.amount;
		this.clipLeft += this.amount;
		this.lefter -= this.amount;
		// Check if we reach right edge
		if (this.visible_items_num * this.frame_width - this.lefter >= this.container_width) {
			drift = this.items_num*this.frame_width;
			
			this.moveContainerX(drift);
			this.moveClipX(-drift);
			this._changeClip();
			
			// Move pointers on real div
			if (this.onHighlightHide != null) {
				this.onHighlightHide(this.getCurrentElement());
			}

			this.all_divs_prev_pointer -= this.items_num;
			this.all_divs_pointer -= this.items_num;

			//this.stopScroll();
			//return;
		}
		// Check if we reach left edge
		if (this.lefter >= 0) {//this.init_left) {
			drift = this.items_num*this.frame_width;
			
			this.moveContainerX(-drift);
			this.moveClipX(drift);
			this._changeClip();
			// Move pointers on real div
			if (this.onHighlightHide != null) {
				this.onHighlightHide(this.getCurrentElement());
			}
			this.all_divs_prev_pointer += this.items_num;
			this.all_divs_pointer += this.items_num;
			
			//this.stopScroll();
			//return;			
		}
		if (document.getElementById || document.all) {
			clipstring = 'rect('+this.clipTop+'px,'+this.clipRight+'px,'+this.clipBottom+'px,' + this.clipLeft + 'px)'
			this.container.style.clip = clipstring;
			this.container.style.left = this.lefter + 'px';
		} else if (document.layers) {
			this.container.style.clip.left = this.clipLeft;
			this.container.style.clip.right = this.clipRight;
			this.container.style.left = this.lefter;
		}

		this.lcounter++;
		this.ctime = setTimeout('scroller.scroll()', this.time);
	},
	
	nextFrame: function(direction, frames_num) {
		this.mode = 'slideshow';
		
		if (this.lcounter > 0) {
			return;
		}
		if (!this.DHTML) return;
		this.scroll_frames_num = frames_num;

		if (direction == "left") {
			this.amount = Math.abs(scroller.amount);
		} else {
			this.amount = -Math.abs(scroller.amount);
		}
		
		if (this.amount < 0) {
			this.counter = -parseInt(this.scroll_frames_num * this.frame_width/this.amount);
		} else {
			this.counter = parseInt(this.scroll_frames_num * this.frame_width/this.amount);
		}
		this.scroll();
	},
	
	_changeClip: function() {
		if (document.getElementById || document.all) {
			clipstring = 'rect('+this.clipTop+'px,'+this.clipRight+'px,'+this.clipBottom+'px,' + this.clipLeft + 'px)'
			this.container.style.clip = clipstring;
		} else if (document.layers) {
			this.container.style.clip.left = this.clipLeft;
			this.container.style.clip.right = this.clipRight;
		}
	},
	
	getCurrentItemId: function() {
		return this.current_item_id;
	},
	
	getCurrentElement: function() {
		return this.all_divs[this.all_divs_pointer];
	},
	
	getPrevElement: function() {
		return this.all_divs[this.all_divs_prev_pointer];
	}
	
}
