
var xmlreqs = new Array();

function xmlreqGET(url, handler) {
   var pos = indexCXMLReq();
   if (xmlreqs[pos].xmlhttp) {
      xmlreqs[pos].handler = handler;
      xmlreqs[pos].free = 0;
      xmlreqs[pos].xmlhttp.open("GET",url,true);
      xmlreqs[pos].xmlhttp.onreadystatechange = function() {
         if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
      }
      if (window.XMLHttpRequest)
         xmlreqs[pos].xmlhttp.send(null);
      else if (window.ActiveXObject)
         xmlreqs[pos].xmlhttp.send();
   }
}

function xmlreqPOST(url, data, handler) {
   var pos = indexCXMLReq();
   if (xmlreqs[pos].xmlhttp) {
      xmlreqs[pos].handler = handler;
      xmlreqs[pos].free = 0;
      xmlreqs[pos].xmlhttp.open("POST",url,true);
      xmlreqs[pos].xmlhttp.onreadystatechange = function() {
         if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos); }
      }
      xmlreqs[pos].xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xmlreqs[pos].xmlhttp.send(data);
   }
}

// return the index of an available xmlreq[] element
function indexCXMLReq()
{
   for ( var i = 0 ; i < xmlreqs.length ; i++ )
      if ( xmlreqs[i].free )
         break;
   if ( i == xmlreqs.length )
      xmlreqs[i] = new CXMLReq();
   return( i );
}

function CXMLReq() {
   this.free = 1;
   this.xmlhttp = false;
   this.handler = false;
   if (window.XMLHttpRequest)
      this.xmlhttp = new XMLHttpRequest();
   else if (window.ActiveXObject)
      this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function xmlhttpChange(pos) {
   if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].free == 0 && xmlreqs[pos].xmlhttp.readyState == 4) {
      if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) {
         xmlreqs[pos].handler(xmlreqs[pos].xmlhttp.responseXML);
      } else {
         handle_error(xmlreqs[pos].xmlhttp.status);
      }
      xmlreqs[pos].free = 1;
   }
}

function handle_error(s)
{
   alert("eh?  "+s);
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return("");
}

function getpics() {
   var m = getQueryVariable("m");
   if ( m != "" )
      xmlreqGET("getplist2.php?m="+m, processPicsResponse);
}

var piclist = new Array();

function processPicsResponse( response )
{
   var piccount = 0;

   for ( var i = 0 ; i < (response.getElementsByTagName("pic").length) ; i++ ) {
      w = response.getElementsByTagName("dir")[i].firstChild.nodeValue;
     x = response.getElementsByTagName("base")[i].firstChild.nodeValue;
     y = response.getElementsByTagName("text")[i].firstChild.nodeValue;

     if ( y == '-' )
        y = ""; 

      piclist[piccount] = new Array();
      piclist[piccount]['dir'] = w;
      piclist[piccount]['base'] = x;
      piclist[piccount]['text'] = y;
      piccount++;
   }
	return( true );
}

var curindex = 0;
ImagesLib = "./images";

function ShowPics( which )
{
   if ( piclist.length == 0 ) {
      // we're not ready yet
      window.setTimeout( "ShowPics("+which+")", 500);
      return false;
   }
   if ( (which < 0) || (which >= piclist.length) ) {
      ShowAll();
      return false;
   }

   var m = document.getElementById("multizone");
   var s = document.getElementById("single");
   s.style.display = "block"
   m.style.display = "none";

   e = document.getElementById("current");
   t = document.getElementById("currenttext");
   tp = document.getElementById("thumbprev");
   tt = document.getElementById("thumbthis");
   tn = document.getElementById("thumbnext");

   e.src = piclist[which].dir+"/"+piclist[which].base+".jpg";
   t.innerHTML = piclist[which].text;

   if ( which == 0 )
      tp.src = ImagesLib+"/SOL.gif";
   else
      tp.src = piclist[which-1].dir+"/th_"+piclist[which-1].base+".gif";

   tt.src = piclist[which].dir+"/th_"+piclist[which].base+".gif";

   if ( (which+1) == (piclist.length ))
      tn.src = ImagesLib+"/EOL.gif";
   else
      tn.src = piclist[which+1].dir+"/th_"+piclist[which+1].base+".gif";

   curindex = which;

}

function ShowPrev()
{
   ShowPics( curindex-1 );
}

function ShowNext()
{
   ShowPics( curindex+1 );
}

function ShowAll()
{
   var e = document.getElementById("multipic");
   var m = document.getElementById("multizone");
   var s = document.getElementById("single");

	if ( e.innerHTML == "") {
      for ( var i = 0 ; i < piclist.length; i++ ) {
         e.innerHTML += "<img onclick='ShowPics("+i+")' class='thmulti' src="+piclist[i].dir+"/th_"+encodeURI(piclist[i].base)+".gif>";
         if ( ! ((i+1)%100 ))
            e.innerHTML += "<br>";
      }
	}
   s.style.display = "none"
   m.style.display = "block";

}


