
Event.observe(window, 'load', function() {
	if ($('cover-flow')) {
		try {
			var coverFlow = new Spotlight('cover-flow');
		} catch (e) { }
	}
});


	Spotlight = Class.create({
		NODE : {},
		initialize : function( obj, options )
		{
			this.options = $H( {
				debug : false,
				intervalSpeed : 1000,
	//	minimum should be 3
				rotationSpeed : 4
			} ).merge( options || {} );

			this.cont = Builder.node( 'div', { 'class' : "ps-container" } );
			this.NODE.container = this.cont.cloneNode( false );

			this.slider = $(obj);
			this.spotlights = $$('#'+ obj +' > li');
			this.spotlights.each( function( spotlight )
			{
				Event.observe( spotlight, 'mouseover', this.pauseRotation.bind( this ), false );
				Event.observe( spotlight, 'mouseout', this.playRotation.bind( this ), false );
			}.bind( this ) );

			this.count = 0;
			this.currentSpotlight = this.spotlights[this.count];
			this.spotlightWidth = Element.getWidth( this.currentSpotlight );
			this.buildSliderConsole();

			this.rotateCovers();
			this.initPeriodicalRotation();

			Event.observe( window, 'unload', this.killPeriodical.bind( this ) );
		},
		buildSliderConsole : function()
		{
			this.slider.parentNode.insertBefore( this.NODE.container, this.slider );
			this.NODE.container.appendChild( this.slider );
		},

		initPeriodicalRotation : function()
		{
			this.CoversRotator = new PeriodicalExecuter( this.rotateCovers.bind( this ), this.options.get('rotationSpeed') );

			this.rotateCovers();
		},

		killPeriodical : function()
		{
			this.CoversRotator.stop();
			delete this.CoversRotator;
		},

		pauseRotation : function()
		{
			this.CoversRotator.stop()
		},

		playRotation : function()
		{
			this.initPeriodicalRotation();
		},

		reset : function()
		{
			this.count = 0;
			this.currentSpotlight = this.spotlights[this.count];
			this.rotateCovers();
		},

		rotateCovers : function()
		{
			if ( !this.rotating )
			{
				this.rotating = true;

				if ( this.previousSpotlight )
				{
					new Effect.MoveBy(
						this.previousSpotlight,
						0,
						-this.spotlightWidth,
						{
							afterFinish : function()
							{
								try {
									this.previousSpotlight.style.left = this.spotlightWidth + 'px';
								} catch ( e ) { }
							}.bind( this )
						}
					);
				}

				if ( this.currentSpotlight ) {
					new Effect.MoveBy(
						this.currentSpotlight,
						0,
						-this.spotlightWidth,
						{
							afterFinish : function()
							{
								try {
									this.count++;
									this.previousSpotlight = this.currentSpotlight;

									if ( this.count >= this.spotlights.length )
										this.count = 0;

									this.currentSpotlight = this.spotlights[this.count];
									this.rotating = false;
								} catch ( e ) { }
							}.bind( this )
						}
					);
				}
				else this.reset();
			}
		}
	});

