// element handlers
var objLoaderMsg = new Object();
var objPage = new Object();

// current page
var currPage = "home";

// timeout handlers
var mystifier = null;
var pageShower = null;

// page positioning for the various pages
var pgPositioning = new Array();
// left, top, width, height
pgPositioning["home"] = Array(125,0,700,490);
pgPositioning["photos"] = Array(0,0,900,490);
pgPositioning["guestbook"] = Array(125,0,700,490);

var pgDispName = new Array();
pgDispName["default"] = "Butterflies & Lilies";
pgDispName["home"] = 'Thanks!';
pgDispName["photos"] = "Wedding Photos";
pgDispName["guestbook"] = "Our Guestbook";


function init(action)
{
  switch (action)
  {
    case 0:
      // point the element handlers to the objects on the page
      objLoaderMsg = document.getElementById('pgLoader');
      objPage.doc = document.getElementById('myPageFrame');
      objPage.page = document.getElementById('myPage');
      objPage.actioning = 0;
      preLoadImages("home");
      setTimeout("init(1)",1000);
      break;;
    case 1:

      document.getElementById('content').style.background = 'url(images/header_tile.jpg)';
      document.getElementById('content').style.backgroundRepeat = 'repeat-x';


      document.getElementById('mainWindow').style.background = 'url(images/header.jpg)';
      document.getElementById('mainWindow').style.backgroundRepeat = 'no-repeat';
      document.getElementById('menu').style.visibility = 'visible';
      setTimeout("init(2)",15);
      break;;
    case 2:
      rollMe('btnPhotos',1);
      setTimeout("init(3)",150);
      break;;
    case 3:
      rollMe('btnHome',1);
      setTimeout("init(4)",150);
      break;;
    case 4:
      rollMe('btnGuestbook',1);

      setTimeout("init(5)",150);
      break;;
    case 5:
      rollMe('btnPhotos',0);
      setTimeout("init(6)",150);
      break;;
    case 6:
      rollMe('btnHome',0);
      setTimeout("init(7)",150);
      break;;
    case 7:
      rollMe('btnGuestbook',0);
      setTimeout("init(8)",150);
      break;;
    case 8:
      showPage('home',1);
      break;;
  }

}

function showPage(pgName,currAction)
{
  // ignore any requests to the current page
  if ((pgName == currPage) && (currAction == 0)) return;

  switch (parseInt(currAction))
  {
    // hide the current page or force a new page to load if a page
    // is already loading
    case 0:
      // hide the current page content
      objPage.page.style.visibility = 'hidden';
      // set the loader message and change the title to the default message
      objLoaderMsg.src = "images/loader_msg_" + pgName + ".jpg";
      document.title = pgDispName["default"];
      // if a page is loading, clear the current timeout
      // and force the new page into loading
      if (objPage.actioning == 1 && objPage.action == "loading") 
      {
        clearTimeout(pageShower);
        clearTimeout(mystifier);
        objPage.actioning = 0;
        objPage.action = "";
        pageShower = setTimeout("showPage('" + pgName + "',1)",15); 
      }
      // if no action is taking place, proceed with normal page display
      else if (objPage.actioning == 0){
        objPage.action = "";
        loader(1,pgName);
      }
      break;;
    // load a requested page
    case 1:
      //document.getElementById('loaderProgress').innerHTML = pgName;
      // wait for the page to load, the loaded page is responsible
      // for updating the actioning value of objPage once it is
      // done loading
      objPage.actioning = 1;
      objPage.action = "loading";
      // resize the page frame
      objPage.page.style.left = parseInt(pgPositioning[pgName][0] + 10) + 'px';
      objPage.page.style.top = parseInt(pgPositioning[pgName][1] + 10) + 'px';;
      objPage.page.style.width = parseInt(pgPositioning[pgName][2] - 20) + 'px';
      objPage.page.style.height = parseInt(pgPositioning[pgName][3] - 20) + 'px';
      // replace the background image
      objPage.doc.src = "images/" + pgName + "_tile.gif";
      // load the page content
      //objPage.page.src = pgName + '.php';
      self.frames["myPage"].location.replace(pgName + '.php');
      break;;
    // show the page
    case 2:
      loader(0, pgName);
      break;;
    default:
      // do nothing
      break;;
  }

 // if an action is pending, recall this function to determine
 // what to wait for the action to finish
  if (objPage.actioning == 1)
  {
    pageShower = setTimeout("showPage('" + pgName + "',5)",15);
  }
  // if no action is pending, determine the next action to perform
  else if (objPage.actioning == 0)
  {
    switch (objPage.action)
    {
      // if the last known action was hiding, then the next action would be to 
      // load the page, recall this function with the load option
      case "hiding":
        pageShower = setTimeout("showPage('" + pgName + "',1)",15);
        break;;
      // if the last known action was loading, then the next action would be to
      // show the page, recall this function with the show option
      case "loading":
        pageShower = setTimeout("showPage('" + pgName + "',2)",15);
        break;;
      case "showing":
        // show the page content and clear the actions
        objPage.page.style.visibility = 'visible';
        document.title = pgDispName["default"] + " :: " + pgDispName[pgName];
        objPage.action = "";
        break;;
      // do nothing
      default:
        break;;
    }
  }
}                             

