/*
cleanCSS.js is originally written by Anthony Lieuallen
    http://tools.arantius.com/tabifier

This code is used with permission from the original author and is
modified to meet the strict demands imposed by the Pretty Diff tool and
JSLint.

http://prettydiff.com/
http://jslint.com/

The comment argument is my addition, which allows comments to be
white space collapsed and unindented if passed the value "noindent".

css_summary is a function that is not provided a scope by the cleanCSS
function.  It is intended to be provided as a closure that it can access
the interiors of cleanCSS, but remain accessible outside cleanCSS.  This
function returns the number of HTTP requests from the beautified CSS and
what those requests are.
------------------------------------------------------------------------
*/
"use strict";
var cleanCSS = function (x, size, character, comment) {
    if (new RegExp(/if\s*\(\s*[a-z0-9]+\s*((={2,3})|(\!={1,2}))\s*[a-z0-9]+\s*\)\s*\{/i).test(x) === true && new RegExp(/var\s+[a-z]+[a-z0-9]*/i).test(x) === true) {
        css_summary = function () {
            return;
        };
        return "Error: Input appears to be JavaScript, but the application is set to beautify CSS.";
    } else if (new RegExp(/>\s*<\/[a-z]+>/i).test(x) === true && new RegExp(/[a-z]+[a-z0-9]*\s*\{\s*[a-z]+(\-[a-z]+)*\s*\:\s*[a-z]+(\-[a-z]+)*\s*;/i).test(x) === false) {
        css_summary = function () {
            return;
        };
        return "Error: Input appears to be markup, but the application is set to beautify CSS.";
    }
    var q = x.length,
    level = 0,
    tab = '',
    dashfix, dashfind = new RegExp(/url\(['"\d\w\.\/]*( -\d)['"\d\w\.\/]*\)/),
    tabmaker = (function () {
        var i;
        for (i = 0; i < Number(size); i += 1) {
            tab += character;
        }
    }()),
    finishTabifier = function (z) {
        z = z.replace(/\n\s*\n/g, '\n').replace(/^[\s\n]*/, '').replace(/[\s\n]*$/, '');
        x = z;
        level = 0;
    },
    tabs = function () {
        var j, s = '';
        for (j = 0; j < level; j += 1) {
            s += tab;
        }
        return s;
    },
    out = tabs(),
    li = level,
    cleanAsync = function () {
        var i, a;
        for (i = 0; i < x.length; i += 1) {
            if ('{' === x.charAt(i)) {
                level += 1;
                out += ' {\n' + tabs();
            } else if ('}' === x.charAt(i)) {
                out = out.replace(/\s*$/, "");
                level -= 1;
                if (x.charAt(i + 1) + x.charAt(i + 2) !== "*/") {
                    out += "\n" + tabs() + "}\n" + tabs();
                } else {
                    out += "\n" + tabs() + "}";
                }
            } else if ("/*" === x.charAt(i) + x.charAt(i + 1) && comment === "noindent") {
                for (a = i; a < x.length; a += 1) {
                    out = out.replace(/\s*$/, "");
                    if (a === i) {
                        out += "\n";
                    }
                    out += x.charAt(a);
                    if ("*/" === x.charAt(a - 2) + x.charAt(a - 1)) {
                        break;
                    }
                    i = a;
                }
            } else if (';' === x.charAt(i) && "}" !== x.charAt(i + 1)) {
                out += ';\n' + tabs();
            } else if ('\n' === x.charAt(i)) {
                out += '\n' + tabs();
            } else {
                out += x.charAt(i);
            }
        }
        if (i < x.length) {
            setTimeout(cleanAsync, 0);
        } else {
            level = li;
            out = out.replace(/[\s\n]*$/, '');
            finishTabifier(out);
        }
    },
    colonspace = function () {
        var i, d, j, space = new RegExp(/\s/);
        x = x.split('');
        d = x.length;
        for (i = 0; i < d; i += 1) {
            if (x[i] === '{') {
                for (j = i; j < d; j += 1) {
                    if (x[j - 1] !== " " && x[j] === "#") {
                        x[j] = " #";
                    }
                    if (x[j] === ':' && x[j + 1] !== '/' && x[j + 2] !== '/' && x[j + 1] !== "#") {
                        x[j] = ': ';
                        if (space.test(x[j + 1]) === true) {
                            x[j + 1] = '';
                        }
                    }
                    if (x[j] === '}') {
                        i = j;
                        break;
                    }
                }
            }
        }
        x = x.join('');
        return x;
    };
    if ('\n' === x[0]) {
        x = x.substr(1);
    }
    x = x.replace(/([\^\/])?\n*/g, '$1').replace(/\n\s+/g, '\n').replace(/[  ]+/g, ' ').replace(/\s?([;:{},+>])\s?/g, '$1').replace(/\{(\.*):(\.*)\}/g, '{$1: $2}').replace(/http:xx/g, 'http://').replace(/,/g, ', ').replace(/\b\*/g, ' *').replace(/\n[\s]+/g, '\n').replace(/[ ]+/g, ' ').replace(/\s*-(?=\d)()/g, ' -').replace(/\*\//, "*/\n").replace(/\*\//g, "*/\n").replace(/\*\/\s+/g, "*/\n");
    if (dashfind.test(x) === true) {
        dashfix = function () {
            var a = dashfind.exec(x)[0],
            b = x.indexOf(a),
            c = a.length,
            d = a.replace(/\s-(?=\d)/g, "-");
            x = x.replace(a, d);
        };
        do {
            dashfix();
        } while (dashfind.test(x) === true);
    }
    cleanAsync();
    colonspace();
    if (comment === "noindent") {
        x = x.replace(/\n\s+\*\//g, "\n*/");
    }
    css_summary = function () {
        var a = 0,
        b = [],
        c = x.split("\n"),
        d = c.length,
        e = [],
        f = q.toString().split("").reverse(),
        g = x.length.toString().split("").reverse();
        for (; a < d; a += 1) {
            if (c[a].charAt(0) === "/" && c[a].charAt(1) === "*") {
                for (; a < d; a += 1) {
                    if (c[a].charAt(c[a].length - 2) === "*" && c[a].charAt(c[a].length - 1) === "/") {
                        break;
                    }
                }
            } else if (c[a].indexOf("url") !== -1 && c[a].indexOf("url(\"\")") === -1 && c[a].indexOf("url('')") === -1 && c[a].indexOf("url()") === -1) {
                b.push(c[a]);
            }
        }
        d = b.length;
        for (a = 0; a < d; a += 1) {
            b[a] = b[a].substr(b[a].indexOf("url(") + 4, b[a].length);
            b[a] = b[a].substr(0, b[a].indexOf(")"));
            if (b[a].charAt(0) === "\"" || b[a].charAt(0) === "'") {
                b[a] = b[a].substr(1, b[a].length);
            }
            if (b[a].charAt(b[a].length - 1) === "\"" || b[a].charAt(b[a].length - 1) === "'") {
                b[a] = b[a].substr(0, b[a].length - 1);
            }
        }
        for (a = 0; a < d; a += 1) {
            e[a] = 1;
            for (c = a + 1; c < d; c += 1) {
                if (b[a] === b[c]) {
                    e[a] += 1;
                    b[c] = "";
                }
            }
        }
        for (a = 0; a < d; a += 1) {
            if (b[a] !== "") {
                e[a] = e[a] + "x";
                if (e[a] !== "1x") {
                    e[a] = "<em>" + e[a] + "</em>";
                }
                b[a] = "<li>" + e[a] + " - " + b[a] + "</li>";
            }
        }
        if (d === 0) {
            b = "";
        } else {
            b = "<h4>List of HTTP requests:</h4><ul>" + b.join("") + "</ul>";
        }
        c = f.length;
        for (a = 2; a < c; a += 3) {
            f[a] = "," + f[a];
        }
        c = g.length;
        for (a = 2; a < c; a += 3) {
            g[a] = "," + g[a];
        }
        f = f.reverse().join("");
        g = g.reverse().join("");
        if (f.charAt(0) === ",") {
            f = f.slice(1, f.length);
        }
        if (g.charAt(0) === ",") {
            g = g.slice(1, g.length);
        }
        return "<p><strong>Total input size:</strong> <em>" + f + "</em> characters</p><p><strong>Total output size:</strong> <em>" + g + "</em> characters<p><strong>Number of HTTP requests:</strong> <em>" + d + "</em></p>" + b;
    };
    return x;
};