/**
 * widget for picking tags using yui menus
 */
tagPick = {}; // namespace object;

/**
 * Setup tag picking widget, consisting of autocomplete field and tree.
 * @param fieldBase String id of text field.
 * @param tags Tag data. Will contain data for some of interests, spots, and areas
 * @param placeType key for lib/ajax/autocomplete_tag. One of 'I' (interests), 'SA' (spots & areas)
*/
tagPick.setup = function(fieldBase, tags, placeType) {
  var tagData = tagTree.build(tags);
  var oMenu = new YAHOO.widget.Menu(fieldBase + '_xxx', { position: 'static', width: '95px' });
  var items = [];
  if (tagData.spots) { items.push(tagPick._menuItems(fieldBase, tagData.spots, 's_id_')); }
  if (tagData.areas) { items.push(tagPick._menuItems(fieldBase, tagData.areas, 'a_id_')); }
  if (tagData.interests) { items.push(tagPick._menuItems(fieldBase, tagData.interests, 'i_id_')); }
  oMenu.addItems(items);
  oMenu.render(fieldBase + '_tree');
  oMenu.show();
  tagPick._setupAutocomplete(fieldBase, placeType);
};

tagPick._menuItems = function(fieldBase, tagData, idPrefix) {
  var onClick = {
      fn: function(event, xy, oneTagData) {
        $(fieldBase + '_tree').style.display = 'none'; // hide tree
        $(fieldBase + '_id').value = oneTagData.id;
        $(fieldBase).value = oneTagData.id == 0 ? '' : oneTagData.label;
      },
      obj: tagData.data, scope: null};
  var ret = { text: tagData.data.label, onclick: onClick };
  if (tagData.children.length > 0) { // has children
    var itemData = [];
    for (var i = 0; i < tagData.children.length; i++) {
      itemData.push(tagPick._menuItems(fieldBase, tagData.children[i], idPrefix));
    }
    ret.submenu = { id: idPrefix + tagData.data.id, itemdata: itemData };
  }
  return ret; 
};

tagPick._setupAutocomplete = function(fieldBase, placeType) {
  var dataSource = new YAHOO.widget.DS_XHR(
      'lib/ajax/autocomplete_tag.php',
      ['result', 't_name']);  // what we want out of returned data
  dataSource.responseType = YAHOO.widget.DS_XHR.TYPE_JSON; // default
  dataSource.scriptQueryAppend = 'tagType=' + placeType;
  var autoComp = new YAHOO.widget.AutoComplete(
      fieldBase,  // input field
      fieldBase + '_complete', // container to display results
      dataSource); // data source
  autoComp.maxResultsDisplayed = 50;
  autoComp.forceSelection = true;
  // format the strings we display. p_id of 0 is special.
  autoComp.formatResult = function(aResultItem, sQuery) {
    var rd = aResultItem[1];
    return (rd.p_id != null ? rd.p_name+' > ' : '') + '<b>' + rd.t_name + '<\/b>' +
    	(rd.t_alt.length > 0 ? ' [' + rd.t_alt.replace(/;/g,', ') + ']' : '');
  };
  // when something selected, set hidden field to t_id
  // type is type of event.
  // args is array of: the AutoComplete instance, the selected element, the data for the item
  autoComp.itemSelectEvent.subscribe(function(type, args) {
    $(fieldBase + '_id').value = args[2][1].t_id;
  });
};
