﻿/********************************************/
/* LAPTOP Benchmark Graph Plugin            */
/* Created By Brandon Drake  	            */
/* Version: 3.1.1   			    */
/* Date: Dec 28, 2011			    */
/* Requires 				    */
/*  jCarousel                               */
/********************************************/

(function ($) {
    var defaults = { columns: 6, imgHeight: 50, imgWidth: 75, jsonScript: '', jsonObject: null, ajaxData: {}, cssClass: 'jcarousel-skin-benchmark', imgEvent: 'click', afterEvent: $.noop, jCarouselEnabled: true, onComplete: $.noop, showNext: true, graphClickNext: true, sliderEvent: $.noop }, methods = { _init: function (options) {
        return this.each(function () {
            var $this = $(this), data = $this.data('graph'); if (!data) {
                if (options) { options = $.extend({}, defaults, options) }
                var data = { element: $this, elementID: $this.attr('id'), options: options, total: 0, icons: 0, bars: [], timer: null, initLoad: true }
                if (options.jsonObject === null) { methods._processAjax(data); } else { methods._process(data, options.jsonObject); } 
            } 
        });
    }, _getIntervalMax: function (max) {
        var multiplier = 1; if (max > 100 && max < 999) { multiplier = 10; } else if (max > 1000 && max < 9999) { multiplier = 100; } else if (max > 10000 && max < 99999) { multiplier = 1000; } else if (max > 100000 && max < 999999) { multiplier = 10000; }
        max = parseInt(max / multiplier, 0) + 10; while (max % 6 != 0) { max += 1; }
        return max * multiplier;
    }, _animate: function (bar, steps, data) {
        var target = document.getElementById(bar._id); if (bar._step <= steps) {
            var w = parseInt(target.style.width) || 0, change = Math.abs(w - bar._end); if (bar._start < bar._end) { w = w + Math.ceil((change / 2)); } else { w = w - Math.ceil((change / 2)); }
            if (typeof [].indexOf !== 'undefined') {
                if (bar._end === w && data.col.indexOf(bar._id) === -1) { data.col.push(bar._id); data.complete += 1; }
                if (data.complete === data.total) { clearInterval(data.timer); target.style.width = bar._end + 'px'; } 
            }
            target.style.width = w + 'px'; bar._step = bar._step + 1;
        } else { clearInterval(data.timer); } 
    }, _drawGraph: function (data, jsonData) {
        var graph = $("#" + data.elementID + " .graphs"), caption = $("#" + data.elementID + " .graph_caption"), scaleMsg = $("#" + data.elementID + " .scaleMsg"), width = graph.width(), values = jsonData.results, drawWidth = 0, max = methods._getValueMax(values), scale = methods._getIntervalMax(max), temp = 0, scaleMax = (7 * scale) / 6, options = data.options, steps = 55, bars = data.bars, textVal = ""
        barTotal = 0; caption.find("h2").html(jsonData.title); caption.find("h3").html(jsonData.subscript); scaleMsg.html(jsonData.scaleMsg); clearInterval(data.timer); var elem_counter = 0; for (var j = 0, lenPrime = values.length; j < lenPrime; j += 1) {
            var valSet = values[j], subBars = bars[elem_counter], offset = [], valLen = valSet.length, len = subBars.length; if (valLen == 1) { offset = [0]; } else if (valLen == 2) { offset = [-6, 6]; } else if (valLen == 3) { offset = [-6, 0, 6]; } else if (valLen >= 4) { offset = [-6, -2, 2, 6]; }
            for (var i = 0; i < len; i++) {
                var score = (typeof valSet[i] !== 'undefined') ? valSet[i]["score"] : null, notes = score != null ? valSet[i]["notes"] : "", $bar = $('#' + subBars[i]._id); elem = document.getElementById(subBars[i]._id); start = parseInt(elem.style.width) || 0; end = Math.round((score / scaleMax) * width); subBars[i]._start = start
                subBars[i]._end = end
                subBars[i]._delta = Math.ceil((Math.abs(start - end) / steps)); subBars[i]._step = 1; var barVal = $('#' + subBars[i]._id + ' .value'); textVal = methods._getValueString(score, jsonData.scale); barVal.html(textVal); if (i > 0) { $bar.addClass('behind'); } else { $bar.addClass('uno'); }
                $bar.css('top', offset[i]); $bar.find('.notes').html(notes); barTotal += 1;
            }
            elem_counter += 1;
        }
        var newData = { timer: data.timer, total: barTotal, complete: 0, col: [] }
        newData.timer = setInterval(function () { for (var i = 0, len_1 = bars.length; i < len_1; i += 1) { subBars = bars[i]; for (var j = 0, len_2 = subBars.length; j < len_2; j += 1) { var bar = subBars[j]; methods._animate(bar, steps, newData); } } }, steps); if (data.initLoad === false) { options.afterEvent.call(this, data.caller); }
        for (i = 6; i >= 0; i -= 1) { $("#" + data.elementID + "_grid_base_" + i.toString()).html("&nbsp;" + methods._getValueString((scale / 6) * i, jsonData.scale)); } 
    }, _getValueMax: function (values) {
        var max = 0; for (var keys in values) {
            res = values[keys]; for (var val in res) {
                var tempMax = res[val]["score"]; if (typeof tempMax !== 'undefined' && tempMax > max)
                    max = tempMax;
            } 
        }
        return max;
    }, _getValueString: function (value, unit) {
        if (value !== null) { switch (unit.toLowerCase()) { case "minutes": case "hours": var time = parseInt(value), t1 = parseInt(time / 60), t2 = time - (t1 * 60); value = t1 + ":" + ((t2 < 10) ? "0" : "") + t2; break; default: break; } } else { value = ""; }
        return value;
    }, _processAjax: function (data) { var options = data.options; $.ajax({ url: options.jsonScript, type: 'GET', data: options.ajaxData, dataType: 'json', success: function (jsonData) { methods._process(data, jsonData); }, complete: function (jqXHR, textStatus) { if (options.onComplete !== $.noop) { options.onComplete.call(); } } }); }, _newBar: function (id) { return bar = { _id: id, _delta: 0, _start: 0, _end: 0, _step: 1 }; }, _process: function (data, jsonData) {
        if (!$.isEmptyObject(jsonData)) {
            $('#benchmark').css('display', 'block'); var ct = jsonData.systems.length, total = ct, id, options = data.options, i = 0, nextBtn = "<div class='graph_ctrl'><div><div class='left_part'>next test</div></div><div><div class='right_part'></div></div></div>"; html = "<div class='graph_body'>\n" + "<div class='header' >\n" + "<div class='graph_caption'><h2></h2><h3></h3></div>\n" + "<div class='scaleMsg'></div>\n" +
(options.showNext && options.jCarouselEnabled ? nextBtn : '') + "</div>\n" + "<div class='graph_content'>\n" + "<div class='titles'>\n", html2 = "<div class='graphs'>\n", html3 = "", bar = null, bars = [], subBars = null; for (i = 0; i < ct; i += 1) {
                if (i === 0) { extraClass = " first_bar"; } else if (i === (ct - 1)) { extraClass = " last_bar"; } else { extraClass = ""; }
                html += "<div class='title' >" + jsonData.systems[i] + "</div>"; html2 += "<div class='row'>\n"
                var repeat = (ct === (i + 1)) ? 1 : 4, offset = 0; subBars = []; for (var j = 0; j < repeat; j += 1) { bar = methods._newBar(data.elementID + "_graph_" + i + "_" + j); html2 += "<div id='" + bar._id + "' class='bar" + extraClass + "' style='z-index: " + (10 - j) + "; top: 0px;' >" + "<div class='notes'></div>\n" + "<div class='value'></div>\n" + "</div>"; subBars.push(bar); }
                html2 += "</div>"; bars.push(subBars);
            }
            data.bars = bars; data.total = ct; for (i = 0; i < 7; i += 1) { html3 += "<div id='" + data.elementID + "_grid_base_" + i.toString() + "' class='axis'></div>"; }
            html2 += "</div>\n"; html += "</div>\n" +
html2 + "<div class='spacer'></div>" +
html3 + "</div>\n" + "</div>\n"; if (options.jCarouselEnabled === true) {
                data.icons = ct = jsonData.tests.length; html += "<ul class='tests " + options.cssClass + "' >\n"; for (i = 0; i < ct; i += 1) {
                    html += "<li " + (i === 0 ? "class='active'" : "") + "><img src='" + jsonData.path + jsonData.tests[i].img + "' alt='" + jsonData.tests[i].title + "' height='" +
options.imgHeight + "' width='" + options.imgWidth + "' /><p>" + jsonData.tests[i].name + "</li>\n";
                }
                html += "</ul>\n"; data.element.html(html); ct = 0; $("#" + data.elementID + " .tests li").each(function () {
                    $(this).data("test", jsonData.tests[ct]).data("num", ct + 1); ; if (ct === 0) { methods._drawGraph(data, jsonData.tests[ct]); $('#' + data.elementID + " .graph_body").data("num", ct + 1); }
                    $(this).bind(options.imgEvent, function () { var me = $(this); if (!me.hasClass("active")) { $("#" + data.elementID + " .tests li").removeClass("active"); me.addClass("active"); data.caller = "thumbnail"; $('#' + data.elementID + " .graph_body").data("num", me.data("num")); methods._drawGraph(data, me.data("test")); } }); ct += 1;
                }); $("#" + data.elementID + " .tests").jcarousel({ scroll: options.columns, vertical: options.vertical }); if (options.sliderEvent !== $.noop) { $("#" + data.elementID + " .jcarousel-prev").click(function () { options.sliderEvent.call(this, "prev"); }); $("#" + data.elementID + " .jcarousel-next").click(function () { options.sliderEvent.call(this, "next"); }); } 
            } else { data.element.html(html); methods._drawGraph(data, jsonData.tests[0]); }
            id = data.elementID; $("#" + data.elementID).data('graph', data); $("#" + data.elementID + " .bar").hover(function () { var me = $(this); if (!me.hasClass('uno')) { me.removeClass('behind').addClass('hover'); me.parent().find('.uno').addClass('behind'); } }, function () { var me = $(this); if (!me.hasClass('uno')) { me.removeClass('hover').addClass('behind'); me.parent().find('.uno').removeClass('behind'); } }); if (options.jCarouselEnabled === true) {
                if (options.showNext) { $('#' + data.elementID + ' .graph_ctrl').click(function () { var graphName = '#' + this.parentNode.parentNode.parentNode.id; $(graphName).graph("next", "next"); }); }
                if (options.graphClickNext) { $('#' + data.elementID + ' .graph_content').click(function () { var graphName = '#' + this.parentNode.parentNode.id; $(graphName).graph("next", "graph"); }); } 
            }
            data.initLoad = false;
        } 
    }, next: function (caller) { var data = $(this).data('graph'), graph = $('#' + data.elementID + " .graph_body"), num = graph.data("num") + 1, nthChild = null; data.caller = caller; num = num <= data.icons ? num : 1; graph.data("num", num); nthChild = $("#" + data.elementID + " .tests li:nth-child(" + num + ")"); $("#" + data.elementID + " .tests li").removeClass("active"); nthChild.addClass("active"); $('#' + data.elementID + " .tests").data('jcarousel').scroll($.jcarousel.intval(num)); methods._drawGraph(data, nthChild.data("test")); } 
    }
    $.fn.graph = function (method) { if (methods[method]) { return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); } else if (typeof method === 'object' || !method) { return methods._init.apply(this, arguments); } else { $.error('Method ' + method + ' does not exist on jQuery.graph'); } };
})(jQuery)
