/* hslUtils.js */
/*  Copyright © 2005-2009, Hanson-Smith, Ltd. All rights reserved. */

function implode(insert, within) {
    return ((within instanceof Array) ? within.join(insert):within);
}
function trim(str,chrs) {
	var x;
	x = ltrim(str,chrs);
	return (rtrim(x,chrs));
}
function strip(str,chrs) {
	var i, j, x;
	x = str;
	for (i = 0; i < chrs.length; i++) {
		x = x.replace("/"+chrs[i]+"/","");
	}
	return (x);
}
function strBegins(haystack, needle) {
   return (haystack.substr(0,needle.length) == needle);
}
function strEnds(haystack, needle) {
   return (haystack.substr(haystack.length-needle.length) == needle);
}
function ltrim(str,chrs) {
	var i, j, x;
	x = str;		
	i = 0;
	j = chrs.length;
	while (i < j) {
		if (x[0]==chrs[i++]) {
			x = x.substr(1,x.length-1);
			i = 0; // restart with zero after each match
		}
	}
	return (x);
}
function rtrim(str,chrs) {
	var i, j, k, x;
	x = str;		
	i = 0;
	j = chrs.length;
	k = x.length-1;
	while ((i < j) && (k>0)) {
		if (x[k]==chrs[i++]) {
			x = x.substr(0,x.length-1);
			k--;
			i = 0; // restart with zero after each match
		}
	}
	return (x);
}

