$(function() {
    // don't even bother trying to fix ie6
    if (jQuery.browser.msie && (jQuery.browser.version.substr(0,1) == '6')) { return; }
    
    $('<img src="' + gBaseUrl + 'images/pixel.gif' + '#' + Math.random() + '"/>').load(function () {
        // images are on
        $('#content-container .content h1.image').each(function() {
            var h1 = this;
            var img = h1ToImageUrl(this);
            $('<img src="' + img + '"/>').load(function() {
                $(h1).html('<img src="' + img + '" alt="' + $(h1).text() + '"/>');
                if (jQuery.browser.msie && (jQuery.browser.version.substr(0,1) != '8')) {
                  $('img', h1).load(function() {
                    $(this).css('width', $(this).width() + 'px');
                    $(this).css('height', $(this).height() + 'px');
                    // $(this).css('behavior', 'url(css/iepngfix.htc)');
                  });
                }
            });
        });
        $('#content-container .content h2.image').each(function() {
            var h2 = this;
            var img = h2ToImageUrl(this);
           $('<img src="' + img + '"/>').load(function() {
                $(h2).html('<img src="' + img + '" alt="' + $(h2).text() + '"/>');
                if (jQuery.browser.msie && (jQuery.browser.version.substr(0,1) != '8')) {
                  $('img', h2).load(function() {
                    $(this).css('width', $(this).width() + 'px');
                    $(this).css('height', $(this).height() + 'px');
                    // $(this).css('behavior', 'url(css/iepngfix.htc)');
                  });
                }
            });
        });
        $('.sidebar h1.sidebar-title.image').each(function() {
            var h1 = this;
            var img = sidebarH1ToImageUrl(this);
            $('<img src="' + img + '"/>').load(function() {
                $(h1).html('<img src="' + img + '" alt="' + $(h1).text() + '"/>');
                if (jQuery.browser.msie && (jQuery.browser.version.substr(0,1) != '8')) {
                  $('img', h1).load(function() {
                    $(this).css('width', $(this).width() + 'px');
                    $(this).css('height', $(this).height() + 'px');
                    // $(this).css('behavior', 'url(css/iepngfix.htc)');
                  });
                }
            });
        });
        $('#book-online-widget h1, #content-container h3.image').each(function() {
            var p = this;
            var img = textToImageUrl(this);
            $('<img src="' + img + '"/>').load(function() {
                $(p).html('<img src="' + img + '" alt="' + $(p).text() + '"/>');
                if (jQuery.browser.msie && (jQuery.browser.version.substr(0,1) != '8')) {
                  $('img', p).load(function() {
                    $(this).css('width', $(this).width() + 'px');
                    $(this).css('height', $(this).height() + 'px');
                    // $(this).css('behavior', 'url(css/iepngfix.htc)');
                  });
                }
            });
        });
        // $('#page-header-logo img, #book-online-widget h1 img').each(function() {
        //     $(this).css('behavior', 'url(' + gBaseUrl + 'css/iepngfix.htc)');
        // });
    });
});

function h1ToImageUrl(h1) {
    var image_filename = gBaseUrl + 'images/' + gActionName + '-title.png';
    return image_filename;
}

function sidebarH1ToImageUrl(h1) {
    var image_filename = gBaseUrl + 'images/' + gActionName + '-sidebar-title.png';
    return image_filename;
}

function h2ToImageUrl(h2) {
    var image_filename = gBaseUrl + 'images/' + gActionName + '-subtitle.png';
    return image_filename;
}

function textToImageUrl(p) {
    var image_filename = jQuery.trim($(p).text());
    image_filename = image_filename.toLowerCase();
    image_filename = image_filename.replace(/ /g, '-');
    return gBaseUrl + 'images/' + image_filename + '.png';
}

function titleToImageName(title) {
    image_filename = $(title).text();
    image_filename = jQuery.trim(image_filename);
    image_filename = image_filename.replace(/\.\.+$/, '');
    image_filename = image_filename.replace(/[ :]/g, '-');
    image_filename = image_filename.replace(/&amp;/g, '&');
    image_filename = image_filename.replace(/&/g, 'and');
    image_filename = image_filename.replace(/--+/g, '-');
    image_filename = image_filename.toLowerCase();
    return image_filename;
}