Gauge = function () {
    var needles;
    var cX;
    var cY;
}

Gauge.prototype.CreateGauge = function (opts) {
    if (!opts || "object" !== typeof opts) { return false; }

    this.needles = new Array;

    if (!opts.gauge.width) { opts.gauge.width = parseInt($("#" + opts.gauge.id).width()); }
    if (!opts.gauge.height) { opts.gauge.height = parseInt($("#" + opts.gauge.id).height()); }
    if (!opts.needle.base) { opts.needle.base = 0; }
    if (!opts.needle.offsetX) { opts.needle.offsetX = 0; }
    if (!opts.needle.offsetY) { opts.needle.offsetY = (opts.gauge.height - opts.needle.base) / 2; }
    if (!opts.needle.x) { opts.needle.x = (opts.gauge.width - opts.needle.width) / 2; }
    if (!opts.needle.y) { opts.needle.y = opts.gauge.height - opts.needle.height - opts.needle.base; }

    this.cX = (opts.gauge.width / 2) + opts.needle.offsetX;
    this.cY = (opts.gauge.height / 2) + opts.needle.offsetY;

    var gaugeCanvas = Raphael(opts.gauge.id, opts.gauge.width, opts.gauge.height);
    var context = this;
    $("#" + opts.gauge.id + " img").each(function () {
        context.needles.push(gaugeCanvas.image(this.src, opts.needle.x, opts.needle.y, opts.needle.width, opts.needle.height));
    });

    if (opts.scale) {
        if (!opts.scale.fontColor) { opts.scale.fontColor = "#4375B0"; }
        if (!opts.scale.fontSize) { opts.scale.fontSize = 10; }
        if (!opts.scale.unit) { opts.scale.unit = ""; }
        if (!opts.gauge.borderMargin) { opts.gauge.borderMargin = 16; }

        // Generate labels
        var r = this.cX - opts.gauge.borderMargin;
        var labelWidth = 50;
        var stepData = new Array;
        if ((opts.scale.max - opts.scale.min) % opts.scale.step != 0) { opts.scale.max = Math.ceil(opts.scale.max / opts.scale.step) * opts.scale.step; }
        for (var i = opts.scale.max; i >= opts.scale.min; i -= opts.scale.step) { stepData.push(i); }
        var steps = stepData.length - 1;
        var stepDegrees = opts.scale.degrees / steps;

        for (var i = 0; i <= steps; i++) {
            var x = parseInt(r * Math.sin((270 + stepDegrees * i) * Math.PI / 180));
            var y = parseInt(r * Math.cos((270 + stepDegrees * i) * Math.PI / 180));
            var px = parseInt(this.cX + x);
            var py = parseInt(this.cY - y - (opts.scale.fontSize / 2));
            if (i == 0 || i == steps) { py = this.cY - opts.scale.fontSize; }

            var html = "<div style='position:absolute;font-size:" + opts.scale.fontSize + "px;color:" + opts.scale.fontColor + ";top:" + py + "px;width:" + labelWidth + "px;";
            if (i > (steps / 2)) { html += "left:" + (opts.gauge.width - 5 - px) + "px;text-align:left;"; }
            if (i == (steps / 2)) { html += "left:" + (this.cX - (labelWidth / 2)) + "px;text-align:center;"; }
            if (i < (steps / 2)) { html += "right:" + (px - 5) + "px;text-align:right;"; }
            html += "'>" + stepData[i] + "</div>";
            $("#" + opts.gauge.id).append(html);
        }

        if (opts.scale.factor) {
            if (opts.scale.factor > 1) {
                var description = "";
                if (opts.scale.unit != "") { description += opts.scale.unit + " "; }
                description += "x " + Math.pow(10, opts.scale.factor);
                $("#" + opts.gauge.id).append("<div style='position:absolute;font-size:" + opts.scale.fontSize + "px;color:" + opts.scale.fontColor + ";width:" + opts.gauge.width + "px;text-align:center;top:" + (opts.gauge.height / 3 * 2) + "px;left:0px'>" + description + "</div>");
            }
        }
    }
}

Gauge.prototype.RotateNeedle = function (needleIndex, deg) {
    this.needles[needleIndex].rotate(deg, this.cX, this.cY);
}

