﻿function colorToHex(color) {
    try {
        if (color.charAt(0) == '#') {

            return color.replace("#", "");
        }
        color = color.replace(" ", "");
        color = color.replace("rgb(", "");
        color = color.replace(")", "");
        var r = color.split(",")[0];
        var g = color.split(",")[1];
        var b = color.split(",")[2];

        var res = RGBtoHexColor(r, g, b);

        return res;
    }
    catch (x) {
        alert(x)
    }
}

function RGBtoHexColor(r, g, b) {
    var hex = '';
    var hexStr = '0123456789ABCDEF';
    // R
    low = r % 16;
    high = (r - low) / 16;
    hex += hexStr.charAt(high) + hexStr.charAt(low);
    // G
    low = g % 16;
    high = (g - low) / 16;
    hex += hexStr.charAt(high) + hexStr.charAt(low);
    // B
    low = b % 16;
    high = (b - low) / 16;
    hex += hexStr.charAt(high) + hexStr.charAt(low);
    return hex;
}


function getStyle(oElm, strCssRule) {
    var strValue = "";
    if (document.defaultView && document.defaultView.getComputedStyle) {
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if (oElm.currentStyle) {
        strCssRule = strCssRule.replace(/\-(\w)/g, function(strMatch, p1) {
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}

function getWidth(oElm) {
    var width = getStyle(oElm, "width");
    if (width == "auto")
        width = oElm.clientWidth;

    return width;
}


function initRoundedCorners() {

    if (!document.getElementsByTagName) { return; }
    var anchors = document.getElementsByTagName("div");

    // loop through all anchor tags
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];


        if ((anchor.getAttribute("rel") == "rounded")) {
            try {
                var borderWidth = anchor.style.borderTopWidth;
                var borderColor = anchor.style.borderTopColor;

                if (borderColor == undefined) {
                    borderColor = "ff00ff";
                }
                else {
                    borderColor = colorToHex(borderColor);
                }
                //                alert(borderColor);
                var cornerSize = anchor.getAttribute("corner");
                if (cornerSize == undefined)
                    cornerSize = 10;

                var backgroundColor = getStyle(anchor, "background-color");
                if (backgroundColor == undefined) {
                    backgroundColor = "ff0000";
                }
                else {
                    backgroundColor = colorToHex(backgroundColor);
                }

                var outerColor = anchor.getAttribute("outercolor");
                if (outerColor == undefined)
                    outerColor = "ffffff";
                else
                    outerColor = colorToHex(outerColor);
                makeRoundRectangle(anchor, cornerSize, backgroundColor, borderWidth, borderColor, outerColor);
                anchor.style.borderWidth = "0px";
            }
            catch (x) {
                alert(x);
            }
        }
    }

}
function makeRoundRectangle(object, roundSize, backColor, borderSize, borderColor, outerColor) {

    if (object == undefined) {
        alert(object);
        return;
    }
    try {
        var width = object.clientWidth;
        var height = object.clientHeight;
        var bColor = borderColor;

        var bk = "url(" + _siteRoot + "/Helpers/ImageBuilder.aspx?type=RoundedCorners&width=" + width + "&height=" + height + "&background-mode=solid&corner=" + roundSize + "&inner-background=" + backColor + "&outer-background=" + outerColor + "&border-size=" + borderSize + "&border-color=" + borderColor + "&imageCodec=png)";
        object.style.backgroundImage = bk;
        object.style["background-repeat"] = "no-repeat";
    }
    catch (x) {
        alert(x);
    }
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEventC(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }

}


addLoadEventC(initRoundedCorners); // run initLightbox onLoad