// -----------------------------------------------------------------------------
// Code by Leigh Harrison   http://www.else.co.nz   January 2009
// -----------------------------------------------------------------------------

var oPrevMenuItem = null;

function ajaxFetch(sFile, sTarget, fMethod) { 
  var oTarget = document.getElementById(sTarget);
  if (!oTarget){
    oTarget = parent.document.getElementById(sTarget);
  }
  if (oTarget) {
    ajaxLoad(sFile, oTarget, fMethod);
  }
  else {
    alert('No element to place the file data in.');
  }
}

function ajaxLoad(sFile, oTarget, fMethod) {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        //alert("Your browser is not able to display this data.");
        return false;
      }
    }
  }

  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      if (oTarget) {
        oTarget.innerHTML = xmlHttp.responseText;
        if (fMethod) {
          fMethod();
        }
      }
      else {
        //alert('No element to place the file data in.');
      }
    }
  }
  xmlHttp.open("GET", sFile);
  xmlHttp.send(null);
}

function pageLoad(sFile, sAnchor) {
  // Load page
  var sSuffix = (sAnchor && sAnchor != '') ? '.htm#' + sAnchor : '.htm';
  switch(sFile) {
    case 'welcome':
      ajaxFetch('content/page_' + sFile + sSuffix, 'content', function(){showSlide(true)});
      break;
    case 'whoweare':
      ajaxFetch('content/page_' + sFile + sSuffix, 'content', function(){afterWhoWeAre()});
      break;
    case 'diploma':
      ajaxFetch('content/page_' + sFile + sSuffix, 'content', function(){afterDiploma()});
      break;    
    case 'newsletter':
      ajaxFetch('content/page_' + sFile + '.php', 'content', function(){afterNewsletter()});
      break;  
    case 'contact':
      ajaxFetch('content/page_' + sFile + sSuffix, 'content', function(){afterContact()});
      break;
  }  
  
  // Set menu
  if (oPrevMenuItem) {
    oPrevMenuItem.style.fontWeight = 'normal';
    oPrevMenuItem.style.color = ''; 
  }
  var oEl = obj(sFile);
  if (oEl) {
    oEl.style.fontWeight = 'bold';
    oEl.style.color = '#ffd000';
    oPrevMenuItem = oEl; 
  }

  // Load quote
  ajaxFetch('content/quote_' + sFile + '.htm', 'testimonial');
}

function afterWhoWeAre() {
  showSlide(false);
  ajaxFetch('content/faculty_charlesbishop.htm', 'facultydetail');
}

function afterDiploma() {
  showSlide(false);
  ajaxFetch('content/diploma_year01.htm', 'yeardetail');
  ajaxFetch('content/diploma_subject_humanenergyfield.htm', 'subjectdetail');
}

function afterNewsletter() {
  showSlide(false);
  //ajaxFetch('newsletters/list.txt', 'newsletterlist');
}

function afterContact() {
  showSlide(false);
  serverTimeShort('nztime');
}

function serverTimeShort(sDiv) {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        //alert("Your browser is not able to display this data.");
        return false;
      }
    }
  }

  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      // produces "Date: Sun, 01 Feb 2009 03:03:30 GMT"
      timeOffset(sDiv, xmlHttp.getAllResponseHeaders().replace(/^(.*)[\s\S]*/,"$1"))
      //oTarget.innerHTML = xmlHttp.getAllResponseHeaders().replace(/^(.*)[\s\S]*/,"$1");
      /*
      if (fMethod) {
        fMethod();
      }
      else {
        //alert('No element to place the file data in.');
      }
      */
    }
  }
  xmlHttp.open("GET", 'http://www.healingenergiesfoundation.com/styles/welcome.css');
  xmlHttp.send(null);
}

function timeOffset(sDiv, sGMT) {
  // sGMT >= "Date: Sun, 01 Feb 2009 03:03:30 GMT"
  var sTimeOfDay;
  var arrDays = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
  var sDay = sGMT.substr(11, 2) + ', ';
  var sMon = sGMT.substr(14, 4);
  var sYear = sGMT.substr(18, 5);
  var sTime = sGMT.substr(23, 8);
  var dtNZ = new Date();
  //var iTemp = Date.parse(sMon + sDay + sYear + sTime);
  dtNZ.setTime(Date.parse(sMon + sDay + sYear + sTime) + 45000000);
  //var iMS = dtGMT.GetTime();
  //iMS +=  Math.round(1000 * 60 * 60 * 12.5);
  //dtNZ = new Date(iMS);
  var iHour = dtNZ.getHours(); 
  if (iHour < 6 || iHour > 22) {
    sTimeOfDay = 'the middle of the night on ';
  }
  else if (iHour < 8) {
    sTimeOfDay = 'early morning on ';
  }
  else if (iHour < 11) {
    sTimeOfDay = 'mid morning on ';
  }
  else if (iHour < 14) {
    sTimeOfDay = 'the middle of the day on ';
  }
  else if (iHour < 16) {
    sTimeOfDay = 'mid afternoon on ';
  }
  else if (iHour < 18) {
    sTimeOfDay = 'late afternoon on ';
  }
  else if (iHour < 21) {
    sTimeOfDay = 'early evening on ';
  }
  else {
    sTimeOfDay = 'late evening on ';
  };
  obj(sDiv).innerHTML = sTimeOfDay + arrDays[dtNZ.getDay()];
}
