
var _fields= {};
var bubble= null;
var _my_editor= null;
var _bubbleTimer= null;
var showTooltip= true;

/** Aufruf beim Fokussieren des Feldes */
function tooltipInit ()
{
  var _id= $(this).attr ("id");
  var _name= $(this).attr ("name").split ("[")[0];
  var _lbl= findLabel(_id);
  
  if (_bubbleTimer) clearTimeout(_bubbleTimer);
  if (bubble)
  {
    bubble.hide().remove();
    bubble= null;
  }
  
  $.ajax(
  {
    type:"post", url:"ajax/form.help.php",
    data: {script: _fields[_id].data("script"), fieldname: _name, fieldid: _id, fieldlabel: _lbl},
    success:function(html)
    { 
      bubble= $("<div class=\"fodb_bubble_container\"></div>").html(html);
      _fields[_id].after (bubble);
      var preview= bubble.find("p");
      preview.html(bbcode2html(preview.text()));
      bubble.show();
      //_bubbleTimer= setTimeout("$('div.fodb_bubble_container').hide();", timeoutTime);
      //if (user.ip!='141.44.191.11' && user.ip!='141.44.191.23') 
      _bubbleTimer= setTimeout("countDown();", 1000);
    }
  });
}

function countDown()
{
  if(bubble) {
	  var countdown= bubble.find("input[name=countdown]");
	  if (0<countdown.val())
	  {
	    countdown.val(countdown.val()-1);
	    _bubbleTimer= setTimeout("countDown();", 1000);
	  }
	  else {
		if (bubble)
	  	{
		    bubble.hide().remove();
		    bubble= null;
		    _bubbleTimer= null;
	  	}
		$(".fodb_bubble_container").remove();
	  }
  }	
}

/** Aufruf beim Klicken auf die Buttons */
function tooltipAction (_script, _id, _action)
{  
  if (_action=='hide' && !confirm("Soll der Dialog wirklich entfernt werden?"))
    return;
    
  clearTimeout(_bubbleTimer); _bubbleTimer= null;
  
  var _lbl= findLabel(_id);
  var _name= _fields[_id].attr('name').split ("[")[0];
  var _data= {script: _script, fieldname: _name, cmd: _action, fieldid: _id, fieldlabel: _lbl};
    
  if (_action=='commit')
  {
    _data.value= _my_editor.val();
    if(bubble) bubble.hide().remove();
    bubble= null;
  }

  if (_action=='close')
  {
	if(bubble) bubble.hide().remove();
    bubble= null;
    return;
  }
  
  $.post("ajax/form.help.php",_data, function(html)
  {  
    if (_action=='commit')
      return;
      
    // inject bubble
    if(bubble) bubble.html(html);

    var preview= bubble.find("p");
    if (preview.size())
      preview.html(bbcode2html(preview.html()));
    else
    {
      _my_editor= bubble.find("textarea.editor");
      editor= $.markup(_my_editor, {menuContainer: bubble.find("div.fodb_bubble_content"), markupSet: bbcodeMarkupSet, updateInterval: 250, keywords: {fieldname: _name, fieldlabel: _lbl}});
    }
  });
}

function findLabel(_id)
{
  var _lbl;
  //if (_fields[_id].data('field').attr('id') && (_lbl= $('label[for='+_fields[_id].data('field').attr('id')+']')).size())
  if ((_lbl= $('label[for='+_id+']')).size())
    _lbl= $.trim(_lbl.text());
  else
  {
    _lbl= _fields[_id].parent();
    while(_lbl.size() && _lbl.get(0).tagName.toLowerCase() != "tr") _lbl= _lbl.parent();
    _lbl= (_lbl= _lbl.find(":first-child").eq(0)).size()? $.trim(_lbl.text()): '';
  }
  return _lbl;
}

(function($) {
  $.fn.extend({
    addFormHelp: function ()
    {
	  //,:checkbox,:radio
	    if (!user.agent.match(/^Opera/i))
      return this.find(":text,:file,textarea,select").each (function (i)
      {
  	    //var _id= $(this).attr ("name").split ("[")[0];
  	    var _id= $(this).attr ("id");
  	    if (!_id)
  	    {
  	      _id= $(this).attr ("name").split ("[")[0];
  	      var i= 0;
  	      while(_fields[_id+"_"+i]) i++;
  	      _id += "_"+i;
  	      $(this).attr ("id", _id);
  	    }
  	    if (_id)
  	    {
  	      _fields[_id]= $(this);
  	      _fields[_id].data("script", user.option);
  	      $(this).focus (tooltipInit);      
  	    }
      });
    }     
  }); 
})(jQuery);

addReadyFunction(function()
{
  if (showTooltip) $("form[name!=login][name!=menu_r_search]").addFormHelp();
});
