/**
 * Interface Elements for jQuery
 * 3D Carousel
 *
 * http://interface.eyecon.ro
 *
 * Copyright (c) 2006 Stefan Petre
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * 20090706 Modified by Phil Smith <phil@profitmaster.co.uk> to implement slowOnHover
 * option as documented at http://interface.eyecon.ro/docs/carousel
 * 
 * 20090707 Modified by Phil Smith <phil@profitmaster.co.uk> to remove reflection code
 * and correct vertical scaling/positioning when itemMinWidth is different from itemWidth
 *
 */
/**
 * Created a 3D Carousel from a list of images, with reflections and animated by mouse position
 *
 * @example window.onload =
 *			function()
 *			{
 *				$('#carousel').Carousel(
 *					{
 *						itemWidth: 110,
 *						itemHeight: 62,
 *						itemMinWidth: 50,
 *						items: 'a',
 *						reflections: .5,
 *						rotationSpeed: 1.8
 *					}
 *				);
 *			}
 * HTML
 *			<div id="carousel">
 *				<a href="" title=""><img src="" width="100%" /></a>
 *				<a href="" title=""><img src="" width="100%" /></a>
 *				<a href="" title=""><img src="" width="100%" /></a>
 *				<a href="" title=""><img src="" width="100%" /></a>
 *				<a href="" title=""><img src="" width="100%" /></a>
 *			</div>
 * CSS
 *			#carousel
 *			{
 *				width: 700px;
 *				height: 150px;
 *				background-color: #111;
 *				position: absolute;
 *				top: 200px;
 *				left: 100px;
 *			}
 *			#carousel a
 *			{
 *				position: absolute;
 *				width: 110px;
 *			}
 *
 * @desc Creates a 3D carousel from all images inside div tag with id 'carousel'
 *
 *
 * @name 3D Carousel
 * @description Created a 3D Carousel from a list of images, with reflections and animated by mouse position
 * @param Hash hash A hash of parameters
 * @option String items items selection
 * @option Integer itemWidth the max width for each item
 * @option Integer itemHeight the max height for each item
 * @option Integer itemMinWidth the minimum width for each item, the height is automaticaly calculated to keep proportions
 * @option Float rotationSpeed the speed for rotation animation
 * @option Float reflectionSize the reflection size a fraction from items' height
 *
 * @type jQuery
 * @cat Plugins/Interface
 * @author Stefan Petre
 */
