/**
* justcorners.js 2.1 (22-Apr-2011) (c) by Christian Effenberger
* All Rights Reserved. Source: corner.netzgesta.de
* Distributed under Netzgestade Software License Agreement.
* This license permits free of charge use on non-commercial
* and private web sites only under special conditions.
* Read more at... http://www.netzgesta.de/cvi/LICENSE.html
* Commercial licenses available via... cvi[at]netzgesta[dot]de
**/ 

var defaultRadius = 8;
var tmp = navigator.appName == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf('Opera') < 1 ? 1 : 0;
if (tmp) var isIE = document.namespaces && !window.opera && ( !document.documentMode || document.documentMode < 9 ) ? 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 roundedCorners(ctx,width,height,radius,lt,rt,lb,rb,bezel){
    ctx.beginPath(); ctx.moveTo(0,radius);
    if(lb==1) {ctx.lineTo(0,height-radius);if(bezel) {ctx.lineTo(radius,height);}else {ctx.quadraticCurveTo(0,height,radius,height);}}else {ctx.lineTo(0,height);}
    if(rb==1) {ctx.lineTo(width-radius,height);if(bezel) {ctx.lineTo(width,height-radius);}else {ctx.quadraticCurveTo(width,height,width,height-radius);}}else {ctx.lineTo(width,height);}
    if(rt==1) {ctx.lineTo(width,radius);if(bezel) {ctx.lineTo(width-radius,0);}else {ctx.quadraticCurveTo(width,0,width-radius,0);}}else {ctx.lineTo(width,0);}
    if(lt==1) {ctx.lineTo(radius,0);if(bezel) {ctx.lineTo(0,radius);}else {ctx.quadraticCurveTo(0,0,0,radius);}}else {ctx.lineTo(0,0);}
    ctx.closePath();
} 

