function XMLHTTP_Item() {
  var xmlhttp=null;
  // first for all but IE, else for IE
  if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest()
  } else if (window.ActiveXObject) {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  return xmlhttp;
}

function Replace_Items() {
    // now replace markers with real stuff
    var xmlhttp = XMLHTTP_Item();
    if(xmlhttp) {         // only if we have it
      var today=new Date();
      var d = today.getFullYear()+'_'+(today.getMonth()+1)+"_"+today.getDate();
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) {
          if (xmlhttp.status==200) {
	      Change_Everything(eval('('+xmlhttp.responseText+')'));
          }
        }
    };
    xmlhttp.open("GET","cgi-bin/index.cgi?"+d,true);
    xmlhttp.send(null);
  }
}

function Change_Everything(data) {
    // we call this before the page is fully loaded, so any of the fields
    // may not exist.  We repeat until they are all filled
    var todo = [{id:"index-today",
		 done:false,
		 innerh:function() {
		var x = document.getElementById('index-today');
		x.innerHTML = '<p>It is '+data.info.date+'</p>';
		x.innerHTML+= '<p><img height="70px" src="images/moons/moon_'+data.info.moonpercent+'.png" alt="The moon is '+data.info.moon+'"><br>The moon tonight is '+data.info.moon+'</p><hr>';
		if(data.event && data.event != '!') {
		    x.innerHTML+='<p>'+data.event+'</p><hr>';
		}
	    }},
	{id:"quote_wrapper",
	 done:false,
	 innerh:function() {
		var x = document.getElementById('quote-noindent');
		x.innerHTML = data.quote.text;
		var x = document.getElementById('quote-source');
		var y = data.quote.author;
		if(data.quote.date)
		    y+= ' ('+data.quote.date+')';
		x.innerHTML = ' From <em>'+data.quote.source+'</em> '+y;
	    }},
	{id:"photo_wrapper",
	 done:false,
	 innerh:function() {
		var x = document.getElementById('photo_xref');
		x.href = "cgi-bin/gazetteer.cgi?id="+data.photo.place.id;
		x = document.getElementById('photo_photo');
		x.src = data.photo.normal_url;
		x = document.getElementById('photo_caption');
		x.innerHTML = data.photo.caption;
		x = document.getElementById('photo_note');
		var y = data.photo.place.name;
		y+=', '+data.photo.place.canal;
		y+='. Picture: <a class="quiet" href="cgi-bin/showstuff.cgi?mode=user&id='+data.photo.puserid+'">'+data.photo.user.copyright+'</a>';
		y+='. Taken '+data.photo.taken;
		x.innerHTML = y;
		
	    }},
	{id:"searchfor",
	 done:false,
	 innerh:function() {actb(document.getElementById("searchfor"),new Array(),"cgi-bin/match.cgi");}},
	{id:"menulist",
	 done:false,
	 innerh:function() {Patch_Links();}},
	{id:"newphotos",
	 done:false,
	 innerh:function() {Add_New_Thumbnails(data.photo.newphotos);}},
	{id:"index-stats",
	 done:false,
	 innerh:function() {
		var statgen = new Date(data.statistics.datestamp*1000);
		var today = new Date();
		var x = document.getElementById('index-stats');
		var y = '<div class="small">At '+statgen.getHours()+':'+statgen.getMinutes()+' '+(today.getDay()==statgen.getDay()?"today":"yesterday")+', the database contained<ul class="small_list">';
		y += '<li>'+data.statistics.realpl+' places on</li>';
		y += '<li>'+data.statistics.waterways+' waterways totalling </li>';
		y += '<li>'+Math.round(data.statistics.extent/1000)+' km, or</li>';
		y += '<li>'+Math.round(data.statistics.extent/1609)+' miles</li>';
		y += '</ul></div>'
		x.innerHTML = y;
	    }},
	{id:"logon_status",
	 done:false,
	 innerh:function() {Check_Logon_Status('homepage');}}
	];
    Doemall(todo);
}

function Doemall(todo) {
    if(Do_Once(todo) == false)
	setTimeout(function() {Doemall(todo)},100);   // try again in 1/10 of a second
}
 
function Do_Once(todo) {
    var alldone = true;
    for(i in todo) {
	if(todo[i].done == false) {
	    var id = document.getElementById(todo[i].id);
	    if(id) {
		todo[i].innerh();
		id.style.display = "block";
		todo[i].done = true;
	    } else
		alldone = false;
	}
    }
    return alldone
}

// additional function causes proper closure creation and so differing tips
function Set_Tips(bx,tit) {
    bx.onmouseover = function() {Tip(tit,FONTSIZE,'1em');};
    bx.onmouseout = function() {UnTip();};
}

function Add_New_Thumbnails(pdat) {
    for(var i=0;i<pdat.length;i++) {
	var bx = document.getElementById('tnp_'+i);
	//create the anchor node
	var lnk=document.createElement("a");
	lnk.setAttribute("href","cgi-bin/showstuff.cgi?mode=photo&id="+pdat[i].id);
	//create the image node
	var imge=document.createElement("img");
	imge.setAttribute("src",pdat[i].thumbnail);
	imge.setAttribute("width","100");
	imge.setAttribute("height","100");
	imge.setAttribute("border","0");
	// add to document tree in correct order
	lnk.appendChild(imge);
	bx.appendChild(lnk);
	// create hover title on image
	var tit = '';
	if(pdat[i].caption) tit = '<i>'+pdat[i].caption+'</i><br>';
	tit += '<b>'+pdat[i].plinf.name + '</b><br>'+pdat[i].plinf.ww;
	tit += '<br>Taken '+pdat[i].taken;
	Set_Tips(imge,tit);
	// create user credit
	var att = document.createElement('div');
	att.innerHTML = 'By:<a class="quiet" href="cgi-bin/showstuff.cgi?mode=user&id='+pdat[i].puserid+'">'+pdat[i].user.copyright+'</a>';
	bx.appendChild(att);
    }
}