$(function() {
    // don't even bother trying to fix ie6
    if (jQuery.browser.msie && (jQuery.browser.version.substr(0,1) == '6')) { return; }
    // don't even bother trying to fix ie6
    //if (jQuery.browser.msie && (jQuery.browser.version.substr(0,1) == '8')) { return; }
    
    $('<img src="' + gBaseUrl + 'images/pixel.gif' + '#' + Math.random() + '"/>').load(function () {
        $('a.image').each(function() {
            var a = this;
            var img = hrefToImageUrl(this);
            $('<img src="' + img + '"/>').load(function() {
                $(a).html('<img src="' + img + '" alt="' + $(a).text() + '"/>');
                if (jQuery.browser.msie && (jQuery.browser.version.substr(0,1) != '8')) {
                  $('img', a).load(function() {
                    $(this).css('width', $(this).width() + 'px');
                    $(this).css('height', $(this).height() + 'px');
                    // $(this).css('behavior', 'url(' + gBaseUrl + 'css/iepngfix.htc)');
                  });
                }
            });
        });
    });
    
    if (gActionName) {
        var actionNameParts = gActionName.split('-');
        var menuEl = null;
        var menuId = 'menu';
        do {
            menuId += '-' + actionNameParts.shift();
            $('#' + menuId).each(function() {
                $(this).addClass('active');
            });
        } while (actionNameParts.length)
        
        $('#' + menuId + '-intro').each(function() {
            $(this).addClass('active');
        });
    }
});

function hrefToImageUrl(href) {
    var image_filename = $(href).text();
    image_filename = jQuery.trim(image_filename);
    image_filename = image_filename.toLowerCase();
    image_filename = image_filename.replace(/&/g, 'and');
    image_filename = image_filename.replace(/ /g, '-');
    image_filename = image_filename.replace(/--+/g, '_');
    image_filename = gBaseUrl + 'images/menu/' + image_filename + '.png';
    return image_filename;
}