/**
 * jQuery Tagnput plugin
 * This jQuery plugin is to build a popup that help inserting
 * existing tags into a textfield. Check this blog post:
 * http://ideadeployment.de/2009/01/jquery-tag-inputjquery-tag-input/
 *
 * @name jquery.taginput-1.0.js
 * @author Tillmann Carlos Bielefeld - http://TillmannCarlosBielefeld.de
 * @version 1.0
 * @date January 24, 2009
 * @category jQuery plugin
 * @copyright (c) 2009 Tillmann Carlos Bielefeld (TillmannCarlosBielefeld.de)
 * @license http://www.opensource.org/licenses/apache2.0.php
 * @example Visit eventize.de for the first use of this plugin
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
 */


jQuery.Taginput = function(){
  this.popupHideTimeout = null;
};



jQuery.Taginput.prototype.showTagList = function (field_id, popup_id){
  this.setAlreadyUsedTags(field_id, popup_id);
  var pos = $(field_id).position();
  $(popup_id).fadeIn()
  $(popup_id).css('left', pos.left);
  $(popup_id).css('top', pos.top + 15);
  me = this;
  $(popup_id).click(function() {me.stopHiding(); $(field_id).focus()} );
}

jQuery.Taginput.prototype.hideTagList = function (field_id, popup_id){
  me = this; 
  me.popupHideTimeout = setInterval(function() {
    $(popup_id).fadeOut();
    clearTimeout(me.popupHideTimeout);
  }, 200);
}

jQuery.Taginput.prototype.getTagsFromField = function (field_id) {
  var tags = $(field_id).val().split(",");
  tags = jQuery.map(tags, function (t) {
    trimmed = jQuery.trim(t)
    return trimmed ? trimmed : null;
  });
  return tags;
}

jQuery.Taginput.prototype.stopHiding = function() {
  clearTimeout(this.popupHideTimeout);
}


jQuery.Taginput.prototype.addTagToField = function (field_id, tag, popup_id){
  this.stopHiding();
  
  tag = jQuery.trim(tag);

  var tags = this.getTagsFromField(field_id);

  if (-1 == jQuery.inArray( tag, tags )) {
    tags.push(tag);
  }
  else {
    var index = jQuery.inArray( tag, tags );
    tags.splice(index, 1);
  }
  $(field_id).val(tags.join(', '));

  this.setAlreadyUsedTags(field_id, popup_id);
  $(field_id).focus();
}

jQuery.Taginput.prototype.setAlreadyUsedTags = function(field_id, popup_id){
  var tags = this.getTagsFromField(field_id);
  $(popup_id + ' .tag').each(function (i) {
    var el = $(this)
    // -1 == not found. Damnit!
    if (-1 == jQuery.inArray(jQuery.trim(el.html()), tags)) {
      el.removeClass('tag_used')
    }
    else {
      el.addClass('tag_used')
    }
  }
  );
}




/* INIT */
$(function(){ 
  jQuery.taginput = new jQuery.Taginput();
});




