/* Given an array of args for "-webkit-gradient(...)" returned by
oldGradientParser(), this function constructs a string representing the
equivalent arguments for a standard "linear-gradient(...)" or
"radial-gradient(...)" expression.

@param type  Either 'linear' or 'radial'.
@param args  An array of args for a "-webkit-gradient(...)" expression,
provided by oldGradientParser() (not including gradient type).
function standardizeOldGradientArgs(type, args){
        var stdArgStr = '';
        var stops = [];
        if(/^linear/.test(type)){
            // linear gradient, args 1 and 2 tend to be start/end keywords
            var points = [].concat(args[0].name.split(/\s+/), args[1].name.split(/\s+/)); // example: [left, top, right, top]
            // Old webkit syntax "uses a two-point syntax that lets you explicitly state where a linear gradient starts and ends"
            // if start/end keywords are percentages, let's massage the values a little more..
            var rxPercTest = /\d+\%/;
            if(rxPercTest.test(points[0]) || points[0] === 0){
                var startX = parseInt(points[0]), startY = parseInt(points[1]), endX = parseInt(points[2]), endY = parseInt(points[3]);
                stdArgStr += Math.atan2(endY - startY, endX - startX) * (180 / Math.PI) + 90;
                stdArgStr += 'deg';
            }else{
                if(points[1] === points[3]){ // both 'top' or 'bottom, this linear gradient goes left-right
                    stdArgStr += 'to ' + points[2];
                }else if(points[0] === points[2]){ // both 'left' or 'right', this linear gradient goes top-bottom
                    stdArgStr += 'to ' + points[3];
                }else if(points[1] === 'top'){ // diagonal gradient - from top left to opposite corner is 135deg
                    stdArgStr += '135deg';
                }else{
                    stdArgStr += '45deg';
                }
            }

        }else if(/^radial/i.test(type)){ // oooh, radial gradients..
            stdArgStr += 'circle ' + args[3].name.replace(/(\d+)$/, '$1px') + ' at ' + args[0].name.replace(/(\d+) /, '$1px ').replace(/(\d+)$/, '$1px');
        }

        var toColor;
        var startStep = type === 'linear' ? 2 : 4;
        for (var j = startStep; j < args.length; j++) {
            var position, color, colorIndex;
            if(args[j].name === 'color-stop'){
                position = args[j].args[0].name;
                if (args[j].args[1]) {
                    colorIndex = 1;
                } else {
                    colorIndex = 0;
                    position = (j - startStep) / (args.length - startStep - 1);
                }
            }else if (args[j].name === 'to') {
                position = '100%';
                colorIndex = 0;
            }else if (args[j].name === 'from') {
                position = '0%';
                colorIndex = 0;
            }
            if (position >= 0 || position.indexOf('%') === -1) { // original Safari syntax had 0.5 equivalent to 50%
                position = parseFloat(position) * 100 + '%';
            }
            color = args[j].args[colorIndex].name;
            if (args[j].args[colorIndex].args) { // the color is itself a function call, like rgb()
                color += '(' + colorValue(args[j].args[colorIndex].args) + ')';
            }
            if (args[j].name === 'from'){
                stops.unshift(color + ' ' + position);
            }else if(args[j].name === 'to'){
                toColor = color;
            }else{
                stops.push(color + ' ' + position);
            }
        }

        // translating values to right syntax
        for(j = 0; j < stops.length; j++){
            stdArgStr += ', ' + stops[j];
        }
        if(toColor){
            stdArgStr += ', ' + toColor + ' 100%';
        }
        return stdArgStr;
    }