/*
* element scroller script
* Scrolling with buttons element in container 
* Element nust be wider than container and
* ver 1.2
* 
* History:
*	1.2	20.07.09/goshi 	fix bug in opera > 9.6 with document height
*	1.1	06.07.09/goshi 	add rows parameter
*/

/*
* @param	contID	container identifier
* @param	params	hash with parameters
*/
function oScroller(contID, params){
	this._init(contID, params);
	oScroller.Instances[contID] = this;
}

oScroller.prototype = {

	// identifier of container
	_contID: null,
	// element identifier
	_elemID: null,
	// links to objects
	_cont: null,
	_elem: null,
	
	// element width
	_elemWidth: null,
	_minElemWidth: null,
	
	// container width
	_contWidth: null,
	
	_orientation: 'horizontal', // orientation - may be horizontal, or vertical
	
	// scroll speed
	_speed: 1,
	_base_speed: 5,
	_speed_step: 2,
	_max_speed: 25,
	
	// rows count - if we want not one line, but always some row
	_rows: 0,
	
	// interval
	_intval: null,
	
	// flag , that says to us - if user pressed to tthe mouse button and don't released it
	_scrolling: false,
	
	_init: function(contID, params){
	
		// adding params
		this._add_params(params);
	
		this._contID = contID;
		this._cont = $_('oScont_'+contID);
		this._elem = $_('oSelem_'+contID);
		this._contWidth = this._orientation == 'horizontal' ? this._cont.offsetWidth : this._cont.offsetHeight;
		
		

		// updating element width
		var _this = this;
		portal.ready(
			function() {_this._updElemWidth()}
		);
		this._elemWidth = 0;
			
		//this._elemWidth = this._elem.style.width;
	},
	
	_add_params : function(params){
		if ((typeof params).toLowerCase() == "array" || (typeof params).toLowerCase() == "object"){
			for (var i in params){
				if (typeof this["_"+i] != "undefined" && typeof this["_"+i] != "null")
					this["_"+i] = params[i];
			}
		}
	},
	
	_updElemWidth: function(){
	
		var all_cnt = 0;
		
		// restore zero value
		if (this._minElemWidth != null && this._minElemWidth > 0)
			this._elemWidth = 0;
		
		for (var i = 0, cnt=this._elem.childNodes.length;i < cnt; i++){
					
			if (this._elem.childNodes[i].nodeName != '#text'){
				//alert(this._elem.childNodes[i].clientWidth + "::" + this._elem.childNodes[i].offsetWidth);
				this._elemWidth += this._orientation == 'horizontal' ? this._elem.childNodes[i].offsetWidth : this._elem.childNodes[i].offsetHeight;
				all_cnt++;
			}
		}

		// check for minimum width
		if (this._minElemWidth != null && this._minElemWidth > 0 && this._elemWidth < this._minElemWidth){
			this._elemWidth = this._minElemWidth;
		}
		
		if (this._rows > 0){
			this._elemWidth = Math.round(this._elemWidth/this._rows);
		}

		if (this._orientation == 'horizontal')
			this._elem.style.width = this._elemWidth  + "px";
		else
			this._elem.style.height = this._elemWidth  + "px";
		

	},
	

	
	addHandler : function(object, event, handler, useCapture) {
		if (object.addEventListener) {
			object.addEventListener(event, handler, (useCapture ? useCapture : false));
		} else if (object.attachEvent) {
			object.attachEvent('on' + event, handler);
		} else {
			//alert(this.errorArray[9]);
		}
	},
	
	stopScroll: function(){ 
		clearInterval(this._intval);
		this._scrolling = false;
		this._speed = this._base_speed;
		document.body.style.height = document.body.offsetHeight + 'px';
		},

	scrollLeft: function(){
	
		if (!this._contID) return false;
		// checking - if we scrolling to left border
		if (this._orientation == 'horizontal'){
			this._elem.style.left = parseInt(this._elem.offsetLeft) + this._speed + "px";
			if (parseInt(this._elem.offsetLeft) >= 0)
				this._elem.style.left = 0 + "px";
		} else {
			this._elem.style.top = parseInt(this._elem.offsetTop) + this._speed + "px";
			if (parseInt(this._elem.offsetTop) >= 0)
				this._elem.style.top = 0 + "px";
		}
		
		if (this._max_speed > 0 && this._speed < this._max_speed)
			this._speed += this._speed_step;
			
		if (this._speed > this._max_speed)
			this._speed = this._max_speed;
				
		if (!this._scrolling){
			// adding handler for mouse release
			this._scrolling = true;
			var _this = this;
			this.addHandler(document, "mouseup", function(){ _this.stopScroll()});
			this.addHandler(document, "mouseout", function(){ _this.stopScroll()});
			this._intval = window.setInterval("oScroller.Instances[" + this._contID + "].scrollLeft()",100);
		}
	
	},
	scrollRight: function(){
	
		if (!this._contID) return false;
				
		// check if border of container > right border of the 
		if (this._orientation == 'horizontal'){
			this._elem.style.left = parseInt(this._elem.offsetLeft) - this._speed + "px";
			
			if (parseInt(this._elem.offsetLeft) + this._elem.offsetWidth < this._contWidth && this._elem.offsetWidth >= this._contWidth){
				
				this._elem.style.left = this._contWidth - this._elem.offsetWidth + "px";
			} else if (this._elem.offsetWidth < this._contWidth){
			
				this._elem.style.left = 0;
			}
		} else {
			this._elem.style.top = parseInt(this._elem.offsetTop) - this._speed + "px";
			if (parseInt(this._elem.offsetTop) + this._elem.offsetHeight < this._contWidth){
				this._elem.style.top = this._contWidth - this._elem.offsetHeight + "px";
			}
		}
		
		this._speed += this._speed_step;

		if (!this._scrolling){
			// adding handler for mouse release
			this._scrolling = true;
			var _this = this;
			this.addHandler(document, "mouseup", function(){ _this.stopScroll()});
			this.addHandler(document, "mouseout", function(){ _this.stopScroll()});
			this._intval = window.setInterval("oScroller.Instances[" + this._contID + "].scrollRight()",100);
		}
	
	}

};

oScroller.Instances = new Array();