(function (r) {
	var s;
	s = r.fn.gallery_reloaded = function (k) {
		if (!s.hasCSS()) {
			return false
		}
		r.historyInit(s.onPageLoad);
		var l = {
			insert: '.gallery_reloaded_container',
			history: true,
			clickNext: true,
			onImage: function (a, b, c) {},
			onThumb: function (a) {}
		};
		var m = r.extend(l, k);
		for (var i in m) {
			if (i) {
				r.gallery_reloaded[i] = m[i]
			}
		}
		var n = (r(m.insert).is(m.insert)) ? r(m.insert) : r(document.createElement('div')).insertBefore(this);
		var o = r(document.createElement('div')).addClass('gallery_reloaded_wrapper');
		var metawrapper = r(document.createElement('div')).addClass('gallery_reloaded_meta');
		var p = r(document.createElement('span')).addClass('caption');
		var q = r(document.createElement('a'));
		metawrapper.append(p).append(q);
		n.addClass('gallery_reloaded_container').append(o).metawrapper;
		return this.each(function () {
			r(this).addClass('gallery_reloaded');
			r(this).children('li').each(function (i) {
				var b = r(this);
				var c = r.meta ? r.extend({},
				m, b.data()) : m;
				c.clickNext = r(this).is(':only-child') ? false: c.clickNext;
				var d = r(this).find('a').is('a') ? r(this).find('a') : false;
				var e = r(this).children('img').css('display', 'none');
				var f = d ? d.attr('href') : e.attr('src');
				var g = d ? d.attr('title') : e.attr('title');
				var j = new Image();
				if (c.history && (window.location.hash && window.location.hash.replace(/\#/, '') == f)) {
					b.siblings('.active').removeClass('active');
					b.addClass('active')
				}
				r(j).load(function () {
					r(this).attr('alt', e.attr('alt'));
					var a = d ? d.find('img').addClass('thumb noscale').css('display', 'none') : e.clone(true).addClass('thumb').css('display', 'none');
					if (d) {
						d.replaceWith(a)
					}
					if (!a.hasClass('noscale')) {
						var w = Math.ceil(e.width() / e.height() * b.height());
						var h = Math.ceil(e.height() / e.width() * b.width());
						if (w < h) {
							a.css({
								height: 'auto',
								width: b.width(),
								marginTop: -(h - b.height()) / 2
							})
						} else {
							a.css({
								width: 'auto',
								height: b.height(),
								marginLeft: -(w - b.width()) / 2
							})
						}
					} else {
						window.setTimeout(function () {
							a.css({
								marginLeft: -(a.width() - b.width()) / 2,
								marginTop: -(a.height() - b.height()) / 2
							})
						},
						1)
					}
					a.attr('rel', f);
					a.attr('title', g);
					a.click(function () {
						r.gallery_reloaded.activate(f)
					});
					a.hover(function () {
						r(this).addClass('hover')
					},
					function () {
						r(this).removeClass('hover')
					});
					b.hover(function () {
						b.addClass('hover')
					},
					function () {
						b.removeClass('hover')
					});
					b.prepend(a);
					a.css('display', 'block');
					c.onThumb(r(a));
					if (b.hasClass('active')) {
						r.gallery_reloaded.activate(f)
					}
					e.remove()
				}).error(function () {
					b.html('<span class="error" style="color:red">Error loading image: ' + f + '</span>')
				}).attr('src', f)
			})
		})
	};
	s.nextSelector = function (a) {
		return r(a).is(':last-child') ? r(a).siblings(':first-child') : r(a).next()
	};
	s.previousSelector = function (a) {
		return r(a).is(':first-child') ? r(a).siblings(':last-child') : r(a).prev()
	};
	s.hasCSS = function () {
		r('body').append(r(document.createElement('div')).attr('id', 'css_test').css({
			width: '1px',
			height: '1px',
			display: 'none'
		}));
		var a = (r('#css_test').width() != 1) ? false: true;
		r('#css_test').remove();
		return a
	};
	s.onPageLoad = function (a) {
		var b = r('.gallery_reloaded_wrapper');
		var c = r('.gallery_reloaded img[rel="' + a + '"]');
		if (a) {
			if (r.gallery_reloaded.history) {
				window.location = window.location.href.replace(/\#.*/, '') + '#' + a
			}
			c.parents('li').siblings('.active').removeClass('active');
			c.parents('li').addClass('active');
			var d = r(new Image()).attr('src', a).addClass('replaced');
			b.empty().append(d);
			b.siblings('.caption').text(c.attr('alt'));
			b.siblings('a').text('Large View').attr({
				href: a,
				title: c.attr('alt') + " - Full Size"
			}).css('display', 'none').fadeIn(1000);
			r.gallery_reloaded.onImage(d, b.siblings('.caption'), c);
			if (r.gallery_reloaded.clickNext) {
				d.css('cursor', 'pointer').click(function () {
					r.gallery_reloaded.next()
				})
			}
		} else {
			b.siblings().andSelf().empty();
			r('.gallery_reloaded li.active').removeClass('active')
		}
		r.gallery_reloaded.current = a
	};
	r.extend({
		gallery_reloaded: {
			current: '',
			onImage: function () {},
			activate: function (a) {
				if (r.gallery_reloaded.history) {
					r.historyLoad(a)
				} else {
					s.onPageLoad(a)
				}
			},
			next: function () {
				var a = r(s.nextSelector(r('.gallery_reloaded img[rel="' + r.gallery_reloaded.current + '"]').parents('li'))).find('img').attr('rel');
				r.gallery_reloaded.activate(a)
			},
			prev: function () {
				var a = r(s.previousSelector(r('.gallery_reloaded img[rel="' + r.gallery_reloaded.current + '"]').parents('li'))).find('img').attr('rel');
				r.gallery_reloaded.activate(a)
			}
		}
	})
})(jQuery);
jQuery.extend({
	historyCurrentHash: undefined,
	historyCallback: undefined,
	historyInit: function (a) {
		jQuery.historyCallback = a;
		var b = location.hash;
		jQuery.historyCurrentHash = b;
		if (jQuery.browser.msie) {
			if (jQuery.historyCurrentHash === '') {
				jQuery.historyCurrentHash = '#'
			}
			jQuery("body").prepend('<iframe id="jQuery_history" style="display: none;"></iframe>');
			var c = jQuery("#jQuery_history")[0];
			var d = c.contentWindow.document;
			d.open();
			d.close();
			d.location.hash = b
		} else if (jQuery.browser.safari) {
			jQuery.historyBackStack = [];
			jQuery.historyBackStack.length = history.length;
			jQuery.historyForwardStack = [];
			jQuery.isFirst = true
		}
		jQuery.historyCallback(b.replace(/^#/, ''));
		setInterval(jQuery.historyCheck, 100)
	},
	historyAddHistory: function (a) {
		jQuery.historyBackStack.push(a);
		jQuery.historyForwardStack.length = 0;
		this.isFirst = true
	},
	historyCheck: function () {
		if (jQuery.browser.msie) {
			var a = jQuery("#jQuery_history")[0];
			var b = a.contentDocument || a.contentWindow.document;
			var c = b.location.hash;
			if (c != jQuery.historyCurrentHash) {
				location.hash = c;
				jQuery.historyCurrentHash = c;
				jQuery.historyCallback(c.replace(/^#/, ''))
			}
		} else if (jQuery.browser.safari) {
			if (!jQuery.dontCheck) {
				var d = history.length - jQuery.historyBackStack.length;
				if (d) {
					jQuery.isFirst = false;
					var i;
					if (d < 0) {
						for (i = 0; i < Math.abs(d); i++) {
							jQuery.historyForwardStack.unshift(jQuery.historyBackStack.pop())
						}
					} else {
						for (i = 0; i < d; i++) {
							jQuery.historyBackStack.push(jQuery.historyForwardStack.shift())
						}
					}
					var e = jQuery.historyBackStack[jQuery.historyBackStack.length - 1];
					if (e !== undefined) {
						jQuery.historyCurrentHash = location.hash;
						jQuery.historyCallback(e)
					}
				} else if (jQuery.historyBackStack[jQuery.historyBackStack.length - 1] === undefined && !jQuery.isFirst) {
					if (document.URL.indexOf('#') >= 0) {
						jQuery.historyCallback(document.URL.split('#')[1])
					} else {
						c = location.hash;
						jQuery.historyCallback('')
					}
					jQuery.isFirst = true
				}
			}
		} else {
			c = location.hash;
			if (c != jQuery.historyCurrentHash) {
				jQuery.historyCurrentHash = c;
				jQuery.historyCallback(c.replace(/^#/, ''))
			}
		}
	},
	historyLoad: function (a) {
		var b;
		if (jQuery.browser.safari) {
			b = a
		} else {
			b = '#' + a;
			location.hash = b
		}
		jQuery.historyCurrentHash = b;
		if (jQuery.browser.msie) {
			var c = jQuery("#jQuery_history")[0];
			var d = c.contentWindow.document;
			d.open();
			d.close();
			d.location.hash = b;
			jQuery.historyCallback(a)
		} else if (jQuery.browser.safari) {
			jQuery.dontCheck = true;
			this.historyAddHistory(a);
			var e = function () {
				jQuery.dontCheck = false
			};
			window.setTimeout(e, 200);
			jQuery.historyCallback(a);
			location.hash = b
		} else {
			jQuery.historyCallback(a)
		}
	}
});