//- 2009-12-07
//- http://www.andrewsellick.com/75/simple-3d-carousel-using-mootools

var Carousel =new Class ({
	count: 0,
	baseSpeed: 0.05, speed: 0.3,
	radiusX: 0, radiusY: 0, centerX: 0, centerY: 0,
	images: 0, carousel: 0,
	
	initialize: function (selector, parent, bUseMouse) {
		this.carousel =$(parent) ;
		this.images =this.carousel.getElements (selector) ; //- i.e. 'div.carousel'
		if ( this.images.length == 0 )
			return ; //- Stop here!
		this.images.each (function (elt) {
			elt.getElements ('img').each (function (img) {
				img.setStyles ({
					width: '100%', height: 'auto',
					border: '0px solid #FFFFFF',
					display: 'block'
				}) ;
			}) ;
		}) ;
	
		if ( $defined(bUseMouse) )
			this.carousel.addEvent ('mousemove', this.onMouseMove.bind (this)) ;
		this.start () ;
	},
	
	initSizes: function () {
		var box =this.carousel.getCoordinates () ;
		this.centerX =box.left + box.width / 2 - 100 ;
		this.centerY =box.top + box.height / 2 / 2 ;
		this.radiusX =0.4 * box.width ;
		this.radiusY =this.radiusX / 4.75 ;
		
		this.carousel.setStyle ('height', 5 * this.radiusY) ;
	},
	
	onMouseMove: function (evt) {
		var tempX =evt.client.x ;
		this.speed =(tempX - this.centerX) / 2500 ;
	},
	
	runCarousel: function () {
		this.initSizes () ;
		for ( i =0 ; i < this.images.length ; i++ ) {
			var angle =i * (Math.PI * 2) / this.images.length ;
			var posX =(Math.sin (this.count * (this.baseSpeed * this.speed) + angle) * this.radiusX + this.centerX) ;
			var posY =(Math.cos (this.count * (this.baseSpeed * this.speed) + angle) * this.radiusY + this.centerY) ;
			var width =posY / 3 ;
			var ZIndex =Math.round (width) + 100 ;
			this.images [i].setStyles ({
				'position': 'absolute', 'left': posX, 'top': posY,
				'width': width,
				'z-index': ZIndex
			}) ;
			if ( this.images [i].hasClass ('HideElement') )
				this.images [i].removeClass ('HideElement') ;
		}
		this.count++ ;
	},

	start: function () {
		this.anim =this.runCarousel.periodical (40, this) ;
	},
	
	stop: function () {
		$clear(this.anim) ;
		this.anim =null ;
	}

}) ;
