/*
Programmer: CJ de Vos (cdevos@hvdh.nl)
Scriptname: images.js
Functionality: Preload images and use for mouseover and mouseout
*/

swFlag=true;
var NS6 = (navigator.appName=="Netscape" && (document.getElementById!=undefined));
var NS4 = (navigator.appName=="Netscape");
var IE  = (navigator.appName=="Microsoft Internet Explorer");

/*
Object: imageAction
parameters: normal: normal image to show
	    mo: mouseover image to show
	    active: active image to show
functionality: having an object all together so it can be used by all the objects	   
*/
function imageAction(imgname, normal, mo, active)
{
	this.name=imgname
	this.normalimg = new Image();
	this.normalimg.src = normal;
	this.mouseoverimg = new Image();
	this.mouseoverimg.src = mo;	
	this.activeimg = new Image();
	this.activeimg.src = active;
}

/*
function: iPreload()
parameters: normalimg (string seperated by pipes ("|"), to preload those images)
functionality: preloading of the images
*/
function iPreload(imgname, normalimg, mouseoverimg, activeimg)
{
    // Check if array already exists, if not, create it
    if(!document.imagesarray) document.imagesarray = new Array();
    
    // Array should've been created now, let's continue.
    szImgname	= imgname.split("|");
    szNormal 	= normalimg.split("|");
    szMouseover	= mouseoverimg.split("|");
    szActive	= activeimg.split("|");
    for(var i=0;i<szNormal.length;i++)
    	document.imagesarray[document.imagesarray.length] = new imageAction(szImgname[i], szNormal[i], szMouseover[i], szActive[i]);
}

/*
function: addPreloadImage()
parameters: normalimg (string seperated by pipes ("|"), to preload those images)
functionality: alows for postload of preload-images...
*/
function addPreloadImage(imgname, normalimg, mouseoverimg, activeimg)
{
		if(!document.imagesarray) document.imagesarray = new Array();
		
		// Array should excist now, let's continue.
		document.imagesarray[document.imagesarray.length] = new imageAction(imgname, normalimg, mouseoverimg, activeimg);
}

/*
function: iSwap()
parameters: imgname (name of the image which has to be swapped)
functionality: swapping images (mouseover, mouseout)
*/
function iSwap(imgname, state)
{
    iObj = GetImage(imgname);
    if(!iObj || !document.imagesarray) return;
    if(document.imageCurrent && (iObj==document.imageCurrent) && swFlag) return;
    for(i=0;i<document.imagesarray.length;i++)
   	if(document.imagesarray[i].name==imgname)
   	    iObj.src = eval("document.imagesarray[i]."+((state==1)?"mouseoverimg":"normalimg")+".src");
}

/*
function: iClick()
parameters: imgname (name of the image on which has been clicked)
functionality: swapping images to a clicked state, and unclick the last one clicked!
*/
function iClick(imgname)
{
    iObj = GetImage(imgname)
    if((!iObj || !document.imagesarray) || iObj==document.imageCurrent) return;
    if(document.imageCurrent && document.imageNormal)
        document.imageCurrent.src = document.imageNormal.src;    
    if(document.imageCurrent && iObj==document.imageCurrent) return;
    for(i=0;i<document.imagesarray.length;i++)
    {
    	if(document.imagesarray[i].name==imgname)
    	{
   	    iObj.src = document.imagesarray[i].activeimg.src; 	
   	    document.imageCurrent = iObj;
   	    document.imageNormal = document.imagesarray[i].normalimg;
    	}
    }    
}

/*
function GetImage() / FindImage()
parameters: imgname (name of the image on which has been clicked)
functionality: find an image for both IE and NS, especially for NS which has layers, so we need to search the layer to find the image in there, recursive!
*/
function GetImage(imgname)
{
	if(IE||NS4) return (NS4)?FindImage(imgname):document.all[imgname];
	else if(NS6) return eval("document."+imgname);
}

function FindImage(imgname, obj)
{
    var i=0;
    var perObj = null;
    var tmpObj = (obj) ? obj.document.layers : document.layers;
    if(eval("document."+imgname)) return eval("document."+imgname);
    for(i=0;i<tmpObj.length;i++)
    {
        if(eval("tmpObj[i].document."+imgname)) return eval("tmpObj[i].document."+imgname);
        perObj = FindImage(imgname, tmpObj[i])
 	if(perObj) return perObj;
    } 
    return false;	
}
