function LGHorisontalDragScrollController(owner){
	this.owner = owner;
	this.ihs = this.owner.imageHolders; //ezt a paramétert rövidítem mert igen sokszor elő fog fordulni
	this.xPadding = 2;
	this.IMGBuffersize = 1;
	this.firtsInRow = null;
	this.lastInRow = null;
	this.nextLoadable = 0;
	this.highLightColor = "#666666";
	this.normalColor = "#999999";
	this.fontSize = '0.7em';
	
	this.navigationBar = null;
	
	this.moveAllImageHolder = function(x){
		for (n=0;n<this.ihs.length;n++){
			if (this.ihs[n].ready){
				this.ihs[n].setPositionX(this.ihs[n].getPositionX() + x);
			}
		}
	}
	
	this.preTime = new Date();
	
	this.preX = 0;
	this.impulseX = 0;
	this.opposition = 0.006;
	this.spiralForce = 0.00005;
	this.loopInterval = 15;
	this.toCenterImpulseLimit = 0.2;
	this.leftImpact = false;
	this.rightImpact = false;
	this.centerIH = null;
	this.keepCenterIH = false;
	
	this.freeRunEnabled = false;
	var _this = this;
	this.timer = setInterval(function() {_this.freeRunLoop()},this.loopInterval);
	
	this.freeRunLoop = function(){
		if (this.firstInRow != null){
			if (this.freeRunEnabled){
				var actTime = new Date();
				if ( (actTime - this.preTime) != 0){
					
					var rightSpiralF = 0;
					var leftSpiralF = 0;
					
					if (this.centerIH != null){
					
						if ((this.centerIH != null) && this.leftImpact && !this.rightImpact && (this.centerIH.getPositionX() < ((this.owner.getWidth()-this.centerIH.getWidth())/2)) ){
							this.impulseX = 0;
							moveDist = - (this.centerIH.getPositionX() - ((this.owner.getWidth()-this.centerIH.getWidth())/2) );
							this.moveAllImageHolder(moveDist);
							this.leftImpact = false;
							this.keepCenterIH = false;
							//debug("left");
							this.centerIH = null;
						}
						
						if ( (this.centerIH != null) && !this.rightImpact && (this.centerIH.getPositionX() > ((this.owner.getWidth()-this.centerIH.getWidth())/2)) ) {
							leftSpiralF = (((this.owner.getWidth()-this.centerIH.getWidth())/2) - this.centerIH.getPositionX()) * this.spiralForce;
							this.leftImpact= true;
						}
						
						if ( (this.centerIH != null) && !this.leftImpact && (this.rightImpact && (this.centerIH.getPositionX() > ((this.owner.getWidth()-this.centerIH.getWidth())/2)) ) ){
							this.impulseX = 0;
							moveDist = ( ((this.owner.getWidth()-this.centerIH.getWidth())/2) - this.centerIH.getPositionX());
							this.moveAllImageHolder(moveDist);
							this.rightImpact = false;
							this.keepCenterIH = false;
							//debug("right");
							this.centerIH = null;
						}
						
						if ( (this.centerIH != null) && !this.leftImpact && ( this.centerIH.getPositionX() < ((this.owner.getWidth()-this.centerIH.getWidth())/2) ) ) {
							rightSpiralF = (((this.owner.getWidth()-this.centerIH.getWidth())/2) - this.centerIH.getPositionX()) * this.spiralForce;
							this.rightImpact= true;
						}
						
					} else {
						if (this.firstInRow.getPositionX() > ((this.owner.getWidth()-this.firstInRow.getWidth())/2) ) {
							this.centerIH = this.firstInRow;
						}
						if (this.lastInRow.getPositionX() < ((this.owner.getWidth()-this.lastInRow.getWidth())/2) ) {
							this.centerIH = this.lastInRow;
						}
					}
					
					moveDist = this.impulseX * (actTime - this.preTime);
					if ( (Math.abs(moveDist) > 0.2) || ( this.rightImpact || this.leftImpact ) ){
						this.moveAllImageHolder(moveDist);
						this.impulseX =  this.impulseX - this.impulseX*this.opposition*(actTime - this.preTime) + (leftSpiralF+rightSpiralF)*this.loopInterval;//(actTime - this.preTime);
					} else {
						this.freeRunEnabled = false;
					}
					this.preTime = actTime;
				}
			}
		}
	}
	
	this.freeRunStart = function(event){
		if (!this.freeRunEnabled){
			this.preTime = new Date();
			this.freeRunEnabled = true;
			this.moveAllImageHolder(0);
		}
	}
	
	this.loadNextImage = function(){
		if (this.nextLoadable < this.ihs.length){
			this.ihs[this.nextLoadable].loadIMG();
			this.nextLoadable++;
		}
	}
	
	this.receveMessage = function(eventDescr){
		switch (eventDescr.message) {
			case "mousedown":
				eventDescr.sender.HTMLElement.style.cursor = "e-resize";
				this.preX = eventDescr.event.clientX;
				this.freeRunEnabled = false;
				this.impulseX = 0;
				this.preTime = new Date();
				this.centerIH = null;
				this.leftImpact = false;
				this.rightImpact = false;
			break;
			
			case "mousedrag":
				var actTime = new Date();
				if ((actTime - this.preTime) != 0){
					var Xdif = eventDescr.event.clientX - this.preX;
					this.moveAllImageHolder(Xdif);
					this.impulseX = (eventDescr.event.clientX - this.preX)/(actTime - this.preTime);
					this.preX = eventDescr.event.clientX;
					this.preTime = actTime;
				}
			break;
			
			case "mouseup":
				eventDescr.sender.HTMLElement.style.cursor = "pointer";
				this.freeRunStart();
			break;
			
			case "imgLoaded":
				//sender megkeresése a ihs tömbben
				var n=0;
				var lastXend = this.xPadding;
				//var lastXend = this.owner.getWidth();
				var be = false;
				while (n<this.ihs.length && this.ihs[n] != eventDescr.sender){
					if (this.ihs[n].ready){
						lastXend = this.ihs[n].getPositionX() + this.ihs[n].getWidth() + this.xPadding;
						be = true;
					}
					n++;
				}
				//hely adása
				eventDescr.sender.ready = true;
				eventDescr.sender.setRelativeBackgroundPositionY(25);
				eventDescr.sender.HTMLElement.style.height = eventDescr.sender.getHeight()+25+"px";
				eventDescr.sender.HTMLElement.style.cursor = "pointer";
				var ihTitleWidth = 20;
				var ihTitle = document.createElement('div');
				ihTitle.innerHTML = eventDescr.sender.title;
				ihTitle.style.width = ihTitleWidth +'px';
				ihTitle.style.textAlign = "center";
				ihTitle.style.paddingTop = "3px";
				ihTitle.style.position = "absolute";
				ihTitle.style.fontFamily = "Sans-serif";
				ihTitle.style.fontWeight = "900";
				ihTitle.style.color = this.highLightColor;
				ihTitle.style.fontSize = "0.7em";
				eventDescr.sender.addChildNode(ihTitle,(eventDescr.sender.getWidth()-ihTitleWidth)/2,0);
				
				eventDescr.sender.setPositionX(lastXend + this.xPadding);
				if (!be) { this.firstInRow = eventDescr.sender; };
				
				//ami mögötte van az összes hátrébb tolása
				be = false;
				var xMove = eventDescr.sender.getWidth() + this.xPadding;
				n++;
				while (n<this.ihs.length){
					if (this.ihs[n].ready){
						this.ihs[n].setPositionX(this.ihs[n].getPositionX() + xMove);
						be = true;
					}
					n++;
				}
				if (!be) { this.lastInRow = eventDescr.sender; };
				
				//köetkező betöltés indítása (ha van még mit)
				setTimeout(function(){_this.loadNextImage()}, 100);
				
			break;
			
			case "inCenter":
				if (!this.keepCenterIH){
					if(this.freeRunEnabled && (Math.abs(this.impulseX) < this.toCenterImpulseLimit)){
						this.centerIH = eventDescr.sender;
					}
					if (this.navigationBar){
						this.navigationBar.setActualImageHolder(eventDescr.sender);
					}
				}
			break;
			
			case "dbclick":
				//alert("dbclick");
			break;
			
			case "mouseout":
				eventDescr.sender.HTMLElement.style.cursor = "pointer";
			break;
				
		}
	}
	
	this.bringToCenter = function(ih){
		this.freeRunStart();
		this.keepCenterIH = true;
		this.centerIH = ih;
	}
	
	//kezelő
	this.drawTools = function() {
		var inPadding = 0;
		var color = this.highLightColor;
		var lineHeight = "1px";
		
		var titleD = document.createElement('div');;
		titleD.style.width = (this.owner.getWidth()-2*inPadding)+"px";
		titleD.style.height = lineHeight;
		titleD.style.color = color;
		titleD.style.position = "absolute";
		titleD.style.top = (this.owner.getHeight() - 45)+"px";
		titleD.style.left = (inPadding+10)+"px";
		titleD.style.fontFamily = "Sans-serif";
		titleD.style.fontWeight = "900";
		titleD.style.fontSize = "0.8em";
		titleD.innerHTML = owner.title;
		this.owner.frame.appendChild(titleD);
		
		color = this.normalColor;
		
		var lineOne = document.createElement('div');;
		lineOne.style.width = (this.owner.getWidth()-2*inPadding)+"px";
		lineOne.style.height = lineHeight;
		lineOne.style.backgroundColor = color;
		lineOne.style.position = "absolute";
		lineOne.style.top = (this.owner.getHeight() - 20)+"px";
		lineOne.style.left = inPadding+"px";
		this.owner.frame.appendChild(lineOne);
		
		var lineTwo = document.createElement('div');
		lineTwo.style.width = (this.owner.getWidth()-2*inPadding)+"px";
		lineTwo.style.height = lineHeight;
		lineTwo.style.backgroundColor = color;
		lineTwo.style.position = "absolute";
		lineTwo.style.top = (this.owner.getHeight() - 2)+"px";
		lineTwo.style.left = inPadding+"px";
		this.owner.frame.appendChild(lineTwo);
		
		this.navigationBar.HTMLElement.style.width = (this.owner.getWidth()-2*inPadding)+"px";
		this.navigationBar.HTMLElement.style.position= "absolute";
		this.navigationBar.HTMLElement.style.top = (this.owner.getHeight() - 20)+"px";
		this.navigationBar.HTMLElement.style.height = 25+"px";
		this.owner.frame.appendChild(this.navigationBar.HTMLElement);
	}
	
	this.start = function(){
		this.navigationBar = new LGNavigationBar(this.owner,this);
		this.drawTools();
		var n = 0;
		while (n<this.ihs.length && n<this.IMGBuffersize){
			this.ihs[n].loadIMG();
			n++;
		}
		this.nextLoadable = n;
	}
}
