// ------------------------------
//  ImageHandler Class
// ------------------------------

function ImageHandler() {
	this.root = "/lib/images/";
	this.extension = ".gif";
	this.onstate = "_on";
	this.atstate = "_at";
	this.atimageid = "";
	this.labelstart = "";
	this.labelend = "";
	this.showlabel = 1;
	this.activeid = "";
	this.uselayers = 0;
	this.layerprefix = "l";
	this.coimage = "";
	this.coimageonstate = "_on";
	this.browser  = new ih_browser();
	this.px = (this.browser.mac && !this.browser.nn4)?"px":"";
	this.images = new Array();
	this.add = new Function("id","name","label","ih_add(this,id,name,label)");
	this.swap = new Function("img","ison","return ih_swap(this,img,ison)");
	this.setup = new Function("ih_setup(this)")
	return this;
}

function ih_browser() {
	this.agent = navigator.userAgent;
	this.mac = (this.agent.indexOf("Mac")>-1) ? 1 : 0;		
	this.nn4 = (document.layers) ? 1 : 0;	
	this.nn = (this.agent.indexOf("Netscape")>-1 || this.nn4) ? 1 : 0;
	if (this.nn4) {
		this.styleref = "";
		this.hide = "hide";
		this.show = "show";
	} else {
		this.styleref = ".style";
		this.hide = "hidden";
		this.show = "visible";
	}
	return this;
}

function ih_add(ih,id,name,label) {
	ih.images[ih.images.length] = new ih_image(ih,id,name,label);
}

function ih_image(ih,id,name,label) {
	this.id = id;
	this.name = name;
	this.label = ih.labelstart + ((label==null) ? name : label) + ih.labelend;
     	this.img = new Image();
	this.img.src = ih.root + name + ((ih.atimageid==id)?ih.atstate:ih.onstate) + ih.extension;
	this.layer = null;
	return this;
}

function ih_swap(ih,img,ison) {
	// Iterate through images
	var newactiveid = "";
	for (var i=0;i<ih.images.length;i++) {
		var ih_img = ih.images[i];

		// If not 'at' page
		if (ih_img.id!=ih.atimageid) {

			// If old active or new deactive
			if ((ison && ih_img.id==ih.activeid) || (!ison && ih_img.id==img)) {

				// Deactivate image
				document.images[ih_img.id].src = ih.root + ih_img.name + ih.extension;

				// Deactivate co-image
				if (ih.coimage && !ison) {
					var coimg=document.getElementById(ih_img.id+ih.coimage);
					var onpatt=new RegExp(ih.onstate,"i");
					if(coimg)coimg.src=coimg.src.replace(onpatt,"");
				}
			
				// Deactivate layer
				if (ih.uselayers && ih_img.layer) ih_setvisibility(ih,ih_img.layer,0);

				// Set message if deactivating
				if (!ison)  window.status="";
			}

			// If new active
			if (ison && ih_img.id==img) {

				// Activate image
				document.images[ih_img.id].src = ih_img.img.src;
			
				// Activate co-image
				if (ih.coimage) {
					var coimg=document.getElementById(ih_img.id+ih.coimage);
					if(coimg)coimg.src=coimg.src.replace(/(\.\w+)$/i,(ih.onstate)+"$1");
				}

				// Activate layer
				if (ih.uselayers && ih_img.layer) ih_setvisibility(ih,ih_img.layer,1);

				// Set message
				if (ison)  window.status=ih_img.label;

				// Set new active item
				newactiveid  = img
			}
		}
	}

	// Set active id and exit
	ih.activeid = newactiveid;
	return true;
}

function ih_setvisibility(ih,obj,ison) {
	eval('obj' + ih.browser.styleref + '.visibility="' + ((ison)?ih.browser.show:ih.browser.hide) + '"' );
}

function ih_setup(ih) {
	// Set up layers
	if (ih.uselayers) {
		for (i=0;i<ih.images.length;i++) {
			var ih_img = ih.images[i];
			ih_img.layer = ih_getlayer(ih.layerprefix + ih_img.id);
		}
		if (document.layers) window.onresize=new Function("location.reload(true)");
	}
	// Set 'at' image
	if (ih.atimageid) {
		var tmp=ih.atimageid;
		ih.atimageid=null;
		ih.swap(tmp,1);
		ih.atimageid=tmp;
	}
}

function ih_getlayer(id,typ) {
	if (document.layers && typ=='IMG') return document.images[id];
	else if (document.layers) return document.layers[id];
	else return document.getElementById(id);
}

