Raphael.fn.pieChart = function (cx, cy, r, values, labels, stroke, colors, printText, cats) {
    var paper = this,
    rad = Math.PI / 180,
    chart = this.set();
    function sector(cx, cy, r, startAngle, endAngle, params) {
        var x1 = cx + r * Math.cos(-startAngle * rad),
        x2 = cx + r * Math.cos(-endAngle * rad),
        y1 = cy + r * Math.sin(-startAngle * rad),
        y2 = cy + r * Math.sin(-endAngle * rad);
        return paper.path(["M", cx, cy, "L", x1, y1, "A", r, r, 0, +(endAngle - startAngle > 180), 0, x2, y2, "z"]).attr(params);
    }
    var angle = 0,
    total = 0,
    process = function (j, catid) {
        var value = values[j],
        angleplus = 360 * value / total,
        popangle = angle + (angleplus / 2),
        color = Raphael.hsb(colors[j], .75, 1),
        ms = 500,
        delta = 30,
        bcolor = Raphael.hsb(colors[j], 1, 1),
        p = sector(cx, cy, r, angle, angle + angleplus, {
            fill: "90-" + bcolor + "-" + color, 
            stroke: stroke, 
            "stroke-width": 3
        }),
        txt = paper.text(cx + (r + delta + 80) * Math.cos(-popangle * rad), cy + (r + delta + 25) * Math.sin(-popangle * rad), labels[j]).attr({
            fill: bcolor, 
            stroke: "none", 
            opacity: 0, 
            "font-size": 15
        });                

        if (printText) {
            p.mouseover(function () {
                if ( jQuery.browser.msie && parseInt(jQuery.browser.version, 10) > 8 || !jQuery.browser.msie) {
                    p.stop().animate({
                        transform: "s1.1 1.1 " + cx + " " + cy
                    }, ms, "elastic");
                }                    
            }).mouseout(function () {
                if ( jQuery.browser.msie && parseInt(jQuery.browser.version, 10) > 8 || !jQuery.browser.msie) {
                    p.stop().animate({
                        transform: ""
                    }, ms, "elastic");
                }
            });
            
            $(p[0]).click(function() {
                if ( jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 9 ) {
                    throw "Workaround IE";
                }
            });
            
            p.click(function() { 
                showCategories(catid);
            });
        }
            
        angle += angleplus;
        chart.push(p);
            
        if (printText) {
            txt.stop().animate({
                opacity: 1
            }, ms, "elastic");
            chart.push(txt);    
        }
            
    };
    for (var i = 0, ii = values.length; i < ii; i++) {
        total += values[i];
    }
    for (i = 0; i < ii; i++) {
        process(i, cats[i]);
    }
    return chart;
};

function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
        node = document;
    
    if ( tag == null )
        tag = '*';

    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");

    for (i = 0, j = 0; i < elsLen; i++) {
        try {
            var testClassName = els[i].className;
            if ( pattern.test(testClassName) ) {
                classElements[j] = els[i];
                j++;
            }    
        } catch (Ex) {
        }
        
    }

    return classElements;
}

showProgresses = function(catid) {
    $('.catbar').hide();
    $('.progressbarall').show();
    $("html, body").animate({
        scrollTop: $(document).height()
        }, 300);    
}

showCategories = function(catid) {
    
    var elements = getElementsByClass('catbar');
    var i = 0;
    for(i=0; i<elements.length; i++) {
        var catelement = elements[i];
        var attributes = catelement.className
        if (attributes == null) {
            continue;
        }
        var classes = attributes.split(' ');

        var found = false;
        for(var j=0; j < classes.length; j++) {
            if (classes[j] == catid) {
                found = true;  
            } 
        }
        
        if (found) {
            catelement.style.display = '';
        } else {
            catelement.style.display = 'none';
        }
    }
    
    $("html, body").animate({
        scrollTop: $(document).height()
        }, 300);
}

initcakes = function () {
    $('.cake').each(function() {
        var values = [],
        labels = [],
        colors = [];
        var cats = [];
        
        $(this).find('tr').each(function () {
            cats.push(jQuery(this).attr('catid'));
            values.push(parseInt($("td", this).text(), 10));
            labels.push($("th", this).text());
            colors.push($(this).attr('col'));
            $(this).hide();
        });
        
        holderid = $(this).attr('holderid');
        width = $(this).attr('cakewidth')
        $(this).hide();
        
        if (width != undefined) {
            Raphael("holder_"+holderid, (parseInt(width,10)+10), (parseInt(width,10)+10)).pieChart((width/2)+5, (width/2)+5, width/2, values, labels, "#fff", colors, false, cats);
        } else {
            Raphael("holder_"+holderid, 700, 500).pieChart(350, 230, 150, values, labels, "#fff", colors, true, cats);
        }
    });
}
    


function initProgress() {
    barheight = 250;
    
    var progel = $('.progress').css('border','solid 2px #FFFFFF');
    progel.height(barheight);
    progel.width(50);
    
    barheight = 240;
    $('.progressbar').each(function() {
        var progressori = $(this).html();
        $(this).html(""); 
        progress = (progressori/100)*barheight;
        
        colorcode = $(this).attr('colorcode');
        
        if (colorcode == undefined) {
            colorcode = 11;
        }
        
        color = (colorcode == 0) ? 'red' : colorcode;
        color = (colorcode == 0.16) ? 'yellow' : color;
        color = (colorcode == 0.3) ? 'green' : color;
        color = (colorcode > 10 ) ? '#FFFFFF' : color;
        
        progbar = $('<div></div>').css({
            'background-color' : color, 
            'position' : 'relative'
        }).animate( {
            height: '+='+progress
        }, 1000);
        progbar.append($('<div></div>').css({
            'position' : 'absolute', 
            'padding-left' : '3px', 
            'left' : '1px', 
            'top' : '5px', 
            'color' : '#000000', 
            'font-weight' : 'bold', 
            'font-size' : '13px'
        }).html(progressori+"%"));
        $(this).append(progbar);
        
    });
}

sendrequest = function(testid, userid) {
    var executeAjaxEvent = {
        "executionClass" : 'QuestBackAdmin',
        "executionCommand" : 'sendRequestMail',
        "domElement" : 'requestform',
        "type" : "PostAjaxEvent",
        "testid" : testid,
        "userid" : userid
    };
    
    var thearray = new Array();
    var i = 0;
    $('.requestcandidate:checked').each(function() {
        thearray[i] = $(this).val();
        i++;
    });
    
    executeAjaxEvent.candidates = thearray;
    
    CrappleCommon.postAjax(executeAjaxEvent, true);
}
