/*
 * 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 ]
 *
 * Usage:
 *
 *  jQuery(document).ready(function() {
 *    jQuery('a[rel*=facebox]').facebox()
 *  })
 *
 *  <a href="#terms" rel="facebox">Terms</a>
 *    Loads the #terms div in the box
 *
 *  <a href="terms.html" rel="facebox">Terms</a>
 *    Loads the terms.html page in the box
 *
 *  <a href="terms.png" rel="facebox">Terms</a>
 *    Loads the terms.png image in the box
 *
 *
 *  You can also use it programmatically:
 *
 *    jQuery.facebox('some html')
 *    jQuery.facebox('some html', 'my-groovy-style')
 *
 *  The above will open a facebox with "some html" as the content.
 *
 *    jQuery.facebox(function($) {
 *      $.get('blah.html', function(data) { $.facebox(data) })
 *    })
 *
 *  The above will show a loading screen before the passed function is called,
 *  allowing for a better ajaxy experience.
 *
 *  The facebox function can also display an ajax page, an image, or the contents of a div:
 *
 *    jQuery.facebox({ ajax: 'remote.html' })
 *    jQuery.facebox({ ajax: 'remote.html' }, 'my-groovy-style')
 *    jQuery.facebox({ image: 'stairs.jpg' })
 *    jQuery.facebox({ image: 'stairs.jpg' }, 'my-groovy-style')
 *    jQuery.facebox({ div: '#box' })
 *    jQuery.facebox({ div: '#box' }, 'my-groovy-style')
 *
 *  Want to close the facebox?  Trigger the 'close.facebox' document event:
 *
 *    jQuery(document).trigger('close.facebox')
 *
 *  Facebox also has a bunch of other hooks:
 *
 *    loading.facebox
 *    beforeReveal.facebox
 *    reveal.facebox (aliased as 'afterReveal.facebox')
 *    init.facebox
 *    afterClose.facebox
 *
 *  Simply bind a function to any of these hooks:
 *
 *   $(document).bind('reveal.facebox', function() { ...stuff to do after the facebox and contents are revealed... })
 *
 */



