﻿(function($) {

  /*** DatePicker Defaults ***/
  $.datepicker.setDefaults({
    showAnim: 'fade',
    duration: 100
  });

  /*** Dialog - add option for a print button in the title bar ***/
  $.ui.dialog.prototype.options.showPrint = false;
  $.ui.dialog.prototype._createOrig = $.ui.dialog.prototype._create;
  $.ui.dialog.prototype._create = function() {
    var retValue = this._createOrig();
    if (this.options.showPrint) {
      $(".ui-dialog-title", this.element.parent()).after('<div class="printDialogIcon"><img alt="PrintImage" src="/ols/images/common/print_icon.png" />&nbsp;<a href="javascript:window.print();">Print</a><div>');
    }
    return retValue;
  };

  /*** Dialog - avoid IE8 resize problem ***/
  $.ui.dialog.overlay.createOrig = $.ui.dialog.overlay.create;
  $.ui.dialog.overlay.create = function(dialog) {
    var retValue = this.createOrig();
    // handle window resize
    if (($.browser.msie && $.browser.version.substr(0, 1) > 7)) {
      // Causes endless loop in IE8 http://dev.jqueryui.com/ticket/4758
      $(window).unbind('resize.dialog-overlay', $.ui.dialog.overlay.resize);
    }
    return retValue;
  };

  /*** Slider - avoid percent not a number problem ***/
  $.ui.slider.prototype._refreshValue = function() {
    var oRange = this.options.range, o = this.options, self = this;
    var animate = (!this._animateOff) ? o.animate : false;

    if (this.options.values && this.options.values.length) {
      var vp0, vp1;
      this.handles.each(function(i, j) {
        var valPercent = (self.values(i) - self._valueMin()) / (self._valueMax() - self._valueMin()) * 100;

        /*** Added Block ***/
        if (isNaN(valPercent)) {
          valPercent = 0;
        }
        /*** End Added Block ***/

        var _set = {}; _set[self.orientation == 'horizontal' ? 'left' : 'bottom'] = valPercent + '%';
        $(this).stop(1, 1)[animate ? 'animate' : 'css'](_set, o.animate);
        if (self.options.range === true) {
          if (self.orientation == 'horizontal') {
            (i == 0) && self.range.stop(1, 1)[animate ? 'animate' : 'css']({ left: valPercent + '%' }, o.animate);
            (i == 1) && self.range[animate ? 'animate' : 'css']({ width: (valPercent - lastValPercent) + '%' }, { queue: false, duration: o.animate });
          } else {
            (i == 0) && self.range.stop(1, 1)[animate ? 'animate' : 'css']({ bottom: (valPercent) + '%' }, o.animate);
            (i == 1) && self.range[animate ? 'animate' : 'css']({ height: (valPercent - lastValPercent) + '%' }, { queue: false, duration: o.animate });
          }
        }
        lastValPercent = valPercent;
      });
    } else {
      var value = this.value(),
				valueMin = this._valueMin(),
				valueMax = this._valueMax(),
				valPercent = valueMax != valueMin
					? (value - valueMin) / (valueMax - valueMin) * 100
					: 0;

      if (isNaN(valPercent)) {
        valPercent = 0;
      }
      var _set = {}; _set[self.orientation == 'horizontal' ? 'left' : 'bottom'] = valPercent + '%';
      this.handle.stop(1, 1)[animate ? 'animate' : 'css'](_set, o.animate);

      (oRange == "min") && (this.orientation == "horizontal") && this.range.stop(1, 1)[animate ? 'animate' : 'css']({ width: valPercent + '%' }, o.animate);
      (oRange == "max") && (this.orientation == "horizontal") && this.range[animate ? 'animate' : 'css']({ width: (100 - valPercent) + '%' }, { queue: false, duration: o.animate });
      (oRange == "min") && (this.orientation == "vertical") && this.range.stop(1, 1)[animate ? 'animate' : 'css']({ height: valPercent + '%' }, o.animate);
      (oRange == "max") && (this.orientation == "vertical") && this.range[animate ? 'animate' : 'css']({ height: (100 - valPercent) + '%' }, { queue: false, duration: o.animate });
    }
  };

})(jQuery);

