var Conleys_BrandsScroller = Class.create();
Conleys_BrandsScroller.prototype = {
	spacerPX: 16,			// distance in px
	checkIntervall: 100,	// interval in ms
	aniPixelPerSecond: 50,	// distance, pixel per second -- change this for more or less speed
	framesPerSecond: 100,	// animation FPS
	animationActive: undefined,
	currentEffect: false,
	currentEffect2: false,
	containerWidth: 1,

	initialize: function() {
		var cObject = $('con_brands_scroller');
		if(cObject)
		{
			var cObjectDown = cObject.down();

			// fix width of container
			this.fixContainerWidth();
			
			// random position at start
			randomPos = Math.floor(Math.random()*(this.containerWidth-50))+1;
			cObjectDown.setStyle({width : this.containerWidth+'px', left : (-1*randomPos)+'px'});
			
			// copy container1 data c2
			var cObjectDownNext = cObjectDown.next();
			cObjectDownNext.setStyle({width : this.containerWidth+'px', left : ((this.containerWidth-(randomPos))+(this.spacerPX*1))+'px'});
			cObjectDownNext.innerHTML = cObjectDown.innerHTML;

			// register the mouse observer
			cObject.observe('mouseover', (function(event){
				this.animationActive = false;
				if(this.currentEffect) { 
					this.currentEffect.cancel();
					this.currentEffect = false;
				}
				if(this.currentEffect2) {
					this.currentEffect2.cancel();
					this.currentEffect2 = false;
				}
			}).bind(this));
			cObject.observe('mouseout', (function(event){
				this.animationActive = true;
			}).bind(this));

			// start timer
			this.animationActive = true;
			(function(){ this.timer(); }).bind(this).delay((0.5));
		}
	},
	
	fixContainerWidth: function() {
		var cObject = $('con_brands_scroller');
		if(cObject)
		{
			// fix width of container
			this.containerWidth = 0;
			var cObjectDown = cObject.down();
			var cOChilds = cObjectDown.select('div[class^=cbs_elem]');
			for(i=0; i<cOChilds.length; i++) {
				this.containerWidth += cOChilds[i].getWidth();
			}
			cObjectDown.setStyle({width : this.containerWidth+'px'});
		}
	},
	
	animation: function() {
		var cObject = $('con_brands_scroller').down();
		if(cObject) {
			cObjOffsetLeft = cObject.getStyle('left');
			cObjOffsetLeft = cObjOffsetLeft.substring(0, cObjOffsetLeft.indexOf('px'));
			cObjOffsetLeft2 = cObject.next().getStyle('left');
			cObjOffsetLeft2 = cObjOffsetLeft2.substring(0, cObjOffsetLeft2.indexOf('px'));
			
			if(cObjOffsetLeft<(-1*this.containerWidth))
			{
				this.currentEffect.cancel();
				this.currentEffect = false;
				newOffsetOfObj = (cObjOffsetLeft2*1) + (this.containerWidth*1) + (this.spacerPX*1);
				cObject.setStyle({left : newOffsetOfObj+'px'});
			}
			if(cObjOffsetLeft2<(-1*this.containerWidth))
			{
				this.currentEffect2.cancel();
				this.currentEffect2 = false;
				newOffsetOfObj = (cObjOffsetLeft*1) + (this.containerWidth*1) + (this.spacerPX*1);
				cObject.next().setStyle({left : newOffsetOfObj+'px'});
			}
			
			cObjOffsetLeft = cObject.getStyle('left');
			cObjOffsetLeft = cObjOffsetLeft.substring(0, cObjOffsetLeft.indexOf('px'));
			cObjOffsetLeft2 = cObject.next().getStyle('left');
			cObjOffsetLeft2 = cObjOffsetLeft2.substring(0, cObjOffsetLeft2.indexOf('px'));
			
			obj1_duration = ((this.containerWidth*1) + (cObjOffsetLeft*1) + (this.spacerPX*1)) / this.aniPixelPerSecond;
			obj1_distance = ((this.containerWidth*1) + (cObjOffsetLeft*1) + (this.spacerPX*1));

			obj2_duration = ((this.containerWidth*1) + (cObjOffsetLeft2*1) + (this.spacerPX*1)) / this.aniPixelPerSecond;
			obj2_distance = ((this.containerWidth*1) + (cObjOffsetLeft2*1) + (this.spacerPX*1));
			
			if(this.currentEffect===false) {
				this.currentEffect = new Effect.Move(cObject, { 
						x: -obj1_distance, y: 0, mode: 'relative',
						duration: obj1_duration,
						fps: this.framesPerSecond,
						transition: Effect.Transitions.linear
					});
			}

			if(this.currentEffect2===false) {
				this.currentEffect2 = new Effect.Move(cObject.next(), { 
						x: -obj2_distance, y: 0, mode: 'relative',
						duration: obj2_duration,
						fps: this.framesPerSecond,
						transition: Effect.Transitions.linear
					});
			}
			
		}
	},
	
	timer: function() {
		if(this.containerWidth<=1) {
			this.fixContainerWidth();
		}
		
		if(this.animationActive) {
			this.animation();
		}
		
		(function(){ this.timer(); }).bind(this).delay((this.checkIntervall/1000));
	}
	
}
document.observe('dom:loaded', function () { var myConleys_BrandsScroller = new Conleys_BrandsScroller(); });
