;
/**
 * (c) 2010 Marcus Mennemeier <marcus(at)mennemeier.de>
 * TODO Wertigkeit von + selektoren (li + li + li) wird identisch zu (li + li)
 */
(function($){
    $.extend(true, {
        IE6Options: {
            off: false,
            async: true
        }
    });

    if (document['documentMode'] >= 7 || !/MSIE [56]/.exec(window.navigator.appVersion)) {
        $.fn.extend({
            ie6: function(){return this;},
            ie6Stylesheets: function(callback){callback(false); return this; }
        });
        return;
    }

    var neighbor = /[^+\s]+\s*\+\s*/g;
    var child = /\s*>\s*/g;
    var classCount = 0;
    var classPrefix = "IE6-";
    var selectors = {};

    function getLeafSelector(selectors) {
        var copy = {};
        $.each(selectors, function(k) {
            var p = k.lastIndexOf(" ");
            copy[p < 0 ? k : k.substring(p + 1)] = true;
        });
        return getSelector(copy);
    }

    function getSelector(selectors) {
        var s = "";
        $.each(selectors, function(k) {
            s += (s ? ", " : "") + k
        });
        return s;
    }

    function hasUnknown(styleSheet) {
        return styleSheet.cssText.indexOf("UNKNOWN") >= 0;
    }

    function removeComments(cssText) {
        var s = "";
        var last = 0;
        var p;
        while (last >= 0 && (p = cssText.indexOf("/*", last)) >= 0) {
            s += cssText.substring(last, p);
            last = cssText.indexOf("*/", p + 2);
            if (last >= 0) last += 2;
        }
        if (last < 0) {
            cssText = s;
        } else if (last > 0) {
            cssText = s + cssText.substring(last);
        }
        return cssText;
    }

    function fixStylesheet(styleSheet, cssText){
        cssText = removeComments(cssText);
        // remove comments
        var ruleNum = 0;
        var style = "";
        var result = false;

        function rule(ruleText) {
            var r = ruleText.split(/\s*\{\s*/);
            var selectorText = styleSheet.rules[ruleNum].selectorText;
            var simpleSelectors = r[0].split(/\s*,\s*/);
            if (selectorText != "UNKNOWN") { // if selector is understood, IE6 splits it into seperate rules
                style += ruleText + "\n";
                ruleNum += simpleSelectors.length;
                return;
            }
            result = true;
            $.each(simpleSelectors, function(i, s) {
                var f = s.replace(child, " ").replace(neighbor, "");
                if (f != s) {
                    var cssClass = selectors[s];
                    if (!cssClass) {
                        selectors[s] = cssClass = classPrefix + ++classCount;
                    }
                    f += "." + cssClass;
                }
                if (i > 0) style += ", ";
                style += f;
            });
            style += " {" + r[1] + "\n";
        }

        var last = 0;
        for (var i = cssText.indexOf('}'); i >= 0; i = cssText.indexOf('}', last)) {
            rule(cssText.substring(last, i + 1));
            last = i + 1;
        }

        if (result) styleSheet.cssText = style;
        return result;
    }

    $.fn.extend({
        ie6: function(){
            if ($.IE6Options.off) return this;
            var context = this;
            if (context.is("html")) {
                $.each(selectors, function(s, cssClass){
                    context.find(s).addClass(cssClass);
                });
            } else {
                var leafSelector = getLeafSelector(selectors);
                var allMatches = this.find(leafSelector).andSelf().filter(getSelector(selectors));
                $.each(selectors, function(s, cssClass){
                    allMatches.filter(s).addClass(cssClass);
                });
            }
            return this;
        },
        ie6Stylesheets: function(callback){
            var result = true;
            this.find("style[type=text/css]").each(function(){
                if (!hasUnknown(this.styleSheet)) return;
                if (fixStylesheet(this.styleSheet, this.innerHTML)) result = true;
            });

            var me = this;
            me.find("link[rel=stylesheet][type=text/css][href]").each(function(){
                if (!hasUnknown(this.styleSheet)) return;
                var link = this;
                var href = $(link).attr("href");
                me.queue("ie6", function(){
                    $.ajax({
                        url: href,
                        async: $.IE6Options.async,
                        dataType: "text",
                        global: false,
                        success: function(cssText){
                            if (fixStylesheet(link.styleSheet, cssText)) result = true;
                        },
                        complete: function(){
                            me.dequeue("ie6");
                        }
                    });
                });
            });
            me.queue("ie6", function(){
                callback(result);
                me.dequeue("ie6");
            });
            me.dequeue("ie6");
        }
    });

    $(function(){
        $("html").ie6Stylesheets(function(result){
            if (result) $("body").ie6();
        });
    });
})(jQuery);
