var Modal = {
  BorderWidth: 4,
  Width: {
    Narrow: 310,
    Wide: 500
  },
  
  backdrop: null,
  shadow: null,
  window: null,
  
  init: function() {
    $("a.executive-team-details").click(function() {
      if (this.href != null) {
        this.blur();
        Modal.show(this.href, Modal.Width.Narrow);
        return false;
      }
    });
    $("a.subject-matter-expert-details").click(function() {
      if (this.href != null) {
        this.blur();
        Modal.show(this.href, Modal.Width.Narrow);
        return false;
      }
    });
  },
  
  show: function(contentUrl, width) {
    var _this = this;
    
    if (isNaN(width) || width <= 0)
      width = 480;
    
    // Hide if already visible
    this.hide(true);
    
    // Load popup content
    var body = $("body");
    var modal = $(document.createElement("div")).css("width", width);
    modal.load(contentUrl + " .popup-container", function(responseText, textStatus, xhr) {
      if (textStatus != "success")
        return;
    
      // Determine size & positioning
      var bodyWidth = body.width();
      var bodyHeight = body.height();
      
      // Create backdrop
      _this.backdrop = $(document.createElement("div")).addClass("modal-backdrop");
      _this.backdrop.css({
        width: bodyWidth,
        height: bodyHeight,
        opacity: 0.001
      });
      _this.backdrop.click(function() {
        _this.hide(true);
        return false;
      });
      body.append(_this.backdrop);
      
      // Create modal window
      var closeButton = $("<div class='modal-close'><a href='#'>close</a></div>");
      closeButton.find("a").click(function() {
        _this.hide(true);
        return false;
      });
      
      _this.window = $(this);
      _this.window.addClass("modal-window");
      _this.window.css({
        visibility: "hidden"
      });
      _this.window.prepend(closeButton);
      body.append(_this.window);
      
      // Position window
      var visibleHeight = $(window).height();
      var windowHeight = _this.window.innerHeight();
      var windowTop = $(window).scrollTop();
      if (windowHeight + 40 > visibleHeight)
        windowTop += 20;
      else
        windowTop += (visibleHeight - windowHeight) / 2;
      var windowLeft = (bodyWidth - width) / 2;
      
      _this.window.css({
        top: windowTop,
        left: windowLeft,
        visibility: "visible"
      });
      
      // Create shadow
      _this.shadow = UIUtilities.createShadowDiv(width + (Modal.BorderWidth * 2), _this.window.innerHeight() + (Modal.BorderWidth * 2));
      _this.shadow.css({
        position: "absolute",
        zIndex: 9001,
        top: (windowTop - UIUtilities.Shadow.BlurRadius) + UIUtilities.Shadow.Offset,
        left: (windowLeft - UIUtilities.Shadow.BlurRadius) + UIUtilities.Shadow.Offset
      });
      body.append(_this.shadow);
      
      // Fix subject matter expert PNGs in IE6
      UIUtilities.fixImages(function() {
        return _this.window.find("img.expert-photo");
      });
    });
  },
  
  hide: function(immediate) {
    if (immediate == true) {
      if (this.backdrop != null) {
        this.backdrop.stop();
        this.backdrop.remove();
        this.backdrop = null;
      }
      
      if (this.shadow != null) {
        this.shadow.stop();
        this.shadow.remove();
        this.shadow = null;
      }
      
      if (this.window != null) {
        this.window.stop();
        this.window.remove();
        this.window = null;
      }
      
      return;
    }
  }
};

