
(function($) {
  $.imgbox = function(data, klass) {
    $.imgbox.init()
    $.imgbox.loading()
    $.isFunction(data) ? data.call($) : $.imgbox.reveal(data, klass)
  }

  $.imgbox.settings = {
    loading_image : 'images/loading.gif',
    close_image   : 'images/closelabel.gif',
    image_types   : [ 'png', 'jpg', 'jpeg', 'gif' ],
    imgbox_html  : '\
  <div id="imgbox" style="display:none;"> \
    <div class="popup"> \
      <table> \
        <tbody> \
          <tr> \
            <td class="tl"/><td class="b"/><td class="tr"/> \
          </tr> \
          <tr> \
            <td class="b"/> \
            <td class="body"> \
              <div class="content"> \
              </div> \
              <div class="footer"> \
                <a href="#" class="close"> \
                  <img src="'+this.close_image+'" title="close" class="close_image" /> \
                </a> \
              </div> \
            </td> \
            <td class="b"/> \
          </tr> \
          <tr> \
            <td class="bl"/><td class="b"/><td class="br"/> \
          </tr> \
        </tbody> \
      </table> \
    </div> \
  </div>'
  }

  $.imgbox.loading = function() {
    if ($('#imgbox .loading').length == 1) return true

    $('#imgbox .content').empty()
    $('#imgbox .body').children().hide().end().
      append('<div class="loading"><img src="'+$.imgbox.settings.loading_image+'"/></div>')

    var pageScroll = $.imgbox.getPageScroll()
    $('#imgbox').css({
      top:	pageScroll[1] + ($.imgbox.getPageHeight() / 10),
      left:	pageScroll[0]
    }).show()

    $(document).bind('keydown.imgbox', function(e) {
      if (e.keyCode == 27) $.imgbox.close()
    })
  }

  $.imgbox.reveal = function(data, klass) {
    if (klass) $('#imgbox .content').addClass(klass)
    $('#imgbox .content').append(data)
    $('#imgbox .loading').remove()
    $('#imgbox .body').children().fadeIn('normal')
  }

  $.imgbox.close = function() {
    $(document).trigger('close.imgbox')
    return false
  }

  $(document).bind('close.imgbox', function() {
    $(document).unbind('keydown.imgbox')
    $('#imgbox').fadeOut(function() {
      $('#imgbox .content').removeClass().addClass('content')
    })
  })

  $.fn.imgbox = function(settings) {
    $.imgbox.init(settings)

    var image_types = $.imgbox.settings.image_types.join('|')
    image_types = new RegExp('\.' + image_types + '$', 'i')

    function click_handler() {
      $.imgbox.loading(true)

      // support for rel="imgbox[.inline_popup]" syntax, to add a class
      var klass = this.rel.match(/imgbox\[\.(\w+)\]/)
      if (klass) klass = klass[1]

      // div
      if (this.href.match(/#/)) {
        var url    = window.location.href.split('#')[0]
        var target = this.href.replace(url,'')
        $.imgbox.reveal($(target).clone().show(), klass)

      // image
      } else if (this.href.match(image_types)) {
        var image = new Image()
        image.onload = function() {
          $.imgbox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
        }
        image.src = this.href

      // ajax
      } else {
        $.get(this.href, function(data) { $.imgbox.reveal(data, klass) })
      }

      return false
    }

    this.click(click_handler)
    return this
  }

  $.imgbox.init = function(settings) {
    if ($.imgbox.settings.inited) {
      return true
    } else {
      $.imgbox.settings.inited = true
    }

    if (settings) $.extend($.imgbox.settings, settings)
    $('body').append($.imgbox.settings.imgbox_html)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $.imgbox.settings.close_image
    preload[1].src = $.imgbox.settings.loading_image

    $('#imgbox').find('.b:first, .bl, .br, .tl, .tr').each(function() {
      preload.push(new Image())
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
    })

    $('#imgbox .close').click($.imgbox.close)
    $('#imgbox .close_image').attr('src', $.imgbox.settings.close_image)
  }

  // getPageScroll() by quirksmode.com
  $.imgbox.getPageScroll = function() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;	
    }
    return new Array(xScroll,yScroll) 
  }

  // adapter from getPageSize() by quirksmode.com
  $.imgbox.getPageHeight = function() {
    var windowHeight
    if (self.innerHeight) {	// all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }	
    return windowHeight
  }
})(jQuery);
