/********************************************************************************
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.
********************************************************************************/


/********************************************************************************
function       : imageAction(imgname, normal, mo, active)
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)
				 active - source of the clicked state 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, 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       : 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(imgname, state)
{
    iObj = GetImage(imgname);
    if(!iObj) return;
    if(document.imageCurrent && iObj==document.imageCurrent) return;
    if(document.imagesarray)
    {
      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(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  : Swaps the image to it's clicked state.
Author         : CJ de Vos
Still Needs    : -
********************************************************************************/
function iClick(imgname)
{
    iObj = GetImage(imgname)
    if(!iObj) return;
    if(document.imageCurrent && iObj==document.imageCurrent) return;
    if(document.imageCurrent && document.imageNormal)
        document.imageCurrent.src = document.imageNormal.src;
    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       : iPreload(imgname, normalimg, mouseoverimg, activeimg)
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)
				 activeimg - source of the clicked 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(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       : 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)
{
	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;
}