(function($) {
  $.jmodal = function(data, klass) {
    $.jmodal.loading()

    if (data.ajax) filljmodalFromAjax(data.ajax, klass)
    else if (data.image) filljmodalFromImage(data.image, klass)
    else if (data.div) filljmodalFromHref(data.div, klass)
    else if ($.isFunction(data)) data.call($)
    else $.jmodal.reveal(data, klass)
  }

  /*
   * Public, $.jmodal methods
   */

  $.extend($.jmodal, {
    settings: {
      opacity      : 0.5,
      overlay      : true,
      loadingImage : 'content/js/jmodal/loading.gif',
      closeImage   : 'content/js/jmodal/closelabel.png',
      imageTypes   : [ 'png', 'jpg', 'jpeg', 'gif' ],
      jmodalHtml  : '\
    <div id="jmodal" style="display:none;"> \
      <div class="popup"> \
        <div class="content"> \
        </div> \
        <a href="#" class="close"><img src="content/js/jmodal/closelabel.png" title="close" class="close_image" /></a> \
      </div> \
    </div>'
    },

    loading: function() {
      init()
      if ($('#jmodal .loading').length == 1) return true
      showOverlay()

      $('#jmodal .content').empty()
      $('#jmodal .body').children().hide().end().
        append('<div class="loading"><img src="'+$.jmodal.settings.loadingImage+'"/></div>')

      $('#jmodal').css({
        top:	getPageScroll()[1] + (getPageHeight() / 10),
        left:	$(window).width() / 2 - 205
      }).show()

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

    reveal: function(data, klass) {
      $(document).trigger('beforeReveal.jmodal')
      if (klass) $('#jmodal .content').addClass(klass)
      $('#jmodal .content').append(data)
      $('#jmodal .loading').remove()
      $('#jmodal .body').children().fadeIn('normal')
      $('#jmodal').css('left', $(window).width() / 2 - ($('#jmodal .popup').width() / 2))
      $(document).trigger('reveal.jmodal').trigger('afterReveal.jmodal')
    },

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

  /*
   * Public, $.fn methods
   */

  $.fn.jmodal = function(settings) {
    if ($(this).length == 0) return

    init(settings)

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

      // support for rel="jmodal.inline_popup" syntax, to add a class
      // also supports deprecated "jmodal[.inline_popup]" syntax
      var klass = this.rel.match(/jmodal\[?\.(\w+)\]?/)
      if (klass) klass = klass[1]

      filljmodalFromHref(this.href, klass)
      return false
    }

    return this.bind('click.jmodal', clickHandler)
  }

  /*
   * Private methods
   */

  // called one time to setup jmodal on this page
  function init(settings) {
    if ($.jmodal.settings.inited) return true
    else $.jmodal.settings.inited = true

    $(document).trigger('init.jmodal')
    makeCompatible()

    var imageTypes = $.jmodal.settings.imageTypes.join('|')
    $.jmodal.settings.imageTypesRegexp = new RegExp('\.(' + imageTypes + ')$', 'i')

    if (settings) $.extend($.jmodal.settings, settings)
    $('body').append($.jmodal.settings.jmodalHtml)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $.jmodal.settings.closeImage
    preload[1].src = $.jmodal.settings.loadingImage

    $('#jmodal').find('.b:first, .bl').each(function() {
      preload.push(new Image())
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
    })

    $('#jmodal .close').click($.jmodal.close)
    $('#jmodal .close_image').attr('src', $.jmodal.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 = $.jmodal.settings

    $s.loadingImage = $s.loading_image || $s.loadingImage
    $s.closeImage = $s.close_image || $s.closeImage
    $s.imageTypes = $s.image_types || $s.imageTypes
    $s.jmodalHtml = $s.jmodal_html || $s.jmodalHtml
  }

  // Figures out what you want to display and displays it
  // formats are:
  //     div: #id
  //   image: blah.extension
  //    ajax: anything else
  function filljmodalFromHref(href, klass) {
    // div
    if (href.match(/#/)) {
      var url    = window.location.href.split('#')[0]
      var target = href.replace(url,'')
      if (target == '#') return
      $.jmodal.reveal($(target).html(), klass)

    // image
    } else if (href.match($.jmodal.settings.imageTypesRegexp)) {
      filljmodalFromImage(href, klass)
    // ajax
    } else {
      filljmodalFromAjax(href, klass)
    }
  }

  function filljmodalFromImage(href, klass) {
    var image = new Image()
    image.onload = function() {
      $.jmodal.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
    }
    image.src = href
  }

  function filljmodalFromAjax(href, klass) {
    $.get(href, function(data) { $.jmodal.reveal(data, klass) })
  }

  function skipOverlay() {
    return $.jmodal.settings.overlay == false || $.jmodal.settings.opacity === null
  }

  function showOverlay() {
    if (skipOverlay()) return

    if ($('#jmodal_overlay').length == 0)
      $("body").append('<div id="jmodal_overlay" class="jmodal_hide"></div>')

    $('#jmodal_overlay').hide().addClass("jmodal_overlayBG")
      .css('opacity', $.jmodal.settings.opacity)
      .click(function() { $(document).trigger('close.jmodal') })
      .fadeIn(200)
    return false
  }

  function hideOverlay() {
    if (skipOverlay()) return

    $('#jmodal_overlay').fadeOut(200, function(){
      $("#jmodal_overlay").removeClass("jmodal_overlayBG")
      $("#jmodal_overlay").addClass("jmodal_hide")
      $("#jmodal_overlay").remove()
    })

    return false
  }

  /*
   * Bindings
   */

  $(document).bind('close.jmodal', function() {
    $(document).unbind('keydown.jmodal')
    $('#jmodal').fadeOut(function() {
      $('#jmodal .content').removeClass().addClass('content')
      $('#jmodal .loading').remove()
      $(document).trigger('afterClose.jmodal')
    })
    hideOverlay()
  })

})(jQuery);

