/********************************************************************************
FILE:					imageshandling.js
PURPOSE:				General mouseover, mouseclick functions
WHAT IT	CONTAINS:		Functions for general imagehandling, including mouseover
						mouseout and click states. Also handles preloading.
AUTHOR:					CJ de Vos.
********************************************************************************/

isNS = navigator.appName=="Netscape"
/********************************************************************************
function		: imageAction(imgname, normal, mo)
parameters		: imgname - name of the image as found in the image tag (input, string)
				  normal - source of the normalstate image (input, string)
				  mo - source of the mouseover image (input, string)
Date added		: 10 Feb 2001
Changed			: 19 Feb 2002 by Max Davidse (added comments)
Functionality	: Holds all imagenames and sources used for preloading.
Author			: CJ de Vos
Still Needs		: -
********************************************************************************/
function imageAction(imgname, normal, mo)
{
	this.name=imgname
	this.normalimg = new Image();
	this.normalimg.src = normal;
	this.mouseoverimg = new Image();
	this.mouseoverimg.src = mo;
}

/********************************************************************************
function		: iSwap(imgname, state)
parameters		: imgname - name of the image as found in the image tag (input, string)
				  state - either 1 or 0, to turn the image on or off (input, integer)
Date added		: 10 Feb 2001
Changed			: 19 Feb 2002 by Max Davidse (added comments)
Functionality	: Swaps the image to on or off, depending on the state parameter.
Author			: CJ de Vos
Still Needs		: -
********************************************************************************/
function iSwap()
{
    args = iSwap.arguments;
    for(var e=0;e<args.length;e+=2)
    {
    	bIsMatch=false
    	if (document.activearray)
    	{
    		for(i=0;i<document.activearray.length;i++) if(document.activearray[i]==args[e]) bIsMatch=true;
    		if(!bIsMatch && (iObj = GetImage(args[e])) && document.imagesarray)
	   		for(i=0;i<document.imagesarray.length;i++)
	   			if(document.imagesarray[i].name==args[e])
				 iObj.src = eval("document.imagesarray[i]."+((args[e+1]==1)?"mouseoverimg":"normalimg")+".src");
	}
    }
}

/********************************************************************************
function       : iClick()
parameters     : arguments - array of imagenames as found in the image tag (input, string)
Date added     : 10 Feb 2001
Changed        : 19 Feb 2002 by Max Davidse (added comments)
Functionality  : Swaps the image to it's clicked state.
Author         : CJ de Vos
Still Needs    : -
********************************************************************************/

function iClick()
{
    var bIsMatch;
    args = iClick.arguments; // ** all images that need to be "clicked"
    if (!document.activearray) return;

    for(i=0;i<document.activearray.length;i++)
    {
    	bIsMatch = false;
    	for(e=0;e<args.length;e++)
    		if(args[e]==document.activearray[i]){ bIsMatch=true; break; }
    	if(!bIsMatch)
    	{
    		// alert("there is an active image that needs to be set back! ("+document.activearray[i]+")")
    		iObj = GetImage(document.activearray[i]);
    		if(iObj) iObj.src=document.normalarray[i];
    	}
    }

    // ** we have our buffer, turn all on!
    for(i=0;i<args.length;i++)
    {
    	iObj = GetImage(args[i]);
    	if(iObj)
  	   for(e=0;e<document.imagesarray.length;e++)
    	      if(document.imagesarray[e].name==args[i])
    	      {
    		 iObj.src = document.imagesarray[e].mouseoverimg.src;
    		 document.normalarray[i] = document.imagesarray[e].normalimg.src;
    	      }
    }
    document.activearray = args;

}

/********************************************************************************
function		: iPreload(imgname, normalimg, mouseoverimg)
parameters		: imgname - name of the image as found in the image tag (input, string)
				  normalimg - source of the normalstate image (input, string)
				  mouseoverimg - source of the mouseover image (input, string)
Date added		: 10 Feb 2001
Changed			: 19 Feb 2002 by Max Davidse (added comments)
Functionality	: Preloads all used images.
Author			: CJ de Vos
Still Needs		: -
********************************************************************************/
function iPreload(szImgname, szNormal, szMouseover)
{
	// Check if array already exists, if not, create it
	if(!document.imagesarray) document.imagesarray = new Array();
	if(!document.activearray) document.activearray = new Array();
	if(!document.normalarray) document.normalarray = new Array();

	// Array should've been created now, let's continue.
	for(var i=0;i<szNormal.length;i++)
		document.imagesarray[document.imagesarray.length] = new imageAction(szImgname[i], szNormal[i], szMouseover[i]);
}

/********************************************************************************
function		: GetImage(imgname)
parameters		: imgname - name of the image as found in the image tag (input, string)
Date added		: 10 Feb 2001
Changed			: 19 Feb 2002 by Max Davidse (added comments)
Functionality		: Finds the image by calling the function FindImage.
Author			: CJ de Vos
Still Needs		: -
********************************************************************************/
function GetImage(imgname)
{
	if(isNS) if(document.images[imgname]) return document.images[imgname];
	return (document.layers)?FindImage(imgname):document.all[imgname];
}

/********************************************************************************
function		: FindImage(imgname, obj)
parameters		: imgname - name of the image as found in the image tag (input, string)
				  obj - reference to the object of the image. (input, object)
Date added		: 10 Feb 2001
Changed			: 19 Feb 2002 by Max Davidse (added comments)
Functionality	: Swaps the image to it's clicked state.
Author			: CJ de Vos
Still Needs		: -
********************************************************************************/
function FindImage(imgname, obj)
{
	var i=0;
	var perObj = null;
	var tmpObj = (obj) ? obj.document.layers : document.layers;
	for(i=0;i<tmpObj.length;i++)
	{
		str += tmpObj[i].id;
		if(tmpObj[i].document.images[imgname]) return tmpObj[i].document.images[imgname];
	if(perObj = FindImage(imgname, tmpObj[i])) return perObj;
	}
	return false;
}
