function f_Gen_OpenLegend(o) {
  var w=140;
  var h=280;
  window.open(
     'http://www.chipple.net/mt/legend.php',
     '',
     'width='+w+','+
     'height='+h+
     (self&&self.screenLeft&&o&&o.offsetLeft&&document&&document.body
        ?',left='+Math.max(Math.min(self.screenLeft+o.offsetLeft+o.offsetWidth+10-document.body.scrollLeft,screen.width-w-10),10)+
         ',top='+Math.max(Math.min(self.screenTop+o.offsetTop-self.document.body.scrollTop-Math.floor(h/2),screen.height-h-32),10)
        :''));
}

var bAddPopupImagesDone = false;
function f_Gen_PopupImage (href,alt,w,h) {
  window.open("/mt/archives/popup.php?fn="+escape(href)+"&alt="+escape(alt),"","width="+w+",height="+h+",left="+((screen.width-w)/2)+",top="+((screen.height-h-30)/2)+",scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0");
}
function f_Gen_AddPopupImages () {
  if (bAddPopupImagesDone) return; else bAddPopupImagesDone = true;
  var sTitle;
  var asImgDim;
  var rx = new RegExp("^[0-9]{1,4}x[0-9]{1,4}$","");
  var rxSnglQ = new RegExp("'","g");
  // NN6+, Moz, IE5+, Opera
  if (document.getElementsByTagName) {
    var aoA = document.getElementsByTagName("a");
    for (var i = 0; i < aoA.length; i++)
      if ((sTitle = aoA[i].getAttribute("title")) && rx.test(sTitle) && (asImgDim = sTitle.split("x")).length == 2) {
        aoA[i].onclick = new Function("f_Gen_PopupImage(this.href,this.getElementsByTagName('img')[0].alt.replace(new RegExp(\"'\",\"g\"),\"\\'\"),"+asImgDim.join(",")+");return false;");
        aoA[i].setAttribute("title","");
      }
  }
  // IE4+
  else if (document.links) {
    var aoA = document.links;
    for (var i = 0; i < aoA.length; i++)
      if ((sTitle = aoA[i].title) && rx.test(sTitle) && (asImgDim = sTitle.split("x")).length == 2) {
        aoA[i].onclick = new Function("f_Gen_PopupImage(this.href,'',"+asImgDim.join(",")+"); return false;");
        aoA[i].title = "";
      }
  }
/*
  // Flickr
  var aoImg = document.images;
  for (var i = 0; i < aoImg.length; i++)
    if ((aoImg[i].width == 114 || aoImg[i].width == 55) && aoImg[i].src.indexOf("http://static.flickr.com/") == 0)
      aoImg[i].src = "/mt/moblog/im/"+aoImg[i].height.toString()+"/"+escape(aoImg[i].src);
*/
}
// NN6+, Moz
if (window.addEventListener)
  window.addEventListener("load",f_Gen_AddPopupImages,false);
// IE5+, Opera
else if (window.attachEvent)
  window.attachEvent("onload",f_Gen_AddPopupImages);
// IE4+
else
  window.onload = f_Gen_AddPopupImages;

////////////////////////////////////////
// Hi-res Flickr images when printing //
////////////////////////////////////////

function f_Gen_onBeforePrint () {
  document.body.innerHTML = document.body.innerHTML.replace(new RegExp('\"(/mt/moblog/im/[0-9]+/)?(http://static\.flickr\.com/[0-9]+/[0-9a-f]+_[0-9a-f]+)_m\.jpg\"',"g"),'"$2.jpg#_m#$1"');
}
function f_Gen_onAfterPrint () {
  document.body.innerHTML = document.body.innerHTML.replace(new RegExp('\"(http://static\.flickr\.com/[0-9]+/[0-9a-f]+_[0-9a-f]+)\.jpg#_m#([^\"]*)\"',"g"),'"$2$1_m.jpg"');
}
// IE
function f_Gen_onBeforePrintAuto () {
  if (  document.body.innerHTML.indexOf("_m.jpg") != -1
     && confirm("Print using hi-res photos?\n(Download will occur, which may delay printing.)"))
    f_Gen_onBeforePrint();
}
window.onbeforeprint = f_Gen_onBeforePrintAuto;
window.onafterprint = f_Gen_onAfterPrint;
// Other browsers (using link)
function f_Gen_Print () {
  f_Gen_onBeforePrint();
  window.print();
//  f_OnAfterPrint(); // <- Can't use here because Firefox executes it before printing
}
