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