/**
 * Tooltip component for jasmin and cobranded site-s
 *
 * @author: Attila Nyeste
 */
var toolTipComponent = function()
{
	var toolTipHolder;
	var text = "";
	var url = "";
	var rendered = false;
	var coordX = "0px";
	var coordY = "0px";
	var handlerFunction = function(){};
	var mouse = { x: 0, y: 0};

	document.onmousedown = getMouseCoordinates;

	function getMouseCoordinates(e)
	{
		if (navigator.appName == "Microsoft Internet Explorer")
		{
			// grab the x-y pos.s if browser is IE
			mouse.x = event.clientX + document.body.scrollLeft;
			mouse.y = event.clientY + document.documentElement.scrollTop;
		}
		else
		{
			// grab the x-y pos.s if browser is NS
			mouse.x = e.pageX;
			mouse.y = e.pageY;
		}
  	}

	function initialize()
	{
		content = "";

		toolTipHolder = document.createElement("div");
		document.body.appendChild(toolTipHolder);
		toolTipHolder.setAttribute("id", "tooltip_top");

		content = '<div id="tooltip_textbox"><div id="buttons_box" class="buttons_box">';
		content += '<span id="toolTipText" class="text"></span>';
		content += '<div id="toolTipFormHolder" style="height: 18px;"><form id="tooltipForm" action="" method="POST" onsubmit="return false;" ><input type="submit" class="buttons tooltip_yes_btn" value="Yes" onclick="" /><input type="button" class="buttons" value="No" onclick="toolTipComponent.hide(); return false;" /></form></div>';
		content += '</div></div>';

		toolTipHolder.innerHTML = content;
		toolTipHolder.style.display = "none";

		document.body.appendChild(toolTipHolder);

		rendered = true;
	}

	/**
	 * initObject must have these properties:
	 * - text: The text of the box, what we want to display for the users.
	 * - yesBtnText: The text of the yes button.
 	 * - noBtnText: The text of the no button.
 	 * - buttonsNeeded: TRUE / FALSE. You can specify here, if you need buttons or not.
	 * - url: The URL, the we going to navigate the user to this URL.
	 * - handlerFunction: a javascript function which handle the "yes" button keypressing
	 * - x: The component's horizontal position.
	 * - y: The component's vertical position.
	 *
	 * @param {Object} initObject
	 */
	function show(initObject)
	{
	  	if (!rendered) initialize();

		text = initObject.text;
		url  = initObject.url;
		butt1 = initObject.butt1;
		butt2 = initObject.butt2;

		componentForm = document.getElementById("tooltipForm");
		buttonYes = componentForm.elements[0];
		buttonNo  = componentForm.elements[1];

		if (typeof initObject.text == "undefined" )
		{
			alert("Error: No text parameter defined!");
			return false;
		}

		if (typeof initObject.url == "undefined" )
		{
			alert("Error: No post URL parameter defined!");
			return false;
		}

		buttonYes.value  = (typeof initObject.yesBtnText != "undefined" ) ?  initObject.yesBtnText : butt1;
		buttonNo.value   = (typeof initObject.noBtnText  != "undefined" ) ?  initObject.noBtnText : butt2;

		if (typeof initObject.buttonsNeeded != "undefined" && initObject.buttonsNeeded == false)
		{
			document.getElementById("toolTipFormHolder").innerHTML = "";
		}

		coordX = (typeof initObject.x != "undefined" ) ?  initObject.x : mouse.x+"px";
		coordY = (typeof initObject.y != "undefined" ) ?  initObject.y : mouse.y+"px";



		componentForm.setAttribute("action", url);

		if (navigator.appName == "Microsoft Internet Explorer")
		{
			buttonYes.detachEvent("onclick", handlerFunction);
		}
		else
		{
			buttonYes.removeEventListener("click", handlerFunction, false);
		}

		if (typeof initObject.handlerFunction == "function")
		{
			componentForm.setAttribute("onsubmit", "return false;");

			if (navigator.appName == "Microsoft Internet Explorer")
			{
				buttonYes.attachEvent("onclick", initObject.handlerFunction);
			}
			else
			{
				buttonYes.addEventListener("click", initObject.handlerFunction, false);
			}

			handlerFunction = initObject.handlerFunction;
		}
		else
		{
			// Firefox hack
			componentForm.removeAttribute("onsubmit");

			// IE hack
			componentForm.onsubmit = "";
		}

		document.getElementById("toolTipText").innerHTML = text;
		toolTipHolder.style.display = "block";
		toolTipHolder.style.top  = coordY;
		toolTipHolder.style.left = coordX;
	}

	function hide()
	{
		toolTipHolder.style.display = "none";
	}

	return {
		show: function(initObject)
		{
			show(initObject);
		},

		hide: function()
		{
			hide();
		},

		isVisible: function()
		{
			return (typeof toolTipHolder == "undefined" || toolTipHolder.style.display == "none") ? false : true;
		}
	}
}();
