/*
 * Facebox (for jQuery)
 * version: 1.2 (05/05/2008)
 * @requires jQuery v1.2 or later
 *
 * Examples at http://famspam.com/facebox/
 *
 * Licensed under the MIT:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2007, 2008 Chris Wanstrath [ chris@ozmm.org ]
 *
 */
(function($) {
  $.facebox = function(data, klass) {
    $.facebox.loading()
	if ( typeof data == 'string' ) $.facebox.reveal(data, klass)
	else fillFaceboxFromHref(data, klass)
  }

  $.extend($.facebox, {
    settings: {
      opacity      : 0.8,
      closeImage   : './images/img-closepopup.gif',
      faceboxHtml  : '\
      <div id="facebox" style="display:none;"> \
        <div class="popup"> \
          <a href="#" class="close">close</a> \
          <div class="content"> \
          </div> \
        </div> \
      </div>'
    },

    loading: function() {
      init()
      showOverlay()
      $('#facebox').css({
        top:	getPageScroll()[1] + (getPageHeight() / 10),
        left:	$(window).width() / 2 - 600
      }).show()

      $(document).bind('keydown.facebox', function(e) {
        if (e.keyCode == 27) $.facebox.close()
        return true
      })
    },

    reveal: function(data, klass) {
      if (klass) $('#facebox .content').addClass(klass)
      $('#facebox:first .content').empty();
      $('#facebox:first .content').append(data)
      $('#facebox').css('left', $(document).width() / 2 - ($('#facebox .popup').width() / 2))
    },

    close: function() {
      $(document).trigger('close.facebox')
      return false
    }
  })

  $.fn.facebox = function(settings) {
    init(settings)

    function clickHandler() {
      $.facebox.loading(true)

      var klass = this.rel.match(/facebox\[?\.(\w+)\]?/)
      if (klass) klass = klass[1]

      fillFaceboxFromHref(this.href, klass)
      return false
    }
//$.dump(this)
    return this.bind('click.facebox', clickHandler);
  }

  // called one time to setup facebox on this page
  function init(settings) {
    if (settings) $.extend($.facebox.settings, settings)
    $('body').append($.facebox.settings.faceboxHtml)
    $('#facebox .close').click($.facebox.close)
  }
  // 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
  }

  function fillFaceboxFromHref(href, klass) {
      var url    = window.location.href.split('#')[0]
      var target = href.substr(href.indexOf('#'));
      $.facebox.reveal($(target).show().replaceWith("<div id='facebox_moved'></div>"), klass)
  }

  function showOverlay() {
    if ($('#facebox_overlay').length == 0) 
      $("body").append('<div id="facebox_overlay" class="facebox_hide"></div>')

    $('#facebox_overlay').addClass("facebox_overlayBG")
      .css('opacity', $.facebox.settings.opacity)
      .click(function() { $(document).trigger('close.facebox') })
    $('select').each(function(){
	$(this).css('visibility','hidden');
    });
    return false
  }

  function hideOverlay() {
    $('#facebox_overlay').hide(1, function(){
      $("#facebox_overlay").removeClass("facebox_overlayBG")
      $("#facebox_overlay").addClass("facebox_hide") 
      $("#facebox_overlay").remove()
    })
    $('select').each(function(){
	$(this).css('visibility','visible');
    });
    
    return false
  }

  $(document).bind('close.facebox', function() {
    $(document).unbind('keydown.facebox')
    $('#facebox').hide(1,function() {
      if ($('#facebox_moved').length == 0) $('#facebox .content').removeClass().addClass('content')
      else $('#facebox_moved').replaceWith($('#facebox .content').children().hide())
      hideOverlay()
    })
  })

})(jQuery);
