function BtnMode(css, attr) {
	this.css = css;
	this.attr = attr;
}
function BtnBase(node) {
    var def = null;
    var hl = null;
    var d = null;
    var p = null;
    var pd = null;
    var enabled = true;
    var pushed = false;
    var isHL = false;
    var clickHandler = null;

    this.setModes = function(defMode, hlMode, pMode, dMode, pdMode) {
		def = defMode;
        hl = hlMode;
        p = pMode;
        d = dMode;
        pd = pdMode;
    }
    this.setId = function(id) {
        node.id = id;
    }
    this.onClick = function(handler) {
        clickHandler = handler;
    }

    this.enable = function(state) {
        enabled = state;
        setImage();
    }
    this.push = function(state) {
        pushed = state;
        setImage();
    }

    this.getDOM = function() {
        return node;
    }

	var setMode = function(mode) {
		if(mode.css)
			node.className = mode.css;
		if(mode.attr) {
			for (var a in mode.attr) {
				var v = mode.attr[a];
				if(v)
					node.setAttribute(a, mode.attr[a]);
			}	
        }
	}
    var setImage = function() {
        if (pushed && enabled) {
            setMode(p);
        }
        else if (!pushed && enabled) {
            setMode(isHL ? hl : def);
        }
        else if (pushed && !enabled) {
            setMode(pd);
        }
        else if (!pushed && !enabled) {
            setMode(d);
        }
    }

    var mouseOver = function(e) {
        if (enabled && !pushed)
            setMode(hl);
        isHL = true;
    }
    var mouseOut = function(e) {
        if (enabled && !pushed)
            setMode(def);
        isHL = false;
    }
    var mouseDown = function(e) {
        if (enabled && !pushed)
            setMode(p);
    }
    var mouseUp = function(e) {
        if (enabled && !pushed) {
            setMode(isHL ? hl : def);
        }
    }
    var click = function(e) {
        if (enabled && clickHandler)
            clickHandler()
    }
    node.onmouseover = mouseOver;
    node.onmouseout = mouseOut;
    node.onmousedown = mouseDown;
    node.onmouseup = mouseUp;
    node.onclick = click;
}

function LinkBtn(link) {
	BtnBase.call(this, link);
	this.setCSS = function(defCSS, hlCSS, pCSS, dCSS, pdCSS) {
		this.setModes(new BtnMode(defCSS), 
					  new BtnMode(hlCSS), 
					  new BtnMode(pCSS), 
					  new BtnMode(dCSS), 
					  new BtnMode(pdCSS));
	}
    this.setText = function(text) {
		link.innerHTML = text;
    }
	this.setToolTip = function(text) {
		link.title = text;
    }
}
function ImgBtn(img) {
	BtnBase.call(this, img);
	this.setImg = function(defImg, hlImg, pImg, dImg, pdImg) {
		this.setModes(new BtnMode('imgbtn',{src:defImg}), 
					  new BtnMode('imgbtn',{src:hlImg}),
					  new BtnMode('imgbtn',{src:pImg}),
					  new BtnMode('imgbtn_d',{src:dImg}),
					  new BtnMode('imgbtn_d',{src:pdImg}));
	}
    this.setToolTip = function(toolTip) {
        img.alt = toolTip;
        img.title = toolTip;
    }
}