/* **********************************************************************
 * MODULE	: image_cache.js
 * AUTHOR	: Brian P. King
 * CREATED	: May 11, 1999
 * COMPATIBILITY: Requires JavaScript 1.0.
 * RCS		: $Id: image_cache.js,v 1.6 1999/07/27 20:17:11 bking Exp $
 * DESCRIPTION	: Implements image caching with storage and retrieval.
 * COPYRIGHT    : Copyright (c) 1999 Symantec Corporation.
 * DISTRIBUTION : Freely redistributable.  Use at your own risk.                              
 * **********************************************************************/

if ( ! versions_loaded ) {

    // Determine Browser Version
    var bV   = parseInt( navigator.appVersion );
    var NS4  = (document.layers)         ? true : false;
    var NS3  = (document.images)         ? true : false;
    var IE4  = ((document.all)&&(bV>=4)) ? true : false;
    var ver4 = (NS4 || IE4)              ? true : false;

    // Determine Browser Platform
    var MAC   = (navigator.userAgent.indexOf("Mac")      != -1) ? true : false;
    var Opera = (navigator.userAgent.indexOf("Opera")    != -1) ? true : false;
    var IE5   = (navigator.userAgent.indexOf("MSIE 5.0") != -1) ? true : false;

    var versions_loaded = true;
}


var cached_images = new Object();

function replaceString( string, replacement ) {
    var idx = string.indexOf( "%s" );
    var newstring = string;
    if ( idx >= 0 ) {
	newstring = string.substring( 0, idx ) + replacement + string.substring( idx + 2 );
    }
    return( newstring );
}


/*
 * Outputs necessary JavaScript code to cache images and
 * make them easily retrieved later by rollover code.
 * Form1: cacheImage( name, width, height, directory )
 * Form2: cacheImage( name, width, height, pattern, on_text, off_text )
 * In the former case, the "on" image (the image displayed when
 * the mouse is over the image) will be directory + '/' + name + '2.gif'.
 * The off image, the default state, will be
 * directory + '/' + name + '1.gif'.
 * In the latter case, the pattern should contain %s (for example,
 * /images/product_menu%s.gif).  The %s will then be replaced with the
 * value of on_text when the mouse is over the image, or will be
 * replaced with the value of off_text when the mouse is not.
 */
function cacheImage( name, w, h, directory_or_pattern, on_text, off_text ) {
    if ( NS3 ) {
	if ( directory_or_pattern.indexOf("%s") != -1  ) {
	    var pattern = directory_or_pattern;
	    cached_images[name]            = new Object();
	    cached_images[name].imgon      = new Image( w, h );
	    cached_images[name].imgon.src  = replaceString( pattern, on_text );
	    cached_images[name].imgoff     = new Image( w, h );
	    cached_images[name].imgoff.src = replaceString( pattern, off_text );
	}
	else {
	    var directory = directory_or_pattern;
	    cached_images[name]            = new Object();
	    cached_images[name].imgon      = new Image( w, h );
	    cached_images[name].imgon.src  = directory + "/" + name + "2.gif";
	    cached_images[name].imgoff     = new Image( w, h );
	    cached_images[name].imgoff.src = directory + "/" + name + "1.gif";
	}
    }
}


/*
 * Retrieve an image src from the cache
 */
function getImageSource( name, state ) {
    var imgSrc = null;
    if ( NS3 ) {
	if ( cached_images[name] ) {
	    if ( state == "active" ) {
		imgSrc = cached_images[name].imgon.src;
	    } else if ( state == "inactive" ) {
		imgSrc = cached_images[name].imgoff.src;
	    }
	}
    }
    return( imgSrc );
}

function setImage( imgName, state ) {
    if ( NS3 ) {
	var newimg = getImageSource( imgName, state );
	if ( newimg )
	    if ( document.images[imgName] )
		document.images[imgName].src = getImageSource( imgName, state );
    }
}


