/**
 * JBB Job Post
 * class to handle jbb job post / edit
 */
function JBBJobPost(form_id) {
  this.form_id = form_id;

  this.init();
}

JBBJobPost.prototype.init = function() {
  this.element_cache   = new JPageElementBase();
  this.container_cache = new JPageElementBase();
  this.msg_box_cache   = new JPageElementBase();

  this.controller      = new JControllerBase();
  this.validator       = new JValidBase();
  this.DEBUG           = false;
}

JBBJobPost.prototype.set_controller = function(controller) {
  this.controller = controller;
}

JBBJobPost.prototype.set_validator = function(validator) {
  this.validator = validator;
}

/**
 * add highlighting on container when a element comes into focus
 * @param id - id of the element that will trigger highlighting
 * @param container_id - the id of the container that will be highlighted
 */
JBBJobPost.prototype.add_highlight = function(id, container_id) {
  var self       = this;
  var obj        = YAHOO.util.Dom.get(id);
  var controller = this.controller;

  //cannot find object, don't do anything
  if (!obj) {
    this.error_log(id + ' not found for add_highlight()');
    return;
  }

  //store to container cache
  this.add_to_container_cache(container_id);
  //add observer
  this.add_observer(id, container_id, 'focus');
  //multiple observers for id?

  JEventUtil.addEvent(obj, 'focus', function(e) { controller.exec('focus', e, self) }, false);
}

/**
 * add highlighting on container when elements come into focus
 * @param id_list - list of elements id that will trigger highlighting
 * @param container_id - the container id that will be highlighted
 * @see add_highlight()
 */
JBBJobPost.prototype.add_group_highlight = function(id_list, container_id) {
  for (var i = 0; i < id_list.length; i++) {
    this.add_highlight(id_list[i], container_id);
  }
}

/**
 * add an observer relation (when something happens to the observed_id, the observer_id would trigger)
 * @param observed_id the element id that is being observed
 * @param observer_id the element id that is observering
 * @param type
 * @param arg
 */
JBBJobPost.prototype.add_observer = function(observed_id, observer_id, type, arg) {
  this.element_cache.addObserver(observed_id, YAHOO.util.Dom.get(observer_id), type, arg);
}

/**
 * add to container cache (for highlighting)
 * @param id - container id
 */
JBBJobPost.prototype.add_to_container_cache = function(id) {
  if (!this.container_cache.getElement(id)) {
    this.container_cache.addElement(id, YAHOO.util.Dom.get(id));
  }
}

/**
 * add to element cache
 * @param id - element id
 */
JBBJobPost.prototype.add_to_element_cache = function(id) {
  if (!this.element_cache.getElement(id)) {
    this.element_cache.addElement(id, YAHOO.util.Dom.get(id));
  }
}

JBBJobPost.prototype.add_to_message_box_cache = function(id) {
  if (!this.msg_box_cache.getElement(id)) {
    this.msg_box_cache.addElement(id, YAHOO.util.Dom.get(id));
  }
}


/**
 * add a counter
 * @param field_id - the id of the element to count
 * @param display_id - display the count in this element
 */
JBBJobPost.prototype.add_counter = function(field_id, display_id) {
  var self       = this;
  var obj        = YAHOO.util.Dom.get(field_id);
  var controller = this.controller;

  //if not found, just return
  if (!obj) {
    return;
  }
  
  this.add_to_element_cache(field_id);
  this.add_to_element_cache(display_id);
  this.add_observer(field_id, display_id, 'count');
  
  JEventUtil.addEvent(obj, 'keyup', function(e){ controller.exec('count', e, self) }, false);
}

JBBJobPost.prototype.initialize_count = function(field_id, display_id) {
  //??
}

/**
 * add onchange handler
 * @param field_id - the id of the element that was changed
 * @param msg_id - output error message onto this field
 * @param event_name - the event name used to check
 */
JBBJobPost.prototype.add_onchange_handler = function(field_id, msg_id, event_name) {
  var self       = this;
  var msg_obj    = YAHOO.util.Dom.get(msg_id);
  var field_obj  = YAHOO.util.Dom.get(field_id);
  var controller = this.controller;

  if (!field_obj) {
    this.error_log(field_id + ' is not a valid field');
    return;
  }

  if (!msg_obj) {
    this.error_log(msg_id + ' is not a valid field');
    return;
  }
  
  if (!event_name) {
    this.error_log('no event name specified');
    return;
  }
  
  this.add_to_element_cache(field_id);
  this.add_to_message_box_cache(msg_id);
  this.add_observer(field_id, msg_id, event_name);
  //validate when form submits
  this.add_observer(this.form_id, field_id, event_name);

  JEventUtil.addEvent(field_obj, 'change', function(e) { controller.exec(event_name, e, self) }, false);
}

/**
 * set onsubmit handler
 * @param handler - the handler that will be executed
 */
JBBJobPost.prototype.set_onsubmit_handler = function(handler) {
  var self       = this;
  var controller = this.controller;
  var form_obj   = YAHOO.util.Dom.get(this.form_id);

  if (!form_obj) {
    this.error_log(this.form_id + ' is not a valid form');
    return;
  }

  //need to assign onsubmit to stop form submission
  form_obj.onsubmit = function(e) {
    return handler(form_obj, self);
  }
}

/**
 * add toggle
 * @param field_id - this toggler's id
 * @param toggle_id_list - the id of sections that will be toggled
 * @param event_name - the event name which will be executed
 */
JBBJobPost.prototype.add_toggle = function(field_id, toggle_id_list, event_name) {
  var self       = this;
  var controller = this.controller;
  var field_obj  = YAHOO.util.Dom.get(field_id);

  if (!field_obj) {
    this.error_log(field_id + ' is not a valid field');
    return;
  }

  this.add_to_element_cache(field_id);

  for (var i = 0; i < toggle_id_list.length; i++) {
    this.add_to_container_cache(toggle_id_list[i]);
  }

  JEventUtil.addEvent(field_obj, 'change', function(e) { controller.exec(event_name, e, self); }, false);
}

/**
 * log error messages (for use in debugging)
 * @param msg - the msg to log
 */
JBBJobPost.prototype.error_log = function(msg) {
  if (!this.DEBUG) {
    return;
  }
  try {
    console.log(msg);
  }
  catch(e) {
  }
}

