var Wdg = Wdg || {};
Wdg.animScroll = function(){
	return {
		init: function(conf) {
			if ( !conf.contentId || !conf.containerId ) return;
			this.containerId = conf.containerId;
			this.contentId   = conf.contentId;
			this.Container = document.getElementById(this.containerId);
			if ( !this.Container ) return;
			this.Content = document.getElementById(this.contentId);
			if ( !this.Content ) return;
			
			this.upBId = conf.upButtonId;
			if ( this.upBId ) {this.upButton = document.getElementById(this.upBId);}
			this.downBId = conf.downButtonId;
			if ( this.downBId ) {this.downButton = document.getElementById(this.downBId);}
			this.topBId = conf.topButtonId;
			if ( this.topBId ) {this.topButton = document.getElementById(this.topBId);}
			this.bottomBId = conf.bottomButtonId;
			if ( this.bottomBId ) {this.bottomButton = document.getElementById(this.bottomBId);}
			
			this.leftBId = conf.leftButtonId;
			if ( this.leftBId ) {this.leftButton = document.getElementById(this.leftBId);}
			this.rightBId = conf.rightButtonId;
			if ( this.rightBId ) {this.rightButton = document.getElementById(this.rightBId);}
			this.toLeftBId = conf.toLeftButtonId;
			if ( this.toLeftBId ) {this.toLeftButton = document.getElementById(this.toLeftBId);}
			this.toRightBId = conf.toRightButtonId;
			if ( this.toRightBId ) {this.toRightButton = document.getElementById(this.toRightBId);}
			
			this.speed = conf.speed ? conf.speed : 3;
			this.bigSpeed = conf.bigSpeed ? conf.bigSpeed : 20;
			
			this.containerHeight = this.Container.clientHeight;
			this.contentHeight   = this.Container.scrollHeight;
			this.containerWidth  = this.Container.clientWidth;
			this.contentWidth    = this.Container.scrollWidth;
			this.top = parseInt(this.Content.style.top);
			this.left = parseInt(this.Content.style.left);
			this.moveUp    = null;
			this.moveDown  = null;
			this.moveLeft  = null;
			this.moveRight = null;
			
			var _this = this;
			if ( this.upButton ) {
				this.upButton.onmouseover = function(){ _this.triggerScrollUp(_this.speed) }
				this.upButton.onmouseout  = function(){ _this.stopScrollUp(); }
			}
			if ( this.downButton ) {
				this.downButton.onmouseover = function(){ _this.triggerScrollDown(_this.speed) }
				this.downButton.onmouseout  = function(){ _this.stopScrollDown() }
			}
			if ( this.topButton ) {
				this.topButton.onclick = function(){
					_this.stopScrollDown();
					_this.triggerScrollUp(_this.bigSpeed);
				}
			}
			if ( this.bottomButton ) {
				this.bottomButton.onclick = function(){
					_this.stopScrollUp();
					_this.triggerScrollDown(_this.bigSpeed);
				}
			}
			
			if ( this.leftButton ) {
				this.leftButton.onmouseover = function(){ _this.triggerScrollLeft(_this.speed) }
				this.leftButton.onmouseout  = function(){ _this.stopScrollLeft() }
			}
			if ( this.rightButton ) {
				this.rightButton.onmouseover = function(){ _this.triggerScrollRight(_this.speed) }
				this.rightButton.onmouseout  = function(){ _this.stopScrollRight() }
			}
			if ( this.toLeftButton ) {
				this.toLeftButton.onclick = function(){
					_this.stopScrollRight();
					_this.triggerScrollLeft(_this.bigSpeed);
				}
			}
			if ( this.toRightButton ) {
				this.toRightButton.onclick = function(){
					_this.stopScrollLeft();
					_this.triggerScrollRight(_this.bigSpeed);
				}
			}
		},
		stopScrollDown:  function(){if( this.moveDown ) clearInterval(this.moveDown)},
		stopScrollUp:    function(){if( this.moveUp ) {	clearInterval(this.moveUp) }},
		stopScrollLeft:  function(){if( this.moveLeft ) clearInterval(this.moveLeft)},
		stopScrollRight: function(){if( this.moveRight ) clearInterval(this.moveRight)},
		triggerScrollUp: function(s){
			var _this = this;
			this.moveUp = setInterval(function(){
				_this.scrollUp(s)},20);
		},
		triggerScrollDown: function(s){
			var _this = this;
			this.moveDown = setInterval(function(){
				_this.scrollDown(s)},20);
		},
		triggerScrollLeft: function(s){
			var _this = this;
			this.moveLeft = setInterval(function(){
				_this.scrollLeft(s)},20);
		},
		triggerScrollRight: function(s){
			var _this = this;
			this.moveRight = setInterval(function(){
				_this.scrollRight(s)},20);
		},
		scrollDown: function(speed){
			var top = parseInt(this.Content.style.top);
			if ( top <= -(this.contentHeight-this.containerHeight) ) {
				this.stopScrollDown();
				this.Content.style.top = -(this.contentHeight-this.containerHeight)+'px';
			}
			else { 	this.Content.style.top=top-speed+'px'; }
		},
		scrollUp: function(speed){
			var top = parseInt(this.Content.style.top);
			var to = this.top ? this.top : 0;
			if ( top >= to ) {
				this.stopScrollUp();
				this.Content.style.top = to+'px';
			}
			else { this.Content.style.top=top+speed+"px"; }
		},
		scrollRight: function(speed){
			var left = parseInt(this.Content.style.left);
			if ( left <= -(this.contentWidth-this.containerWidth) ) {
				this.stopScrollRight();
				this.Content.style.left = -(this.contentWidth-this.containerWidth)+'px';
			}
			else { this.Content.style.left= (left-speed)+'px'; }
		},
		scrollLeft: function(speed){
			var left = parseInt(this.Content.style.left);
			var le = this.left ? this.left : 0;
			if ( left >= le ) {
				this.stopScrollLeft();
				this.Content.style.left = le+'px';
			}
			else { this.Content.style.left= left+speed+'px'; }
		}
	};
};

