/* Released: 2010-01-28 14:28:25 */


(function($) {
    var ns = $.conditionalContent = $.conditionalContent || {};

    var parseClassConditional = function(className) {
        if (className.indexOf('for-') != 0 && className.indexOf('non-') != 0) return;
        var m = className.substring(0, 3);
        var s = className.substring(4, className.length).split('-');
        var c = {m : m};
        c.a = s[0];
        if (s[2]) { // in case of browser-version-os
            c.b = s[2];
            c.v = parseInt(s[1], 10);
        } else if (s[1]) { // in case of browser-os
            c.b = s[1];
        }
        return c;
    };

    var getConditionalsFromClassName = function(className) {
        var conditionals = [];
        var classes = className.split(' ');

        var i = classes.length;
        while (i--) {
            var cl = classes[i];
            var co = parseClassConditional(cl);
            if (co) conditionals.push(co);
        }
        return conditionals;
    };

    var conditionalMatches = function(conditional, browser, platform, version) {
        var c = conditional;
        var am = !c.a || (c.a == browser || c.a == platform);
        var bm = !c.b || (c.b == browser || c.b == platform);
        var vm = !c.v || c.v == version;
        if (am && bm && vm) return true; else return false;
    };

    $.fn.extend({
        conditionalContent: function() {
            var platforms = $.platform;
            var browsers = $.browser;
            var version = parseInt($.browser.version, 10);

            var platform = (function() {
                for (var p in platforms) {
                    if (platforms[p] === true) return p;
                }
            })();

            var browser = (function() {
                for (var b in browsers) {
                    if (browsers[b] === true) return b;
                }
            })();

            $(this).filter('.conditional-content').each(function() {
                var c = getConditionalsFromClassName(this.className);

                var show = false;
                var i = c.length;
                while (i--) {
                    var ci = c[i];
                    var match = conditionalMatches(ci, browser, platform, version); // TODO: add version
                    if (ci.m == 'for' && match) {
                        // in case it's for this target, say true and go on.
                        show = true;
                    } else if (ci.m == 'non' && match) {
                        // in case it's not for this target, say false and stop!
                        show = false;
                        break;
                    } else if (ci.m == 'non' && !match) {
                        // in case it's not _not_ our target, we say true and go on
                        show = true;
                    }
                }

                if (show) {
                    $(this).removeClass('hidden');
                } else {
                    $(this).addClass('hidden');
                }
            });
            return this;
        }
    });
})(jQuery);
