var PopupSettings = {
  throbber:'images/ajax-loader.gif',           //the throbber image to show when loading
  width:'800px',                               //the width of the popup
  yuiSkinClass:'yui-skin-sam',                 //the css class to apply to the body tag
  matteBackgroundImage:'images/50percent.png', //the background image for the matte border of the popup
  defaultTitle: null                           //default title
};
var PopupState = {};
function getFullPath(relpath) {
  var loc = document.location;
  if (relpath.indexOf('://') > -1) return relpath;
  if (relpath[0] == '/') {
    return loc.protocol + '//' + loc.hostname + relpath;
  } else {
    var lastSlash = loc.pathname.lastIndexOf('/');
    var path = loc.pathname.substring(0,lastSlash+1);
    return loc.protocol + '//' + loc.hostname + path + relpath;
  }
}
function showPopup(tag,path,heading) {
  var dom = YAHOO.util.Dom;
  var heading = heading || tag.title || PopupSettings.defaultTitle;
  var panel;
  if (!tag.panel) {
    panel = tag.panel = new YAHOO.widget.Panel(dom.generateId(),{close:true,draggable:false,modal:true,underlay:'matte',fixedcenter:false,width:(PopupSettings.width)});
    panel.setBody("Loading <img src='" + PopupSettings.throbber + "'>");
    var trans = YAHOO.util.Connect.asyncRequest('GET',getFullPath(path),{
      success:function(o) {
        /* this used to be prepended to the response text: '<div style="width:25px;height:15px;float:right;overflow:hidden;">&nbsp;</div>' */
        panel.setBody(o.responseText);
        panel.center();
        var docHeight = YAHOO.util.Dom.getDocumentHeight();
        YAHOO.util.Dom.setStyle(YAHOO.util.Selector.query('div.mask'),'height',docHeight + 'px');
      },
      failure:function(o) {
        panel.setBody('Error: ' + o.status + ' ' + o.statusText);
        panel.center();
      }
    });
  } else {
    panel = tag.panel;
  }
  if (heading) {
    panel.setHeader(heading);
  }
  panel.render(document.body);
  panel.show();
  panel.center();
  PopupState.currentPanel = panel;
  var matte = YAHOO.util.Selector.query('div.matte');
  YAHOO.util.Dom.setStyle(matte,'background-image','url(' + PopupSettings.matteBackgroundImage + ')');
  YAHOO.util.Dom.setStyle(matte,'background-color','transparent');
  Popup_HookMasks();
}

function Popup_HookMasks() {
  var masks = YAHOO.util.Selector.query('div.mask');
  for (var i=0;i<masks.length;i++) {
    if (!masks[i].popuphooked) {
      masks[i].popuphooked = true;
      YAHOO.util.Event.on(masks[i],'click',function(){PopupState.currentPanel.hide();});
    }
  }
}


YAHOO.util.Event.on(window,'load',function() {
  //apply the skin class, if there is one
  if (PopupSettings.yuiSkinClass) {
    YAHOO.util.Dom.addClass(document.body,PopupSettings.yuiSkinClass);
  }
  //find all links that should be popups
  var links = YAHOO.util.Selector.query('a.popup');
  YAHOO.util.Event.on(links,'click',function(e) {
    YAHOO.util.Event.preventDefault(e);
    showPopup(this,this.href);
  });
});
