jQuery.fn.extend({
	carrossel: function(parans) {
		/*--------------------------------------------------------------------
		* - VARS
		*--------------------------------------------------------------------
		* - Description
		-------------------------------------------------------------------*/
		this.childLength = 0;
		this._delay;
		this._time;
		this.index = 0;
		this.indexAnt = -1;
		this.indexAtu = 0;
		this.isPaused = false;
		this.isStopped = false;
		this.isRunning = false;
		this.width = 0;

		for (attr in parans)
			this[attr] = parans[attr];

		/*--------------------------------------------------------------------
		* - FUNCTION ON ERROR
		*--------------------------------------------------------------------
		* - Description
		-------------------------------------------------------------------*/
		this.onError = function(msg) {
			try {
				$('.debug').append(msg + "<br />");
			}
			catch (e) {

			}
		}

		/*--------------------------------------------------------------------
		* - FUNCTION GO TO SLIDE
		*--------------------------------------------------------------------
		* - Description
		-------------------------------------------------------------------*/
		this.goToSlide = function() {
			if (arguments[1] == undefined)
				if (this.onChanged != undefined)
				this.onChanged(arguments[0]);

			if (arguments[0] > (this.childLength - 1))
				this.onError('<strong>Erro</strong>: O valor informado &eacute; maior que a quantidade de itens.');

			this.index = arguments[0] > (this.childLength - 1) ? 0 : arguments[0];
			$(this).animate({ left: -(this.index * this.width) }, this.time * 1000);
		}

		/*--------------------------------------------------------------------
		* - FUNCTION NEXT SLIDE
		*--------------------------------------------------------------------
		* - Description
		-------------------------------------------------------------------*/
		this.nextSlide = function() {
			if (this.index >= (this.childLength - 1))
				if (this.loop) this.goToSlide(0, 'next');
			else this.stop();
			else this.goToSlide(this.index + 1, 'next');
			if (this.onChanged != undefined) this.onChanged(this.index);
		}

		/*--------------------------------------------------------------------
		* - FUNCTION PREVIOUS SLIDE
		*--------------------------------------------------------------------
		* - Description
		-------------------------------------------------------------------*/
		this.prevSlide = function() {
			if (this.index <= 0)
				if (this.loop) this.goToSlide(this.childLength - 1, 'prev')
			else this.stop();
			else this.goToSlide(this.index - 1, 'prev');
			if (this.onChanged != undefined) this.onChanged(this.index);
		}

		/*--------------------------------------------------------------------
		* - FUNCTION PLAY/PAUSE
		*--------------------------------------------------------------------
		* - Description
		-------------------------------------------------------------------*/
		this.pause = function() {
			if (this.isPaused) {
				this.isPaused = false;
				this.animate(this);
			}
			else {
				this.isPaused = true;
				clearInterval(this._delay);
				clearInterval(this._time);
			}

			if (this.onPaused != undefined) this.onPaused(this.isPaused);
		}

		/*--------------------------------------------------------------------
		* - FUNCTION STOP
		*--------------------------------------------------------------------
		* - Description
		-------------------------------------------------------------------*/
		this.stop = function() {
			clearInterval(this._time);
			clearInterval(this._delay);
			this.isStopped = true;
			this.isRunning = false;
			this.goToSlide(0);

			if (this.onStopped != undefined) this.onStopped();
		}

		/*--------------------------------------------------------------------
		* - FUNCTION ANIMATE
		*--------------------------------------------------------------------
		* - Description
		-------------------------------------------------------------------*/
		this.animate = function(p) {
			try {
				if (!p.isStopped) {
					p._delay = setInterval(function() {
						clearInterval(p._delay);
						if (!p.isPaused) 
						{
							if (p.direction == 'left') p.nextSlide();
							else p.prevSlide();
							p.animate(p);
						}

					}, p.delay * 1000, p);
				}
			}
			catch (e) {

			}
		}

		/*--------------------------------------------------------------------
		* - FUNCTION START
		*--------------------------------------------------------------------
		* - Description
		-------------------------------------------------------------------*/
		this.start = function(p) {
			//p.animate(p);
			//if(this.onStarted != undefined) this.onStarted();

			if (!this.isRunning) {
				clearInterval(this._delay);
				clearInterval(this._time);

				this.isPaused = false;
				this.isRunning = true;
				this.isStopped = false;

				this.index = -1;
				this.animate(p);

				//parans default
				this.direction = this.direction == undefined ? 'left' : this.direction;
				this.time = this.time == undefined ? 0.5 : this.time;
				this.delay = this.delay == undefined ? 1 : this.delay;
				this.loop = this.loop == undefined ? true : this.loop;

				if (this.onStarted != undefined)
					this.onStarted();

				if (!this.autoStart) {
					this.stop();
					this.isRunning = true;
				}
			}
			else {
				this.autoStart = true;
				this.isRunning = false;
				this.isPaused = false;
				this.start(this);
			}
		}

		/*--------------------------------------------------------------------
		* - FUNCTION CONTROLS
		*--------------------------------------------------------------------
		* - Description
		-------------------------------------------------------------------*/
		this._controls = function(p) {
			try {
				$(p.controls.stop).each(function() {
					$(this).click(function(e) {
						e.preventDefault();
						p.stop();
					});
				});

				$(p.controls.start).each(function() {
					$(this).click(function(e) {
						e.preventDefault();
						p.start(p);
					});
				});

				$(p.controls.pause).each(function() {
					$(this).click(function(e) {
						e.preventDefault();
						p.pause(p);
					});
				});

				$(p.controls.prev).each(function() {
					$(this).click(function(e) {
						e.preventDefault();
						p.prevSlide(p);
					});
				});

				$(p.controls.next).each(function() {
					$(this).click(function(e) {
						e.preventDefault();
						p.nextSlide(p);
					});
				});
			}
			catch (e) {
				p.onError(e);
			}
		}

		/*--------------------------------------------------------------------
		* - FUNCTION INIT
		*--------------------------------------------------------------------
		* - Description
		-------------------------------------------------------------------*/
		this.init = function(p) {
			try {
				$(this).each(function() {
					p.width = $(this).find('>:first').width();
					p.childLength = $(this).find('> *').length;
					$(this).css('width', (p.width * p.childLength));
				});
				p._controls(p);
				p.start(p);
			}
			catch (e) {
				p.onError(e);
			}
		}

		this.init(this);

		return this.controls;
	}
});
