/*
Author: Philippe Asselin
Date:   20060629
Desc:   This function is used to loop through all the images on the page and for each that has the
        _Std. or _Down. notation, we preload the _Over. and _Down. image for faster display when the
        image is changed through the changeImage() function.
        This function is called on body pageload of the publis.master page.
*/
function preloadImages() {
    var oImgLoader;
    for(var i = 0; i < document.images.length; i++) {
        if(document.images[i].src.indexOf('_Std.') > 0) {
            oImgLoader = loadImage(document.images[i].src.replace('_Std.', '_Over.'));
        //  oImgLoader = loadImage(document.images[i].src.replace('_Std.', '_Down.'));
        }
        else if(document.images[i].src.indexOf('_Down.') > 0) {
            oImgLoader = loadImage(document.images[i].src.replace('_Down.', '_Over.'));
        }
    }
}

/*
Author: Philippe Asselin
Date:   20060629
Desc:   This function is used to handle the image mouse over, out, down and up. It can receive three
        states:
        - over to show the over image from any starting image saved in sImgSrcTemp
        - down to show the down image from any strating image saved in sImgSrcTemp
        - out the default state to revert the image to the original state.
        Call and usage sample:
        - onmouseover="changeImage(this, 'over');"
        - onmouseout="changeImage(this, 'out');"
        - onmousedown="changeImage(this, 'down');"
        - onmouseup="changeImage(this, 'out');"
*/
var sImgSrcTemp;
function changeImage(oImg, state) {
    if (state == 'over') {
        // On mouse over, the original image state is saved in sImgSrcTemp for replacement on mouse out or mouse up.
        sImgSrcTemp = oImg.src;
        oImg.src = oImg.src.replace('_Std.', '_Over.');
    }
//    else if (state == 'down') {
//        // On mouse down, we must show the mouse down image. At this point we should always already be in mouse over state put this
//        // code does not assume this state and verifies it.
//        if (oImg.src.indexOf('_Over.') > 0) {
//            // If already in mouse over state, the original image is already saved in sImgSrcTemp so we do not overwrite it and 
//            // place the down image.
//            oImg.src = oImg.src.replace('_Over.', '_Down.');
//        }
//        else {
//            // This should never occur, but in case it happens, we save the original image in sImgSrcTemp and then place the down
//            // image.
//            sImgSrcTemp = oImg.src;
//            oImg.src = oImg.src.replace('_Std.', '_Down.');
//        }
//    }
    else { // state == 'out'
        // This state is called by mouse out and mouse up to return to the original image saved in sImgSrcTemp.
        if(sImgSrcTemp != '') {
            oImg.src = sImgSrcTemp;
        }
    }
}

/*
Author: Philippe Asselin
Date:   20060629
Desc:   This function is used to load an image into an image object.
        We use this function to preload all over and down images for the std images. This is used from the preloadImages function.
*/
function loadImage(sImgSrc) {    
   	oImg = new Image();
	oImg.src = sImgSrc;
	return oImg;
}

function reloadImage(sImgSrc) {    
	oImg = new Image();
	oImg.src = sImgSrc + '?' + Math.random();

	return oImg;
}
