/**
 * Function to Slide Elements in a Container-Element when the cursor aim the left or right pointer/arrow
 * @param slideContainer, slideElement, slideElChildEls, slideActorLeft, slideActorRight
 * @author Marcel Fiedrich
 */
	
var loeweElementSlider = function(slideContainer, slideElement, slideElChildEls, slideActorLeft, slideActorRight) {
	
	slideActorLeft.setStyle('visibility', 'visible');
	slideActorRight.setStyle('visibility', 'visible');
	var loeweFxOut = new Fx.Tween(slideElement, {
		fps: 300,
		unit: 'px',
		link: 'ignore',
		duration: 500,
		transition: 'linear'
	});
	
	var loeweFx = new Fx.Tween(slideElement, {
		fps: 300,
		unit: 'px',
		link: 'chain',
		duration: 250,
		transition: 'linear'
	});
	
	//get the maximum position for the slide element
	var getMaxPosition = function(childTag) {
		
		var containerWidth = slideContainer.getStyle('width').toInt();
		
		var slideElChilds = slideElement.getChildren(childTag);
		var allChildWidth = 0;
		slideElChilds.each(function(childEl, index) {
			allChildWidth = allChildWidth+childEl.getStyle('width').toInt();
		});
		var maxPosExtend = slideElement.getFirst(childTag).getStyle('width').toInt()/3;
		var maxSlidePosition = allChildWidth-containerWidth+maxPosExtend;
		
		return -maxSlidePosition;
	}
	
	//declare some variables for the tween function
	var tweenPeriodical = null;
	var bindFx = null;
	var stepNumFx = 125;
	//get the first and the last child of the slider element for the smooth tween-out.
	var firstChildWidth = slideElement.getFirst(slideElChildEls).getStyle('width').toInt();
	var lastChildWidth = slideElement.getLast(slideElChildEls).getStyle('width').toInt();
	//create the variable for the min- and max position value.
	//add and subtract the first and last childs width from the values for the tween-out.
	var minPosFx = slideElement.getStyle('left').toInt()-firstChildWidth;
	var maxPosFx = getMaxPosition(slideElChildEls)+lastChildWidth;
	//console.log(minPosFx);
	//console.log(maxPosFx);
	var maxDiff = lastChildWidth/2;
	var minDiff = firstChildWidth/2;
	var arrowPeriod;
	var arrowHide = function() {
		var actPosition = slideElement.getStyle('left').toInt();
		//the third if-construct is for checking the IE-Browser... o.O Burn IE Burn!!! 
		if(actPosition <= maxPosFx-maxDiff) {
			if(slideActorRight.getStyle('opacity').toInt() != 0)
				if(Browser.Engine.trident) {
					slideActorRight.setStyle('opacity', '0')
				} else {
					slideActorRight.tween('opacity', '0');
				}
		} else if(actPosition > maxPosFx-maxDiff) {
			if(slideActorRight.getStyle('opacity').toInt() == 0)
				if(Browser.Engine.trident) {
					slideActorRight.setStyle('opacity', '1.0')
				} else {
					slideActorRight.tween('opacity', '1.0');
				}
		}
		if(actPosition >= minPosFx+minDiff) {
			if(slideActorLeft.getStyle('opacity').toInt() != 0)
				if(Browser.Engine.trident) {
					slideActorLeft.setStyle('opacity', '0')
				} else {
					slideActorLeft.tween('opacity', '0');
				}
		} else if(actPosition < minPosFx+minDiff) {
			if(slideActorLeft.getStyle('opacity').toInt() == 0)
				if(Browser.Engine.trident) {
					slideActorLeft.setStyle('opacity', '1.0')
				} else {
					slideActorLeft.tween('opacity', '1.0');
				}
		}
	}
	arrowPeriod = arrowHide.periodical(750, slideElement);
	//This function starts the tween in every period
	var periodicalFx = function(counterFx, maxPos, eventElement, parallelElement){
		var actPos = slideElement.getStyle('left').toInt();
		var newPos = this.pos+counterFx;
		
		
		if(eval(counterFx+stepNumFx)==0 && actPos <= maxPos) {
			loeweFx.cancel();
			loeweFxOut.start('left', maxPos-lastChildWidth+18);
			$clear(tweenPeriodical);
		} else if(eval(counterFx+stepNumFx)==stepNumFx*2 && actPos >= maxPos) {
			loeweFx.cancel();
			loeweFxOut.start('left', maxPos+firstChildWidth);
			$clear(tweenPeriodical);
		} else {
			loeweFx.start('left', [this.pos, newPos]);
			this.pos = newPos;
		}
		
	};
	
	//The events for the left button thats starts tweening
	slideActorLeft.addEvents({
		'mouseenter': function(e) {
			bindFx = {pos: slideElement.getStyle('left').toInt()};
			tweenPeriodical = periodicalFx.periodical(100, bindFx, [stepNumFx, minPosFx, this, slideActorRight]);
			this.set('class', 'actRoll');
		},
		'mouseleave': function(e) {
			loeweFx.cancel();
			this.removeEvent('mouseenter');
			$clear(tweenPeriodical);
			bindFx = null;
			this.removeClass('actRoll');
		}
	});
	
	//The events for the right button thats starts tweening
	slideActorRight.addEvents({
		'mouseenter': function(e) {
			bindFx = {pos: slideElement.getStyle('left').toInt()};
			tweenPeriodical = periodicalFx.periodical(100, bindFx, [-stepNumFx, maxPosFx, this, slideActorLeft]);
			this.set('class', 'actRoll');
		},
		'mouseleave': function(e) {
			loeweFx.cancel();
			this.removeEvent('mouseenter');
			$clear(tweenPeriodical);
			bindFx = null;
			this.removeClass('actRoll');
		}
	});
}