/***
*	class brxScroller
*	alex.brunevsky@gmail.com - 05.2008
***/

function brxScroller(el_id, con_style) {
	
	this.MAX_BLOCKS = 100;
	this.id = el_id;
	this.type = null;
	this.uniq_id = 'brxScroller_' + el_id + '_' + (new Date()).getTime();
	this.con = document.getElementById(el_id);
	this._elememnts = [];

	if (this.con) {
		with (this.con.style) {
			position = 'relative';
			overflow = 'hidden';
		}
		for (k in con_style) this.con.style[k] = con_style[k];
	}
	else alert("Error in brxScroller::brxScroller: Couldn't element with id=`" + el_id + "`");
	return this;
}

/***
*	Desc:	inicialize object brxScroller by Scroller DIV. Draw all elements
***/
brxScroller.prototype.initInnerDiv = function () {
	
	if (this.con.offsetHeight > 0) {
		this.offset = 0;
		this.extra_blocks = 0;
		this.innerDiv = this.con.cloneNode(true);

		this.scroll_div = document.createElement('div');
		with (this.scroll_div.style) {
			position = 'relative';
			top = '0';
			left = '0';
		}
//		if (this.scroll_type != 'v') this.scroll_div.style.height = parseInt(this.con.offsetHeight);
		var block = this._buildInnerDivBlock();
		this.scroll_div.appendChild(block);
		this.type = 'innerDiv';
		this.con.innerHTML = '';
		this.con.appendChild(this.scroll_div);

		if (this.scroll_type != 'v') this.scroll_div.style.width = parseInt(block.offsetWidth) + 5 + 'px';

	}
	else alert('Error in brxScroller::initInnerDiv: Height of div id="' + this.id + '" is 0.');

}


/***
*	Desc:	start brxScroller
*	h_step:	horizontal step per tick (px)
*	delay:	delay interval between ticks (mc)
*	type: h/v - horizontal or vertical scrolling
***/
brxScroller.prototype.start = function (h_step, delay, scroll_type) {
	this.scroll_type = scroll_type || 'v';
	this.h_step = h_step;
	this.delay	= delay;
	
	window[this.uniq_id] = {
		_self:	this,
		tick:	this._tick
	}
	window.setTimeout('window.' + this.uniq_id + '.tick()', delay);
}

// private methodes
brxScroller.prototype._tick = function () {
	var _self = this._self;

	if (_self.h_step > 0) {
		if (_self.offset + _self.h_step > 0) {
			var block = (_self.type == 'images')? _self._buildImageBlock(): _self._buildInnerDivBlock();
			_self.scroll_div.insertBefore(block, _self.scroll_div.firstChild);
			if (_self.scroll_type != 'v') {
				_self.scroll_div.style.width = (parseInt(_self.scroll_div.offsetWidth) + parseInt(block.offsetWidth)) + 'px';
				_self.offset -= parseInt(block.offsetWidth);
			}
			else
			{
				_self.offset -= parseInt(block.offsetHeight);
			}
			_self.extra_blocks ++;
			if (_self.extra_blocks > 3) {
				_self.scroll_div.removeChild(_self.scroll_div.lastChild);
				_self.extra_blocks --;
			}
		}

	}
	else {
		var is_need_new = (_self.scroll_type == 'v')
			? (_self.scroll_div.offsetHeight + _self.offset + _self.h_step < _self.con.offsetHeight)
			: (_self.scroll_div.offsetWidth + _self.offset + _self.h_step < _self.con.offsetWidth);

		if (is_need_new) {
			var block = (_self.type == 'images')? _self._buildImageBlock(): _self._buildInnerDivBlock();
			_self.scroll_div.appendChild(block);
			if (_self.scroll_type != 'v') {
				_self.scroll_div.style.width = parseInt(_self.scroll_div.offsetWidth) + parseInt(block.offsetWidth) + 'px';
			}
			_self.extra_blocks ++;
			if (_self.extra_blocks > 3) {
				_self.offset += (_self.scroll_type == 'v')? parseInt(block.offsetHeight): parseInt(block.offsetWidth);
				_self.scroll_div.removeChild(_self.scroll_div.firstChild);
				_self.extra_blocks --;
			}
		}
	}
	_self.offset += _self.h_step;

	if (_self.scroll_type == 'v') _self.scroll_div.style.top = _self.offset + 'px';
	else  _self.scroll_div.style.left = _self.offset + 'px';
	window.setTimeout('window.' + _self.uniq_id + '.tick()', _self.delay);
}

brxScroller.prototype._buildImageBlock = function () {
	var block = document.createElement('div');
	for (var i = 0; i < this.elements.length; i ++) {
		var el_obj = this._buildImageElement(this.elements[i]);
		block.appendChild(el_obj);
	}
	return block;
}

brxScroller.prototype._buildInnerDivBlock = function () {
	var block = document.createElement('div');
	var need_to_push = (this._elememnts.length == 0);
	for (var i = 0; i < this.innerDiv.childNodes.length; i ++) {
		if (need_to_push && this.innerDiv.childNodes[i].tagName) this._elememnts.push(this.innerDiv.childNodes[i]);
		var el_obj = this.innerDiv.childNodes[i].cloneNode(true);
		block.appendChild(el_obj);
	}
	block.style.styleFloat = 'right';
	block.style.cssFloat = 'right';
	block._self = this;
	block.onmouseover = this._holdScroller;
	block.onmouseout = this._restoreScroller;
	block.style.width = 160 * this._elememnts.length + 'px';
	return block;
}

brxScroller.prototype._buildImageElement = function (element_info) {
	var el_item = document.createElement('a');
	with (el_item.style) {
		if (this.scroll_type == 'v') width = this.scroll_div.offsetWidth + 'px';
		else height = this.scroll_div.offsetHeight + 'px';
		position = 'relative';
		overflow = 'hidden';
		display = 'block';
		backgroundImage = 'url(' + escape(element_info.src) + ')';
		backgroundPosition = 'top left';
		backgroundRepeat = 'no-repeat';
	}
	
	el_item.setAttribute('href', element_info.href);
	if (element_info.target) el_item.setAttribute('target', element_info.target);
	if (element_info.title) el_item.setAttribute('title', element_info.title);

	el_item._self = this;
	el_item._src = 'url(' + escape(element_info.src) + ')';
	el_item._hover_src = 'url(' + escape(element_info.hover_src) + ')';
	el_item.onmouseover = this._holdScroller;
	el_item.onmouseout = this._restoreScroller;
	
	for (k in this.el_style) el_item.style[k] = this.el_style[k];
	if (element_info.height) el_item.style.height = element_info.height;

	return el_item;
}

brxScroller.prototype._holdScroller = function () {
	var _self = this._self;
	_self.bu_h_step = _self.h_step;
	_self.h_step = 0;
	if (this._hover_src) this.style.backgroundImage = this._hover_src;
}

brxScroller.prototype._restoreScroller = function () {
	var _self = this._self;
	_self.h_step = _self.bu_h_step;
	if (this._src) this.style.backgroundImage = this._src;
}

/***
*	class brxScroller
*	alex.brunevsky@gmail.com - 05.2008
***/
