function imagereplace(selector, url) {
    var found = getElementsBySelector(selector);
    if (found.length) {
        var el = found[0];
        if (el.tagName && 'ul' == el.tagName.toLowerCase()) {
            for (var i = 0; i < el.childNodes.length; i++) {
                var li = el.childNodes[i];
                if (1 == li.nodeType && 'li' == li.tagName.toLowerCase()) {
                    var link = null;
                    for(var j = 0; j < li.childNodes.length; j++) {
                        if (1 == li.childNodes[j].nodeType) {
                            if ('a' == li.childNodes[j].tagName.toLowerCase()) {
                                link = li.childNodes[j];
                                j = li.childNodes[j].length;
                            }
                        }
                    }
                    if (link) {
                        var text = null;
                        for(var j = 0; j < link.childNodes.length; j++) {
                            if (3 == link.childNodes[j].nodeType) {
                                text = link.childNodes[j];
                                j = link.childNodes[j].length;
                            }
                        }
                        var on = document.createElement('img');
                        on.alt = text.nodeValue;
                        on.src = url+'?text='+encodeURIComponent(text.nodeValue)+'&on=true';

                        if (-1 != li.className.indexOf('selected')) {
                            link.replaceChild(on, text);
                        } else {
                            on.style.display = 'none';
                            var off = document.createElement('img');
                            off.alt = text.nodeValue;
                            off.src = url+'?text='+encodeURIComponent(text.nodeValue);

                            li._on = on;
                            li._off = off;
                            var dims = getDimensions(link);
                            link.style.width = dims.width+'px';
                            link.style.height = dims.height+'px';

                            link.replaceChild(on, text);
                            link.insertBefore(off, on)
                            li.onmouseover = function() {
                                this.className += ' hover';
                                this._off.style.display = 'none';
                                this._on.style.display = '';
                            }
                            li.onmouseout = function() {
                                this.className = this.className.replace(' hover', '');
                                this._on.style.display = 'none';
                                this._off.style.display = '';
                            }
                        }
								 link.style.visibility = "visible";
                    }
                }
            }
        }
    } else {
        alert("invalid selector specified: "+selector);
    }
}

/* taken from pototype */
function getDimensions(element) {
    var display = element.style.display;
    if (display != 'none' && display != null) // Safari bug
      return {width: element.offsetWidth, height: element.offsetHeight};

    // All *Width and *Height properties give 0 on elements with display none,
    // so enable the element temporarily
    var els = element.style;
    var originalVisibility = els.visibility;
    var originalPosition = els.position;
    var originalDisplay = els.display;
    els.visibility = 'hidden';
    els.position = 'absolute';
    els.display = 'block';
    var originalWidth = element.clientWidth;
    var originalHeight = element.clientHeight;
    els.display = originalDisplay;
    els.position = originalPosition;
    els.visibility = originalVisibility;
    return {width: originalWidth, height: originalHeight};
}


/*
	Finds elements on page that match a given CSS selector rule. Some
	complicated rules are not compatible.
	Based on Simon Willison's excellent "getElementsBySelector" function.
	Original code (with comments and description):
		http://simon.incutio.com/archive/2003/03/25/getElementsBySelector
*/
function getElementsBySelector(selector)
{
    var tokens = selector.split(' ');
    var currentContext = new Array(document);
    for(var i=0;i<tokens.length;i++)
    {
        token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');
        if(token.indexOf('#') > -1)
        {
            var bits = token.split('#');
            var tagName = bits[0];
            var id = bits[1];
            var element = document.getElementById(id);
            if(tagName && element.nodeName.toLowerCase() != tagName)
            return new Array();
            currentContext = new Array(element);
            continue;
        }

        if(token.indexOf('.') > -1)
        {
            var bits = token.split('.');
            var tagName = bits[0];
            var className = bits[1];
            if(!tagName)
            tagName = '*';

            var found = new Array;
            var foundCount = 0;
            for(var h=0;h<currentContext.length;h++)
            {
                var elements;
                if(tagName == '*')
                elements = currentContext[h].all ? currentContext[h].all : currentContext[h].getElementsByTagName('*');
                else
                elements = currentContext[h].getElementsByTagName(tagName);

                for(var j=0;j<elements.length;j++)
                found[foundCount++] = elements[j];
            }

            currentContext = new Array;
            var currentContextIndex = 0;
            for(var k=0;k<found.length;k++)
            {
                if(found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b')))
                currentContext[currentContextIndex++] = found[k];
            }

            continue;
        }

        if(token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/))
        {
            var tagName = RegExp.$1;
            var attrName = RegExp.$2;
            var attrOperator = RegExp.$3;
            var attrValue = RegExp.$4;
            if(!tagName)
            tagName = '*';

            var found = new Array;
            var foundCount = 0;
            for(var h=0;h<currentContext.length;h++)
            {
                var elements;
                if(tagName == '*')
                elements = currentContext[h].all ? currentContext[h].all : currentContext[h].getElementsByTagName('*');
                else
                elements = currentContext[h].getElementsByTagName(tagName);

                for(var j=0;j<elements.length;j++)
                found[foundCount++] = elements[j];
            }

            currentContext = new Array;
            var currentContextIndex = 0;
            var checkFunction;
            switch(attrOperator)
            {
                case '=':
                checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
                break;
                case '~':
                checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
                break;
                case '|':
                checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
                break;
                case '^':
                checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
                break;
                case '$':
                checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
                break;
                case '*':
                checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
                break;
                default :
                checkFunction = function(e) { return e.getAttribute(attrName); };
            }

            currentContext = new Array;
            var currentContextIndex = 0;
            for(var k=0;k<found.length;k++)
            {
                if(checkFunction(found[k]))
                currentContext[currentContextIndex++] = found[k];
            }

            continue;
        }

        tagName = token;
        var found = new Array;
        var foundCount = 0;
        for(var h=0;h<currentContext.length;h++)
        {
            var elements = currentContext[h].getElementsByTagName(tagName);
            for(var j=0;j<elements.length; j++)
            found[foundCount++] = elements[j];
        }

        currentContext = found;
    }

    return currentContext;
}