/*
 * Create a 3D carousel similar to the popular Flash carousel
 *
 * @name		carousel3d
 * @author		Kevin Crossman
 * @contact		kevincrossman@gmail.com
 * @version		1.1 simplified
 * @date			Oct 29 2008
 * @type    	 	jQuery
 * @example  
 *
 *	 	* Place icon images inside <div id="carousel" />
 *
 * 				<div id='carousel'>
 *						<img src='images/image1.png' alt='image1' /> 
 *						<img src='images/image2.png' alt='image2' /> 
 *						<img src='images/image3.png' alt='image3' /> 
 *						<img src='images/image4.png' alt='image4' /> 
 *
 *		* to make image a page link, add class 'link' and set the longdesc attr to a web address
 *						<img class="link" src='images/image5.png' alt='image5' longdesc="http://plugins.jquery.com/" /> 
 *
 *				</div>
 
 *
 * 				
 *
 */

(function($) {

	$.fn.extend({
		
		carousel3d: function(custom_options) { 
			
				var options = $.extend({},$.carouselSetup.defaults, custom_options);		// extend options
				
				$this = $(this); 
				carousel_id = this[0].id;
				
				options.speed = 5;//parseInt(6 - options.speed);																		
				// by Alex : // * $.browser.msie ? 50000 : 10000;
           		options.speed = options.speed * $.browser.msie ? 2000 : 10000;											// original mouse speed
				
				
				$imgs = $('img', $this).hide();											// set variable with carousel images; hide for now
				
				items = $imgs.size(); 														// number of icons in carousel
				numSlots = items * options.padding; 										// in order for the movement to flow smoothly, there are additional 'slots' in the carousel which the images will pace through
				
				if (options.padding == 0) options.padding = 1;								// padding must be at least 1
				
				$('#'+carousel_id).data('rate',0);
				
				$imgs.each(function(i) { new $.imageSetUp(carousel_id, options, this, i, numSlots, items) });	// setup images
							
				/*$(window).resize(function() { */
				/*$(window).bind('resize', function() {
					var options = $('#'+carousel_id).data('options');
					options.centerX = $this.offset().left+$this.width()/2;
					$('#'+carousel_id).data('options',options);
				});*/
				
				new $.carouselSetup(carousel_id, options, numSlots, items);						// setup carousel     
				
           }
	});
    
    
	$.imageSetUp = function(carousel_id, options, im, _index, numSlots) {
		
		im.orig_w = $(im).width(), im.orig_h = $(im).height();				// save the original dimensions; used when image is clicked
		/* by Alex : define size of the thumbnail */
		var size = get_max_size( im.orig_w, 168, im.orig_h, 125);		// calculate w/h of images in carousel
		im.h = size.height;
		im.w = size.width;
		
		im.slot = _index * options.padding;												// position of the image in the carousel
		im.angle = parseInt(( _index * options.padding ) * (( Math.PI * 2 ) / numSlots )*1000)/1000;		// original angle of the image
		
		$(im).attr('id', 'pix'+carousel_id+_index).css({position: 'absolute'});				// id will be referenced when moving the image
			
		if ($(im).hasClass('link')) {
			$(im).click( function(){ window.open($(this).attr('longdesc')) } );
		}
		
	};  
	
	$.carouselSetup = function(carousel_id, options, numSlots, items) {
	
		var im, _t, _s;
	
		if (options.control=='mouse') {
			$().mousemove(function(e) { 
			// changed by Alex : regule la vitesse
			X2X1 = e.pageX -84 - ( $('#'+carousel_id).offset().left + options.centerX);
			Y2Y1 = e.pageY -84 - ( $('#'+carousel_id).offset().top + options.centerY);
			distance_from_center = Math.abs(X2X1);
			sens_rotation = (X2X1 >= 0) ? 1 : -1;
			vitesse_de_croisiere = 15;
			if ( distance_from_center >= 330 ) { 
				vitesse = vitesse_de_croisiere;
			} else if ( distance_from_center >= 120 && distance_from_center < 330) {
				vitesse = 120;
			} else if ( distance_from_center >= 50 && distance_from_center < 100 ) {
				vitesse = distance_from_center;
			}  else {       // ( distance_from_center < 20  )
				vitesse = 0;
			} 
			if ( Math.abs(Y2Y1) > 110  ) {
				vitesse = vitesse_de_croisiere;
			}
			rate = ( sens_rotation * vitesse ) / options.speed;
			$('#'+carousel_id).data('rate',rate);
			});
		} else {
			rate = 15;
			$('#'+carousel_id).data('rate',rate);
		}
		
		// javascript Motion Tween by PHILIPPE MAEGERMAN; very similar to tweening in Flash.
		// check out the full details at his site: http://jstween.blogspot.com/
		t1 = new Tween(new Object(), 'xyz', Tween.regularEaseInOut, 0, 10000, 10000);
            
		t1.onMotionChanged = function(event) {
		
			for (var j=0; j<items, im=$('#pix'+carousel_id+j)[0]; j++) {
				
				im.slot = (im.slot == numSlots - 1) ? 0: im.slot++;			// if image is in last slot, set as first slot; else advance 1 slot
				_t = Math.sin(im.angle) * options.radiusY + options.centerY;		// calculate top positioning
				_max_t = options.radiusY + options.centerY; // correspond à 100% de la taille
				_min_t = -1 * options.radiusY + options.centerY; // correspond à n% de la taille
				_min_s = 0.5; // pourcentage de reduction au maximun des images : n% de la taille pour l'image la plus éloignée
				coef  =  (1-_min_s) / (_max_t - _min_t);
				_s = _t * coef + (_min_s - _min_t*coef)  ;   // de 1 à n%
				//((_t - options.perspective) / ( 10450 + options.centerY + options.radiusY - options.perspective));	// calculate size of image based on position in carsousel
				
				$(im).css({ 																		// set css values for image
					top: _t, 
					left: Math.cos(im.angle) * options.radiusX + options.centerX, 	// calculate left positioning
					width: im.w * _s, 															// multiply image size by newly calculated size
					height: im.h * _s, 
					zIndex: Math.round(_t)+100,										// z-index makes front images fully visible
					opacity: (options.fadeEffect == 1) ? Math.sin(im.angle) : 1 });	// if fadeEffect is set, calculate opacity based on location in carousel
				
				if (options.fadeEffect == 1) {
					$(im)[ Math.sin(im.angle)<=0 ? 'hide' : !$(im).is(':visible') ? 'show' : ''  ];
				}
				
				im.angle += $('#'+carousel_id).data('rate');			// change image angle based on carousel speed
           	}
       	};
       	
       	t1.start();																					// start the motion
       	$imgs.fadeIn(1500);																// fadeIn the images
    };

	function get_max_size(w, max_w, h, max_h) {										// resize the images
		if (w>max_w || h>max_h) {
			var x_ratio = max_w / w;
			var y_ratio = max_h / h;
			if ((x_ratio * h) < max_h) return { width : max_w , height : Math.ceil(x_ratio * h) };
			else return { width : Math.ceil(y_ratio * w) , height : max_h };
		}
		else return { width : w , height : h };
	};
	

    $.carouselSetup.defaults = {	
        control: 'mouse',	  																	// 'button', 'mouse', or  'continuous' control
        speed: 3,																					// speed of mouse or button.  use scale of 1-5
        radiusX: 250,																			// x radius of the carousel
        radiusY: 40,																				// y radius of the carousel
        centerX: 0,																				// x position on the screen
        centerY: 250,																			// y position on the screen
        perspective: 120,																		// perspective of the image as it travels around the carousel
      	padding: 24, 																			// the number of padded items in between each icon.  
        																								// the more padding, the more precise the incremental movement,
        																								// however this also create a lot more calculations
        																								// to keep icons evenly spaced, the num of icons should be a multiple of the padding
        fadeEffect: 0,																			// fade icons as cycle to the back of the carousel
        textBox: 0																				//  1 = display text area for each icon, 0 = no display
    };

})(jQuery);
