var Carousel = new Class( {
		initialize: function ( mainClass, imagesClass, linksClass, effectTime, setSize, maxOpacity, minOpacity ) {
			this.actImages = null;
			this.actScr = 0;
			
			$$( '.' + mainClass ).each( function ( obj ) {
				this.actImages = new imageBar( $E( '.' + imagesClass, obj ), effectTime, (setSize || 1), (maxOpacity || 1), (minOpacity || 0.5) );
				$ES( '.' + linksClass + ' a', obj ).each( function ( lobj, num ) {
					if( lobj.getProperty( 'rel' ) == 'screenshot' )
						lobj.onclick = this.actImages.showImage.bindAsEventListener( this.actImages, lobj.getProperty('href').replace(/.*#/, '') || num );
				}, this );
			}, this );
		}
	} );

	var imageBar = new Class( {
		initialize: function ( obj, effectTime, setSize, maxOpacity, minOpacity ) {
			this.effectTime = effectTime;
			this.setSize = setSize;
			this.maxOpacity = maxOpacity;
			this.minOpacity = minOpacity;
			this.elements = new Array();
			this.actLeft = 0;
			this.disabled = false;

			this.container = $(obj);
			this.container.setStyles( { position: 'absolute', overflow: 'hidden' } );
			this.bar = new Element( 'div' ).setStyles( { height: this.container.getSize().size.y + 'px', position: 'absolute', top: '0px', left: '0px', opacity: this.maxOpacity } );
			
			$A(this.container.childNodes).each( function ( obj ) {
				if( obj.nodeType == 1 )
				{
					obj = $(obj);
					obj.setProperty( 'align', 'left' );
					var size = obj.getSize().size.x + parseInt((obj.getStyle("margin-left") || "0").replace(/\D*/g, "")) + parseInt((obj.getStyle("margin-right") || "0").replace(/\D*/g, ""));
					this.actLeft += size;
					this.elements.push( {
						element: obj.injectInside( this.bar ),
						width: size,
						position: this.actLeft - size
					} );
				}
			}, this );
			
			this.bar.setStyle( 'width', this.actLeft + 'px' );
			this.bar.injectInside( this.container );
			
			this.actElement = 0;
			this.lastElement = null;
			this.effect = this.bar.effect( 'left', {
				duration: this.effectTime,
				transition: Fx.Transitions.quartInOut,
				onStart: function(){
								this.disabled = true;
								this.effectOpacity.start(this.maxOpacity, this.minOpacity);
				}.bind(this),
				onComplete: function(){	
								this.disabled = false;
								this.effectOpacity.start(this.minOpacity, this.maxOpacity);
				}.bind(this)
			} ); 
			this.effectOpacity = this.bar.effect( 'opacity', { duration: 300 } );
			this.actPosition = 0;
		},
		
		showImage: function ( event, target ) {
			if( target != this.actElement && !this.disabled)
			{
				this.lastElement = this.actElement;
				var actElement = this.actElement;
				if (target == '-')
				{
					actElement -= this.setSize;
				}
				else if (target == '+')
				{
					actElement += this.setSize;
				}
				else
				{
					actElement = target * this.setSize;
				}

				if (-1 < actElement && actElement < this.elements.length)
				{
					this.actElement = actElement;
					var delta = ( this.elements[this.actElement].position - this.elements[this.lastElement].position );
					this.effect.start( this.actPosition, this.actPosition - delta );
					this.actPosition = this.actPosition - delta;
				}
			}
			return false;
		}
	} );
