/*** geändert von waYne ** 28.12.2011
 * hinzugefügt:
 * function showOverlay() {
 *	$("#page_content_content").addClass("fixed");
 * function hideOverlay() {
 *	$("#page_content_content").removeClass("fixed");
**/




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

    if (data.ajax) filloverlayFromAjax(data.ajax, klass)
    else if (data.image) filloverlayFromImage(data.image, klass)
    else if (data.div) filloverlayFromHref(data.div, klass)
    else if ($.isFunction(data)) data.call($)
    else $.overlay.reveal(data, klass)
  }

  /*
   * Public, $.overlay methods
   */

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

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

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

      $('#overlay').css({
        top:	getPageScroll()[1] + (getPageHeight() / 10),
        left:	$(window).width() / 2 - 205,
		"padding-bottom": "80px"
      }).show()

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

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

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

  /*
   * Public, $.fn methods
   */

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

    init(settings)

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

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

      filloverlayFromHref(this.href, klass, this.rev)
      return false
    }

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

  /*
   * Private methods
   */

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

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

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

    if (settings) $.extend($.overlay.settings, settings)
    $('body').append($.overlay.settings.overlayHtml)

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

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

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

    $s.loadingImage = $s.loading_image || $s.loadingImage
    $s.closeImage = $s.close_image || $s.closeImage
    $s.imageTypes = $s.image_types || $s.imageTypes
    $s.overlayHtml = $s.overlay_html || $s.overlayHtml
  }

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

    // image
    } else if (href.match($.overlay.settings.imageTypesRegexp)) {
      filloverlayFromImage(href, klass)
 
	// iframe
	} else if (rev.split('|')[0] == 'iframe') {
	  filloverlayFromIframe(href, klass, rev.split('|')[1], rev.split('|')[2])

    // ajax
    } else {
      filloverlayFromAjax(href, klass)
    }
  }

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

  function filloverlayFromIframe(href, klass, height, width) {
	$.overlay.reveal('<iframe scrolling="no" marginwidth="0" width="' + width + '" height="' + height + '" frameborder="0" src="' + href + '" marginheight="0"></iframe>', klass)
  }

  function filloverlayFromAjax(href, klass) {
    $.get(href, function(data) { $.overlay.reveal(data, klass) })
  }

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

  function showOverlay() {
	$("#page").addClass("fixed");
    if (skipOverlay()) return

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

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

  function hideOverlay() {
	$("#page").removeClass("fixed");
    if (skipOverlay()) return

    $('#overlay_overlay').fadeOut(200, function(){
      $("#overlay_overlay").removeClass("overlay_overlayBG")
      $("#overlay_overlay").addClass("overlay_hide")
      $("#overlay_overlay").remove()
    })

    return false
  }

  /*
   * Bindings
   */

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

})(jQuery);

