Event.observe(window, 'load', attachTooltips );

function attachTooltips( tooltipType )
{
  switch(tooltipType)
  {
    case 'textTooltip':
      //show text tooltips
      attachTextTooltip();
      break;
    case 'htmlTooltip':
      //show html tooltips
      attachHtmlTooltip();
      break;
    case 'htmlClickTooltip':
      //show html click tooltips
      attachHtmlClickTooltip();
      break;
    case 'ajaxTooltip':
      //show ajax tooltips
      attachAjaxTooltip();
      break;
    case 'ajaxClickTooltip':
      //show ajax tooltips
      attachAjaxClickTooltip();
      break;
    default:
      //show text tooltips
      attachTextTooltip();
      //show html tooltips
      attachHtmlTooltip();
      //show html click tooltips
      attachHtmlClickTooltip();
      //show ajax tooltips
      attachAjaxTooltip();
      //show ajax tooltips
      attachAjaxClickTooltip();
  }
}

/**
In each 'attach...' function we must check if title attribute is not empty.
This is need to not overwrite already attached tooltip with empty value,
which left after the first attachement procedure
*/

function attachTextTooltip()
{
  $$(".textTooltip").each(function(item) {
    new Tooltip(item, {});
    item.removeClassName('textTooltip');
	});
}

function attachHtmlTooltip()
{
  var isDraggableRE = /draggable/;
  var isDraggable = false;

  $$(".htmlTooltip").each( function(item) {
    // Check if tooltip should be draggable
    isDraggable = isDraggableRE.test(item.className);
    
    new Tooltip(item, {
		  appointmentTitle: 'id',
		  showHeader: true,
		  draggable: isDraggable
		});
		item.removeClassName('htmlTooltip');
	});
}

function attachHtmlClickTooltip()
{
  var isDraggableRE = /draggable/;
  var isDraggable = false;

  $$(".htmlClickTooltip").each( function(item) {
    // Check if tooltip should be draggable
    isDraggable = isDraggableRE.test(item.className);
    
    new Tooltip(item, {
		  appointmentTitle: 'id',
		  handlerShow: 'click',
		  handlerHide: 'clickCancel',
		  showHeader: true,
		  draggable: isDraggable
		});
		item.removeClassName('htmlClickTooltip');
	});
}

function attachAjaxTooltip()
{
  var isDraggableRE = /draggable/;
  var isDraggable = false;

  $$(".ajaxTooltip").each( function(item) {
    // Check if tooltip should be draggable
    isDraggable = isDraggableRE.test(item.className);
    
    new Tooltip(item, {
	    appointmentTitle: 'url',
		  url: item.title,
		  handlerHide: 'clickCancel',
		  showHeader: true,
		  draggable: isDraggable
	  });
		item.removeClassName('ajaxTooltip');
	});
}

function attachAjaxClickTooltip()
{
  var isDraggableRE = /draggable/;
  var isDraggable = false;

  $$(".ajaxClickTooltip").each( function(item) {
    // Check if tooltip should be draggable
    isDraggable = isDraggableRE.test(item.className);
    
    new Tooltip(item, {
	    appointmentTitle: 'url',
		  url: item.title,
		  handlerShow: 'click',
		  handlerHide: 'clickCancel',
		  showHeader: true,
		  draggable: isDraggable
	  });
		item.removeClassName('ajaxClickTooltip');
	});
}

function hideTooltips()
{
  // important !!! to HIDE tooltips, don't REMOVE

  $$(".tooltip").each( function(item) { item.hide(); });

  if (Prototype.Browser.IE) {
    $$(".tooltip-iframe").each( function(item) { item.hide(); });
  }
}
