/*************************************************************************/

/*  JavaScript utility - rollover                                        */

/*  Copyright 2000, Brooklyn College Academic Information & Technologies */

/*  http://academic.brooklyn.cuny.edu/ait/                               */

/*  Michael A. Mayo                  |        March 14, 2000             */

/*  mmayo@mcauleybrooklyn.org        |        x5279                      */

/*************************************************************************/

// This file includes code for an image swap.



/* imageBank - This is an object which allows us to easily preload and swap images.

*  Usage to preload images:

*       myImages = new imageBank();

*       myImages.add("newImage.gif");

*       myImages.add("newImage2.gif");

*       myImages.add("newImage3.gif");

*       ...

*

*       or:

*       myImages.add("newImage.gif", "newImage2.gif", "newImage3.gif", ...);

*

*       Either synatax is acceptable, but the first is recommended.

*

*

* Usage to swap images:

*       <A HREF="http://link.com">

*               <IMG SRC="newImage.gif" 

*                NAME="newImage"

*                ONMOUSEOVER="myImages.swap('newImage','newImage2.gif')"

*               >

*       </A>

*       Swap's first argument must be the name of an image anywhere in the HTML document.

*       Swap's second argument must be the URL of another image that has been preloaded using the add() function.

*       Swap will give you a warning if you do not preload the second argument.

*

*

*       imageBank will warn you if you do things like forget to preload images or leave out quotes on function paramaters.

*

*

*  All functions return true if they operate successfully, false if they do not.

*/                      

function imageBank() {

        // Variables

        this.enable_imageswap;                                          // tells us if we should do the swap

        this.loaded_images = new Array();                       // holds a list of all the images we have preloaded so far

        this.browser_name = navigator.appName;  // we need the browser name & version to help us tell if the browser is capable of running this script

        this.browser_version = parseFloat(navigator.appVersion);



        // Member Functions

        this.add = add; // adds images to be preloaded

        this.swap = swap;       // swaps 2 images



        // Utility Functions

        // Does the work neccecary to compare a full URL to a relative URL.  Returns true if they point to the same file, false if not.

        this.compareURL = compareURL;   



        // Constructor

        // Do some browser sniffing to see whether the browser is capable of running this code.

        // If the browser is Netscape or MSIE, we know it can run this script only if it's version 3.0 or higher

        // Otherwise, we're not entirely sure, but if document.images is supported, run the script anyway and hope. =)

        if( this.browser_name == "Netscape") {

                this.enable_imageswap = (this.browser_version >= 3.0);

        } else if( this.browser_name == "Microsoft Internet Explorer") {

                this.enable_imageswap = (this.browser_version >= 3.0);

        } else {

                if( document.images ) { this.enable_imageswap = true; }

                else { this.enable_imageswap = false; }

        }

}



/* This is a member function of the imageBank object.

*  It adds another image to be preloaded.

*  Usage:

*  var myImages = new imageBank();

*  myImages.add("imageURL1");

*  myImages.add("imageURL2");

*  or:

*  myImages.add("imageURL1","imageURL2","imageURL3",...);

*

* Either syntax works fine, but the first is recommended as it is harder to mess up.

* Returns true if operates sucesfuly, false otherwise.

*/

function add( ) {

        // Exit immediately if we aren't supposed to run this code.

        if ( ! this.enable_imageswap ) {

                return false;

        }

        

        // Process each image given to function as argument

        for( var i = 0; i < add.arguments.length; ++i ) {

                // Check arguments to make sure user didn't input some weird variable instead

                if( typeof add.arguments[i] != "string" ){

                        alert("Warning: non-string arguments passed to the add function.");

                } else {

                        // Add the image to the list of loaded images

                        this.loaded_images[this.loaded_images.length] = new Image();

                        this.loaded_images[(this.loaded_images.length - 1)].src = add.arguments[i];

                }

        }

        return true;

}

        



/* This is a member function of the imageBank object.

*  It replaces one image in the HTML document with another.

*  Usage:

*       <A HREF="http://link.com">

*               <IMG SRC="newImage.gif" 

*               NAME="newImage"

*               ONMOUSEOVER="myImages.swap('newImage','newImage2.gif')"

*               >

*       </A>

*  Swap's first argument must be the name of an image anywhere in the HTML document.

*  Swap's second argument must be the URL of another image that has been preloaded using the add() function.

*  Swap will give you a warning if you do not preload the second argument.

*

*  Returns true if operates sucessfuly, false otherwise.

*/                      

function swap( image1_name, image2_url ) {

        // Exit immediately if we aren't supposed to run this code

        if( ! this.enable_imageswap ) {

                return false;

        }



        // Make sure the user remembered to quote the arguments

        if( (typeof image1_name != "string") || (typeof image2_url != "string") ) {

                alert("Warning: one of the arguments to the swap function was not quoted. This swap will not work.");

                return false;

        }



        // Make sure the user remembered to preload the images being swapped

        var found = false;

        var full_url;

        for( var i = 0; !found && (i < this.loaded_images.length); ++i) {

                found = compareURL(this.loaded_images[i].src, image2_url);

                full_url = this.loaded_images[i].src;

        }

        if( !found ) {

                var warning_string = "Warning: image " + image2_url + " was not preloaded using the add function.  This may cause the rollover to perform poorly.";

                alert(warning_string);

        }



        // Finally, swap the images!

        document.images[image1_name].src = full_url;

        return true;

}





/* This is a member function of the imageBank object.

*  It is a "utility function" used by other members of imageBank.

*  It is not intended to be used by the user.

*  

*  It compares a full URL (first argument) to a relative URL (second argument), to see if they point to the same file.

*  Usage: compareURL("/a/r/c/help.html","/c/help.html");

*

*/

function compareURL(full_url, relative_url) {

        var start_index = full_url.length - relative_url.length;

        var end_index = full_url.length;

        var converted_full_url = full_url.substring(start_index, end_index);



        return( converted_full_url == relative_url );

}