// Javascript Is Fun - Cabel's Blog Stuff
// http://www.cabel.name
//
// Miscellaneous functions for the ol' blog.

// Convenience Functions for our Buttons

function buttonOn(div) {
  fadeElementSetup(div, 100, 0, 4);
}

function buttonOff(div) {
  fadeElementSetup(div, 0, 100, 4);
}

// Previous Entries switcher

function previousRecent() {
   document.getElementById("previous-monthly").style.display = "none";
   document.getElementById("previous-recent").style.display = "";
   document.getElementById("previous-recent-gfx").src = "/images/previous-recent-on.png";
   document.getElementById("previous-monthly-gfx").src = "/images/previous-monthly-off.png";
   // This happens twice because it's not fully re-flowed the first time (a word is generally wrapped). Awkward.
   centerElement("nav-prev", "RecentBox");
   centerElement("nav-prev", "RecentBox");
}

function previousMonthly() {
   document.getElementById("previous-recent").style.display = "none";
   document.getElementById("previous-monthly").style.display = "";
   document.getElementById("previous-recent-gfx").src = "/images/previous-recent-off.png";
   document.getElementById("previous-monthly-gfx").src = "/images/previous-monthly-on.png";   
   centerElement("nav-prev", "RecentBox");
}

// General Item Centering Code

function centerElement(referenceID, theID) {
  infohost = document.getElementById(referenceID);
  infobox = document.getElementById(theID);

  // Find the location of where we should position the pop-up box
  // "referenceID" is the document element near where the pop-up box should appear.
  // This is passed in as a variable (openHere) from the caller.

  var infohostY = 0;
  var infohostX = 0;
  var count = 0;
  var infohostFind = infohost;

  // Iterate through the target item's many potential parents to calculate position X and Y values

  do {
    infohostY += infohostFind.offsetTop;
    infohostX += infohostFind.offsetLeft;
    // alert(infohostY + " " + infohostX + " / " + infohostFind.offsetTop + " " + infohostFind.offsetLeft + " " + infohostFind.offsetHeight);
  } while ( infohostFind = infohostFind.offsetParent )

  // Now position the pop-up box, based on the parent element's location and height.

  // alert(infohostX + " - " + infobox.offsetWidth + " - " + infohost.offsetWidth);

  infobox.style.left = infohostX - ((infobox.offsetWidth - infohost.offsetWidth) / 2);
  infobox.style.top = infohostY + (infohost.offsetHeight / 2) - 4;
  
  return;
}

// Function to make a box appear...
// This positions the box, too.

function showBox(openHere, doFade, box) {

  // doFade. 1 = Yes, fade it in. 0 = Leave it how it is, assuming it's open

  infohost = document.getElementById(openHere);
  infobox = document.getElementById(box);

  // Center the Element
  
  centerElement(openHere, box);

  // Now do the fade

  if (doFade == 1) {

    // Now make the box visible (fade in?)

    setOpacity(0, box);
    infobox.style.visibility = "visible";
    fadeElementSetup(box, 0, 100, 5);
  } else {
    setOpacity(100, box);
    infobox.style.visibility = "visible";
  }
}

// And to make a box dissapear.

function hideBox(box) {
  infobox = document.getElementById(box);
  if (infobox.style.visibility != "hidden" || infobox.style.visibility != "") {
    fadeElementSetup(box, 100, 0, 5, 1);
  }
}
