//	TVIA Image Handling JS
//	(c)	Andy Belcher	2008
//
var	imgDir		= '/images/';
var tviaPic		= new Array();
var pgLayer		= new Array();
var nLayer		= 0;
var imgNoOffset	= 1001;
var imgCtr		= imgNoOffset;
var dfltLoc		= '';
var dfltLnk		= '';
var dfltAlt		= 'See text for details...';
var dfltBlb		= '';
var dfltNgr		= '';
var dfltDat		= '';
var dfltObj		= '';
var dfltFea		= '';
var dfltPla		= '';
var dfltDis		= '';
var dfltCty		= '';
var dfltTyp		= 'G';	// {G}allery	|| overide as {S}tandalone
var dfltOri		= 'L';	// {L}andscape	|| overide as {P}ortrait
var dfltOwn		= 'TVIA';
var dfltWdt		= 800;
var dfltHgt		= 600;
var dfltthmWdt	= 200;
var dfltthmHgt	= 150;
var dfltTtlTxt	= 'Follow this link for more information on this image...';
var dfltLnkTxt	= 'More information...';
var foreCont	= '';
var backCont	= '';
var galleryUrl	= '/cgi-bin/tviagal.pl';
var galleryArg	= '?imgno=';
var galList		= '';
var shGalForm	= 1;
//
//	Image object definition function
//
function tviaImgObj(ref,url,loc,alt,obj,fea,pla,dis,cty,blb,lnk,typ,ngr,dat,ori,own,wdt,hgt,thmWdt,thmHgt)	{

	//	REF
	if(isNaN(ref))	{return;}
	this.ref	= ref;
	//	imgCtr		= ref;
	//	URL
	if(!url)	{imgCtr--;return;}
	this.url	= imgDir+url;
	//	URLTHM
	this.thmUrl	= this.url.replace('.jpg','.thm.jpg');
	//	URLBIG
	this.bigUrl	= this.url.replace('.jpg','.big.jpg');
	//	ORIentation
	(ori) ? this.ori = ori : this.ori = dfltOri;
	//	TYPe
	(typ) ? this.typ = typ : this.typ = dfltTyp;
	//	WDT - HGT - THMWDT - THMWDT
	this.wdt = (wdt) ? wdt : dfltWdt;
	this.hgt = (hgt) ? hgt : dfltHgt;
	this.thmWdt = (thmWdt) ? thmWdt : dfltthmWdt;
	this.thmHgt = (thmHgt) ? thmHgt : dfltthmHgt;
	//	If P then invert Hgt and Wdt
	if((this.ori == "P")
		&& (this.wdt == dfltWdt)
		&& (this.hgt == dfltHgt)
		&& (this.thmWdt == dfltthmWdt)
		&& (this.thmHgt == dfltthmHgt))
		{this.wdt	= dfltHgt;
		this.hgt	= dfltWdt;
		this.thmWdt = dfltthmHgt;
		this.thmHgt = dfltthmWdt;
		}
	//	Set height and width value display strings
	this.wdtStr		= '';
	this.hgtStr		= '';
	this.thmWdtStr	= '';
	this.thmHgtStr	= '';
	if(this.wdt) 	{this.wdtStr	= 'width="'+this.wdt+'"';}
	if(this.thmWdt) {this.thmWdtStr	= 'width="'+this.thmWdt+'"';}
	if(this.hgt) 	{this.hgtStr	= 'height="'+this.hgt+'"';}
	if(this.thmHgt) {this.thmHgtStr	= 'height="'+this.thmHgt+'"';}

	//	OWN
	if(own)
		{if(!isNaN(own))
			{if(tviaPic[own].url) {this.own = tviaPic[own].own;}
			else {this.own = dfltOwn;}
			}
		else {this.own = own;}
		}
	else {this.own = dfltOwn;}

	//	BLurB
	if(blb)
		{if(!isNaN(blb))
			{if(tviaPic[blb].url) {this.blb = tviaPic[blb].blb;}
			else {this.blb = dfltBlb;}
			}
		else {this.blb = blb;}
		}
	else {this.blb = dfltBlb;}

	//	LiNK
	if(lnk)
		{if(!isNaN(lnk))
			{if(tviaPic[lnk].url) {this.lnk = tviaPic[lnk].lnk;}
			else {this.lnk = dfltLnk;}
			}
		else {this.lnk = lnk;}
		}
	else {this.lnk = dfltLnk;}

	//	NGR
	if(ngr)
		{if(!isNaN(ngr))
			{if(tviaPic[ngr].url) {this.ngr = tviaPic[ngr].ngr;}
			else {this.ngr = dfltNgr;}
			}
		else {this.ngr = ngr;}
		}
	else {this.ngr = dfltNgr;}

	//	DATe
	if(dat)
		{if(!isNaN(dat))
			{if((dat < ref) && (tviaPic[dat].url))
				{this.dat = tviaPic[dat].dat;}
			else {this.dat = dfltDat;}
			}
		else {this.dat = dat;}
		}
	else {this.dat = dfltDat;}

	//	ALTtext
	if(alt)
		{if(!isNaN(alt))
			{if(tviaPic[alt].url) {this.alt = tviaPic[alt].alt;}
			else {this.alt = dfltAlt;}
			}
		else {this.alt = alt;}
		}
	else {this.alt = dfltAlt;}

	//	OBJect
	if(obj)
		{if(!isNaN(obj))
			{if(tviaPic[obj].url) {this.obj = tviaPic[obj].obj;}
			else {this.obj = dfltObj;}
			}
		else {this.obj = obj;}
		}
	else {this.obj = dfltObj;}

	//	FEAture
	if(fea)
		{if(!isNaN(fea))
			{if(tviaPic[fea].url) {this.fea = tviaPic[fea].fea;}
			else {this.fea = dfltFea;}
			}
		else {this.fea = fea;}
		}
	else {this.fea = dfltFea;}

	if(!this.obj)	{this.obj = this.fea;}
	if(!this.fea)	{this.fea = this.obj;}

	//	PLAce
	if(pla)
		{if(!isNaN(pla))
			{if(tviaPic[pla].url) {this.pla = tviaPic[pla].pla;}
			else {this.pla = dfltPla;}
			}
		else {this.pla = pla;}
		}
	else {this.pla = dfltPla;}

	//	DIStrict
	if(dis)
		{if(!isNaN(dis))
			{if(tviaPic[dis].url) {this.dis = tviaPic[dis].dis;}
			else {this.dis = dfltDis;}
			}
		else {this.dis = dis;}
		}
	else {this.dis = dfltDis;}

	//	CounTY
	if(cty)
		{if(!isNaN(cty))
			{if(tviaPic[cty].url) {this.cty = tviaPic[cty].cty;}
			else {this.cty = dfltCty;}
			}
		else {this.cty = cty;}
		}
	else {this.cty = dfltCty;}

	//	LOCation
	if(loc)
		{if(!isNaN(loc))
			{if(tviaPic[loc].url) {this.loc = tviaPic[loc].loc;}
			else {this.loc = '';}
			}
		else {this.loc = loc;}
		}
	else {this.loc = '';}

	if(!this.loc)
		{var tmp	= '';
		var ictr	= 0;
		if(this.cty)	{tmp = this.cty;ictr++;}
		if(this.dis)
			{if(ictr)	{tmp = ', '+tmp;}
			tmp = this.dis+tmp;
			ictr++;
			}
		if(this.pla)
			{if(ictr)	{tmp = ', '+tmp;}
			tmp = this.pla+tmp;
			ictr++;
			}
		if(this.fea)
			{if(ictr)	{tmp = ', '+tmp;}
			tmp = this.fea+tmp;
			ictr++;
			}
		this.loc	= tmp;
		}

	//	ALigN - CLasS
	this.aln	= 'center';
	this.cls	= 'tviaimg';
	this.thmCls	= 'tviathm';
	}
