/**
 * modalContent jQuery Plugin
 *
 * @version   0.11
 * @since     2006-11-28
 * @copyright Copyright (c) 2006 Glyphix Studio, Inc. http://www.glyphix.com
 * @author    Gavin M. Roy <gmr@glyphix.com>
 * @license   MIT http://www.opensource.org/licenses/mit-license.php
 * @requires  >= jQuery 1.0.3 http://jquery.com/
 * @requires  dimensions.js http://jquery.com/dev/svn/trunk/plugins/dimensions/dimensions.js?format=raw
 *
 */

/**
 * modalContent
 * @param content string to display in the content box
 * @param css obj of css attributes
 * @param animation (fadeIn, slideDown, show)
 * @param speed (valid animation speeds slow, medium, fast or # in ms)
 */
jQuery.modalContent = function(content, css, animation, speed) {

  if ( $('#modalBackdrop') ) $('#modalBackdrop').remove();
  if ( $('#modalContent') ) $('#modalContent').remove();

  if (self.pageYOffset) { // all except Explorer
  var wt = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
    var wt = document.documentElement.scrollTop;
  } else if (document.body) { // all other Explorers
    var wt = document.body.scrollTop;
  }

  var docHeight = $(document).outerHeight() + 50;
  var winHeight = $(window).height();
  var winWidth = $(window).innerWidth();
  if( docHeight < winHeight ) docHeight = winHeight;

  $('body').append('<div id="modalBackdrop" style="z-index: 1000; display: none;"></div><div id="modalContent" style="z-index: 1001; position: absolute;"><div id="modalContent2"></div>' + '' + '</div>');

  modalEventHandler = function( event ) {
    target = null;
    if ( event ) { //Mozilla
      target = event.target;
    } else { //IE
      event = window.event;
      target = event.srcElement;
    }
    if( $(target).filter('*:visible').parents('#modalContent').size() ) {
      // allow the event only if target is a visible child node of #modalContent
      return true;
    }
    if ( $('#modalContent') ) $('#modalContent').get(0).focus();
    return false;
  };
  $('body').bind( 'focus', modalEventHandler );
  $('body').bind( 'keypress', modalEventHandler );

  var modalContent = $('#modalContent').css({top:'-1000px'});
  swapNode($('#modalContent2')[0],content[0]);
  $('body').addClass('removeselect');
  
  var modalContent = $('#modalContent').css({top:'-1000px'});
  var mdcTop = wt + ( winHeight / 2 ) - (  modalContent.outerHeight() / 2);
  var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);
  
  $('#modalBackdrop').css({top:0}).css(css).height(docHeight + 'px').width(winWidth + 'px').show().css('filter','alpha(opacity=50)');
  
  
  
  modalContent.css({top: mdcTop + 'px', left: mdcLeft + 'px'}).show();

  modalContentClose = function(){close(); return false;};
  $('.close').bind('click', modalContentClose);

  function swapNode(a,b) {
   var nextSibling=a.nextSibling; if (nextSibling==b) nextSibling=a;
   var parentNode=a.parentNode;
   b.parentNode.replaceChild(a, b);
   parentNode.insertBefore(b, nextSibling); 
  }

  function close() {
    $(window).unbind('resize',  modalContentResize);
    $('body').unbind( 'focus', modalEventHandler);
    $('body').unbind( 'keypress', modalEventHandler );
    $('.close').unbind('click', modalContentClose);

    if ( animation == 'fadeIn' ) animation = 'fadeOut';
    if ( animation == 'slideDown' ) animation = 'slideUp';
    if ( animation == 'show' ) animation = 'hide';

    modalContent.hide()[animation](speed);
    $('body').removeClass('removeselect');
    swapNode($('#modalContent2')[0],content[0]);

    $('#modalContent').remove();$('#modalBackdrop').remove();
  };

   modalContentResize = function(){
    var docHeight = $(document).outerHeight();
    var winHeight = $(window).height();
    var winWidth = $(window).width();
    if( docHeight < winHeight ) docHeight = winHeight;

    var modalContent = $('#modalContent');
    var mdcTop = ( winHeight / 2 ) - (  modalContent.outerHeight() / 2);
    var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2);

    $('#modalBackdrop').height(docHeight + 'px').width(winWidth + 'px').show().css('filter:alpha(opacity=70)');
    modalContent.css({top:mdcTop + 'px',left:mdcLeft + 'px'}).show();
  };
  $(window).bind('resize', modalContentResize);

  $('#modalContent').focus();
};

jQuery.fn.modalContent = function(css, animation, speed)
{
  if (!animation) { var animation = 'show'; } else {
    if ( ( animation != 'fadeIn' ) && ( animation != 'slideDown') ) animation = 'show';
  }

  if ( !speed ) var speed = 'fast';

  css = jQuery.extend({
    position: 'absolute',
    left: '0px',
    margin: '0px',
    background: '#000',
    opacity: '.55'
  }, css);
  this.each(function(){
    //$(this).hide();
    new jQuery.modalContent($(this), css, animation, speed);
  });

  return this;
};

jQuery.fn.unmodalContent = function(animation, speed)
{
  if (!animation) { var animation = 'show'; } else {
    if ( ( animation != 'fadeOut' ) && ( animation != 'slideUp') ) animation = 'show';
  }
  if ( !speed ) var speed = 'fast';

  $(window).unbind('resize', modalContentResize);
  $('body').unbind( 'focus', modalEventHandler);
  $('body').unbind( 'keypress', modalEventHandler);
  $('.close').unbind('click', modalContentClose);

  function swapNode(a,b) {
   var nextSibling=a.nextSibling; if (nextSibling==b) nextSibling=a;
   var parentNode=a.parentNode;
   b.parentNode.replaceChild(a, b);
   parentNode.insertBefore(b, nextSibling); 
  }


  this.each(function(){
    if ( animation == 'fade' ) {
      $('#modalContent').fadeOut(speed,function(){$('#modalBackdrop').fadeOut(speed, function(){$(this).remove();});$(this).remove();});
    } else {
      if ( animation == 'slide' ) {
        $('#modalContent').slideUp(speed,function(){$('#modalBackdrop').slideUp(speed, function(){$(this).remove();});$(this).remove();});
      } else {
        $('body').removeClass('removeselect');
        swapNode($('#modalContent2')[0],this);

        $('#modalContent').remove();$('#modalBackdrop').remove();
      }
    }
  });
};
