/**
 * Copyright (c) 2011 Sylvain Gougouzian (sylvain@gougouzian.fr) MIT
 * (http://www.opensource.org/licenses/mit-license.php) licensed. GNU GPL
 * (http://www.gnu.org/licenses/gpl.html) licensed.
 */
jQuery(function(a) {
	a.fn.moodular = function(c) {
		var d = new Array;
		var e = a.extend({}, a.fn.moodular.defaults, c);
		var f = a.extend({}, a.fn.moodular.controls);
		var g = a.extend({}, a.fn.moodular.effects);
		this.each(function(a) {
			d[a] = new b(this, e, f, g)
		});
		return e.api ? d.length == 1 ? d[0] : d : null
	};
	a.moodular = function(b, c, d, e) {
		this.e = a(b);
		this.aItems = a("> " + c.item, this.e);
		if (c.random) {
			var f = a("> " + c.item, this.e);
			console.log(f);
			f.sort(function() {
				return Math.round(Math.random()) - .5
			});
			this.e.html("");
			for ( var g = 0; g < f.length; g++)
				this.e.append(f[g])
		}
		this.aItems.each(function(b) {
			a(this).attr("data-position", b)
		});
		this.nbItems = this.aItems.length;
		this.current = 0;
		this.dir = 1;
		this.locked = false;
		this.temp = new Array;
		this.timerMoving = null;
		this.opts = c;
		this.controls = d;
		this.effects = e;
		if (this.nbItems > 1)
			this._init()
	};
	var b = a.moodular;
	b.fn = b.prototype = {
		moodular : "3.0"
	};
	b.fn.extend = b.extend = a.extend;
	b.fn.extend({
		_init : function() {
			var b = this;
			this.e.wrap("<div></div>");
			this.e.parent().css({
				position : "relative",
				overflow : "hidden",
				width : this.e.width(),
				height : this.e.height()
			});
			this.e.css({
				position : "absolute",
				left : 0,
				top : 0
			});
			this.control = this.opts.controls.split(" ");
			this.effect = this.opts.effects.split(" ");
			this._resize();
			if (this.opts.percentSize != 0) {
				a(window).bind("resize", function() {
					b._resizePercent()
				})
			}
			var c;
			a(">" + this.opts.item, this.e).attr("data-current", "");
			a(">" + this.opts.item + "[data-position=" + this.current + "]",
					this.e).attr("data-current", "current");
			for (c = 0; c < this.control.length; c++) {
				if (a.isFunction(this.controls.init[this.control[c]]))
					this.controls.init[this.control[c]](this)
			}
			for (c = 0; c < this.effect.length; c++) {
				if (a.isFunction(this.effects.init[this.effect[c]]))
					this.effects.init[this.effect[c]](this)
			}
			if (this.opts.auto) {
				this.timerMoving = setTimeout(function() {
					b.next()
				}, b.opts.dispTimeout)
			}
		},
		_resize : function() {
			var b;
			for (b = 0; b < this.effect.length; b++) {
				if (a.isFunction(this.effects.resize[this.effect[b]]))
					this.effects.resize[this.effect[b]](this)
			}
		},
		_resizePercent : function() {
			var a = this.e.parent().parent().width();
			this.e.parent().width(a * this.opts.percentSize);
			this._resize()
		},
		_move : function() {
			var b;
			for (b = 0; b < this.control.length; b++) {
				if (a.isFunction(this.controls.before[this.control[b]]))
					this.controls.before[this.control[b]](this)
			}
			for (b = 0; b < this.effect.length; b++) {
				if (a.isFunction(this.effects.before[this.effect[b]]))
					this.effects.before[this.effect[b]](this)
			}
			var c = this;
			if (a.isFunction(this.opts.move)) {
				this.opts.move(this, function() {
					c._afterMoving()
				})
			} else {
				var d = false;
				for (b = 0; b < this.effect.length; b++) {
					if (a.isFunction(this.effects.move[this.effect[b]])) {
						d = true;
						this.effects.move[this.effect[b]](this, function() {
							c._afterMoving()
						})
					}
				}
				if (!d)
					this._afterMoving()
			}
		},
		_afterMoving : function() {
			var b = this;
			for (i = 0; i < this.control.length; i++) {
				if (a.isFunction(this.controls.after[this.control[i]]))
					this.controls.after[this.control[i]](this)
			}
			for (i = 0; i < this.effect.length; i++) {
				if (a.isFunction(this.effects.after[this.effect[i]]))
					this.effects.after[this.effect[i]](this)
			}
			for (i = 0; i < this.opts.callbacks.length; i++) {
				this.opts.callbacks[i](this)
			}
			a("> " + this.opts.item, this.e).attr("data-current", "");
			a("> " + this.opts.item + "[data-position=" + this.current + "]",
					this.e).attr("data-current", "current");
			this.locked = false;
			this.dir = 1;
			if (this.opts.auto) {
				this.timerMoving = setTimeout(function() {
					b.next()
				}, this.opts.dispTimeout)
			}
		},
		start : function() {
			this.next();
			return false
		},
		stop : function() {
			clearTimeout(this.timerMoving);
			return false
		},
		next : function() {
			this.moveTo(parseInt(this.current) + 1);
			return false
		},
		prev : function() {
			this.dir = -1;
			this.moveTo(parseInt(this.current) - 1);
			return false
		},
		moveTo : function(a) {
			if (!this.locked) {
				this.locked = true;
				clearTimeout(this.timerMoving);
				if (a >= this.nbItems)
					a -= this.nbItems;
				if (a < 0)
					a += this.nbItems;
				this.current = a;
				this._move()
			}
		}
	});
	a.fn.moodular.defaults = {
		item : "li",
		controls : "",
		effects : "left",
		easing : "",
		auto : true,
		speed : 2e3,
		dispTimeout : 1e3,
		callbacks : [],
		random : false,
		percentSize : 1,
		api : false
	};
	a.fn.moodular.controls = {
		callback : {}
	};
	a.fn.moodular.effects = {
		init : {},
		move : {
			left : function(a, b) {
				a.e.animate({
					left : (a.dir == 1 ? "-=" : "+=") + a.size + "px"
				}, a.opts.speed, a.opts.easing, function() {
					b()
				})
			}
		},
		before : {
			left : function(b) {
				if (b.dir == -1) {
					b.e.prepend(a("> " + b.opts.item + ":last", b.e));
					b.e.css("left", "-" + b.size + "px")
				} else {
					a(">" + b.opts.item + "[data-position=" + b.current + "]",
							b.e).insertAfter(
							a(">" + b.opts.item + ":first", b.e))
				}
			}
		},
		after : {
			left : function(b) {
				if (b.dir == 1) {
					b.e.append(a("> " + b.opts.item + ":first", b.e));
					b.e.css("left", 0)
				}
			}
		},
		resize : {
			left : function(b) {
				b.size = parseInt(b.e.parent().width());
				b.e.width(2 * b.size + "px");
				a("> " + b.opts.item, b.e).width(b.size)
			}
		}
	};
	a.fn.moodular.controls = {
		init : {},
		before : {},
		after : {}
	}
})
jQuery(function(a) {
	a.extend(a.fn.moodular.effects.init, {
		fade : function(b) {
			a("> " + b.opts.item, b.e).css({
				position : "absolute",
				"z-index" : 1,
				opacity : 0
			});
			a("> " + b.opts.item + ":first", b.e).css({
				"z-index" : 3,
				opacity : 1
			});
			a("> " + b.opts.item, b.e).eq(1).css("z-index", 2)
		}
	});
	a.extend(a.fn.moodular.effects.move, {
		fade : function(b, c) {
			a("> " + b.opts.item, b.e).css({
				"z-index" : 1,
				opacity : 0
			});
			a("> " + b.opts.item + "[data-position=" + b.current + "]", b.e)
					.css({
						"z-index" : 2,
						opacity : 1
					});
			a("> " + b.opts.item + "[data-current=current]", b.e).css({
				"z-index" : 3,
				opacity : 1
			}).animate({
				opacity : 0
			}, b.opts.speed, b.opts.easing, function() {
				c()
			})
		}
	});
	a.extend(a.fn.moodular.effects.after, {
		fade : function(b) {
			a(">" + b.opts.item, b.e).css("z-index", 1)
		}
	})
})