// manages the loading and hiding of a page
function loader(dir, page)
{
  switch (dir)
  {
  case 1:
    objPage.fade = 1;
    objPage.shrink = 10;
    objPage.deg = 0;
    // get the position of the current page for fading
    objPage.left = pgPositioning[currPage][0];
    objPage.top = pgPositioning[currPage][1];
    objPage.width = pgPositioning[currPage][2];
    objPage.height = pgPositioning[currPage][3];
    objPage.opacity = 100;
    objPage.action = "hiding";
    objPage.actioning = 1;
    mystify();
    break;;
  case 0:
    objPage.fade = -1;
    objPage.deg = 0;
    objPage.opacity = 1;
    objPage.left = pgPositioning[page][0] + 10;
    objPage.top = pgPositioning[page][1] + 10;
    objPage.width = pgPositioning[page][2] - 20;
    objPage.height = pgPositioning[page][3] - 20;
    objPage.action = "showing";
    objPage.actioning = 1;
    mystify();
    break;;
  }
}

// shrinks or expands the page object by 20 pixels and fades the object in or out
function mystify() {

  // update the size and position of the object to simulate shrink or growth
  objPage.doc.style.left = parseInt(objPage.left) + Math.round((Math.sin(objPage.deg * (Math.PI /180))) * objPage.fade * 10) + 'px';
  objPage.doc.style.top = parseInt(objPage.top) + Math.round((Math.sin(objPage.deg * (Math.PI /180))) * objPage.fade * 10) + 'px';
  objPage.doc.style.width = parseInt(objPage.width) + Math.round((Math.sin(objPage.deg * (Math.PI /180))) * (objPage.fade * -1) * 20) + 'px';
  objPage.doc.style.height = parseInt(objPage.height) + Math.round((Math.sin(objPage.deg * (Math.PI /180))) * (objPage.fade * -1) * 20) + 'px';
  
  // update the opacity of the object for fading or appearing
  if (objPage.doc.filters) 
  {
    objPage.doc.filters.alpha.opacity = parseInt(objPage.opacity) + ((Math.sin(objPage.deg * (Math.PI /180))) * (objPage.fade * -1) * 100);
  }
  else objPage.doc.style.opacity = (parseInt(objPage.opacity) + ((Math.sin(objPage.deg * (Math.PI /180))) * (objPage.fade * -1) * 100))/100;

  // recall this function for 90 desgrees
  if (objPage.deg < 90)
  {
    objPage.deg = objPage.deg + 8;
    mystifier = setTimeout("mystify()",15);
  } 
  // after 90 degrees, set the actioning property to 0 to show that
  // mystify is done
  else objPage.actioning = 0;
}

function flashMe(who,willing)
{
  document.getElementById(who).src = "images/" + who + ((currPage != who.substring(3,who.length).toLowerCase())?willing:'0') + ".jpg";
}
