/////////////////////////////////////////////////////////////////
// JS Object: IE_computedStyle
// Returns the computed style, expressed in pixels or as auto, for the given element
// 
// Usage:
// IE_computedStyle.get(elementNode, CSS-Style);
//
// Examples:
// IE_computedStyle.get(el, "width");
// IE_computedStyle.get(el, "margin-top");
/////////////////////////////////////////////////////////////////
var IE_computedStyle = function () {
    var isIE = /*@cc_on!@*/0;
    var borderRegex = /thin|medium|thick/i; /* Regex for css border width keywords */
    var styleEl;
    
    /* GET POS */
    var getPos = function (which) {
        var curStyle = styleEl.currentStyle[which];
        return (/auto/i.test(curStyle)) ? "auto" : grabLength(curStyle) + "px";
    };
    
    /* GET MARGIN */
    var getMargin = function (which) {
        var curStyle = styleEl.currentStyle["margin" + which];
        return (/auto/i.test(curStyle)) ? "0px" : grabLength(curStyle) + "px";
    };
    
    /* GET BORDER WIDTH */
    var getBorderWidth = function (which) {
        var borderWidth = styleEl.currentStyle["border" + which + "Width"];
        if (styleEl.currentStyle["border" + which + "Style"] !== "none" && 
                ((/Top|Bottom/i.test(which) && styleEl.offsetHeight > styleEl.clientHeight) || 
                (/Right|Left/i.test(which) && styleEl.offsetWidth > styleEl.clientWidth))) {
            if (!borderRegex.test(borderWidth)) {
                return grabLength(borderWidth) + "px";
            } else if (borderRegex.test(borderWidth)) {
                var temp = document.createElement("DIV");
                temp.style.width = "10px";
                temp.style.border = borderWidth + " " + styleEl.currentStyle["border" + which + "Style"] + " #000000";
                styleEl.parentNode.appendChild(temp);
                borderWidth = Math.round((temp.offsetWidth-10)/2);
                styleEl.parentNode.removeChild(temp);
                return borderWidth + "px";
            }
        } else {
            return "0px";
        }
    };
    
    /* GET PADDING */
    var getPadding = function (which) {
        return grabLength(styleEl.currentStyle["padding" + which]) + "px";
    };
    
    /* GET WIDTH */
    var getWidth = function () {
        var width = styleEl.offsetWidth; /* Currently the width including padding + border */
        width -= parseInt(getPadding("Right"));
        width -= parseInt(getPadding("Left"));
        width -= parseInt(getBorderWidth("Right"));
        width -= parseInt(getBorderWidth("Left"));
        return width + "px";
    };
    
    /* GET HEIGHT */    
    var getHeight = function () {
        var height = styleEl.offsetHeight; /* Currently the height including padding + border */
        height -= parseInt(getPadding("Top"));
        height -= parseInt(getPadding("Bottom"));
        height -= parseInt(getBorderWidth("Top"));
        height -= parseInt(getBorderWidth("Bottom"));
        return height + "px";
    };
    
    /* GRAB LENGTH */
    var grabLength = function (length) {
        var temp = document.createElement("DIV");
        temp.style.width = length;
        styleEl.parentNode.appendChild(temp);
        length = Math.round(temp.offsetWidth);
        styleEl.parentNode.removeChild(temp);
        return length;
    };
    
    return {
        /* GET */
        get : function (el, styleProp) {   
            var rValue;
            styleEl = (typeof(el) === "string") ? document.getElementById(el) : el;	
            var styleProp = (typeof(styleProp) === "string") ? styleProp.toLowerCase() : "";
            styleProp = styleProp.replace(/\-/g, "");
            
            if (!isIE || !/block/i.test(styleEl.currentStyle["display"]))
                return;
            
            switch (styleProp) { /* Run through the valid css properties, return undefined if none match */
                case "top": rValue = getPos("top"); break;
                case "right": rValue = getPos("right"); break;
                case "bottom": rValue = getPos("bottom"); break;
                case "left": rValue = getPos("left"); break;
                case "margintop": rValue = getMargin("Top"); break;
                case "marginright": rValue = getMargin("Right"); break;
                case "marginbottom": rValue = getMargin("Bottom"); break;
                case "marginleft": rValue = getMargin("Left"); break;
                case "bordertopwidth": rValue = getBorderWidth("Top"); break;
                case "borderrightwidth": rValue = getBorderWidth("Right"); break;
                case "borderbottomwidth": rValue = getBorderWidth("Bottom"); break;
                case "borderleftwidth": rValue = getBorderWidth("Left"); break;
                case "paddingtop": rValue = getPadding("Top"); break;
                case "paddingright": rValue = getPadding("Right"); break;
                case "paddingbottom": rValue = getPadding("Bottom"); break;
                case "paddingleft": rValue = getPadding("Left"); break;
                case "width": rValue = getWidth(); break;
                case "height": rValue = getHeight(); break;
            }
            
            return rValue;
        }
    };
}();
