/*
 * Paul Bakaus' Carousel @VERSION
 *
 * Copyright (c) 2008 Paul Bakaus
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Depends:
 *  ui.core.js
 */

(function($){

$.widget("ui.carousel", {
	
	_init: function() {
		

		var self = this;
		var element = this.element;
		var masterMod = 0;
		this.items = $(this.options.items, this.element);
		if(this.options.radiusXTemp < 330){
			this.options.radiusX = this.options.radiusXTemp;
		};
    this.button_left = $(this.options.button_left,this.element);
    this.button_right = $(this.options.button_right,this.element);

		
		//Determine the orientation if set to auto
		this.orientation = this.options.orientation == 'auto' ? (this.element[0].offsetWidth/this.element[0].offsetHeight > 1 ? 'horizontal' : 'vertical') : this.options.orientation;
		this.props = this.orientation == 'vertical' ? ['height', 'Height', 'top', 'Top'] : ['width', 'Width', 'left', 'Left'];

		$.extend(this, {
			start: Math.PI/2,
			speed: 0.03,
			step: 2*Math.PI/this.items.length,
			paddingX: this.element.outerWidth() / 2,
			paddingY: this.element.outerHeight() / 2,
			itemHeight: this.options.height || this.items.height(),
			itemWidth: this.options.width || this.items.width()
		});
		
		//Add pause/resume functionality when you hover a item
		this.items
			.css({ position: "absolute", top: 0, left: 0, zIndex: 1 })
			/*.hover(function(e) {
				if(self.options.pausable) self.pause();
			},function(e) {
				self.resume();
			});*/
			

		//Little trick to jump to the first item
		this.rotate("left");
		this.rotate("right");
		
		//Auto animation for the carousel
		if(this.options.animate)
			self._startAnimation();
    
    //Auto animation for 10 seconds on document load
    $(document).ready(function () {
      var elementTemp = $(this.element);
      var speedTemp = Math.abs(masterMod)/9000;
        self._startAnimationTemp('left',speedTemp);
        window.setTimeout(function() {
  			self.pause();
  		}, 10000);
    });
    
    

    
		this.offset = this.element.offset();
		
    //Carousel animation by clicking and holding left arrow-button
  	this.button_left.bind("mousedown", function() { 
        var speedTemp = Math.abs(masterMod)/9000;
        self._startAnimationTemp('left',speedTemp);
		});
		this.button_left.bind("mouseup", function() {
		  clearInterval(self.interval);
    });
    
    //Carousel animation by clicking and holding right arrow-button
  	this.button_right.bind("mousedown", function() { 
        var speedTemp = Math.abs(masterMod)/9000;
        self._startAnimationTemp('right',speedTemp);
		});
		this.button_right.bind("mouseup", function() {
		  clearInterval(self.interval);
    });
		
		/*this.element
			.hover(function() {}, function() {
				if(self.options.animate) {
					self._startAnimation();
				} else {
					if(self.interval) clearInterval(self.interval);
				}
			})
			
			.bind("mousemove", function(e) {
				masterMod = ((e[self.orientation == 'vertical' ? 'pageY' : 'pageX']-self.offset[self.props[2]]) - this['offset'+self.props[1]]/2);
        });*/
		
	},
	
	_startAnimation: function() {
		var self = this;
		if(this.interval) clearInterval(this.interval);
		this.interval = window.setInterval(function() {
			self.rotate('left',self.options.animate);
		}, 13);
	},
	
	_startAnimationTemp: function(direction,speed) {
		var self = this;
		if(this.interval) clearInterval(this.interval);
		this.interval = window.setInterval(function() {
			self.rotate(direction,speed);
		}, 13);
	},
	
	resume: function() {
		var self = this;
		this.paused = false;
		this._startAnimation();
	},
	
	pause: function() {

		var self = this;
		this.paused = true;
		if(this.interval) clearInterval(this.interval);

		if(this.options.pauseSpeed) {
			this.interval = window.setInterval(function() {
				self.rotate('left',0.001);
			}, 13);
		}
	},
	
	rotate: function(direction, speed) {

		var o = this.options, self = this;

		if(speed) this.speed = speed;
		this.start = this.start + (direction == "right" ? -this.speed : this.speed);

		setTimeout(function(){
			self.items.each(function(i) {

				var angle = self.start + i * self.step;
				var x = self.options.radiusX * Math.cos(angle);
				var y = self.options.radiusY * Math.sin(angle);

				var width = self.itemWidth * ((self.options.radiusY+y) / (2 * self.options.radiusY));
				if(self.options.smallerItems) width = Math.pow(width, 3) / Math.pow(self.itemWidth, 2);  //This makes the pieces smaller
				var height = parseInt(width * self.itemHeight / self.itemWidth,10);

				//This is highly custom - it will hide the elements at the back
				//$(this).css({ visibility: height < 30 ? "hidden" : "visible" });
				//if(height < 30) return; //This improves the speed, but cannot be used with animation

				$(this).css({
					top: self.paddingY + (self.orientation == 'vertical' ? x : y) - height/2 + "px",
					left: self.paddingX + (self.orientation == 'vertical' ? y : x) - width/2 + "px",
					width: width + "px",
					height: height + "px",
					zIndex: parseInt(100 * (self.options.radiusY+y) / (2 * self.options.radiusY),10)
				});
				
			});
		}, 0);

	}
	
});

$.extend($.ui.carousel, {
	defaults: {
		items: '> div.wrapper',
		orientation: 'auto',
		radiusX: 330,
		radiusY: -0.1,
		animate: 0.005,
		animateByMouse: true,
		pausable: true,
		pauseSpeed: false, //If you don't want to actually stop the carousel when hovering items, set it to something small, i.e. 0.001
		smallerItems: false, //This creates a somewhat fake mode. Looks better on some wide carousels, the items are smaller
		button_left: '> p.buttonleft',
		button_right: '> p.buttonright'
	}
});
	
})(jQuery);
