

CSSHandler = function () {
    var testelement=document.createElement('div');
    this.useOpacity = (typeof testelement.style.opacity != 'undefined');
    this.useMSIEFilter = !this.useOpacity && (typeof testelement.style.filter != 'undefined');
    delete testelement;
};

/**
 * Reads set CSS Values of property in element
 * FF and IE, others not tested
 *
 * @param element OBJECT
 * @param property STRING
 * @param type STRING (int(Math.round)|float|string)
 * @return STRING Value of Property
 */
CSSHandler.prototype.getCSSValue = function(element, property, type) {
    var value;
    if (typeof element.tagName == 'undefined') {
        return false;
    } else 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;
};

CSSHandler.prototype.getMaxZIndex = function (element) {
    element=element||document.body;
    var zI = 1, i=0, n=0;
    
    for (i;i<element.childNodes.length;i++) {
        n=cssHandler.getCSSValue(element.childNodes[i], 'zIndex', 'int');
        zI = Math.max(n, zI);
    }
    return zI;
};

/**
 * Sets the opacity Crossbrowser
 *
 * @param element OBJECT
 * @param opacity FLOAT
 *
 * 
 */
CSSHandler.prototype.setOpacity = function (element, opacity) {
    opacity = opacity > 1 ? 1 : opacity < 0 ? 0 : opacity;
    if (this.useMSIEFilter) {
        opacity = Math.round(opacity*100);
        try {
            if (element.filters.item('DXImageTransform.Microsoft.Alpha').enabled) {
                throw true;
            } else {
                throw false;
            }
        } catch (e) {
            if (e == true) {
                if (opacity >= 100) {
                    element.style.filter = element.style.filter.replace(/ ?DXImageTransform.Microsoft.Alpha\(opacity:[0-9]+\);/, '');
                    if (element.style.filter == 'progid:') {
                        element.style.filter = '';
                    }
                    //console.log(element.style.filter);
                    return;
                }
                element.filters.item('DXImageTransform.Microsoft.Alpha').opacity = opacity;
            } 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 element OBJECT
 */
CSSHandler.prototype.getOpacity = function (element) {
    var opacity=1;
    if (this.useMSIEFilter) {
        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 = this.getCSSValue(element,'opacity','float');
    }
    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' && userAgent.version <= 8) {
    	if (this.getCSSValue(element, 'clipTop') == 'auto' &&
    			this.getCSSValue(element, 'clipRight') == 'auto' &&
    			this.getCSSValue(element, 'clipBottom') == 'auto' &&
    			this.getCSSValue(element, 'clipLeft') == 'auto') {
        	ret = false;
    	} else {
    		ret = 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';
    }
};
/*
 
// Javascript-Konsole
x = getElementsByClassName('addressSearchFormContainer')[0];
x.style.left = '300px';
x.style.top = '300px';

x.getElementsByTagName('input')[0].value = cssHandler.getCSSValue(x, 'clip');
 
 */
/**
 * Get a Stylesheet
 */
CSSHandler.prototype.getSheet = function (href) {
	for (var i=0;i<document.styleSheets.length;i++) {
		if (document.styleSheets[i].href && document.styleSheets[i].href.indexOf(href) > -1) {
			return document.styleSheets[i];
		}
	}
	return false;
};
/**
 * Add a Stylesheet
 */
CSSHandler.prototype.insertSheet = function () {
    var styleSheet = document.createElement("style");
    styleSheet.setAttribute( "type", "text/css" );
    if (typeof document.getElementsByTagName("head")[0]) 
    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);
        }
    }
};
/**
 * Modify a rule
 * don't use this for animations
 * style attribute is significantly faster
 */
CSSHandler.prototype.modifyRule = function (rule, property, value) {
    rule.style[property] = value;
};
/**
 * Get CSS Rule
 */
CSSHandler.prototype.getRule = function (selector, styleSheet) {
    var l, i=0, rule=false;
    if (!styleSheet) {
        if (document.styleSheets) {
            l=document.styleSheets.length;
            for (i;i<l;i++) {
                rule = this._searchSelectorInSheet(selector, document.styleSheets[i]);
                if (rule) {
                    break;
                }
            }
        }
    } else {
        rule = this._searchSelectorInSheet(selector, styleSheet);
    }
    return rule;
};
CSSHandler.prototype._searchSelectorInSheet = function (selector, sheet) {
    var currentRule=false, n=0;
    do {
        if (sheet.cssRules) {
            currentRule = sheet.cssRules[n];
        } else {
            currentRule = sheet.rules[n];
        }
        if (typeof currentRule != 'undefined' &&
            typeof currentRule.selectorText != 'undefined' &&
            currentRule.selectorText == selector
        ) {
            return currentRule;
        }
        n++;
    } while (currentRule);
    return false;
};
window.cssHandler = new CSSHandler();

