/**
 * Tooltip component for jasmin and cobranded site-s
 * 
 * csak a myaccounthoz-hez hasznalhato
 * 
 * @author: duodecad team
 */
var toolTipComponent = function()
{
	var toolTipHolder;
	var text = "";
	var url1 = "";
	var url2 = "";
	var signup = "";
	var login = "";
	var cancel = "";
	//ez nem kotelezo, ha css-bol akarjuk allitani a box poziciojat
	var crdX = "";
	var crdY = "";
	
	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 class="tooltip_arrow">&nbsp;</div><div id="tooltip_textbox"><span id="toolTipText" class="text"></span><div id="buttons_box" class="buttons_box">';
		content += '<form id="tooltipForm" action="" method="post" onsubmit="return false;" ><input id="input1" type="submit" class="tooltip_buttons" value="'+ signup +'" onclick="" /><input id="input2" type="submit" class="tooltip_buttons" value="'+ login +'" onclick="" /><input type="button" class="tooltip_buttons" value="'+ cancel +'" onclick="toolTipComponent.hide(); return false;" /></form>';
		content += '</div></div>';
		
		toolTipHolder.innerHTML = content;
		toolTipHolder.style.display = "none";
				
		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;
		url1  = initObject.url1;
		url2  = initObject.url2;
		signup = initObject.button1;
		login = initObject.button2;
		cancel = initObject.button3;
		
		//css-bol pozicionaljuk az ablakot
		crdX = initObject.crdX;
		crdY = initObject.crdY;
				
		componentForm = document.getElementById("tooltipForm");
		
		buttonSignUp = componentForm.elements[0];
		buttonLogin = componentForm.elements[1];
		buttonCancel  = componentForm.elements[2];
					
		if (typeof initObject.text == "undefined" )
		{
			alert("Error: No text parameter defined!");	
			return false;
		}
		
		if (typeof initObject.url1 == "undefined" )
		{
			alert("Error: No post URL parameter defined!");	
			return false;
		}
		
		if (typeof initObject.url2 == "undefined" )
		{
			alert("Error: No post URL parameter defined!");	
			return false;
		}
		
		buttonSignUp.value  = (typeof initObject.signupBtnText != "undefined" ) ?  initObject.signupBtnText : signup;
		buttonLogin.value  = (typeof initObject.loginBtnText != "undefined" ) ?  initObject.loginBtnText : login;
		buttonCancel.value   = (typeof initObject.cancelBtnText  != "undefined" ) ?  initObject.cancelBtnText : cancel;
		
		if (typeof initObject.buttonsNeeded != "undefined" && initObject.buttonsNeeded == false) 
		{
			document.getElementById("toolTipFormHolder").innerHTML = "";
		}
				
		$("#input1, #input2").click(function() {
			if(this.id == "input1") {
				componentForm.setAttribute("action", url1);
			}
			else if(this.id == "input2") {
				componentForm.setAttribute("action", url2);
			}
		});
		
		if (navigator.appName == "Microsoft Internet Explorer")
		{
			buttonSignUp.detachEvent("onclick", handlerFunction);
		}
		else
		{
			buttonSignUp.removeEventListener("click", handlerFunction, false);
		}
		if (navigator.appName == "Microsoft Internet Explorer")
		{
			buttonLogin.detachEvent("onclick", handlerFunction);
		}
		else
		{
			buttonLogin.removeEventListener("click", handlerFunction, false);
		}
		
		if (typeof initObject.handlerFunction == "function")
		{
			componentForm.setAttribute("onsubmit", "return false;");
			
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				buttonSignUp.attachEvent("onclick", initObject.handlerFunction);
			}
			else
			{
				buttonSignUp.addEventListener("click", initObject.handlerFunction, false);
			}
			
			if (navigator.appName == "Microsoft Internet Explorer")
			{
				buttonLogin.attachEvent("onclick", initObject.handlerFunction);
			}
			else
			{
				buttonLogin.addEventListener("click", initObject.handlerFunction, false);
			}
			
			handlerFunction = initObject.handlerFunction;
		}
		else
		{
			// Firefox hack
			componentForm.removeAttribute("onsubmit");
			
			// IE hack
			componentForm.onsubmit = "";
		}
		
		//ha nem akarunk css-bol fix vizszintes koordinatat állítani
		if (typeof initObject.crdX == "undefined" )
		{
			coordX = (typeof initObject.x != "undefined" ) ?  initObject.x : mouse.x + "px";
			//atadjuk az erteket
			toolTipHolder.style.left = coordX;
		}
		
		//ha nem akarunk css-bol fix fuggoleges koordinatat adni
		if (typeof initObject.crdY == "undefined" )
		{
			//coordY = (typeof initObject.y != "undefined") ? initObject.y : mouse.y + "px";
			var coordm = 8;
			var coordy = (typeof initObject.y != "undefined") ? initObject.y : mouse.y;
			coordY = (coordy-coordm) + "px";
			//atadjuk az erteket
			toolTipHolder.style.top  = coordY;
		}
		
		document.getElementById("toolTipText").innerHTML = text;
		toolTipHolder.style.display = "block";
	}
	
	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;
		} 
	}
}();
