

CSSHandler = function () {};

/**
 * Reads set CSS Values of property in element
 * FF and IE, others not tested
 *
 * @param OBJECT element
 * @param STRING property
 * @param STRING type (int(Math.round)|float|string)
 * @return STRING Value of Property
 */
CSSHandler.prototype.getCSSValue = function(element, property, type) {
    var value;
    if (typeof(window.getComputedStyle) == 'function') {
        value = window.getComputedStyle(element, null)[property];
    } else if (element.currentStyle) {
        value = element.currentStyle[property];
    } else {
        return false;
    }
    switch (type) {
        case 'int':
            value = Math.round(parseFloat(value)) || 0;
            break;
        case 'float':
            value = parseFloat(value) || 0;
            break;
        case 'string':
            break;
        default:
        break;
    }
    return value;
};

/**
 * Sets the opacity Crossbrowser
 *
 * @param OBJECT element
 * @param FLOAT opacity
 *
 * 
 */
CSSHandler.prototype.setOpacity = function (element, opacity) {
    opacity = opacity > 1 ? 1 : opacity < 0 ? 0 : opacity;
    if (userAgent.app=='MSIE') {
        opacity = Math.round(opacity*100);
        try {
            if (element.filters.item('DXImageTransform.Microsoft.Alpha').enabled) {
                throw true;
            } else {
                throw false;
            }
        } catch (e) {
            if (e == true) {
                element.filters.item('DXImageTransform.Microsoft.Alpha').enabled = 0;
                element.filters.item('DXImageTransform.Microsoft.Alpha').opacity = opacity;
                if (opacity < 1) {
                    element.filters.item('DXImageTransform.Microsoft.Alpha').enabled = 1;
                }
            } else {
                if (element.style.filter.indexOf('progid:') > -1) {
                    element.style.filter +=' DXImageTransform.Microsoft.Alpha(opacity:'+opacity+');';
                } else {
                    element.style.filter ='progid:DXImageTransform.Microsoft.Alpha(opacity:'+opacity+');';
                }
            }
        }
    } else {
        element.style.opacity = opacity;
    }
};
/**
 * Returns the opacity Crossbrowser
 *
 * @param OBJECT element
 */
CSSHandler.prototype.getOpacity = function (element) {
    var opacity;
    if (userAgent.app=='MSIE') {
        try {
            if (element.filters.item('DXImageTransform.Microsoft.Alpha').enabled) {
                throw true;
            } else {
                throw false;
            }
        } catch (e) {
            if (e == true) {
                opacity = element.filters.item('DXImageTransform.Microsoft.Alpha').opacity / 100;
            } else {
                opacity = 1; // No opacity set, element is completely opaque
            }
        }
    } else {
        opacity = parseFloat(this.getCSSValue(element,'opacity'));
    }
    return opacity;
};
/**
 * clip Crossbrowser
 */
CSSHandler.prototype.clip = function (element, topToTop, leftToRight, topToBottom, leftToLeft) {
    var clip;
    if (userAgent.app=='MSIE') {
        clip = topToTop + ',' + leftToRight + ',' + topToBottom + ',' + leftToLeft;
    } else {
        clip = topToTop + ' ' + leftToRight + ' ' + topToBottom + ' ' + leftToLeft;
    }
    element.style.clip = 'rect('+clip+')';
};
/**
 * is it clipped?
 */
CSSHandler.prototype.isClipped = function (element) {
    var ret = false;
    if (userAgent.app=='MSIE') {
        ret = this.getCSSValue(element, 'clip') == 'rect(auto)' ? false : true;
    } else {
        ret = this.getCSSValue(element, 'clip') == 'auto' ? false : true;
    }
    return ret;
};
/**
 * unset clip Crossbrowser
 */
CSSHandler.prototype.unclip = function (element) {
    if (userAgent.app=='MSIE' && userAgent.version < 8) {
        element.style.clip = 'rect(auto)';
    } else {
        element.style.clip = 'auto';
    }
};

/**
 * Add a Stylesheet
 */
CSSHandler.prototype.insertSheet = function () {
    var styleSheet = document.createElement("style");
    styleSheet.setAttribute( "type", "text/css" );
    document.getElementsByTagName("head")[0].appendChild(styleSheet);
    return document.styleSheets[document.styleSheets.length-1];
}
/**
 * Add a css Rule at runtime
 */
CSSHandler.prototype.insertRule = function (styleSheet, selector, rules) {
    if( styleSheet.cssRules ) {
        styleSheet.insertRule(selector+" { "+rules+" }", 0);
    } else {
        if ( styleSheet.rules ) {
            styleSheet.addRule(selector, rules);
        }
    }
}

window.cssHandler = new CSSHandler();
