/**
 * Rhinofader 1.0
 * http://rhinoslider.com/rhinofader/
 *
 * Copyright 2011, Sebastian Pontow, Rene Maas (http://renemaas.de/)
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://rhinoslider.com/license/
 */
(function(a, b, c) {
	var d = function(b, c) {
		var d = jQuery.extend({}, jQuery.fn.rhinofader.defaults, c),e = {isPlaying:false,intervalAutoPlay:false,active:"",next:"",container:"",items:"",buttons:[],prefix:"rhino-"},f = a(b);
		d.controlsPrevNext = Boolean(d.controlsPrevNext);
		d.controlsPlayPause = Boolean(d.controlsPlayPause);
		d.pauseOnHover = Boolean(d.pauseOnHover);
		d.showTime = parseInt(d.showTime, 10);
		d.effectTime = parseInt(d.effectTime, 10);
		d.controlFadeTime = parseInt(d.controlFadeTime, 10);
		f.data("slider:vars", e);
		f.wrap('<div class="' + e.prefix + 'container">');
		e.container = f.parent("." + e.prefix + "container");
		e.isPlaying = d.autoPlay;
		var g = "";
		if (d.controlsPrevNext) {
			g = '<span class="' + e.prefix + "prev " + e.prefix + 'btn">zurück</span><span class="' + e.prefix + "next " + e.prefix + 'btn">weiter</span>';
			e.container.append(g);
			e.buttons.prev = a("." + e.prefix + "prev", e.container);
			e.buttons.next = a("." + e.prefix + "next", e.container);
			e.buttons.prev.click(function() {
				m(f, d);
				if (d.autoPlay) {
					k()
				}
			});
			e.buttons.next.click(function() {
				n(f, d);
				if (d.autoPlay) {
					k()
				}
			})
		}
		if (d.controlsPlayPause) {
			g = d.autoPlay ? '<span class="' + e.prefix + "toggle " + e.prefix + "pause " + e.prefix + 'btn">Pause</span>' : '<span class="' + e.prefix + "toggle " + e.prefix + "play " + e.prefix + 'btn">Abspielen</span>';
			e.container.append(g);
			e.buttons.play = a("." + e.prefix + "toggle", e.container);
			e.buttons.play.click(function() {
				if (e.isPlaying === false) {
					l()
				} else {
					k()
				}
			})
		}
		a("." + e.prefix + "btn", e.container).css({position:"absolute",display:"block",cursor:"pointer"});
		e.items = f.children("li");
		e.items.first().addClass(e.prefix + "active");
		e.active = a("." + e.prefix + "active", f);
		e.items.css({margin:0,width:f.css("width"),height:f.css("height"),position:"absolute",top:0,left:0,zIndex:0,opacity:0 });
		e.active.css({opacity:1,zIndex:1});
		var h = d.styles.split(",");
		for (i = 0; i < h.length; i++) {
			e.container.css(a.trim(h[i]), f.css(a.trim(h[i])))
		}
		e.container.css("position", "relative");
		f.css({top:"auto",left:"auto",position:"relative"});
		if (d.autoPlay) {
			e.intervalAutoPlay = setInterval(function() {
				n(f, d)
			}, d.showTime)
		} else {
			e.intervalAutoPlay = false
		}
		if (d.pauseOnHover) {
			f.mouseenter(
					function() {
						if (e.isPlaying) {
							clearInterval(e.intervalAutoPlay);
							if (d.controlsPlayPause) {
								e.buttons.play.removeClass(e.prefix + "pause").addClass(e.prefix + "play")
							}
						}
					}).mouseleave(function() {
				if (e.isPlaying) {
					e.intervalAutoPlay = setInterval(function() {
						n(f, d)
					}, d.showTime);
					if (d.controlsPlayPause) {
						e.buttons.play.removeClass(e.prefix + "play").addClass(e.prefix + "pause")
					}
				}
			})
		}
		if (d.showControls === "hover" || d.showControls === "never") {
			var j = a("." + e.prefix + "btn", e.container);
			j.hide();
			if (d.showControls === "hover") {
				e.container.mouseenter(
						function() {
							j.stop(true, true).fadeIn(d.controlFadeTime)
						}).mouseleave(function() {
					j.delay(200).fadeOut(d.controlFadeTime)
				})
			}
		}
		var k = function() {
			var a = f.data("slider:vars");
			clearInterval(a.intervalAutoPlay);
			a.isPlaying = false;
			if (d.controlsPlayPause) {
				a.buttons.play.removeClass(a.prefix + "pause").addClass(a.prefix + "play")
			}
		},l = function() {
			var a = f.data("slider:vars");
			a.intervalAutoPlay = setInterval(function() {
				n(f, d)
			}, d.showTime);
			a.isPlaying = true;
			if (d.controlsPlayPause) {
				a.buttons.play.removeClass(a.prefix + "play").addClass(a.prefix + "pause")
			}
		},m = function(b, c) {
			var d = b.data("slider:vars");
			if (a("li:animated", d.container).length > 0) {
				return false
			}
			if (d.items.first().hasClass(d.prefix + "active")) {
				d.next = d.items.last()
			} else {
				d.next = d.active.prev()
			}
			if (d.next.hasClass(d.prefix + "active")) {
				return false
			}
			p(b, c)
		},n = function(b, c, d) {
			var e = b.data("slider:vars");
			if (a("li:animated", e.container).length > 0) {
				return false
			}
			if (!d) {
				if (e.items.last().hasClass(e.prefix + "active")) {
					e.next = e.items.first()
				} else {
					e.next = e.active.next()
				}
			} else {
				e.next = d
			}
			if (e.next.hasClass(e.prefix + "active")) {
				return false
			}
			p(b, c)
		},o = function(a, b) {
			var c = a.data("slider:vars");
			c.next.addClass(c.prefix + "active").css({zIndex:1});
			c.active.css({zIndex:0}).removeClass(c.prefix + "active");
			c.active = c.next
		},p = function(a, b) {
			var c = a.data("slider:vars");
			c.next.css({zIndex:2}).animate({opacity:1}, b.effectTime, b.easing, function() {
				o(a, b)
			});
			c.active.animate({opacity:0}, b.effectTime)
		}
	};
	jQuery.fn.rhinofader = function(b) {
		return this.each(function() {
			var c = a(this);
			if (c.data("rhinofader")) {
				return c.data("rhinofader")
			}
			var e = new d(this, b);
			c.data("rhinofader", e)
		})
	};
	jQuery.fn.rhinofader.defaults = {controlsPrevNext:true,controlsPlayPause:true,pauseOnHover:true,autoPlay:false,showTime:3e3,effectTime:1e3,controlFadeTime:650,showControls:"hover",styles:"position,top,right,bottom,left,margin-top,margin-right,margin-bottom,margin-left,width,height"}
})(jQuery, window)
