/*
 * mughpetti (for jQuery)
 * version: 1.1 (03/01/2008)
 * @requires jQuery v1.2 or later
 *
 * Examples at http://famspam.com/mughpetti/
 *
 * Licensed under the MIT:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2007, 2008 Chris Wanstrath [ chris@ozmm.org ]
 *
 * Usage:
 *  
 *  jQuery(document).ready(function() {
 *    jQuery('a[rel*=mughpetti]').mughpetti() 
 *  })
 *
 *  <a href="#terms" rel="mughpetti">Terms</a>
 *    Loads the #terms div in the box
 *
 *  <a href="terms.html" rel="mughpetti">Terms</a>
 *    Loads the terms.html page in the box
 *
 *  <a href="terms.png" rel="mughpetti">Terms</a>
 *    Loads the terms.png image in the box
 *
 *
 *  You can also use it programmatically:
 * 
 *    jQuery.mughpetti('some html')
 *
 *  This will open a mughpetti with "some html" as the content.
 *    
 *    jQuery.mughpetti(function() { ajaxes })
 *
 *  This will show a loading screen before the passed function is called,
 *  allowing for a better ajax experience.
 *
 */
(function($) {
  $.mughpetti = function(data, klass) {
    $.mughpetti.init()
    $.mughpetti.loading()
    $.isFunction(data) ? data.call($) : $.mughpetti.reveal(data, klass)
  }

  $.mughpetti.settings = {
    loading_image : 'facefiles/loading.gif',
    close_image   : 'facefiles/closelabel.gif',
    image_types   : [ 'png', 'jpg', 'jpeg', 'gif' ],
    mughpetti_html  : '\
  <div id="mughpetti" style="display:none;"> \
    <div class="popy"> \
      <table> \
        <tbody> \
          <tr> \
            <td class="topl"/><td class="btm"/><td class="topr"/> \
          </tr> \
          <tr> \
            <td class="btm"/> \
            <td class="bod"> \
              <div class="content"> \
              </div> \
              <div class="foot"> \
                <a href="#" class="close"> \
                  <img src="'+this.close_image+'" title="close" class="close_image" /> \
                </a> \
              </div> \
            </td> \
            <td class="btm"/> \
          </tr> \
          <tr> \
            <td class="btml"/><td class="btm"/><td class="btmr"/> \
          </tr> \
        </tbody> \
      </table> \
    </div> \
  </div>'
  }

  $.mughpetti.loading = function() {
    if ($('#mughpetti .loading').length == 1) return true

    $('#mughpetti .content').empty()
    $('#mughpetti .bod').children().hide().end().
      append('<div class="loading"><img src="'+$.mughpetti.settings.loading_image+'"/></div>')

    var pageScroll = $.mughpetti.getPageScroll()
    $('#mughpetti').css({
      top:	pageScroll[1] + ($.mughpetti.getPageHeight() / 10),
      left:	pageScroll[0]
    }).show()

    $(document).bind('keydown.mughpetti', function(e) {
      if (e.keyCode == 27) $.mughpetti.close()
    })
  }

  $.mughpetti.reveal = function(data, klass) {
    if (klass) $('#mughpetti .content').addClass(klass)
    $('#mughpetti .content').append(data)
    $('#mughpetti .loading').remove()
    $('#mughpetti .bod').children().fadeIn('normal')
  }

  $.mughpetti.close = function() {
    $(document).trigger('close.mughpetti')
    return false
  }

  $(document).bind('close.mughpetti', function() {
    $(document).unbind('keydown.mughpetti')
    $('#mughpetti').fadeOut(function() {
      $('#mughpetti .content').removeClass().addClass('content')
    })
  })

  $.fn.mughpetti = function(settings) {
    $.mughpetti.init(settings)

    var image_types = $.mughpetti.settings.image_types.join('|')
    image_types = new RegExp('\.' + image_types + '$', 'i')

    function click_handler() {
      $.mughpetti.loading(true)

      // support for rel="mughpetti[.inline_popup]" syntax, to add a class
      var klass = this.rel.match(/mughpetti\[\.(\w+)\]/)
      if (klass) klass = klass[1]

      // div
      if (this.href.match(/#/)) {
        var url    = window.location.href.split('#')[0]
        var target = this.href.replace(url,'')
        $.mughpetti.reveal($(target).clone().show(), klass)

      // image
      } else if (this.href.match(image_types)) {
        var image = new Image()
        image.onload = function() {
          $.mughpetti.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
        }
        image.src = this.href

      // ajax
      } else {
        $.get(this.href, function(data) { $.mughpetti.reveal(data, klass) })
      }

      return false
    }

    this.click(click_handler)
    return this
  }

  $.mughpetti.init = function(settings) {
    if ($.mughpetti.settings.inited) {
      return true
    } else {
      $.mughpetti.settings.inited = true
    }

    if (settings) $.extend($.mughpetti.settings, settings)
    $('body').append($.mughpetti.settings.mughpetti_html)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $.mughpetti.settings.close_image
    preload[1].src = $.mughpetti.settings.loading_image

    $('#mughpetti').find('.btm:first, .btml, .btmr, .topl, .topr').each(function() {
      preload.push(new Image())
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
    })

    $('#mughpetti .close').click($.mughpetti.close)
    $('#mughpetti .close_image').attr('src', $.mughpetti.settings.close_image)
  }

  // getPageScroll() by quirksmode.com
  $.mughpetti.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
  $.mughpetti.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);
