/*
* element scroller script
* Scrolling with buttons element in container 
* Element nust be wider than container and
*
* ver 1.4
* Author: goshi
* 
* History:
*	1.4	03.12.08/goshi	remove serious bug with handler on document, added removeHandler function
*	1.3	03.12.08/goshi	added cookie for saving position
*	1.2	02.12.08/goshi	added button controls
*/

/*
* @param	contID	container identifier
*/
function oScroller(contID){
	this._init(contID);
	oScroller.Instances[contID] = this;
}

oScroller.prototype = {

	// identifier of container
	_contID: null,
	// element identifier
	_elemID: null,
	
	//butons
	_buttons: {'left': null, 'right': null},
	
	// links to objects
	_cont: null,
	_elem: null,
	
	// flag for set listener
	_listnerAdd: false,
	
	// element width
	_elemWidth: null,
	// container width
	_contWidth: null,
	
	// scroll speed
	_speed: 145,
	_base_speed: 145,
	_speed_step: 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){
		this._contID = contID;
		this._cont = document.getElementById('oScont_'+contID);
		this._elem = document.getElementById('oSelem_'+contID);
		this._buttons['left'] = document.getElementById('l_btn_'+contID);
		this._buttons['right'] = document.getElementById('r_btn_'+contID);
		this._contWidth = this._cont.offsetWidth;
		
		// updating element width
		var _this = this;
		this.domReady(
			function() {_this._updElemWidth()}
		);
		this._elemWidth = 0;
			
		//this._elemWidth = this._elem.style.width;
	},
	
	_updElemWidth: function(){
	
		for (var i = 0;i < this._elem.childNodes.length; i++){
					
			if (this._elem.childNodes[i].nodeName != '#text'){
				//alert(this._elem.childNodes[i].clientWidth + "::" + this._elem.childNodes[i].offsetWidth);
				this._elemWidth += this._elem.childNodes[i].offsetWidth;
			}
		}
		this._elem.style.width = this._elemWidth  + "px";//+ 10
		if (this._buttons['right'] != undefined && parseInt(this._elem.offsetLeft) + this._elem.offsetWidth > this._contWidth){
			// show buttons
			this._buttons['right'].style.display = '';
		}
	
	},
	
	domReady : function(i) { /* Copyright http://ajaxian.com/ */
		var u =navigator.userAgent;
		var e=/*@cc_on!@*/false;
		var st = setTimeout;
		if (/webkit/i.test(u)) {
			st(
				function() {
					var dr=document.readyState;
					if(dr=="loaded"||dr=="complete") i();
					else st(arguments.callee,10);
				},
				10
			);
		} else if ((/mozilla/i.test(u)&&!/(compati)/.test(u)) || (/opera/i.test(u))) {
			document.addEventListener("DOMContentLoaded", i, false);
		} else if (e) {(
			function(){
				var t=document.createElement('doc:rdy');
				try {
					t.doScroll('left');	i(); t=null;
				} catch(e) {st(arguments.callee,0);}
			})();
		} else window.onload=i;
	},
	
	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]);
		}
	},
	
	removeHandler : function(object, event, handler, useCapture) {
		if (object.removeEventListener) {
			object.removeEventListener(event, handler, (useCapture ? useCapture : false));
		} else if (object.detachEvent) {
			object.detachEvent('on' + event, handler);
		} else {
		//alert(this.errorArray[9]);
		}
	},
	
	stopScroll: function(nocookie){ 
		// remove handler
		
		clearInterval(this._intval);
		this._scrolling = false;
		this._speed = this._base_speed;
		// saving cookie 
		// using cookies library
		var _contID = this._contID;

		this.removeHandler(document, "mouseup", function(){ oScroller.Instances[_contID].stopScroll()});
		
		if (nocookie == undefined){
			//setCookie('oscrlr_'+this._contID, this._elem.style.left);
			saveMenuPos('oscrlr_'+this._contID, this._elem.style.left);
		}
		
		},

	scrollLeft: function(nocookie){
	
		if (!this._contID) return false;
		// checking - if we scrolling to left border
		this._elem.style.left = parseInt(this._elem.offsetLeft) + this._speed + "px";
		if (parseInt(this._elem.offsetLeft) >= 0){
			this._elem.style.left = 0 + "px";
			// hide left button
			if (this._buttons['left'] != undefined)
				this._buttons['left'].style.display = 'none';
			
		}
			
		
		// update speed
		this._speed += this._speed_step;
		
		// show right button
		if (this._buttons['right'] != undefined && parseInt(this._elem.offsetLeft) + this._elem.offsetWidth > this._contWidth){
			this._buttons['right'].style.display = '';
		}
				
		if (!this._scrolling){
			// adding handler for mouse release
			this._scrolling = true;
			if (!this._listnerAdd){
				var _this = this;			
				this.addHandler(document, "mouseup", function(){ _this.stopScroll()});
				this._listnerAdd = true;
			}
			this._intval = window.setInterval("oScroller.Instances[" + this._contID + "].scrollLeft()",100);
		}
	
	},
	scrollRight: function(nocookie){
	
		if (!this._contID) return false;
				
		// check if border of container > right border of the 
		this._elem.style.left = parseInt(this._elem.offsetLeft) - this._speed + "px";
		if (parseInt(this._elem.offsetLeft) + this._elem.offsetWidth < this._contWidth){
			this._elem.style.left = this._contWidth - this._elem.offsetWidth + "px";
			// hide right button
			if (this._buttons['right'] != undefined)
				this._buttons['right'].style.display = 'none';
		}
		

		// Because Kolot - pidar - now we don't use speed_step
		this._speed += this._speed_step;
		
		// show left button
		if (this._buttons['left'] != undefined && this._elem.offsetLeft < 0){
			this._buttons['left'].style.display = '';
		}
		
		if (!this._scrolling){
			// adding handler for mouse release
			this._scrolling = true;
			if (!this._listnerAdd){
				var _this = this;			
				this.addHandler(document, "mouseup", function(){ _this.stopScroll()});
				this._listnerAdd = true;
			}
			this._intval = window.setInterval("oScroller.Instances[" + this._contID + "].scrollRight()",100);
		}
	
	},
	
	scrollTo: function(x){
		if (!this._contID) return false;
		
		// check for scroll position
		this._scrolling = true;
		this._speed = 0;
		if (parseInt(this._elem.offsetLeft) >= x){
			this._elem.style.left = parseInt(x) + "px";
			this.scrollLeft(true);
		} else {
			this._elem.style.left = parseInt(x) + "px";
			this.scrollRight(true);		
		}
		this.stopScroll(true);
	
	}

};

oScroller.Instances = new Array();
