console.log('Xyrah Js Ext loaded: http://www.nederspania.eu/wiki/index.php?edit=XyrahJsExt');
xy.url.redmine = 'https://redmine.toltech.nl/';
xy.redmine.setTaskTimes = function(times) {
var done = 0;
for (var i = 0; i < times.length; i++) {
var info = timesi?;
var task = xy.redmine.findTaskByName(info[0]);
var tId = xy.redmine.getTaskId(task);
var current = xy.redmine.parseTask(task);
if (tId && Number.isInteger(info[1]) && !current ) {
done++;
console.log(info[0], task, tId, info[1]);
xy.redmine.setTime(tId, info[1], done * 5000);
}
}
}
xy.redmine.splitText = function(txt) {
    var res = txt.split("|");
    var res2 = [];
    for (var i = 0; i < res.length; i++ ) {
          var tmp = resi?.split("\t");
          var time = parseInt(tmp[1].trim());
          if (time) {
              res2.push(?);
          }
    }
    return res2;
}
xy.redmine.findTaskByName = function(name) {
   var task = $( "div.subject:contains('" + name + "')" ).parent();
   return task;
}
xy.redmine.getTaskId = function(task) {
   if (task && task.length == 1) {
       return $('.id .v', task).text();
   }
   return false;
}
xy.redmine.setTime = function( taskId, time, timeout ) {
   if (!timeout) {
       timeout = 10;
   }
   // console.log(taskId, time, timeout);
   setTimeout(function() {
        $.post( "/rb/task/" + taskId, { remaining_hours: time, project_id: 16, _method: "put"  } )
   }, timeout );
}
xy.redmine.listTasks = function (more) {
    var report = , paste = ;
    var total = 0;
    var today = new Date();
    var totals = [];
    $('.story-swimlane').each(function () {
            var lane = $(this);
            var story = $(this).find('.story');
            var id = lane.attr('id').substr(9, 5);
            var name = $('.subject', story).text();
            var points = parseFloat($('.story_points', story).text());
            var left = 0;
            report = report + "\n" + "Story #" + id + " " + name + "\n";
            var sel = '.swimlane-rb-status-id="1"?, .swimlane-rb-status-id="2"?';
            if (more) {
                sel = sel + ',.swimlane-rb-status-id="3"?, .swimlane-rb-status-id="4"?';
            }
            $(sel, lane)
            .find('.task').each(function () {
                var task = $(this);
                var id = xy.redmine.getTaskId(task);
                var subject = $('.subject', task).text();
                var description = $('.description', task).text().replace(/(\r\n|\n|\r)/gm," | ");;
                report = report + subject + "\t" + description + "\n";
                });
            paste = paste + left + "\n";
            total = total + left;
            totals.push(id, points?);
            });
    console.log(report);
};
xy.redmine.maxWidth = function () {
    $('#content').css('margin-right', "0px");
    $('#sidebar').hide();
    $('.swimlane').not('.ui-sortable').hide(); $('td-rb-status-id="3"?').hide(); $('td-rb-status-id="4"?').hide(); $('td-rb-status-id="5"?').hide(); $('td-rb-status-id="6"?').hide();  $('td-rb-status-id="1"?').css('width', '960px');
}
xy.redmine.parseTaskboard = function () {
    var report = , paste = ;
    var total = 0;
    var today = new Date();
    var totals = [];
    $('.story-swimlane').each(function () {
            var lane = $(this);
            var story = $(this).find('.story');
            var id = lane.attr('id').substr(9, 5);
            var name = $('.subject', story).text();
            var points = parseFloat($('.story_points', story).text());
            var left = 0;
            $('.swimlane-rb-status-id="1"?, .swimlane-rb-status-id="2"?, .swimlane-rb-status-id="4"?', lane)
            .find('.task').each(function () {
                left = left + xy.redmine.parseTask($(this));
                });
            report = report + "#" + id + ": " + name + " (" + points + ") \t" + left + "\n";
            paste = paste + left + "\n";
            total = total + left;
            totals.push(id, points?);
            });
    console.log(today);
    console.log(report);
    console.log(paste);
    return {today: today, total: total, report: report, paste: paste, totals: totals} ;
};
xy.redmine.summary = function() {
    var factor = 1.8;
    var developers = 4.5/5 * 2 + 4/5 + 2/5;
    var extraDays = 0;
    var report = xy.redmine.parseTaskboard();
    var est = xy.calc.sprintEstimate(report.total, factor, developers, extraDays);
    // console.log(est);
    return est;
}
xy.load.jQuery = function() {
    var fr = xy.load.resource(xy.url.jquery, 'js');
    document.addEventListener("DOMContentLoaded", function(event) {
       jQuery.noConflict();
    });
    return fr;
}
if (window.jQuery == undefined) {
    xy.load.jQuery();
}
xy.load.css('https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css');
xy.load.js('https://code.jquery.com/ui/1.11.4/jquery-ui.min.js');
xy.load.js('https://www.google.com/jsapi');
xy.tests.dialog = function() {
    if ($('#xyDialog').length == 0) {
        $('body').append('<div id="xyDialog"><div id="chart"></div></div>');
    }
    // http://api.jqueryui.com/dialog/
    $('#xyDialog').dialog({
          modal: true
    });
}
xy.redmine.plot = function() {
    xy.tests.dialog();
    setTimeout(function(){
         // http://stackoverflow.com/questions/9519673/why-does-google-load-cause-my-page-to-go-blank
         google.load('visualization', '1', {'callback':xy.tests.drawBasic, 'packages':'corechart','line'?});
         // google.setOnLoadCallback(xy.tests.drawBasic);
    }, 1000);
}
xy.tests.drawBasic = function() {
var title = 'End of the year tweaks';
var startDate = '2015-12-29';
var endDate = '2016-02-04';
var estimatedDaysRequired = 27;
var progress = [
        '2015-12-29', 27?,
        '2016-01-13', 16?,
        '2016-01-20', 13?,
        '2016-02-02', 11?
    ];
      var ideal = new google.visualization.Data Table?();
      ideal.addColumn('date', 'date');
      ideal.addColumn('number', 'ideal');
      var start = new Date(startDate);
      var end = new Date(endDate);
      ideal.addRows([
        start, estimatedDaysRequired?, end, 0?
      ]);
      var data = new google.visualization.Data Table?();
      data.addColumn('date', 'date');
      data.addColumn('number', 'actual');
      data.addRows([
        start, estimatedDaysRequired?
      ]);
      for (var i = 0; i < progress.length; i++) {
          data.addRows([new Date(progress[i?[0]), progressi?[1]] ]);
      }
      var options = {
        hAxis: {
          title: 'Date'
        },
        vAxis: {
          title: 'Days required'
        },
          title: title,
        height: 480,
        width: 640,
        interpolateNulls: true
      };
      var chart = new google.visualization.Line Chart?(document.getElementById('chart_sprint_progress'));
     var joinedData = google.visualization.data.join(ideal, data, 'full', [0, 0]], [1], [1]);
      chart.draw(joinedData, options);
}
if (window.jQuery && $('#chart_sprint_progress').length && xy.tests.drawBasic) {
 setTimeout(function(){
         google.load('visualization', '1', {'callback':xy.tests.drawBasic, 'packages':'corechart','line'?});
    }, 100);
}