function unhide(lid,bid) {
   var b, i, j, x, y, z;
   var newTop;       /* if true, make a history entry; doesn't work with IE */
   if (!document.getElementById) {return true;}	// reject if W3C DOM not present

   for (i=1, x=1; i<=9; i++) { /* e.g. bb1x through bb9x */
      y = 'lb'+i;
      for (j=1; j<=900; j++) { /* e.g. bbx1 through bbx900...large # for pix */
         /* left links/body blocks */
         z = y+j;
         x = document.getElementById(z);
         b = ((i==lid) && (j==bid));
         if (x) {
				x.style.display = (i==lid ? "block" : "none");
				x.className = (b ? "me" : "");
				z = 'bb'+i+''+j;
				x = document.getElementById(z);
				if (x) {x.style.display = (b ? "block" : "none");}
			}
      }
      b = (i==lid);
      y = 'lb'+i;
      x=document.getElementById(y);
		if (x) {
	      x.style.display = (b ? "block" : "none");
	
	      z = 'tb'+i;
	      x = document.getElementById(z);
	      if (x.className == "me") newTop = (i!=lid);
	      x.className = (b ? "me" : "");
      }
   }
   return (false);
}
function showTab(lid,bid) {
	x = 'http://' + location.host + location.pathname;
	location.href = x+"?tab=bb"+lid+bid;
}
function showTabNoXfer(lid,bid) {
	return (unhide(lid,bid));
}
function checkContent(dlid,dbid) {
	/* handles #/? refs in URL; called onLoad */
   var b, n, x, y, z;
   z = window.location.href;
   x = z.split("#");
   b = (x.length>1);
   if (!b) {
      x = z.split("?");
      b = (x.length>1);
   }
   if (b) {
      y = x[1].substr(2,1);
      z = x[1].substr(3,2);
      b = (y>0 && z>0);
      if (b) { unhide(y,z); }
   }
   if (!b) { unhide(dlid,dbid); }
   n = document.getElementById('bodyblock');
   if (n) {n.style.display = 'block';}
}
function refImage(basename,ix,isExplicit,title) {
	var p1 = "/images/" + basename + "/thumbs/";
	var p2 = "/images/" + basename + "/";
	var p3 = "' alt='larger version' target=_blank><img src='";
	var tit = title;
	var x, y, z;
	if (!isExplicit) {
		p1 += basename;
		p2 += basename;
		if (!tit) tit = basename + ' image ' + (ix+1);
		y = (ix > 0 ? "-" + ix : '');
		return ("<a href='" + p2 + y + ".jpg' title='" + tit + 
						p3 + p1 + y + ".jpg' alt='" + tit + "' /></a> ");
	}
	/* explicit; ix is actual filename */
	z = ix + '';
	x = z.split("\.");
	if (!tit) tit = x[0];
	y = ix + '';
	if (x.length<2) {y += '.jpg';}
	z = "<a href='" + p2 + y + "' title='" +  tit +
						p3 + p1 + y + "' alt='" + tit + "' /></a> ";
	return (z);
}
function fillImages(lid,blk,basename,isIndirect,maxPlusOne) {
	/* e.g. fillImages(5,2,2005,0,506):
			5 = lid = left navbar id, e.g. lb5
			2 = blk = body block id, e.g. bb52
			2005 = basename = image subdirectory
			0 = isIndirect = whether to probe body block for file refs
			506 = maxPlusOne = displayed ID of last image, else go through blk*50+50
		
		Directory structure:
			
			/images/<basename>/<basename>[-<n>].jpg
			/images/<basename>/thumbs/<basename>[-<n>].jpg
			e.g. /images/2005/2005-2.jpg
			note special case for /images/prior/...
		*/
		
	var i, j, txt, cap, col;
	var r, s, y, z;
	var bComment;
	var x = document.getElementById('bb' + lid + '' + blk);
	
	txt = "<table id=gallery>";

	if (x) {
		cap = "";
		col = 0;
		
		if (!isIndirect) {
			i = ((blk-1)%20)*50; /* i.e. 0, 50, 100 ... */ 
			j = i+49;
			
			for (col=0;i<=j;i++) {
				/* e.g. refImage(2005, 2, false, '') == '2005 image 3' */
				if (col == 0) txt += '<tr>';
				txt += '<td>' + refImage(basename,i,0,'') + '</td>';
				cap += '<td>' + basename + ' image ' + (i+1) + '</td>';
				if (++col >4) {
					txt += '</tr><tr>' + cap + '</tr>';
					cap = "";
					col = 0;
				}
				if (maxPlusOne && ((i+1) >= maxPlusOne)) break;
			}
		} else {
			/* special case for prior year images, hard-coded refs */
			r = x.innerHTML.split(/[\s]+/);
			for (i=0, bComment=false, col=0;i<r.length;i++) {
				s = r[i];
				if (bComment) {
					/* IE doesn't break after the comment, despite \n ! */
					bComment = (s.substr(0,3) != '-->');
					if (!bComment) {s = s.substr(3);}
				}
				if (!bComment && (s.length > 0)) {
					bComment = (s == '<\!--');
					if (!bComment && (s.length>0)) {
						y = s.split(":");
						if (y.length>1) {
							/* from:to */
							for (j=y[0];j<=y[1];j++) {
								/* e.g. refImage(prior, 1974, true, '1974 image') */
								if (col == 0) txt += '<tr>';
								txt += '<td>' + refImage(basename,j,1,j + ' image') + '</td>';
								cap += '<td>' + j + ' image</td>';
								if (++col >4) { /* similar block */
									txt += '</tr><tr>' + cap + '</tr>';
									cap = "";
									col = 0;
								}
							}
						} else {
							y = s.split("-");
							if (y.length>1) {
								/* yyyy-max */
								for (j=0;j<=y[1];j++) {
									if (col == 0) txt += '<tr>';
									z = y[0] + '';
									if (j>0) z+= "-" + j;
									txt += '<td>' + refImage(basename,z,1,y[0] + ' image ' + (j+1)) + '</td>';
									cap += '<td>' + y[0] + ' image ' + (j+1) + '</td>';
									if (++col >4) { /* similar block */
										txt += '</tr><tr>' + cap + '</tr>';
										cap = "";
										col = 0;
									}
								}
							} else {
								/* plain name */
								if (col == 0) txt += '<tr>';
								txt += '<td>' + refImage(basename,s,1,'') + '</td>';
								cap += '<td>' + s + '</td>';
								if (++col >4) { /* same block */
									txt += '</tr><tr>' + cap + '</tr>';
									cap = "";
									col = 0;
								}
		}	}	}	}	}	}	
		if (col >0) {
			txt += '</tr><tr>' + cap + '</tr>';
		}
		txt += '</table>';
		x.innerHTML = txt;
	}
}
function testImages(lid,blk,basename,isIndirect,max) {
	var y = 'bb' + lid + "" + blk;
	var z;

	var x = document.getElementById(y);
	if (!x) return (true);
	z = x.innerHTML;			
	if (z.search(/empty|--/) >=0) { 
		fillImages(lid,blk,basename,isIndirect,max);
	}
	return (unhide(lid,blk));
}

/* end of hslUtils.js */