﻿// Classical Inheritance Sugar Routine
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

var Artforge = {};

Artforge.Menu = function(menuItems,strImageRoot,options){
    this.options = options || {};
    this.menuItems = menuItems
    this.useInnerImage = (this.options.useInnerImage) || false;
    this.imageRoot = strImageRoot;
    
    this.initialize();

}

 Artforge.Menu.method("initialize",function(){
    var me = this;
    this.menuItems.each(function(menuItem){
        var objItem = new Artforge.MenuItem(
            menuItem[0],
            menuItem[2],
            menuItem[1],
            me.imageRoot,
            {useInnerImage:me.useInnerImage}
        );
    });
});

Artforge.MenuItem = function(elementID,imageOn,imageOff,strImageRoot,options){
    this.options = options || {};
    this.element = $(elementID);
    this.imageElement = ((this.options.useInnerImage == true) ? this.getImageElement() : this.element);
    this.imageOn = imageOn;
    this.imageOff = imageOff;
    this.imageRoot = strImageRoot;
    
    this.preloadImages()
    
    this.element.onmouseover = this.mouseover.bindAsEventListener(this);
    this.element.onmouseout = this.mouseout.bindAsEventListener(this);
}

Artforge.MenuItem.method("preloadImages",function(){
    var myImage = new Image;
    myImage.src = this.imageRoot + this.imageOn;
});

Artforge.MenuItem.method("getImageElement",function(){
    if (this.element.childNodes){
        for (var i = 0 ; i < this.element.childNodes.length ; i++){
            if (this.element.childNodes[i].tagName == "IMG"){
                return this.element.childNodes[i];
            }
        }
    }
});
Artforge.MenuItem.method("mouseover",function(){
    this.imageElement.src = this.imageRoot + this.imageOn;
});

Artforge.MenuItem.method("mouseout",function(){
    this.imageElement.src = this.imageRoot + this.imageOff;
});