function addRoundedIECorners(wrapper, all) {
    var image, object, vml, classNames, data, classes = '';
    var iradius = 0, iborder = 0, icolor = '', newClasses = '';
    var maxdim = 0, mindim = 0, i, j, child, tmp = '';
    var radius = null, display = "", flt = "", color = '';
    var tl = 1, tr = 1, bl = 1, br = 1;
    var children = wrapper.getElementsByTagName('img');
    var theimages = new Array();
    for (i = 0; i < children.length; i++) {
        child = children[i];
        classNames = child.className.split(' ');
        if (all == true && child.className.contains('nocorner') == false) {
            theimages.push(child);
        }
        else {
            for (j = 0; j < classNames.length; j++) {
                if (classNames[j] == 'corners')
                { theimages.push(child); break; }
            }
        }
    }
    
    for (i = 0; i < theimages.length; i++) {
        image = theimages[i]; iradius = defaultRadius; color = '0000ff';
        tl = 1; tr = 1; bl = 1; br = 1; iborder = 0; ibezel=false;
        object = image.parentNode;
        classes = image.className.split(' ');
        for (j = 0; j < classes.length; j++) {
            if (classes[j].indexOf("iradius") == 0) {
                iradius = classes[j].substring(7);
            } else if (classes[j].indexOf("iborder") == 0) {
                iborder = classes[j].substring(7);
            } else if (classes[j].indexOf("icolor") == 0) {
                icolor = classes[j].substring(6);
            }else if(classes[j].indexOf("ibezel") == 0) {
				ibezel = true;
			} else if (classes[j].indexOf("iradiae") == 0) {
                tmp = classes[j].substring(7);
                tl = (tmp.substr(0, 1) != "1" ? 0 : 1);
                tr = (tmp.substr(1, 1) != "1" ? 0 : 1);
                bl = (tmp.substr(2, 1) != "1" ? 0 : 1);
                br = (tmp.substr(3, 1) != "1" ? 0 : 1);
            }
        } newClasses = '';
        for (j = 0; j < classes.length; j++) {
            if (classes[j] != "corners") {
                if (newClasses) { newClasses += ' '; }
                newClasses += classes[j];
            }
        }
        maxdim = Math.min(image.width, image.height) / 2;
        mindim = (iradius == 0 ? 16 : iradius);
        radius = Math.min(maxdim, mindim);
        iborder = Math.min(iborder, 2);
        color = (icolor.match(/^[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i) ? icolor : color);
        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;
        if (bl == 1) { path += " l 0," + (image.height - radius) + (ibezel?" l ":" qy ") + radius + "," + image.height; } else { path += " l 0," + image.height; }
        if (br == 1) { path += " l " + (image.width - radius) + "," + image.height + (ibezel?" l ":" qx ")  + image.width + "," + (image.height - radius); } else { path += " l " + image.width + "," + image.height; }
        if (tr == 1) { path += " l " + image.width + "," + radius + (ibezel?" l ":" qy ") + (image.width - radius) + ",0"; } else { path += " l " + image.width + ",0"; }
        if (tl == 1) { path += " l " + radius +  ",0 " +(ibezel?" l ":" qx ")+ " 0," + radius; } else { path += " l 0,0"; }
        path += " x e"; data = '<v:shape strokeweight="' + iborder + 'px" stroked="' + (iborder > 0 ? "t" : "f") + '" strokecolor="#' + color + '" filled="t" fillcolor="#ffffff" coordorigin="0,0" coordsize="' + image.width + ',' + image.height + '" path="' + path + '" style="zoom:1;margin:-1px 0 0 0px;padding: 0;display:' + display + ';width:' + image.width + 'px;height:' + image.height + 'px;"><v:fill src="' + image.src + '" type="frame" /></v:shape>';
        //		path += " x e"; data = '<v:shape strokeweight="'+iborder+'px" stroked="'+(iborder>0?"t":"f")+'" strokecolor="#'+color+'" filled="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);
        if (image.getAttribute("usemap")) {
            object.style.position = 'relative';
            object.style.height = image.height + 'px';
            object.style.width = image.width + 'px';
            image.left = 0; image.top = 0;
            image.style.position = 'absolute';
            image.style.height = image.height + 'px';
            image.style.width = image.width + 'px';
            image.style.left = 0 + 'px';
            image.style.top = 0 + 'px';
            image.style.filter = "Alpha(opacity=0)";
        } else {
            image.style.display = 'none';
        }
    }
    
}

function addRoundedCorners(wrapper, all) {
    var image, object, canvas, context, classNames, classes = '';
    var iradius = 0, iborder = 0, icolor = '', newClasses = '';
    var maxdim = 0, mindim = 0, i, j, child, tmp = '';
    var tl = 1, tr = 1, bl = 1, br = 1, color = '';
    var children = wrapper.getElements('img');
    var theimages = new Array();
    for (i = 0; i < children.length; i++) {
        child = children[i];
        classNames = child.className.split(' ');
        if (all == true && child.className.contains('nocorner') == false) {
            theimages.push(child);
        }
        else {
            for (j = 0; j < classNames.length; j++) {
                if (classNames[j] == 'corners')
                { theimages.push(child); break; }
            }
        }
    }
    for (i = 0; i < theimages.length; i++) {
        image = theimages[i]; iradius = defaultRadius; color = '0000ff';
        tl = 1; tr = 1; bl = 1; br = 1; iborder = 0;
        object = image.parentNode;
        canvas = document.createElement('canvas');
        if (canvas.getContext) {
            classes = image.className.split(' ');
            for (j = 0; j < classes.length; j++) {
                if (classes[j].indexOf("iradius") == 0) {
                    iradius = classes[j].substring(7);
                } else if (classes[j].indexOf("iborder") == 0) {
                    iborder = classes[j].substring(7);
                } else if (classes[j].indexOf("icolor") == 0) {
                    icolor = classes[j].substring(6);
                } else if (classes[j].indexOf("iradiae") == 0) {
                    tmp = classes[j].substring(7);
                    tl = (tmp.substr(0, 1) != "1" ? 0 : 1);
                    tr = (tmp.substr(1, 1) != "1" ? 0 : 1);
                    bl = (tmp.substr(2, 1) != "1" ? 0 : 1);
                    br = (tmp.substr(3, 1) != "1" ? 0 : 1);
                }
            } newClasses = '';
            for (j = 0; j < classes.length; j++) {
                if (classes[j] != "corners") {
                    if (newClasses) { newClasses += ' '; }
                    newClasses += classes[j];
                }
            }
            canvas.className = newClasses;
            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;
            if (image.getAttribute('onclick') != '') canvas.setAttribute('onclick', image.getAttribute('onclick'));
            maxdim = Math.min(canvas.width, canvas.height) / 2;
            mindim = (iradius == 0 ? 16 : iradius);
            iradius = Math.min(maxdim, mindim);
            iborder = Math.min(iborder * 2, iradius);
            color = (icolor.match(/^[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i) ? icolor : color);
            context = canvas.getContext("2d");
            if (image.getAttribute("usemap")) {
                object.style.position = 'relative';
                object.style.height = image.height + 'px';
                object.style.width = image.width + 'px';
                canvas.left = 0; canvas.top = 0;
                canvas.style.position = 'absolute';
                canvas.style.left = 0 + 'px';
                canvas.style.top = 0 + 'px';
                image.left = 0; image.top = 0;
                image.style.position = 'absolute';
                image.style.height = image.height + 'px';
                image.style.width = image.width + 'px';
                image.style.left = 0 + 'px';
                image.style.top = 0 + 'px';
                image.style.opacity = 0;
                object.insertBefore(canvas, image);
            } else {
                object.replaceChild(canvas, image);
            }
            context.clearRect(0, 0, canvas.width, canvas.height);
            globalCompositeOperation = "source-in";
            roundedCorners(context, canvas.width, canvas.height, iradius, tl, tr, bl, br);
            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);
            if (iborder > 0) {
                roundedCorners(context, canvas.width, canvas.height, iradius, tl, tr, bl, br);
                context.strokeStyle = '#' + color;
                context.lineWidth = iborder;
                context.stroke();
            }
            canvas.style.visibility = 'visible';
        }
    }
}

window.addEvent('load', function() {

    var content = $('content');
    var related = $('related');

    if (isIE) {
        if (typeof document.body.style.maxHeight != "undefined") {
            if (content) addRoundedIECorners(content, true);
            if (related) addRoundedIECorners(related, false);
        }
        else
            if (related) addRoundedIECorners(related, false);
    }
    else {
        if (content) addRoundedCorners(content, true);
        if (related) addRoundedCorners(related, false);
    }
});