//
//	Function prototypes...
//
tviaImgObj.prototype.returnThumb = function(lrc) {
	var clsStr	= 'class="'+this.thmCls+'"';
	var alnStr	= 'align="'+this.aln+'"';
	lrc = (lrc) ? lrc : '';
	if(lrc == "right")
		{clsStr	= 'class="tviathmr"';
		alnStr	= 'align="right"';
		}
	if(lrc == "left")
		{clsStr	= 'class="tviathml"';
		alnStr	= 'align="left"';
		}
	var altStr	= this.makeAltStr(' Click for larger image...');
	if(this.typ == 'X')
		{return '<div '
			+ clsStr+' '
			+ 'onclick="location.href = \''+galleryUrl+galleryArg+this.ref
			+ '\'" >' 
			+ this.alt
			+ '<br /><small>Unfortunately the size or resolution of this image is not '
			+ 'suitable for display as a thumbnail; instead click to view '

			+ 'full-size version...</small></div>';
		}
	else
		{return '<img id="img'+this.ref+'" src="'
			+ this.thmUrl+'" '
			+ clsStr+' '
			+ alnStr+' '
			+ this.thmWdtStr+' '
			+ this.thmHgtStr+' '
			+ altStr+' '
			+ 'onclick="location.href = \''+galleryUrl+galleryArg+this.ref
			+ '\'" />';
		}
	}
