// ------------------------------------------------------------------------------------------
// This page contains routines for managing the positioning of the page, and of page elements
// ------------------------------------------------------------------------------------------

// 0 = unassigned, 1 = New Zealand, -1 = Overseas
var userNZ;

// Shaded border
var boxBorder = null;

function getElement(sElement) {
  if (document.getElementById) {
    return(document.getElementById(sElement));
  }
  else if (document.all) {
    return(document.all[sElement]);
  }
  else if (document.layers) {
    return(document.layers[sElement]);
  }
  else {
    return(null);
  }
}

function setFooterDate() {
  var elDate = getElement('footerdate');
  if (elDate) {
    var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var dateObj = new Date(document.lastModified)
    var wday = days[dateObj.getDay()]
    var lmonth = months[dateObj.getMonth() ]
    var date = dateObj.getDate()
    var fyear = dateObj.getYear()
    var fHours = dateObj.getHours()
    var fMins = dateObj.getMinutes()

    if (fyear < 2000) {
      fyear = fyear + 1900
    }

    if (fHours >= 12) {        
      sClock = "pm";
    } else {
      sClock = "am";
    }

    if (fHours > 12) {
      fHours -= 12;
    }

    if (fHours == 0) {
      fHours = 12;
    }

    if (fMins < 10) {
      fMins = "0" + fMins
    }
    
    elDate.innerHTML = 'this page was updated on ' + wday + ' ' + date + 
      ' ' + lmonth + ' ' + fyear + ' at ' + fHours + ':' + fMins + sClock;
  }
}

function setUserLocale() {
  if (!userNZ) {
    var dtNow = new Date(); 
    var iGMT = (dtNow.getTimezoneOffset() / 60); 

    // Kiwis are 1, the rest of the world is 2
    if (iGMT <= -11) {
      userNZ = 1;
    }
    else {
      userNZ = -1;
    }  
  }
}

function setLeighLocation() {
  var elLocation = getElement('leighlocation');
  if (elLocation) {
    elLocation.innerHTML = 'Wellington';
  }
}

function setLeighPhone() {
  var elPhone = getElement('leighphone');
  
  if (elPhone) {
    elPhone.innerHTML = '021 933 913';
  }
  setDisplay('menuleighphone_int', 'none');
  setDisplay('menuleighphone_nz', 'inline');
}

function setNoScript() {
  var elNoScript = getElement('noscript');
  if (elNoScript) {
    elNoScript.style.display = 'none';
    //elNoScript.innerHTML = '';
  }
}

function setBoxBackground(sBox) {
  var elBox = getElement(sBox);
  if (elBox) {
    elBox.style.background = 'none';
  }
}

function includeScript(sFilename) {
  var elHead = document.getElementsByTagName('head').item(0);
  var elScript = document.createElement('script');
  if (elHead && elScript) {
    elScript.setAttribute('language', 'javascript');
    elScript.setAttribute('type', 'text/javascript');
    elScript.setAttribute('src', sFilename);
    elHead.appendChild(elScript);
  }
  // document.write('<script type="text/javascript" src="' + sFilename + '"></script>');
}

function createBoxBorder() {
  boxBorder = RUZEE.ShadedBorder.create({corner: 12, shadow: 24});
}

function setFooterJavascript() {
  var elJavascript = getElement('footerjs');
  if (elJavascript) {
    elJavascript.innerHTML = 'isn\'t there always?';
  }
}

function populateSearch() {
  // If we have a value in the search box ...
  txtSearch = document.getElementById("keyword"); 
  if (txtSearch) {
    sSearch = txtSearch.getAttribute("value");
    if (sSearch && sSearch != '') {
      btnSearch = document.getElementById("searchbutton");
      if(btnSearch) {
        btnSearch.click();
      }
    }
  }
}

function setActiveContent(iPage) {
  if (iPage == 0) {                     //Normal pages
    createBoxBorder();
    setBoxBackground("menu");
    setUserLocale();
    
    if (userNZ = 1) {
      setLeighLocation();
      setLeighPhone();
    }
    
    populateSearch();
    //setFooterDate();
    setFooterJavascript();
    //setQuote(); 
    boxBorder.render("menu");
  }
}

function onWindowClose(iPage) {
  if (iPage == 1) {                      // Search
    var iLeft = getWindowLeft(self.opener);
    var iTop = getWindowTop(self.opener);
	var iWidth = getScreenWidth(self.opener) - iLeft;
	var iHeight = getWindowHeight(self.opener);
    setWindowPosition(self.opener, iTop, iWidth, iHeight, iLeft);
  }
}