var brand = brand || {};

/**
 * brand.img 
 * This class creates and preloads an image object for rollovers, src switching, etc...
 * @memberOf brand
 */
brand.img = Class.create({
     /**
     * @return {Object} instance of brand.img with the following methods available: changeSrc
     * @example
            var imgNode = link.select('img')[0];
            var imgObj = new generic.img(imgNode, ["on", "off"]);
            imgObj.changeSrc("on");
     * @param {Node} imgNode   
     * @param {Array} states  array of states/versions of the image, by standardized suffix, such as "on"
     * @methodOf brand.img
     */ 
    initialize: function(imgNode, states) {
        if (!imgNode) {
            console.log("brand.img: imgNode UNDEFINED");
            return false;
        }
        //console.log("brand.img: called on imgNode "+imgNode.id);
        this.node = imgNode;
        this.preloaded = {};
        // get filename bits 
        var src = this.node.src;
        var bits = src.match(/^(.*)_(on|off|sel|dis)\.(.*?)$/);
        this.suffix = "";

        if ( bits == null ) {
            var bits = src.match(/^(.*)_(on|off|sel|dis)_(.*?)\.(.*?)$/); // look for suffix between on|off & extension, ex: base_on_fr_ca.gif
            if ( bits == null ) { return false; }
            this.suffix = "_" + bits[3];
        }

        this.srcBase = bits[1];
        this.srcExt = bits[bits.length-1];
        if (!this.srcExt) return false;
        this.states = states;

        // preload img
        // note: version arg must correspond to suffix of image, i.e.:
        // if default image name is image_name_off.jpg, arg value "on" looks for image_name_on.jpg
        if (!this.states) return;
        for (var i=0;i<this.states.length;i++) {
            var state = this.states[i];
            var separator = (state !== "" ? "_" : "");
            var preloadSrc = this.srcBase + separator + state + this.suffix + '.' + this.srcExt;
            var pl = new Image();               
            pl.src = preloadSrc; 
            this.preloaded[state] = pl;
        }
    },
    changeSrc: function(state) {
        var p = (this.preloaded ? this.preloaded[state] : null);
        if (!this.srcBase) return;
        // use preloaded src ref
        if (p) {
            this.node.src = p.src;
            
        // else load img via path
        } else {
            this.node.src = this.srcBase + '_' + state + this.suffix + '.' + this.srcExt;
        }
    }
});

/**
 * brand.rollover 
 * Applies image switching behavior to an image node on mouseover/out of the specified
 * image node or (if specified) to a different trigger node
 * @memberOf brand
 */
brand.rollover = Class.create({
     /**
     * Creates instances of brand.rollover
     * @example
            var rollover = new generic.rollover($("my-image"), $("my-button")); 
     * @param {Node} imgNode   image whose src will be changed on mouseover/out
     * @param {Node} triggerNode  *Optional* target node of mouse event.  Default is imgNode.
     */
    initialize: function(/* Object */imgNode,/* Object */triggerNode){
        var trigger = (triggerNode == null ? imgNode : trigger);
        this.img = new brand.img(imgNode, ["off","on"]);
        trigger.observe("mouseover", this.onMouseOver.bind(this));
        trigger.observe("mouseout", this.onMouseOut.bind(this));
    },
    onMouseOver: function(e) {
        if (!e.target.hasClassName("disable_rollover")) {
            this.img.changeSrc("on");
        }
    },
    onMouseOut: function(e) {
        if (!e.target.hasClassName("disable_rollover")) {
            this.img.changeSrc("off");
        }
    }
});

/**
 * Preloads and stores a batch of images for use with image src switching 
 * @return {Object} Hash of preloaded images
 * @param {DOM Node} args.node  Image node
 * @param {String} args.imagePath  Image file path to preload and store 
 * @param {Object} args.imageStore  Previously saved image hash to append to
 * @param {String} args.imageId  ID key for image in hash 
 * @memberOf brand
 */
brand.loadImage = function(args) {
    var imgStore = args.imageStore;
    var imgId = args.imgId;
    var imgPath = args.imagePath;
    var node = args.node;
    if (!node || (typeof(imgId) === "undefined") || !imgStore || !imgPath) {
        return imgStore;
    }
    imgId.toString();
    var preloadedImg = imgStore[imgId];
    
    // if first request for image, store it
    if (!preloadedImg) {        
        imgStore[imgId] = new Image();
        imgStore[imgId].src = imgPath;
        node.src = imgStore[imgId].src;
    
    // if 2nd+ request get preloaded image
    } else {
        node.src = preloadedImg.src;
    }
    return imgStore;
}
