/*
 * facebox2 (for jQuery)
 * version: 1.2 (05/05/2008)
 * @requires jQuery v1.2 or later
 *
 * Examples at http://famspam.com/facebox2/
 *
 * 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*=facebox2]').facebox2() 
 *  })
 *
 *  <a href="#terms" rel="facebox2">Terms</a>
 *    Loads the #terms div in the box
 *
 *  <a href="../../facebox/terms.html" rel="facebox2">Terms</a>
 *    Loads the terms.html page in the box
 *
 *  <a href="../../facebox/terms.png" rel="facebox2">Terms</a>
 *    Loads the terms.png image in the box
 *
 *
 *  You can also use it programmatically:
 * 
 *    jQuery.facebox2('some html')
 *
 *  The above will open a facebox2 with "some html" as the content.
 *    
 *    jQuery.facebox2(function($) { 
 *      $.get('blah.html', function(data) { $.facebox2(data) })
 *    })
 *
 *  The above will show a loading screen before the passed function is called,
 *  allowing for a better ajaxy experience.
 *
 *  The facebox2 function can also display an ajax page or image:
 *  
 *    jQuery.facebox2({ ajax: 'remote.html' })
 *    jQuery.facebox2({ image: 'dude.jpg' })
 *
 *  Want to close the facebox2?  Trigger the 'close.facebox2' document event:
 *
 *    jQuery(document).trigger('close.facebox2')
 *
 *  facebox2 also has a bunch of other hooks:
 *
 *    loading.facebox2
 *    beforeReveal.facebox2
 *    reveal.facebox2 (aliased as 'afterReveal.facebox2')
 *    init.facebox2
 *
 *  Simply bind a function to any of these hooks:
 *
 *   $(document).bind('reveal.facebox2', function() { ...stuff to do after the facebox2 and contents are revealed... })
 *
 */
