function ajaxConn()
{
  var conn = null;
  try { conn = new XMLHttpRequest(); }
  catch (e)
  { try { conn = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e)
    { conn = new ActiveXObject("Microsoft.XMLHTTP"); } }
  return conn;
}
function addTDToRow(tr, text)
{
  var td = document.createElement('td');
  td.appendChild(document.createTextNode(text));
  tr.appendChild(td);
}

function init()
{
}

function doSearch()
{
  window.location="displayGallery.php?gallery="+galleryName+
                  "&search="+document.forms['search'].search.value;
}
function onPictureClick(picNum, subPicNum)
{
  // Find out if we are on a large monitor or not
  var pictureHeight = (f_clientHeight()>820 ? 600 : 480);
  var pic = pictures[picNum];
  var msg = '<table><tr>';
  if ( picNum!=0 )
  {
    msg += '<td><a href="First" onclick="onPictureClick(0); return false;">';
    msg += '<img src="start_blue.png"/></a></td>';
    msg += '<td><a href="Prev" onclick="onPictureClick('+
              (picNum-1)+'); return false;">';
    msg += '<img src="back_blue.png"/></a></td>';
  }
  else
  {
    msg += '<td><a><img src="start.png"/></a></td>';
    msg += '<td><a><img src="back.png"/></a></td>';
  }
  msg += '<td width="5px"></td>';
  msg += '<td><a href="download.php?filename='+encodeURIComponent(pic.path+'/'+pic.large)+'"><img src="download.jpg" width="16px" height="16px"/></a></td>';
  msg += '<td width="5px"></td>';
  if ( picNum!=pictures.length-1 )
  {
    msg += '<td><a href="Next" onclick="onPictureClick('+
              (picNum+1)+'); return false;">';
    msg += '<img src="forward_blue.png"/></a></td>';
    msg += '<td><a href="Last" onclick="onPictureClick('+
              (pictures.length-1)+'); return false;">';
    msg += '<img src="end_blue.png"/></a></td>';
  }
  else
  {
    msg += '<td><a><img src="forward.png"/></a></td>';
    msg += '<td><a><img src="end.png"/></a></td>';
  }
  msg += '<td width="5px"></td>';
  if ( subPicNum!=null && pic.subPictures[subPicNum].comment )
    msg += '<td width="600px">'+pic.subPictures[subPicNum].comment+'</td>';
  else if ( pic.comment ) 
    msg += '<td width="600px">'+pic.comment+'</td>';
  msg += "</tr>";
  msg += "</table><table>";
  msg += '<tr><td colspan="100%">';
  if ( subPicNum!=null )
  {
    var subPic = pic.subPictures[subPicNum];
    msg += '<a target="_fullsize" href="'+subPic.path+'/'+subPic.large+'">';
    msg += '<img id="mediumPic" height="'+pictureHeight+'px" src="'+
                          subPic.path+"/"+subPic.medium+'"/></a></td></tr>';
  }
  else
  {
    msg += '<a target="_fullsize" href="'+pic.path+'/'+pic.large+'">';
    msg += '<img id="mediumPic" height="'+pictureHeight+'px" src="'+
                          pic.path+"/"+pic.medium+'"/></a></td></tr>';
  }
  msg += '</table>';
  if ( pic.subPictures.length > 0 )
  {
    msg += 'More Pictures:';
    msg += '<div id="scrollDiv" style="width: '+(pictureHeight*4/3)+
              'px; height: 140px; overflow: scroll; overflow-y: hidden">';
    msg += '<div style="width: '+((pic.subPictures.length+1)*170)+
              'px; height: 120px;">';
    msg += '<img class="subPic" onclick="onPictureClick('+picNum+');" src="'+
                          pic.path+'/'+pic.small+'"/>';
    for ( var i=0; i<pic.subPictures.length; ++i )
    {
      var subPic = pic.subPictures[i];
      msg += '<img class="subPic" onclick="onPictureClick('+
             picNum+', '+i+');" src="'+ subPic.path+'/'+subPic.small+'"/>';
    }
    msg += '</div></div>';
    resizeMessageWin(pictureHeight*4/3+30, pictureHeight+220);
  }
  else
    resizeMessageWin(pictureHeight*4/3+30, pictureHeight+80);
  displayMessage(pic.large+" ("+(picNum+1)+"/"+pictures.length+")", msg);
}

/** Utility function to display a message to the user. This brings up a <div>
 * that looks and acts like a non-modal popup. The window can be moved and
 * closed but not resized. This function relies on there being a
 * <div id='pictureDlg'> in the HTML that is setup to have a 'titleBar' and
 * 'message' sub div
 *
 * @param {String} title The title of the message box
 * @param {String} message The text to add to the message box
 * @param {boolean} append Whether or not to append the new message to the
 *	                text that already exists in the message box
 */ 
function displayMessage(title, message, append)
{
  var errorWin = document.getElementById('pictureDlg');
  //errorWin.style.top = Geometry.getVerticalScroll()+150+"px" ;
  var titleWin = document.getElementById('titleBar') ;
  titleWin.innerHTML = title;
  var msgWin = document.getElementById('message');
  if ( append )
    msgWin.innerHTML+=message+"<br>"; 
  else
    msgWin.innerHTML=message;
  errorWin.style.display='block';
}
/** Utility function to hide the message box brought up by showMessage
 */
function hideMessage()
{
  var errorWin = document.getElementById('pictureDlg');
  errorWin.style.display='none';
} 
/* Utility function to change the size of the message box that is brought up
* by showMessage
*/
function resizeMessageWin(width, height)
{
  if ( !width ) width = 820;
  if ( !height ) height = 675;
  var errorWin = document.getElementById('pictureDlg');
  errorWin.style.width = width+"px";
  errorWin.style.height = height+"px";
  errorWin.style.top = (f_scrollTop()+50)+"px";
  var titleWin = document.getElementById('titleBar');
  titleWin.style.width = (width-10)+"px";
  var closeBtn = document.getElementById('closeBtn');
  closeBtn.style.left = (width-25)+"px";
  var msgWin = document.getElementById('message');
  msgWin.style.width = (width-10)+"px";
  msgWin.style.height = (height-35)+"px";
}

function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


