(function($){
    $.fn.extend({
      modalPanel: function() {

          //Create our overlay object
          var overlay = $("<div id='modal-overlay'></div>");
          // Create the window
          var modalWindow = $("<div id='modal-window'></div>");
          var modalWrap = $("<div id='cardpopup'></div>");
          var modalClose = $("<div class='modalClose' rel='modalClose'></div>");

          return this.each(function() {
        
              //Listen for clicks on objects passed to the plugin
              $(this).click(function(e) {
            	  // *** IE6 : select control bleeds through overlay so hide it 
            	  $("#selteam").css("visibility", "hidden");
            	  
            	  //Append the overlay to the document body
                  overlay.appendTo('body');
                  //Set the css and fade in our overlay
                  overlay.css("opacity", 0.6);
                  overlay.fadeIn(500);
                 
                  
                  // Modal window operations.
            	  // get the href url from the <a> tag.
            	  _hRef = $(this).attr('href');
            	  $.get(_hRef, function(data){
            		  modalWrap.empty();
            		  modalWrap.append(modalClose);
            		  modalWrap.append(data);
            		  modalWindow.append(modalWrap);
            		  
            		  modalWindow.prependTo('body').fadeIn(500);
            		  var mW = ($(window).width() - 850)/2;
                	  var mH = ($(window).height() - modalWindow.height())/2+$(window).scrollTop();
                	// we need the top of the modal window to always be visible.
                	  if (mH < 10) {
                		  mH = 20;
                	  }
                      modalWindow.css("opacity", 1.0);
                      modalWindow.css({left:mW, top:mH});
                      
                      $(modalClose).click(function() {
                    	  return modalHide();
                      });
            	  });
                  //Prevent the anchor link from loading
                  e.preventDefault();

                  //Activate a listener 
                  $(document).keydown(handleEscape);
                  
              });
          });
          
          //Our function for hiding the modalbox
          function modalHide() {
        		$(document).unbind("keydown", handleEscape)
        		var remove = function() { $(this).remove(); };
        		overlay.fadeOut(remove);
        		modalWindow
        			.fadeOut(remove)
        			.empty();
        		$("#selteam").css("visibility", "visible");
          }

          //Our function that listens for escape key.
          function handleEscape(e) {
              if (e.keyCode == 27) {
                  modalHide();
              }
          }
      }
  });
})(jQuery);

$(function() {
	$('a[rel*=overLay]').modalPanel();		
})