jQuery.iCarousel = {
	maxRotation : 2*Math.PI,
	
	build : function(options)
	{
		return this.each(
			function()
			{
				var el = this;
				var increment = 2*Math.PI/360;
				if(jQuery(el).css('position') != 'relative' && jQuery(el).css('position') != 'absolute') {
					jQuery(el).css('position', 'relative');
				}
				el.carouselCfg = {
					items : jQuery(options.items, this),
					itemWidth : options.itemWidth,
					itemHeight : options.itemHeight,
					itemMinWidth : options.itemMinWidth,
					itemMinHeight : options.itemHeight * (options.itemMinWidth / options.itemWidth),
					size : jQuery.iUtil.getSize(this),
					position : jQuery.iUtil.getPosition(this),
					start : Math.PI/2,
					rotationSpeed : options.rotationSpeed,
					protectRotation : false,
					paused : false,
					increment: 2*Math.PI/360,
					slowOnHover: options.slowOnHover
				};
				el.carouselCfg.radiusX = (el.carouselCfg.size.w - el.carouselCfg.itemWidth)/2;
				el.carouselCfg.radiusY = (el.carouselCfg.size.h - (el.carouselCfg.itemHeight / 2) - (el.carouselCfg.itemMinHeight / 2)) / 2; 
				el.carouselCfg.step =  2*Math.PI/el.carouselCfg.items.size();
				el.carouselCfg.paddingX = el.carouselCfg.size.w/2;
				el.carouselCfg.paddingY = el.carouselCfg.radiusY + (el.carouselCfg.itemMinHeight / 2);
				el.carouselCfg.items
					.bind(
						'mouseover',
						function(e)
						{
							if (el.carouselCfg.slowOnHover) {
								/* slow rotation and protect from mouse movement until mouseout */
								el.carouselCfg.protectRotation = true;
								el.carouselCfg.speed = el.carouselCfg.increment*0.1 * el.carouselCfg.speed / Math.abs(el.carouselCfg.speed);
							} else {
								/* set rotation speed according to mouse position */
								pointer = jQuery.iUtil.getPointer(e);
								mousex =  el.carouselCfg.size.w - pointer.x + el.carouselCfg.position.x;
								el.carouselCfg.speed = el.carouselCfg.rotationSpeed * el.carouselCfg.increment * (el.carouselCfg.size.w/2 - mousex) / (el.carouselCfg.size.w/2);
							}
							return false;
						}
					)
					.bind(
						'mouseout',
						function(e)
						{
							/* remove protection of rotation speed from mouse movement */
							el.carouselCfg.protectRotation = false;

							/* set rotation speed according to mouse position */
							pointer = jQuery.iUtil.getPointer(e);
							mousex =  el.carouselCfg.size.w - pointer.x + el.carouselCfg.position.x;
							el.carouselCfg.speed = el.carouselCfg.rotationSpeed * el.carouselCfg.increment * (el.carouselCfg.size.w/2 - mousex) / (el.carouselCfg.size.w/2);

							return false;
						}
					);
				/* set initial speed to half maximum */
				el.carouselCfg.speed = el.carouselCfg.rotationSpeed * el.carouselCfg.increment * .5;
				/* start the carousel */
				jQuery.iCarousel.start(el);
				jQuery(el)
					.bind(
						'mouseout',
						function()
						{
							/* set speed to half maximum, in current direction */
							if (el.carouselCfg.speed != 0) {  // avoid division by zero
								el.carouselCfg.speed = (el.carouselCfg.rotationSpeed * el.carouselCfg.increment* .5) * el.carouselCfg.speed / Math.abs(el.carouselCfg.speed);
							} else {
								el.carouselCfg.speed = (el.carouselCfg.rotationSpeed * el.carouselCfg.increment* .5);
							}
						}
					)
					.bind(
						'mousemove',
						function(e)
						{
							if (el.carouselCfg.protectRotation == false) {
								pointer = jQuery.iUtil.getPointer(e);
								mousex =  el.carouselCfg.size.w - pointer.x + el.carouselCfg.position.x;
								el.carouselCfg.speed = el.carouselCfg.rotationSpeed * el.carouselCfg.increment * (el.carouselCfg.size.w/2 - mousex) / (el.carouselCfg.size.w/2);
							}
						}
					);
				/* pause the carousel on any click on the page to save the processor */
				jQuery('body').bind('click', function(e) {
					jQuery.iCarousel.pause(el);
					return true;
				});
			}
		);
	},

	rotateItems : function(el)
	{
		el.carouselCfg.start += el.carouselCfg.speed;
		if (el.carouselCfg.start > jQuery.iCarousel.maxRotation) {
			el.carouselCfg.start = 0;
		}

		el.carouselCfg.items.each(
			function (nr)
			{
				angle = el.carouselCfg.start+nr*el.carouselCfg.step;
				x = el.carouselCfg.radiusX*Math.cos(angle);
				y = el.carouselCfg.radiusY*Math.sin(angle) ;
				itemZIndex = parseInt(100*(el.carouselCfg.radiusY+y)/(2*el.carouselCfg.radiusY));
				parte = (el.carouselCfg.radiusY+y)/(2*el.carouselCfg.radiusY);

				width = parseInt((el.carouselCfg.itemWidth - el.carouselCfg.itemMinWidth) * parte + el.carouselCfg.itemMinWidth);
				height = parseInt(width * el.carouselCfg.itemHeight / el.carouselCfg.itemWidth);
				this.style.top = el.carouselCfg.paddingY + y - height/2 + "px";
	     		this.style.left = el.carouselCfg.paddingX + x - width/2 + "px";
	     		this.style.width = width + "px";
	     		this.style.height = height + "px";
	     		this.style.zIndex = itemZIndex;
	     		this.style.fontSize = parseInt((width / el.carouselCfg.itemWidth) * 100) + "%"; 
			}
		);
	},
	
	start : function(el)
	{
		el.carouselCfg.rotationTimer = window.setInterval(
			function()
			{
				jQuery.iCarousel.rotateItems(el);
			},
			40
		);
	},
	
	pause : function(el)
	{
		/* if we're not currently paused */
		if (!el.carouselCfg.paused) {
			/* mark as paused */
			el.carouselCfg.paused = true;
			/* stop rotation */
			window.clearInterval(el.carouselCfg.rotationTimer);
			/* set timeout to restart rotation after 3 seconds */
			window.setTimeout(
				function()
				{
					jQuery.iCarousel.start(el);
					el.carouselCfg.paused = false;
				},
				3000
			);
		}
	}
};
jQuery.fn.Carousel = jQuery.iCarousel.build;
