var tmp = navigator.appName == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf('Opera') < 1 ? 1 : 0;
if (tmp) var isIE = document.namespaces && !window.opera ? 1 : 0;
if (isIE) {
    if (document.namespaces['v'] == null) {
        var e = ["shape", "shapetype", "group", "background", "path", "formulas", "handles", "fill", "stroke", "shadow", "textbox", "textpath", "imagedata", "line", "polyline", "curve", "roundrect", "oval", "rect", "arc", "image"],
            s = document.createStyleSheet();
        for (var i = 0; i < e.length; i++) {
            s.addRule("v\\:" + e[i], "behavior: url(#default#VML);");
        }
        document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
    }
}

function rounded_corners_ie() {
    var image, object, vml, classNames, data, classes = '';
    var newClasses = '';
    var i, j, child;
    var radius = 5;
    color = '0000ff',
    display = "",
    flt = "";
    var children = document.getElementsByTagName('img');
    var theimages = new Array();
    for (i = 0; i < children.length; i++) {
        child = children[i];
        classNames = child.className.split(' ');
        for (j = 0; j < classNames.length; j++) {
            if (classNames[j] == 'rounded_corners') {
                theimages.push(child);
                break;
            }
        }
    }
    for (i = 0; i < theimages.length; i++) {
        image = theimages[i];
        object = image.parentNode;
        classes = image.className.split(' ');
        newClasses = '';
        for (j = 0; j < classes.length; j++) {
            if (classes[j] != "corners") {
                if (newClasses) {
                    newClasses += ' ';
                }
                newClasses += classes[j];
            }
        }
        display = (image.currentStyle.display.toLowerCase() == 'block') ? 'block' : 'inline-block';
        vml = document.createElement(['<var style="zoom:1;overflow:hidden;display:' + display + ';width:' + image.width + 'px;height:' + image.height + 'px;padding:0;">'].join(''));
        flt = image.currentStyle.styleFloat.toLowerCase();
        display = (flt == 'left' || flt == 'right') ? 'inline' : display;
        var path = "m 0," + radius;
        path += " l 0," + (image.height - radius) + " qy " + radius + "," + image.height;
        path += " l " + (image.width - radius - 1) + "," + image.height + " qx " + (image.width - 1) + "," + (image.height - radius);
        path += " l " + (image.width - 1) + "," + radius + " qy " + (image.width - radius - 1) + ",0";
        path += " l " + radius + ",0 qx 0," + radius;
        path += " x e";
        data = '<v:shape strokeweight="1px" stroked="t" strokecolor="#e0e0e0" fill="t" fillcolor="#ffffff" coordorigin="0,0" coordsize="' + image.width + ',' + image.height + '" path="' + path + '" style="zoom:1;margin:-1px 0 0 -1px;padding: 0;display:' + display + ';width:' + image.width + 'px;height:' + image.height + 'px;"><v:fill src="' + image.src + '" type="frame" /></v:shape>';
        vml.innerHTML = [data].join('');
        vml.className = newClasses;
        vml.style.cssText = image.style.cssText;
        vml.style.visibility = 'visible';
        vml.src = image.src;
        vml.alt = image.alt;
        if (image.id != '') vml.id = image.id;
        if (image.title != '') vml.title = image.title;
        if (image.getAttribute('onclick') != '') vml.setAttribute('onclick', image.getAttribute('onclick'));
        image.insertAdjacentElement('BeforeBegin', vml);
        image.style.display = 'none';
    }
}

function rounded_corners() {
    var image, object, canvas, context, classNames, classes = '';
    var newClasses = '';
    var i, j, child;
    var children = document.getElementsByTagName('img');
    var theimages = new Array();
    for (i = 0; i < children.length; i++) {
        child = children[i];
        classNames = child.className.split(' ');
        for (j = 0; j < classNames.length; j++) {
            if (classNames[j] == 'rounded_corners') {
                theimages.push(child);
                break;
            }
        }
    }
    for (i = 0; i < theimages.length; i++) {
        image = theimages[i];
        object = image.parentNode;
        canvas = document.createElement('canvas');
        if (canvas.getContext) {
            canvas.style.cssText = image.style.cssText;
            canvas.style.height = image.height + 'px';
            canvas.style.width = image.width + 'px';
            canvas.height = image.height;
            canvas.width = image.width;
            canvas.src = image.src;
            canvas.alt = image.alt;
            if (image.id != '') canvas.id = image.id;
            if (image.title != '') canvas.title = image.title;
            context = canvas.getContext("2d");
            object.replaceChild(canvas, image);
            context.clearRect(0, 0, canvas.width, canvas.height);
            globalCompositeOperation = "source-in";
            drawbox(context, canvas.width, canvas.height);
            context.clip();
            context.fillStyle = 'rgba(0,0,0,0)';
            context.fillRect(0, 0, canvas.width, canvas.height);
            context.drawImage(image, 0, 0, canvas.width, canvas.height);
            drawbox(context, canvas.width, canvas.height);
            context.strokeStyle = '#e0e0e0';
            context.lineWidth = 1;
            context.stroke();
            canvas.style.visibility = 'visible';
        }
    }
}

function drawbox(context, width, height) {
    radius = 7;
    context.beginPath();
    context.moveTo(0, 5);
    context.lineTo(0, height - radius);
    context.quadraticCurveTo(0, height, radius, height);
    context.lineTo(width - radius, height);
    context.quadraticCurveTo(width, height, width, height - radius);
    context.lineTo(width, radius);
    context.quadraticCurveTo(width, 0, width - radius, 0);
    context.lineTo(radius, 0);
    context.quadraticCurveTo(0, 0, 0, radius);
    context.closePath();
}

if (window.attachEvent && !window.opera) window.attachEvent("onload", rounded_corners_ie);
else window.addEventListener("load", rounded_corners, false);
