/*
 * GREATLY IMPROVED AND CUSTOMIZED FOR Fisting Underground site (Darkalleymedia)
 *
 * Facebox (for jQuery)
 * version: 1.1 (03/13/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')
 *
 *  This will open a facebox with "some html" as the content.
 *
 *    jQuery.facebox(function() { ajaxes })
 *
 *  This will show a loading screen before the passed function is called,
 *  allowing for a better ajax experience.
 *
 */
(function($) {
  $.facebox = function(data, klass) {
    $.facebox.init()
    $.facebox.loading()
    $.isFunction(data) ? data.call() : $.facebox.reveal(data, klass)
  }

  $.facebox.settings = {
    opacity      : 0.5,
    overlay      : true,
    loading_image : 'css/facebox/loading.gif',
    close_image   : 'css/facebox/closelabel.gif',
    image_types   : [ 'png', 'jpg', 'jpeg', 'gif' ],
    facebox_html  : '\
  <div id="facebox" 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="#a" class="close" >Close</a> \
              </div> \
            </td> \
            <td class="b"/> \
          </tr> \
          <tr> \
            <td class="bl"/><td class="b"/><td class="br"/> \
          </tr> \
        </tbody> \
      </table> \
    </div> \
  </div>'
  }

  $.facebox.loading = function() {
    if ($('#facebox .loading').length == 1) return true
    $.facebox.showOverlay();

    $('#facebox .content').empty()
    $('#facebox .body').children().hide().end().
      append('<div class="loading"><img src="'+$.facebox.settings.loading_image+'"/></div>')

    var pageScroll = $.facebox.getPageScroll()
    $('#facebox').css({
      top:  pageScroll[1],
//      top:  pageScroll[1] + ($.facebox.getPageHeight() / 10),
      left: pageScroll[0]
    }).show()

    $(document).bind('keydown.facebox', function(e) {
      if (e.keyCode == 27) $.facebox.close()
    })
  }

  $.facebox.reveal = function(data, klass) {
    if (klass) $('#facebox .content').addClass(klass)
    $('#facebox .content').append(data)
    $('#facebox .loading').remove()
    $('#facebox .body').children().fadeIn('normal')
  }

  $.facebox.close = function() {
    $(document).trigger('close.facebox')
    return false
  }

  $(document).bind('close.facebox', function() {
    $(document).unbind('keydown.facebox')
    $.facebox.hideOverlay()
    $('#facebox').fadeOut(function() {
      $('#facebox .content').removeClass().addClass('content')
    })
  })

  $.fn.facebox = function(settings) {
    $.facebox.init(settings)

    var image_types = $.facebox.settings.image_types.join('|')
    image_types = new RegExp('\.' + image_types + '$', 'i')

    function click_handler() {
      $.facebox.loading(true)

      // support for rel="facebox[.inline_popup]" syntax, to add a class
      var klass = this.rel.match(/facebox\[\.(\w+)\]/)
      if (klass) klass = klass[1]

      // div
      if (this.href.match(/#/)) {
        var url    = window.location.href.split('#')[0]
        var target = this.href.replace(url,'')
        $.facebox.reveal($(target).clone().show(), klass)

      // image
      } else if (this.href.match(image_types)) {
        $.facebox.handleFaceboxImage(this.href, klass);

      // ajax
      } else {
        $.get(this.href, function(data) { $.facebox.reveal(data, klass) })
      }

      return false
    }

    this.click(click_handler)
    return this
  }

  $.facebox.handleFaceboxImage = function(href, klass) {
    $('#facebox .loading').remove()
    $('#facebox .content').append('<div class="loading"><img src="'+$.facebox.settings.loading_image+'"/></div>')

        $.facebox.settings.image = new Image()
        $.facebox.settings.image.onload = function() {
          var image = $.facebox.settings.image;
          if (!image.width || !image.height) {
            return;
          }

          var screenHeight = $.facebox.getPageHeight();
          var screenWidth = $.facebox.getPageWidth();
          var aspectScreen = screenWidth/screenHeight;
          var aspectImage = image.width/image.height;
          var pageScroll = $.facebox.getPageScroll()

          if (aspectScreen > aspectImage) {
            if (image.height > screenHeight) {
              $.facebox.reveal('<div class="image"><img id="largest_slide" src="' + image.src + '" height="' + (screenHeight-80) + '" /></div>', klass)
                $('#facebox').css({
                    top: pageScroll[1]
                });

            } else {
              $.facebox.reveal('<div class="image"><img id="largest_slide" src="' + image.src + '" /></div>', klass)
              $('#facebox').css({
                  top: pageScroll[1] +  (screenHeight - image.height)/2
              });
            }
          } else {
            if (image.width > screenWidth) {
              $.facebox.reveal('<div class="image"><img id="largest_slide" src="' + image.src + '" width="' + (screenWidth-40) + '" /></div>', klass)
                //NICE TWEAK? :-)
                $('#facebox').css({
                    top: pageScroll[1] +  (screenHeight - (screenWidth / image.width) * image.height)/2
                });

            } else {
              $.facebox.reveal('<div class="image"><img id="largest_slide" src="' + image.src + '" /></div>', klass)
              $('#facebox').css({
                  top: pageScroll[1] +  (screenHeight - image.height)/2
              });
            }
          }
        }
        $.facebox.settings.image.src = href;
  }

  $.facebox.init = function(settings) {
    if ($.facebox.settings.inited) {
      return true
    } else {
      $.facebox.settings.inited = true
    }

    if (settings) $.extend($.facebox.settings, settings)

    $('body').append($.facebox.settings.facebox_html)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $.facebox.settings.close_image
    preload[1].src = $.facebox.settings.loading_image

    $('#facebox').find('.b:first, .bl, .br, .tl, .tr').each(function() {
      preload.push(new Image())
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
    })

    $('#facebox .close').click($.facebox.close)
    $('#facebox .close_image').attr('src', $.facebox.settings.close_image)
  }

  // getPageScroll() by quirksmode.com
  $.facebox.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
  $.facebox.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
  }

  // adapter from getPageSize() by quirksmode.com
  $.facebox.getPageWidth = function() {
    var windowWidth
    if (self.innerWidth) { // all except Explorer
      windowWidth = self.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
    } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
    }
    return windowWidth
  }
  

  $.facebox.skipOverlay = function() {
    return $.facebox.settings.overlay == false || $.facebox.settings.opacity === null 
  }

  $.facebox.showOverlay = function () {
    if ($.facebox.skipOverlay()) return

    if ($('facebox_overlay').length == 0) 
      $("body").append('<div id="facebox_overlay" class="facebox_hide"></div>')

    $('#facebox_overlay').hide().addClass("facebox_overlayBG")
      .css('opacity', $.facebox.settings.opacity)
      .click(function() { $(document).trigger('close.facebox') })
      .fadeIn(200)
    return false
  }

  $.facebox.hideOverlay = function () {
    if ($.facebox.skipOverlay()) return

    $('#facebox_overlay').fadeOut(200, function(){
      $("#facebox_overlay").removeClass("facebox_overlayBG")
      $("#facebox_overlay").addClass("facebox_hide") 
      $("#facebox_overlay").remove()
    })
    
    return false
  }

  
  
})(jQuery);
