var maxEntries = 5;
var mouseHover = false; // to prevent multiple handling of hover mouseenter und mouseleave events
var cancelAnimation = false; // to cancel pop-down animation if mouse is over tooltip

function BodyLoaded()
{
  $('img[@src$=.png]').ifixpng();
  
  /*
  $('#feedback').mousemove(function () {
    if ($('#feedback').offset().top != '-' + startheight)
    {  
      $('#feedback').stop(true, true);
      $('#feedback').animate({color: "#000000"}, 5000).animate({top: '-' + startheight + 'px'}, 1500, function() {
        for (var i = 1; i <= maxEntries; i++)
        {
          $('#' + 'feedback' + i).css('display', 'none');
          $('#' + 'feedback' + i + 'Text').html('');
        }
        alert('#feedbackClosed');
      });
    }
  });
  */
};

/*
 * Add a feedback
 */
function addFeedback(text, type)
{
  cancelAnimation = false;
  var height = 0;
  $('#feedback').stop(true, false);
  $('#' + type).css('display', 'block');
  $('#' + type + 'Text').html(text);
  
  // add mouse hover handler, to prevent tooltip-hiding when mouse is over tootip
  $('#feedback').hover(
    function () // mouseenter
    {
	  if (!mouseHover)		// check if mouseenter handler is called several times in a row
	  {
		  mouseHover = true; // mouseenter handler called firs time
		  cancelAnimation = true; // cancel pop-down animation
		  
		  // if mouse cursor enters tooltip area when it already completely open (offset().top == -height),
		  // we stop the animation.
		  // We do not stop animation, if the tolltip pops-up or -down, to avoid undesirable collapse effects
		  if ($('#feedback').offset().top == '-' + height)
		  {
			$('#feedback').stop(true, true);
		  } 
		  
	  }
    }, 
    function () // mouseleave
    {
	  	  
	  if (mouseHover)  // check if mouseleave handler is called several times in a row
	  {
		  mouseHover = false; // mouseleave handler called first time after last mouseenter
		  
		  // when mouse cursorr leaves tooltip area in opened state (offset().top == -height), 
		  // we restart animation, to automaticalli hide tooltip in 5 seconds 
		  if ($('#feedback').offset().top == '-' + height)
		  {  
		    // stop runing wait-animation
			$('#feedback').stop(true, true);
			
			// restart cancelled wait-and-popdown animation 
			$('#feedback').animate({color: "#000000"}, 5000).animate({top: '-' + startheight + 'px'}, 1500, function() {

				for (var i = 1; i <= maxEntries; i++)
				{
				  $('#' + 'feedback' + i).css('display', 'none');
				  $('#' + 'feedback' + i + 'Text').html('');
				}
				alert('#feedbackClosed');
		  });
		  
		  }
	  }
    }
  );

  //$('#' + type + 'Icon').html('<img src="' + type + '.png" />');
  for (var i = 1; i <= maxEntries; i++)
  {
    if ($('#' + 'feedback' + i + 'Text').text() != '')
    {
      height = $('#' + 'feedback' + i).outerHeight() + height;
    }
  }
  height = startheight - height - 20;
  
  // animate tooltip showing 
  $('#feedback').animate({top: '-' + height + 'px'}, 1500, function() {    

	  // wait 5 seconds till start hiding the tooltip
	  $('#feedback').animate({ color: "#000000" }, 5000, function() {
		  // OnAnimationComplete handler

		  // start hiding the tootip is no mouse pointer hovers over it
		  if (!cancelAnimation) {
			  $('#feedback').animate({ top: '-' + startheight + 'px' }, 1500, function() {

				  for (var i = 1; i <= maxEntries; i++) {
					  $('#' + 'feedback' + i).css('display', 'none');
					  $('#' + 'feedback' + i + 'Text').html('');
					  $('#' + type + 'Icon').html('');
				  }
				  alert('#feedbackClosed');
			  });
		  }

	  });
  });
};

/*
 * Remove a feedback
 */
function removeFeedback(type) 
{
  var height = 0;
  $('#feedback').stop(true, false);
  $('#' + type).css('display', 'none');
  $('#' + type + 'Text').html('');
  //$('#' + type + 'Icon').html('');
  for (var i = 1; i <= maxEntries; i++)
  {
    if ($('#' + 'feedback' + i + 'Text').text() != '')
    {
      height = $('#' + 'feedback' + i).outerHeight() + height;
    }
  }
  height = startheight - height - 20;
  $('#feedback').animate({top: '-' + height + 'px'}, 1500);    
  $('#feedback').animate({color: "#000000"}, 5000).animate({top: '-' + startheight + 'px'}, 1500, function() {
    for (var i = 1; i <= maxEntries; i++)
    {
      $('#' + 'feedback' + i).css('display', 'none');
      $('#' + 'feedback' + i + 'Text').html('');
      $('#' + type + 'Icon').html('');
    }
    alert('#feedbackClosed');
  });
}