/*
 * jQuery 3d Carousel plugin
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * tested with jQuery 1.4.2
 * written by Kae Verens: kae@webworks.ie
 * version: 1.1
 */
(function( $ ){
	$.fn.k3dCarousel=function(options) {  
		return this.each(function() {
			var i=0, sin, position, newWidth, numitems, degs, acss
				positions=[],
				iter=0,
				w=this.offsetWidth,
				items=$(this).css('position','relative').find('li').css({
					"position" : 'absolute',
					"opacity"  : 0,
					"display"  : 'block'
				}),
				settings = {
					"r"  : w*.3, // radius of the carousel
					"cX" : w/2,  // center X of the carousel
					"cY" : this.offsetHeight/2, // center Y
					"sT" : 1000, // how long it takes to move from spot to spot
					"wT" : 2000, // how long to pause before moving again
					"a"  : 0,     // arrows. examples: 0, ['&lt;','&gt;'], ['left','right']
					"d"  : 1     // direction. 1 = ltr, -1 = rtl
				};
			if ( options ) { 
				$.extend( settings, options );
			}
			if ( settings.a.length==2 ) {
				$('<div style="cursor:pointer;position:absolute;left:5px;top:5px" class="left" />')
					.html(settings.a[0])
					.click(function(){
						settings.d=-1;
					})
					.appendTo(this);
				$('<div style="cursor:pointer;position:absolute;right:5px;top:5px" class="right" />')
					.html(settings.a[1])
					.click(function(){
						settings.d=1;
					})
					.appendTo(this);
			}
			items.each(function(index,el){
				el.oW=el.offsetWidth;  // store the original with of the image
				el.oH=el.offsetHeight; // and the original height
				$(el).css({
					"left":settings.cX-el.oH/2,
					"top":0
				});
			});
			numitems=items.length;
			degs=Math.PI/(numitems/2);
			for (; i<numitems; ++i) { // i initialised above
				sin=Math.sin(degs*i);
				positions.push({
					"l" : settings.cX+(Math.cos(degs*i)*settings.r),
					"z" : parseInt(50*sin+50),
					"t" : sin*10+10,
					"o" : .45*sin+.55,
					"m" : .4*sin+.6
				});
				items[i]=$(items[i]);
			}
			function setPositions(){
				for (i=0; i<numitems; ++i) {
					position=positions[i];
					newWidth=position.m*items[i][0].oW;
					items[(i+iter)%numitems].animate(
						{
							'left':    position.l-newWidth/2,
							'opacity': position.o,
							'top':     position.t,
							'width':   newWidth,
							'height':  position.m*items[i][0].oH+10
						},
						settings.sT
					)
					.css('z-index',position.z);
				}
				iter+=settings.d;
				if(iter<0)iter+=numitems;
				setTimeout(setPositions,settings.wT+settings.sT);
			}
			setPositions();
		});
	};
	
	
	$.fn.k3dCarouselLoc=function(options) {  
		return this.each(function() {
			var iLoc=0, sin, positionLoc, newWidthLoc, numitemsLoc, degsLoc, acssLoc
				positionsLoc=[],
				iterLoc=0,
				wLoc=85,
				itemsLoc=$(this).css('position','relative').find('li').css({
					"position" : 'absolute',
					"opacity"  : 1,
					"display"  : 'block'
				}),
				settings = {
					"r"  : wLoc*.3, // radius of the carousel
					"cX" : wLoc/2,  // center X of the carousel
					"cY" : this.offsetHeight/2, // center Y
					"sT" : 1000, // how long it takes to move from spot to spot
					"wT" : 2000, // how long to pause before moving again
					"a"  : 0,     // arrows. examples: 0, ['&lt;','&gt;'], ['left','right']
					"d"  : 1     // direction. 1 = ltr, -1 = rtl
				};
			if ( options ) { 
				$.extend( settings, options );
			}
			if ( settings.a.length==2 ) {
				$('<div style="cursor:pointer;position:absolute;left:5px;top:5px" class="left" />')
					.html(settings.a[0])
					.click(function(){
						settings.d=-1;
					})
					.appendTo(this);
				$('<div style="cursor:pointer;position:absolute;right:5px;top:5px" class="right" />')
					.html(settings.a[1])
					.click(function(){
						settings.d=1;
					})
					.appendTo(this);
			}
			itemsLoc.each(function(index,el){
				el.oW=el.offsetWidth;  // store the original with of the image
				el.oH=el.offsetHeight; // and the original height
				$(el).css({
					"left":settings.cX-el.oH/2,
					"top":0
				});
			});
			numitemsLoc=itemsLoc.length;
			degsLoc=Math.PI/(numitemsLoc/2);
			for (; iLoc<numitemsLoc; ++iLoc) { // iLoc initialised above
				sin=Math.sin(degsLoc*iLoc);
				positionsLoc.push({
					"l" : settings.cX+(Math.cos(degsLoc*iLoc)*settings.r),
					"z" : parseInt(50*sin+50),
					"t" : sin*10+10,
					"o" : 1,
					"m" : .4*sin+.6
				});
				itemsLoc[iLoc]=$(itemsLoc[iLoc]);
			}
			function setPositionsLoc(){
				for (iLoc=0; iLoc<numitemsLoc; ++iLoc) {
					positionLoc=positionsLoc[iLoc];
					newWidthLoc=positionLoc.m*itemsLoc[iLoc][0].oW;
					itemsLoc[(iLoc+iterLoc)%numitemsLoc].animate(
						{
							'left':    0,
							'opacity': 1,
							'top':     0,
							'width':   85,
							'height':  90
						},
						settings.sT
					)
					.css('z-index',positionLoc.z);
				}
				iterLoc+=settings.d;
				if(iterLoc<0)iterLoc+=numitemsLoc;
				setTimeout(setPositionsLoc,settings.wT+settings.sT);
			}
			setPositionsLoc();
		});
	};
	
	
	
})( jQuery );

