// http://jqbug.com/10531
$.event.props = $.event.props.join('|').replace('layerX|layerY|', '').split('|');

// http://mths.be/pagevisibility by @mathias
(function(window, document, $) {

	var hidden,
	    visibilityChange;
	if (typeof document.hidden != 'undefined') {
		hidden = 'hidden';
		visibilityChange = 'visibilitychange';
	} else if (typeof document.mozHidden != 'undefined') {
		hidden = 'mozHidden';
		visibilityChange = 'mozvisibilitychange';
	} else if (typeof document.msHidden != 'undefined') {
		hidden = 'msHidden';
		visibilityChange = 'msvisibilitychange';
	} else if (typeof document.webkitHidden != 'undefined') {
		hidden = 'webkitHidden';
		visibilityChange = 'webkitvisibilitychange';
	}

	$(hidden ? document : window).bind(hidden ? visibilityChange : 'blur focus', function(event) {
		$.event.trigger(hidden && document[hidden] || event.type == 'blur' ? 'hide.visibility' : 'show.visibility');
	});

}(this, document, jQuery));

// jQuery#slideshow by @mathias
(function($) {

	var $document = $(document),
	    $window = $(window),
	    small,
	    slideshow;

	$window.resize(function() {
		small = $window.width() < 961;
	}).resize();

	slideshow = $.fn.slideshow = function(options) {

		options = $.extend(slideshow.options, options);

		var timeout = options.timeout,
		    speed = options.speed,
		    activeClass = options.activeClass;

		return this.each(function() {

			var $container = $(this).wrap('<div class="slideshow-wrapper"></div>'),
			    $children = $container.children(),
			    $nav = $('<ul class="slideshow-nav"></ul>'),
			    $link = $('<a href="#"></a>'),
			    $navChildren,
			    $navFirstChild,
			    $navLastChild,
			    $active,
			    $tmp,
			    interval;

			$children.hide().each(function(index, element) {
				$link.clone().html('<span>' + (index + 1) + '</span>').wrap('<li' + (index ? '' : ' class="' + activeClass + '"') + '></li>').parent().click(function(event) {
					event.preventDefault();
					if (this.className != activeClass) {
						if (small) {
							$tmp = $children.eq(index).show();
							$children.not($tmp).hide();
						} else {
							$children.fadeOut(speed).eq(index).fadeIn(speed);
						}
						$active = $navChildren.removeClass(activeClass).eq(index).addClass(activeClass);
					}
				}).appendTo($nav);
			}).first().show();
			$navChildren = $nav.children();
			$active = $navFirstChild = $navChildren.first();
			$navLastChild = $navChildren.last();
			$container.after($nav);

			function start() {
				clearInterval(interval);
				interval = setInterval(function() {
					var $next = $active.next();
					($next.length ? $next : $navFirstChild).click();
				}, timeout);
			}

			function pause() {
				clearInterval(interval);
			}

			$document.bind({
				'hide.visibility': pause,
				'show.visibility': start
			});

			if ($children.length == 1) {
				return;
			}

			$document.keyup(function(event) {
				var $el;
				if (event.keyCode == 37) {
					// prev
					$el = $active.prev();
					clearInterval(interval);
					($el.length ? $el : $navLastChild).click();
					start();
				} else if (event.keyCode == 39) {
					// next
					$el = $active.next();
					clearInterval(interval);
					($el.length ? $el : $navFirstChild).click();
					start();
				}
			});

			// Pause slideshow when hovering
			$container.add($nav).hover(pause, start);

			start();

		});

	};

	slideshow.options = {
		'timeout': 3000,
		'speed': 400, // 'normal'
		'activeClass': 'current'
	};

}(jQuery));
