
var scrollWindow;

scrollWindow = function(windowid, layerid, orientation, triggerleftid, triggerrightid) {

	var self=this;
	
	this.windowid = windowid;
	this.layerid = layerid;
	this.orientation = orientation;
	this.triggerleftid = triggerleftid;
	this.triggerrightid = triggerrightid;
	this.x = 0;
	this.y = 0;
	this.defaultSpeed = 50;
	this.speed = this.defaultSpeed;
	
	
	Utils.addLoadListener(function() {self.loadListener()}); 
	
};

scrollWindow.prototype.addEvtTrigger = function(triggerid, direction) {
	var self=this;

	Utils.addEventListener(triggerid, 'mouseover', function(e) {self.onMouseOverTrigger(e, direction)} , false);		
	Utils.addEventListener(triggerid, 'mouseout', function(e) {self.onMouseOutTrigger(e, direction)} , false);		
	
	Utils.addEventListener(triggerid, 'mousedown', function(e) {self.doubleSpeed()} , false);		
	Utils.addEventListener(triggerid, 'mouseup', function(e) {self.resetSpeed()} , false);		
	
};

scrollWindow.prototype.loadListener = function() {
	
	this.window = Utils.getElementById(this.windowid);
	this.layer = Utils.getElementById(this.layerid);
	
	this.triggerleft = Utils.getElementById(this.triggerleftid);
	this.triggerright = Utils.getElementById(this.triggerrightid);
	
	// Umwege über zwischenfunktion wg argumente....
	this.addEvtTrigger(this.triggerleftid, 'left');
	this.addEvtTrigger(this.triggerrightid, 'right');
	
	
	this.layer.style.position = 'absolute'; 
	this.layer.style.visibility = "visible";


	
	this.getBoxSize();
	this.shiftTo(0,0);
	
	
};


scrollWindow.prototype.getBoxSize = function() { 
//    this.maxX = (this.wd - this.window.offsetWidth > 0)? this.wd - this.window.offsetWidth: 0;
    this.maxX = this.window.offsetWidth;
    this.maxY = (this.layer.offsetHeight - this.window.offsetHeight > 0)? this.layer.offsetHeight - this.window.offsetHeight: 0;
}


scrollWindow.prototype.shiftTo = function(x,y) {

  if (this.layer) {
        this.layer.style.left = (this.x = x) + "px"; 
        this.layer.style.top = (this.y = y) + "px";
    }


};

scrollWindow.prototype.scroll = function(direction) {


	fx = (direction == 'left') ? 1:-1;


	var now = new Date().getTime();
    var d = (now - this.lastTime)/1000 * this.speed;
    if (d > 0) { 
	 
		this.lastTime = now;
		
		if (this.orientation == 'h') {
			var x = this.x + Math.round(fx * d);
			var y = this.y;
		} else {
			var x = this.x;
			var y = this.y + Math.round(fx * d);
		}
	
		
		
		if (this.orientation == 'h') {
			if (direction == 'left' && x > 0) return;
			if (direction == 'right' && (x*-1) > (this.layer.offsetWidth - this.window.offsetWidth)) return;
		} else {
			/*up*/if (direction == 'left' && y > 0) return;
			/*down*/if (direction == 'right' && (y*-1) > (this.layer.offsetHeight - this.window.offsetHeigth)) return;
		}
		 
		
		
		this.shiftTo(x, y);
	}
	
};


scrollWindow.prototype.scrollBySpeed = function() {
};

 
scrollWindow.prototype.onMouseOverTrigger = function(e, direction) {
	var self=this;
	this.lastTime = new Date().getTime();
	this.mouseScroll = window.setInterval(function() {self.scroll(direction);}, 20);
	
};

scrollWindow.prototype.onMouseOutTrigger = function(e, direction) {
	window.clearInterval(this.mouseScroll);
	this.mouseScroll=null;
};


// increase speed onmousedown of scroll links (for mouseover scrolling)
scrollWindow.prototype.doubleSpeed = function() {

	this.speed = this.speed * 4;
}

scrollWindow.prototype.resetSpeed = function() {

    this.speed =this.speed / 4;
}

/*
scrollWindow.prototype.addTrigger = function (triggerid, direction, autoscroll) {
	this.trigger.push({'triggerid':triggerid, 'direction':direction, 'autoscroll':autoscroll});
};
*/