
// global variables
var myConn = new XHConn();
var myConn2 = new XHConn();
var spinning;
var enough_time_passed = 0;
var request_back = 0;
var received_page = "";
var page_shown = "intro";
var s = 1;

function $(id) { return document.getElementById(id); }
window.onload = function() {

  gototab($("link1"));

}

//reset nav links to not-on-top style
function resetnavlinks() {
  for (var i=1; i<7; i++ ) {
    $("link"+i).className = "othertab";
    $("link"+i).onmouseover = function () { this.className = "othertab_hover"; }
    $("link"+i).onmouseout = function () { this.className = "othertab"; }
    $("link"+i).onclick = function () { 
      clearTimeout(spinning);
      gototab(this);
      return false;
    }
  }
}

function gototab(linkdiv) {
  resetnavlinks();
  linkdiv.className = "toptab";
  linkdiv.onmouseover = null;
  linkdiv.onmouseout = null;
  page_shown = linkdiv.getAttribute("page");
  loadpage( "page="+page_shown );
}

function loadpage( post ) {
  $("maincell").innerHTML = "<img src=\"spinner.gif\">";
  myConn.connect("getdata.php","POST",post,responsehandler);
  spinning = setTimeout( "enough_waiting()", 800 );
}

  // EITHER THE SPINNER TIMEOUT OR THE RECEIPT OF THE NEW DATA
  // WILL TRIGGER UPDATING THE PAGE, WHICHEVER IS LATER.
  // THIS WAY, THERE'S A MINIMUM LOAD TIME AS SPECIFIED ABOVE.
  function enough_waiting() {
    enough_time_passed = 1;
    if (request_back) { displaypage(); }
  }
  responsehandler = function (oXML) {
    received_page = oXML.responseText;
    request_back = 1;
    if (enough_time_passed) { displaypage(); }
  }
  // THE SECOND OF THOSE TO FIRE WILL CALL THE NEXT FUNCTION.

function displaypage( pagecode ) {
  $("maincell").innerHTML = received_page;
  enough_time_passed = 0;
  request_back = 0;
}

function uploadcolors() {
  thestring = "outside=" + $("outside").getAttribute("datum");
  thestring = thestring + "&borders=" + $("borders" ).getAttribute("datum");
  thestring = thestring + "&toptab=" + $("toptab").getAttribute("datum");
  thestring = thestring + "&undertabs=" + $("undertabs").getAttribute("datum");
  thestring = thestring + "&tweentabs=" + $("tweentabs").getAttribute("datum");
  thestring = thestring + "&maintext=" + $("maintext").getAttribute("datum");
  thestring = thestring + "&specialtext=" + $("specialtext").getAttribute("datum");
  thestring = thestring + "&linkcolor=" + $("linkcolor").getAttribute("datum");
  myConn2.connect("updatecolors.php","POST",thestring,resetcss);
}

resetcss = function (oXML) {
  s++; // got to change the URL of the stylesheet or browser won't refresh it
  $("csslink").href="style.css.php?"+s;
}

function reveal_photolist() {
  $("photolist").style.display = "block";
  return false;
}

function choose_picture(node) {
  // update database
  uri = node.getAttribute("datum");
  myConn2.connect("updatepicture.php","POST","pictureuri="+uri,hide_photolist);
  // update page
  $("photo").src = uri;
  $("photolist").style.display = "none";
  return false;
}

hide_photolist = function (oXML) {
  // nothing special
}

