Date.prototype.getFullMonth = function() {
  var m = this.getMonth() + 1;
  return (m < 10) ? '0' + m : m ;
};

var EventManager = {
  $modID: null
, $modOP: null
, $form : null
};
var EM = EventManager;

jQuery.fn.json = function(json) {
  if(json === undefined) {
    return JSON.parse(jQuery(this).val());
  } else {
    return jQuery(this).val(JSON.stringify(json));
  }
};

jQuery.fn.checked = function(checked) {
  if(checked === undefined) {
    return jQuery(this).attr('checked');
  } else {
    if(parseInt(checked)) {
      jQuery(this).attr('checked', 'checked');
    } else {
      jQuery(this).removeAttr('checked');
    }
  }
};

jQuery.fn.fillSelect = function (data, selected, none){
  var id, attr, $this = jQuery(this);
  $this.empty();
  if(none)
    $this.append('<option value="--">' + none + '</option>');
  for(id in data) {
    attr = (id == selected ? 'selected="selected"' : '');
    $this.append('<option value="' + id + '" ' + attr + '>' + data[id].name + '</option>');
  }
};

jQuery.fn.createDatePart = function(part, selected, none) {
  var $this = jQuery(this);
  var min, max, inc = 1, txt, data = {}, now = (new Date()).getFullYear();
  switch(part) {
  case 'Y':
    min = now - 1;
    max = now + 1;
    none = none + none;
    break;
  case 'M':
    min = 1;
    max = 12;
    break;
  case 'D':
    min = 1;
    max = 31;
    break;
  case 'H':
    min = 0;
    max = 23;
    break;
  case 'I':
    min = 0;
    max = 59;
    inc = 15;
    break;
  }
  for(var i = min; i <= max; i += inc) {
    txt = (i < 10) ? '0' + i : i ;
    data[txt] = {name: txt};
  }
  $this.fillSelect(data, selected, none);
};

jQuery.fn.createDate = function(value, none) {
  var $this = jQuery(this);
  var parts = ['Y', 'M', 'D', 'H', 'I'];
  var values = [value.slice(0, 4)]; // year;
  for(var i = 4; i < value.length; i += 2) {
    values.push(value.slice(i, i+2));
  }

  $this.children('select').each(function(i, val) {
    $part = jQuery(val);
    $part.createDatePart(parts[i], values[i], none);
    if(none) {
      $part.change(function() {
        $this = jQuery(this);
        if($this.val() == none) {
          $this.nextAll('select').val(none);
        }
      });
    }
  });
};

jQuery.fn.gatherDate = function() {
  var $this = jQuery(this);
  var date = '';
  $this.children('select').each(function(i, val) {
    date += jQuery(val).val();
  });
  return date;
};

jQuery.fn.createSort = function(fields, directions, selectedField, selectedDirection) {
  var $this = jQuery(this);
  var $ord = $this.children('select:first');
  var $dir = $this.children('select:last');

  $ord.fillSelect(fields, selectedField);
  $dir.fillSelect(directions, selectedDirection);
};

jQuery.fn.gatherSort = function (order, dirct) {
  var $this = jQuery(this);
  order = order ? order : 'order';
  dirct = dirct ? dirct : 'dirct';
  return {
    order : $this.children('select:first').val()
  , dirct : $this.children('select:last').val()
  };
};

jQuery.fn.createFltr = function(data, selectedCat, selectedDoc, none) {
  var $this = jQuery(this);

  var $cats = $this.children('select:first');
  var $docs = $this.children('select:last');

  $cats.fillSelect(data, selectedCat, none);
  if($docs) {
    $cats.change(function() {
      var catID = $cats.val();
      if(catID != none)
        $docs.fillSelect(data[catID].docs, selectedDoc, none);
      else
        $docs.fillSelect({}, selectedDoc, none);
    });
    $cats.change();
  }
};

jQuery.fn.gatherFltr = function (ctID, dcID) {
  var $this = jQuery(this);
  ctID = ctID ? ctID : 'ctID';
  dcID = dcID ? dcID : 'dcID';
  return {
    ctID : $this.children('select:first').val()
  , dcID : $this.children('select:last').val()
  };
};

jQuery.fn.createCategories = function($docs, selectedCat, selectedDoc, none) {
  var $this = jQuery(this);
  $this.fillSelect(EM.categories, selectedCat, none);
  if($docs) {
    $this.change(function() {
      var catID = $this.val();
      if(catID != '--')
        $docs.fillSelect(EM.categories[catID].docs, selectedDoc, none);
      else
        $docs.fillSelect({}, selectedDoc, none);
    });
    $this.change();
  }
};


function splitDate(date) {
  var parts = date.split('-');
  for(var i in parts) {
    parts[i] = parts[i] | 0;
  }
  return parts;
}

function jointDate(parts) {
  for(var i in parts) {
    if(parts[i] < 10)
      parts[i] = '0' + parts[i];
  }
  return parts.join('-');
}

jQuery(document).ready(function() {
  EM.$modID = jQuery('#modID');
  EM.$modOP = jQuery('#modOP');
  EM.$form  = jQuery('#module');
  EM.submit = function(op) {
    this.$modOP.val(op);
    this.$form.submit();
  };

});