//
tviaImgObj.prototype.showThumb = function(lrc) {
	dw(this.returnThumb(lrc));
	}
//
tviaImgObj.prototype.showImage = function() {
	dw(this.returnImage());
	}
//
tviaImgObj.prototype.makeAltStr = function(suf) {
	return (this.alt) ? 'alt="'+this.alt+suf+'"' : '';
	}
//
tviaImgObj.prototype.makeOwnStr = function() {
	return (this.alt) ? '<strong>&copy; <cite>'+this.own+'</cite></strong>' : '';
	}
//
tviaImgObj.prototype.makeLnkStr = function() {
	return (this.lnk) ? '<a href="'+this.lnk+'" title="'+dfltTtlTxt+'">'+dfltLnkTxt+'</a>' : '';
	}
//
//	Object instantiation call
//
function makeTviaImg(ref,url,loc,alt,obj,fea,pla,dis,cty,blb,lnk,typ,ngr,dat,ori,own,wdt,hgt,thmWdt,thmHgt)	{
	tviaPic[ref]	= new tviaImgObj(ref,url,loc,alt,obj,fea,pla,dis,cty,blb,lnk,typ,ngr,dat,ori,own,wdt,hgt,thmWdt,thmHgt);
	imgCtr++;
	}
//
//	Publicly accessible functions
//
function showThumb(ref,lrc)	{
	tviaPic[ref].showThumb(lrc);
	}
//
function showRndThumb(aln,exc) {
	var nImg	= tviaPic.length - imgNoOffset;exc = (exc) ? exc : 0;
	var rndNo	= 0;if(!aln)	{aln = 'center';}
	while(1)
		{rndNo	= Math.round(Math.random()*(nImg-1)+imgNoOffset);
		if(!tviaPic[rndNo].url)	{continue;}
		if((rndNo == exc) || (tviaPic[rndNo].ori == 'P')
			|| (tviaPic[rndNo].thmWdt != dfltthmWdt)
			|| (tviaPic[rndNo].thmHgt != dfltthmHgt)
			) {continue;}
		break;
		}
	showThumb(rndNo,aln);
	}
//
function changeLayer(step)	{
	//	Find the highest layer
	var ctr	= 0;while(1) {if(pgLayer[ctr]) {ctr++;} else {break;}}
	var newLayer	= eval(nLayer+step);
	if(newLayer >= ctr)	{newLayer = ctr-1;}
	if(newLayer < 0)	{newLayer = 0;}
	nLayer	= newLayer;
	//	Remove higher layers
	newLayer++;
	for(var i=newLayer;i<ctr;i++) {pgLayer[i] = '';}
	document.body.innerHTML = pgLayer[nLayer];
	location.href = '#';
	}
//
function initPg()	{
	flushPgLayers();
	}
//
function flushPgLayers()	{
	var i = 0;nLayer = 0;
	while(1) {if(pgLayer[i]) {pgLayer[i] = '';} else {break;}}
	}
//
function makeBodyFrame(str)	{
	return '<div id="frame">'+str+returnFooter()+'</div><!--frame-->';
	}
//
//	EOF
//
