var ImageSlider = {
	create : function(elementId, options) {
		return new ImageSlider.Box(elementId, options);
	}
}
	
ImageSlider.Box = Class.create({
	
    initialize: function(elementId) {
	
        this.elementId = elementId;
        this.currentAutoscrollTargetXValue = 0;
		this.element = $(elementId);
		this.options = Object.extend({
			startImage: 0,
			imageCount: 1,
			imageWidth: 100,
			imageMargin: 10,
			visibleImageCount: 1,
			autoScroll: false,
			onClickFunction: function(img){ }
		}, arguments[1] || {});
		
		this.options.minOffset = (this.options.imageWidth + this.options.imageMargin) * this.options.imageCount - (this.options.imageWidth + this.options.imageMargin);
        this.options.autoScrollDuration = this.options.imageCount * 2;
        this.options.resetDuration = this.options.imageCount / 2;	
        this.options.windowWidth = (this.options.imageWidth + this.options.imageMargin) * this.options.visibleImageCount;
        this.options.initialOffset = -(this.options.imageWidth + this.options.imageMargin) * this.options.startImage + (this.options.windowWidth - this.options.imageWidth - this.options.imageMargin) / 2 ;
        this.options.maxX = (this.options.windowWidth - (this.options.imageWidth + this.options.imageMargin)) / 2;
        this.options.minX = -this.options.minOffset + ((this.options.imageWidth + this.options.imageMargin) - this.options.imageWidth) / 2;
        
        this.element.parentNode.style.width=this.options.windowWidth + 60 + "px";
        this.element.style.left="0px";

        //jump, do not scroll (0.0)
	 	this.scroll(this.options.initialOffset, 0.0);

	 	if (this.options.autoScroll) {
	 		this.scrollSlowly();
	 	}
   	},
   	
   	movePrev: function()
	{	
  		this.stopEffects();
  		this.scroll(this.options.imageWidth + this.options.imageMargin, 0.4);
       // if (this.options.autoScroll) { this.scrollSlowly(); };
        
	},
	
	moveNext: function()
	{	
  		this.stopEffects();
  		this.scroll(-(this.options.imageWidth + this.options.imageMargin), 0.4);
     //   if (this.options.autoScroll) { this.scrollSlowly(); };
        
	},
	
	centerImage: function(img) {
  		var offset = 0 - img.offsetLeft + ((this.options.windowWidth - (this.options.imageWidth + this.options.imageMargin)) / 2);
  		this.offsetAfterCentering = offset;
  		new Effect.Move(this.elementId, { x: offset, y: 0, duration:0.8, mode:"absolute", transition: Effect.Transitions.spring, queue:{position:"end", scope:this.elementId } });
        this.options.onClickFunction(img);
        if (this.options.autoScroll) {
    //    	this.scrollSlowly();
        }
	},
	
	scroll: function(offset, relDuration) {
		var currPos = parseInt(this.element.style.left);
  		var offset = offset - (currPos % (this.options.imageWidth + this.options.imageMargin));
  		if ((currPos + offset) > this.options.maxX) {
  	  		new Effect.Move(this.elementId, { x: this.options.minX, y: 0, duration:1.0, mode:"absolute",  transition: Effect.Transitions.sinoidal, queue:{position:"end", scope:this.elementId }});
  		} else if ((currPos + offset) < this.options.minX) {
  	  		new Effect.Move(this.elementId, { x: this.options.maxX, y: 0, duration:1.0, mode:"absolute",  transition: Effect.Transitions.sinoidal, queue:{position:"end", scope:this.elementId }});
  		} else {
  			var dura = (relDuration / (this.options.imageWidth + this.options.imageMargin) * (offset < 0 ? offset * -1 : offset));
  			new Effect.Move(this.elementId, { x: offset, y: 0, duration:dura, mode:"relative",  transition: Effect.Transitions.sinoidal, queue:{position:"end", scope:this.elementId } });
  		}
  		
  		//hide and show arrows before reaching edge
  		if (this.options.imageCount <=1) {
  			return;
  		}
  		var offsetAfterScrolling = offset * 2;
  		var moveLeft = offsetAfterScrolling > currPos;
  		
  		if ( (currPos + offset) >=0 ) {
  			var leftArrowElem = $("sliderleft-arrow");
  			leftArrowElem.hide();
  		} else {
  			var leftArrowElem = $("sliderleft-arrow");
  			leftArrowElem.show();
  		}
  		if ((currPos + offset) <= (this.options.visibleImageCount - this.options.imageCount) * (this.options.imageWidth + this.options.imageMargin) ) {
  			$("sliderright-arrow").hide();
  		} else {
  			$("sliderright-arrow").show();
  		}
	},
	
	setArrowVisibility: function (offset) {
		var currPos = parseInt(this.element.style.left);
  		var offset = offset - (currPos % (this.options.imageWidth + this.options.imageMargin));
  	//hide and show arrows before reaching edge
  		if (this.options.imageCount <=1) {
  			return;
  		}
  		var offsetAfterScrolling = offset * 2;
  		var moveLeft = offsetAfterScrolling > currPos;
  		
  		if ((currPos + offset) == 2 * (this.options.imageWidth + this.options.imageMargin)) {
  			var leftArrowElem = $("sliderleft-arrow");
  			leftArrowElem.hide();
  		} else {
  			var leftArrowElem = $("sliderleft-arrow");
  			leftArrowElem.show();
  		}
  		if ((currPos + offset) == (this.options.visibleImageCount -2 - this.options.imageCount) * (this.options.imageWidth + this.options.imageMargin)) {
  			$("sliderright-arrow").hide();
  		} else {
  			$("sliderright-arrow").show();
  		}
	},

	stopEffects: function () {
		var queue = Effect.Queues.get(this.elementId);
		queue.each(function(effect) { effect.cancel(); });
	},
	
	scrollSlowly: function() {
		var xOffset;
		var durationSeconds = 10;//(this.options.autoScrollDuration / this.options.minOffset) * (this.options.minOffset + parseInt(this.element.style.left));
		var delaySeconds = 20;
		
			//moving slider from startposition to left
			var minimumX = (this.options.visibleImageCount - this.options.imageCount) * (this.options.imageWidth + this.options.imageMargin);
			var startX =  this.options.initialOffset - (this.options.imageWidth + this.options.imageMargin);
			var delaySeconds = 10;
			for (var xOffset = startX; xOffset >= minimumX; xOffset -= (this.options.imageWidth + this.options.imageMargin)) {
				new Effect.Move(this.elementId, {delay:delaySeconds, x: xOffset, y: 0, duration:durationSeconds, mode:"absolute",  transition: Effect.Transitions.sinoidal, queue:{position:"end", scope:this.elementId},  afterFinish:function(){} });
				delaySeconds = 5;
			}
			
			//only one loop instead of 10
			for(var i = 0; i<1;i++) {
				//moving slider from left to right
				for (xOffset=minimumX + (this.options.imageWidth + this.options.imageMargin); xOffset<=0; xOffset += (this.options.imageWidth + this.options.imageMargin)) {
					new Effect.Move(this.elementId, {delay:delaySeconds, x: xOffset, y: 0, duration:durationSeconds, mode:"absolute",  transition: Effect.Transitions.sinoidal, queue:{position:"end", scope:this.elementId},  afterFinish:function(){} });
				}
				
				//moving slider from right to left
				for (xOffset=0 - (this.options.imageWidth + this.options.imageMargin); xOffset>=minimumX; xOffset -= (this.options.imageWidth + this.options.imageMargin)) {
					new Effect.Move(this.elementId, {delay:delaySeconds, x: xOffset, y: 0, duration:durationSeconds, mode:"absolute",  transition: Effect.Transitions.sinoidal, queue:{position:"end", scope:this.elementId},  afterFinish:function(){} });
				}
			}	
	}
});