

(function($) {
	function absolutize(element) {
		element = $(element);
		if (element.css('position') == 'absolute') return;
		var offsets = element.offset();
		var top = offsets[1] ? offsets[1]: 0;
		var left = offsets[0] ? offsets[0]: 0;
		var width = $(element).innerWidth();
		var height = $(element).innerHeight();

	    element._originalLeft = left - parseFloat(element.css('left')  || 0);
	    element._originalTop = top  - parseFloat(element.css('top') || 0);
	    element._originalWidth = element.width();
	    element._originalHeight = element.height();

	    element.css('position', 'absolute');
	    element.css('top', top + 'px');
	    element.css('left', left + 'px');
	    element.width(width + 'px');
	    element.height(height + 'px');
	    return element;
	}
	function CoverFlow(elem, options) {
		var _this = this;
		this.options = options;
		this.elem = elem;
		$(this.elem).css({position: 'relative'});
		
		this.stack = $(this.elem).children().get();
		this.stackCount = this.stack.length;
		
		this.currPos = this.options.startIndex - 1;
		this.currIndex = this.currPos;
		this.timer = 0;
		/* sets up click listener on all the elements in the stack */
		/*$(this.stack).click(function() {
			_this.handleClick(this);
		});*/
		/*handle key events*/
		$(this.elem).keydown(function(event){
		    _this.handleKey(event);
		});
		
		/* sets up mouse wheel listener */
		$(this.elem).mousewheel(function(event, delta) {
			delta = (delta >= 0) ? 1 : -1;
			_this.handleWheel(delta);
			return false;
		});
		this.goTo(this.currPos);
		/* add navigation */
		$(this.stack).find('a').click(function(e) {
			var classe = $(this).parent().attr("class");
			if(classe=='next'){
			var nextIndex = parseInt(_this.currIndex) + 1;
			}else if(classe=='prev'){
				var nextIndex = parseInt(_this.currIndex) - 1;
			}
			if(nextIndex < _this.stackCount && nextIndex > -1 ){
				_this.goTo(nextIndex);
			}
		});
	}
	CoverFlow.prototype = {
		handleWindowResize: function(event) {
		},
		handleWheel: function(d) {
			var index = this.currIndex + d;
			if(index >= 0 && index < this.stackCount) {
				this.goTo(index);
				
			}
		},
		handleKey: function(v){
		 
		   var cIndex = this.currIndex;
		   
		     /*right arrow key*/
		    if(v.keyCode == 39)
		    {
		       	if( (cIndex +1) >= 0 && (cIndex+1) < this.stackCount) 
		       	{
		            this.goTo(cIndex +1);
		        }
		        
		    }else{
		    
		       /*left arrow key*/
		       if(v.keyCode == 37)
		       {
		          if( (cIndex -1) >= 0 && (cIndex-1) < this.stackCount) 
		       	  {  
			          this.goTo(cIndex -1);
		          }
		       
		       }
		    }
		},
		handleClick: function(elem) {
			v = elem.getAttribute('index');
			this.goTo(v);
		},
		getCurrentPos: function() {
			return this.currPos;
		},
		
		goTo: function(index) {
			this.currIndex = index;
			this.slideTo(index * this.options.flex * -1);
		},
		step: function() {
			if(this.target < this.currPos - 1 || this.target > this.currPos + 1) {
				this.moveTo(this.currPos + (this.target - this.currPos) / 5);
				var _this = this;
				window.setTimeout(function(){
					_this.step();
				}, this.options.interval);
				this.timer = 1;
				this.addMainClass();
			} else {
				this.timer = 0;
			}
		},
		slideTo: function(x) {
			this.target = x;
			if(this.timer == 0) {
				var _this = this;
				window.setTimeout(function(){
					_this.step();
				}, this.options.interval);
				this.timer = 1;
			}
		},

		moveTo: function(currentPos) {
			var x = currentPos;
			this.currPos = currentPos;
			var width = $(this.elem).width();
			var height = $(this.elem).height();
			
			var top = this.elem.offsetTop;
			var size = width * 0.5;
			var biggest = height;
			var zIndex = this.stackCount;
			var _this = this;
			$(this.stack).each(function(index) {
				absolutize(this);
				this.setAttribute('index', index);
				var z = Math.sqrt(10000 + x * x * 1) + 200;
				var xs = x / z * size + size;
				$(this).css({
					left: (xs - 60 / z * biggest) + 'px',
					top: (30 / z * size + 0) + 'px',
					width: (100 / z * biggest) + 'px',
					height: (100 / z * biggest) + 'px',
					zIndex: zIndex
				});
				if(x < -50) zIndex++;
				else zIndex--;
				x += _this.options.flex;
			});
			
		},
		getStackCount: function() {
			return this.stackCount;
		},
		addMainClass: function() {
			// Pour attribuer une classe a la div du dessus
			var maxZIndex=0;
			var idMaxZIndex=null;
			$(this.stack).each(function(index) {
				var zIndex = parseInt($(this).css("zIndex"));
				if(zIndex>maxZIndex){
					maxZIndex=zIndex;
					idMaxZIndex=this;
				}
			});
			$(this.stack).each(function(index) {
				if($(idMaxZIndex).attr("id")!=$(this).attr("id")){
					$(this).removeClass("focus");
				}
			});
			$("#"+$(idMaxZIndex).attr("id")).addClass("focus");
		}
	};

	jQuery.fn.coverflow = function(options) {
		options = $.extend({
			startIndex: 2,
			interval: 60,
			flex: 100,
			captions: false,
			autoplay: false,
			autoplayInterval: 5,
			useReflection: true
		}, options);
		return this.each(function() {
			new CoverFlow(this, options);
		});
	};
})(jQuery);
