// ==UserScript==
// @name        Better Paylocity
// @namespace   betterui
// @description Improvements to the Paylocity UI
// @include     https://webtime2.paylocity.com/webtime/Employee/Timesheet
// @include     https://webtime2.paylocity.com/webtime/Employee/Timesheet#
// @downloadURL https://raw.githubusercontent.com/Anthropohedron/better-paylocity/master/Better_Paylocity.user.js
// @version     0.2.0
// @grant       GM_addStyle
// @grant       unsafeWindow
// ==/UserScript==

(function(win, doc) {

// the page can't call functions defined in this context, so export to the
// page
function ef(fn) { return exportFunction(fn, win); }

var payTypeSuffix = /PayTypeId$/;

var onPayTypeChanged = ef(function onPayTypeChanged() {
  var chargeCode = win.$("#" + this.id.replace(payTypeSuffix, 'LaborLevel'));
  if (this.value == 9) {
    chargeCode.show();
  } else {
    chargeCode.hide();
  }
});

// exported function to be called by jQuery's each
var eachWorked = ef(function eachWorked() { this.value = 9; });

// make any row with an unset "Pay Type" column default to "Worked"
function defaultWorked() {
  win.$('tr.pay-type-description > td > select > option[value=0][selected]')
    .parent()
    .each(eachWorked);
}

// make custom Add Row button markup to be used later
var addRowBtn = win.$.parseHTML([
    '<div id="myAddRowBtn" class="t-link">',
      '<span class="t-sprite p-tool-add"></span>',
      'Add&nbsp;Row',
    '</div>'
  ].join(''))[0];

// attach handlers to the Add Row button click
win.$(addRowBtn)
  .click(win.addShift)
  .click(ef(defaultWorked));

var chargeCodeDropdown = null;

var replaceChargeCode = ef(function replaceChargeCode() {
  var chargeCode = $(this);
  //TODO
});

$.ajax({
  type:"GET",
  url: $('#TimeSheet_0__Entries_0__LaborLevel')
    .data('pPopupWindow')
    .getPopupUrl(),
  dataType:"html",
  success: function(data) {
    chargeCodeDropdown = $($.parseHTML(data)).find('#LaborLevelEdit0');
    win.$('.p-widget.p-filtercontrol[id^=TimeSheet_][id*=__Entries_][id$=__LaborLevel]').each(replaceChargeCode);
  }
});


win.$(doc).on('change',
    'select[id^=TimeSheet_][id*=__Entries_][id$=__PayTypeId]',
    onPayTypeChanged);

// set up some CSS
GM_addStyle([
    // just let the page do the scrolling
    'div#TimesheetContainer { max-height: none; } ',
    // make the Add Row button look good
    '#myAddRowBtn {',
       'background: #DEF1FA;',
       'margin-top: 10px;',
       'border: 1px solid;',
       'padding: 3px;',
       'border-radius: 8px;',
    '} ',
    '#myAddRowBtn .t-sprite {',
       'margin-right: 3px;',
    '} '
  ].join(''));

// default all unset rows to Worked
defaultWorked();

// wrap row selection to place the Add Row button at the associated day
var wrappedOnSelect = win.selectEntryRow;
function wrapOnSelect(row) {
  // call wrapped function
  var result = wrappedOnSelect(row);
  // get the newly-selected row
  var row = win.getSelectedEntryRow();
  if (row) {
    // if it's appropriate, add the Add Row button near the row
    row.parents('.day-end').prev('td.day').append(addRowBtn);
  } else {
    // no appropriate row, so make sure the Add Row button is gone
    win.$(addRowBtn).remove();
  }
  return result;
}
win.selectEntryRow = ef(wrapOnSelect);

})(unsafeWindow, unsafeWindow.document);


@@ -503,16 +503,37 @@
 doc) %7B%0A%0A
+var $ = win.jQuery;%0A%0A
 // the p
@@ -770,20 +770,16 @@
 eCode = 
-win.
 $(%22#%22 + 
@@ -1129,20 +1129,16 @@
 d() %7B%0A  
-win.
 $('tr.pa
@@ -1310,20 +1310,16 @@
 owBtn = 
-win.
 $.parseH
@@ -1521,20 +1521,16 @@
 n click%0A
-win.
 $(addRow
@@ -1534,16 +1534,16 @@
 RowBtn)%0A
+
   .click
@@ -2079,20 +2079,16 @@
 %7D%0A%7D);%0A%0A%0A
-win.
 $(doc).o
@@ -2099,16 +2099,16 @@
 hange',%0A
+
     'sel
@@ -3164,24 +3164,20 @@
 is gone%0A
+
     
-win.
 $(addRow

bring in jQuery