var Wdg = Wdg || {};
Wdg.scroller = function(){
	return {
		init: function(conf){
			if (!document.createElement || !document.getElementsByTagName) return;
			if ( ! conf || ! conf.scrollerId || ! conf.scrolledTableId ) return;
			this.scrollDiv = document.getElementById(conf.scrollerId);
			if ( ! this.scrollDiv ) return;
			this.scrollTable = document.getElementById(conf.scrolledTableId);
			if ( ! this.scrollTable ) return;
			var _this = this;
			this.scrollDiv.onmousemove = function(e){
				var coords = _this.getCoordinates(e);
				_this.gorizontalScrollWidget(coords);
			};
			this.scrollLeft = null;
			this.scrollTop  = null;
			if ( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
					this.scrollLeft = document.body.scrollLeft;
					this.scrollTop  = document.body.scrollTop;
			} else if ( document.documentElement 
					&& ( document.documentElement.scrollLeft 
					|| document.documentElement.scrollTop ) ) {
						this.scrollLeft = document.documentElement.scrollLeft;
						this.scrollTop  = document.documentElement.scrollTop;
			}
		},
		getCoordinates: function(e){
			var Coordinates = {};
			if( !e ) { e = window.event; }
			if( !e ) { return; }
			if( typeof( e.pageX ) == 'number' ) {
				Coordinates.x = e.pageX;
				Coordinates.y = e.pageY;
			} else if ( typeof( e.clientX ) == 'number' ) {
				Coordinates.x = e.clientX;
				Coordinates.y = e.clientY;
				Coordinates.x += this.scrollLeft;
				Coordinates.y += this.scrollTop;
			} else { return; }
			e.cancelBubble = true;
			return Coordinates;
		},
		gorizontalScrollWidget: function(coords){
			var clientWidth = this.scrollDiv.clientWidth;
			var clientOffsetLeft = this.scrollDiv.offsetLeft;
			var tWidth = this.scrollTable.clientWidth;
			var c = tWidth - clientWidth;
			var d = c/clientWidth;
			var f = coords.x - clientOffsetLeft;
			if ( f > clientWidth-4 ) { f = clientWidth; }
			if ( f < 4 ) { f = 0; /* for IE */ }
			var g = -(Math.floor(f*d));
			this.scrollTable.style.left = g+'px';
		}
	};
};
