/**
 * This function takes all the images
 * in the document.images array and
 * attempts to download them. Just a
 * simple loop, is all...
 * This should actually be a drag-n-drop
 * script, too. :)
 *
 * @usage Run at window.onload=function(){doPreloadImages();};
 */
function doPreloadImages(){
    // This represents all the img tags in the
    // page, but not the CSS images, which it will
    // not preload.
    var images = document.images;
    for (i = 0; i < images.length; i++){
        var image = new Image(1,1);
        image.src = images[i].src;
    }
}
/**
 * This function takes a url and checks
 * the file extension to see if it is an
 * image.
 * NOTE: No query strings please!
 *
 * @param url Denotes the url to check.
 */
function checkIfImage(url) {
    var v_switch = url.substring(url.length-3,url.length).toLowerCase();
    switch (v_switch) {
        // If the v_switch var equals any of the following...
        case 'gif':
        case 'jpg':
        case 'png':
        case 'bmp':
            // Then yes, it is an image...
            return true;
        default:
            // Otherwise, it's not an image.
            return false;
    }
}
/**
 * This function is used to preload images that
 * are provided by an array of anchor tags.
 * It checks for image url paths in the
 * REL and HREF attribute.
 *
 * @param arr Represents list of anchor tags to check
 * @usage Run at window.onload=function(){LinkedImagePreloader();};
 */
function doLinkedImagePreloader(arr) {
    // Declare the document.images, for use later.
    var images = new Array();
    var img_set = new Array();
    // Now, for every anchor tag in the arr parameter
    // cycle through and attempt to determine
    // if there are images linked to, and if so, preload.
    for (var i = 0; i < arr.length; i++) {
        // Easier to reference than arr[i].
        var img = arr[i];
        // Used to store the url's to the rel/href images.
        // Check the rel attribute first.
        img_set[img_set.length] = (img.rel && checkIfImage(img.rel) === true ? img.rel : null);
        // Now check the href attribute.
        img_set[img_set.length] = (img.href && checkIfImage(img.href) === true ? img.href : null);
        // If the img_set array has something in it...
        if (img_set.length > 0) {
            // Loop through...
            for (var p = 0; p < img_set.length; p++) {
                // If the current item is not null...
                if (img_set[p] !== null) {
                    // Get the location...
                    var loc = img_set[p];
                    // Make a new image object...
                    images[images.length] = new Image(1,1);
                    // Begin downloading!
                    images[images.length-1].src = loc;
                }
            }
        }
    }
}
function doPreloadLinkedImages() {
    // Replicate for any anchor tags that use rel="" or href="" to link to an image
    // Pass the function the anchor tags with the rel and/or href attributes
    // First, the subnav UL...
    if (document.getElementById('subnav')) {
        var imgs = document.getElementById('subnav').getElementsByTagName('a');
        doLinkedImagePreloader(imgs);
    }
    // Now, the productnav UL...
    if (document.getElementById('nav')) {
        var imgs = document.getElementById('nav').getElementsByTagName('a');
        doLinkedImagePreloader(imgs);
    }
}

