/*globals psc jQuery */
psc.namespace("schedule.preview");

psc.schedule.preview.Parameters = (function ($) {
  var query = null;
  var pending = false;

  function findPairKey(pair) {
    var found;
    $.each(query.get('segment'), function (prop, val) {
      if (val == pair.segment && query.get('start_date[' + prop + ']') === pair.start_date) {
        found = prop;
        return false;
      }
    });
    return found;
  }

  function nextKey() {
    var intKeys = [];
    $.each(query.get('segment'), function (key, _) {
      if (/^\d+$/.match(key)) {
        intKeys.push(parseInt(key));
      }
    });
    intKeys.sort();
    return (intKeys.pop() || 0) + 1;
  }

  return {
    init: function (hashParams) {
      query = $.query.load("#" + hashParams);
      pending = false;
      $('#schedule').bind('schedule-ready', function () {
        pending = false;
      });
    },

    size: function () {
      var ct = 0;
      $.each(query.get('segment'), function (prop, value) { ct += 1; });
      return ct;
    },

    clear: function () {
      query.EMPTY();
      pending = true;
    },

    add: function (newPair) {
      var newKey = nextKey();
      query.SET('segment[' + newKey + ']', newPair.segment);
      query.SET('start_date[' + newKey + ']', newPair.start_date);
      pending = true;
    },

    remove: function(pair) {
      var key = findPairKey(pair);
      if (key) {
        query.REMOVE('segment[' + key + ']');
        query.REMOVE('start_date[' + key + ']');
      }
      pending = true;
    },

    pending: function() {
      return pending;
    },

    requestedSegments: function () {
      var segments = [];
      $.each(query.get('segment'), function (prop, val) {
        segments.push({ segment: val, start_date: query.get('start_date')[prop] })
      });
      segments.sort(function (a, b) {
        var a_date = a.start_date;
        var b_date = b.start_date;
        if (a_date == b_date) {
          return 0;
        } else if (a_date < b_date) {
          return -1;
        } else {
          return 1;
        }
      });
      return segments;
    },

    toQueryString: function () {
      return query.toString();
    }
  };
}(jQuery));

@@ -187,24 +187,31 @@
    var found
+ = null
 ;%0A    $.each
@@ -1404,16 +1404,24 @@
  if (key
+ != null
 ) %7B%0A    

Update the if condition before removal of the segment. Condition 'if(key)' was failing when key value is 0 and segment with index value 0 never deleted from query. #889, 1st scenario.