(function($) {
  $.facebox2 = function(data, klass) {
    $.facebox2.loading()

    if (data.ajax) fillfacebox2FromAjax(data.ajax)
    else if (data.image) fillfacebox2FromImage(data.image)
    else if (data.div) fillfacebox2FromHref(data.div)
    else if ($.isFunction(data)) data.call($)
    else $.facebox2.reveal(data, klass)
  }

  /*
   * Public, $.facebox2 methods
   */

  $.extend($.facebox2, {
    settings: {
      opacity      : 0.2,
      overlay      : true,
      loadingImage : '../facebox/loading.gif',
      closeImage   : '../facebox/closelabel.gif',
      imageTypes   : [ 'png', 'jpg', 'jpeg', 'gif' ],
      facebox2Html  : '\
    <div id="facebox2" 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" onClick="parent.location.reload(1)"> \
                    <img src="../../facebox/closelabel.gif" 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>'
    },

    loading: function() {
      init()
      if ($('#facebox2 .loading').length == 1) return true
      showOverlay()

      $('#facebox2 .content').empty()
      $('#facebox2 .body').children().hide().end().
        append('<div class="loading"><img src="../../facebox/'+$.facebox2.settings.loadingImage+'"/></div>')

      $('#facebox2').css({
        top:	getPageScroll()[1] + (getPageHeight() / 10),
        /* Hides loading bar off the side of the screen
		left:	385.5 (original)
		*/
		left: -1000
		
		
      }).show()

      $(document).bind('keydown.facebox2', function(e) {
        if (e.keyCode == 27) $.facebox2.close()
        return true
      })
      $(document).trigger('loading.facebox2')
    },

    reveal: function(data, klass) {
      $(document).trigger('beforeReveal.facebox2')
      if (klass) $('#facebox2 .content').addClass(klass)
      $('#facebox2 .content').append(data)
      $('#facebox2 .loading').remove()
      $('#facebox2 .body').children().fadeIn('normal')
      $('#facebox2').css('left', $(window).width() / 2 - ($('#facebox2 table').width() / 2))
      $(document).trigger('reveal.facebox2').trigger('afterReveal.facebox2')
    },

    close: function() {
      $(document).trigger('close.facebox2')
      return false
    }
  })

  /*
   * Public, $.fn methods
   */

  $.fn.facebox2 = function(settings) {
    init(settings)

    function clickHandler() {
      $.facebox2.loading(true)

      // support for rel="facebox2.inline_popup" syntax, to add a class
      // also supports deprecated "facebox2[.inline_popup]" syntax
      var klass = this.rel.match(/facebox2\[?\.(\w+)\]?/)
      if (klass) klass = klass[1]

      fillfacebox2FromHref(this.href, klass)
      return false
    }

    return this.click(clickHandler)
  }

  /*
   * Private methods
   */

  // called one time to setup facebox2 on this page
  function init(settings) {
    if ($.facebox2.settings.inited) return true
    else $.facebox2.settings.inited = true

    $(document).trigger('init.facebox2')
    makeCompatible()

    var imageTypes = $.facebox2.settings.imageTypes.join('|')
    $.facebox2.settings.imageTypesRegexp = new RegExp('\.' + imageTypes + '$', 'i')

    if (settings) $.extend($.facebox2.settings, settings)
    $('body').append($.facebox2.settings.facebox2Html)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $.facebox2.settings.closeImage
    preload[1].src = $.facebox2.settings.loadingImage

    $('#facebox2').find('.b:first, .bl, .br, .tl, .tr').each(function() {
      preload.push(new Image())
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
    })

    $('#facebox2 .close').click($.facebox2.close)
    $('#facebox2 .close_image').attr('src', $.facebox2.settings.closeImage)
  }
  
  // getPageScroll() by quirksmode.com
  function getPageScroll() {
    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) 
  }

  // Adapted from getPageSize() by quirksmode.com
  function getPageHeight() {
    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
  }

  // Backwards compatibility
  function makeCompatible() {
    var $s = $.facebox2.settings

    $s.loadingImage = $s.loading_image || $s.loadingImage
    $s.closeImage = $s.close_image || $s.closeImage
    $s.imageTypes = $s.image_types || $s.imageTypes
    $s.facebox2Html = $s.facebox2_html || $s.facebox2Html
  }

  // Figures out what you want to display and displays it
  // formats are:
  //     div: #id
  //   image: blah.extension
  //    ajax: anything else
  function fillfacebox2FromHref(href, klass) {
    // div
    if (href.match(/#/)) {
      var url    = window.location.href.split('#')[0]
      var target = href.replace(url,'')
      $.facebox2.reveal($(target).clone().show(), klass)

    // image
    } else if (href.match($.facebox2.settings.imageTypesRegexp)) {
      fillfacebox2FromImage(href, klass)
    // ajax
    } else {
      fillfacebox2FromAjax(href, klass)
    }
  }

  function fillfacebox2FromImage(href, klass) {
    var image = new Image()
    image.onload = function() {
      $.facebox2.reveal('<div class="image"><img src="../../facebox/' + image.src + '" /></div>', klass)
    }
    image.src = href
  }

  function fillfacebox2FromAjax(href, klass) {
    $.get(href, function(data) { $.facebox2.reveal(data, klass) })
  }

  function skipOverlay() {
    return $.facebox2.settings.overlay == false || $.facebox2.settings.opacity === null 
  }

  function showOverlay() {
    if (skipOverlay()) return

    if ($('facebox2_overlay').length == 0) 
      $("body").append('<div id="facebox2_overlay" class="facebox2_hide"></div>')

    $('#facebox2_overlay').hide().addClass("facebox2_overlayBG")
      .css('opacity', $.facebox2.settings.opacity)
      .click(function() { $(document).trigger('close.facebox2') })
      .fadeIn(200)
    return false
  }

  function hideOverlay() {
    if (skipOverlay()) return

    $('#facebox2_overlay').fadeOut(200, function(){
      $("#facebox2_overlay").removeClass("facebox2_overlayBG")
      $("#facebox2_overlay").addClass("facebox2_hide") 
      $("#facebox2_overlay").remove()
    })
    
    return false
  }

  /*
   * Bindings
   */

  $(document).bind('close.facebox2', function() {
    $(document).unbind('keydown.facebox2')
    $('#facebox2').fadeOut(function() {
      $('#facebox2 .content').removeClass().addClass('content')
      hideOverlay()
      $('#facebox2 .loading').remove()
    })
  })

})(jQuery);

