// MRJ - this file will be replaced with new tooltip functionality. I have renamed it from tipLinkAPI.js
/* Files using:
./app/views/layouts/acc_settings.rhtml
./app/views/layouts/payment.rhtml
./app/views/layouts/tax_defer.rhtml
./app/views/layouts/tax_depreciate.rhtml
./app/views/login/_account_creation_form.rhtml
*/


/**
 * General function to show the tips
 * @param posx - the left postion of the link
 * @param posy - the top postion of the link
 * @param obj - the link element
 * @returns - none
 */
 setTimeout("document.onmousedown = closeToolTip;", 1000);
 var current_tt_handle = null;

function closeToolTip(){
 	if (current_tt_handle){
		current_tt_handle.close();
	}
}

function showTip(posx,posy,obj) {
	closeToolTip();
	var objectId=obj.id.toString();
	var tipcontent = "info" + objectId.substr(obj.className.length);  /* Classname is infoLink + category number. we want to extract the number */

	d = Element.getDimensions($(tipcontent));
	
	
	var height = $(tipcontent).offsetHeight;
	var isHover = (obj.name.indexOf("hover") != -1);
	var isCloseOnMouseOut = (obj.name.indexOf("close_on_mouse_out") != -1);
	if(isCloseOnMouseOut){
	  obj.onmouseout = function(){
	    try{myWin.close();}catch(e){}
	  }
	}

	if(obj.name.indexOf("open-left") != -1){
		height = obj.name.split(' ')[1];
		posx -=450;		
	}
	var hide_effect = (!isHover)? Effect.Shrink : Element.hide;
	var show_effect = (!isHover)? Effect.Appear : Element.show;
	var width = 450;
	if($(tipcontent).style.width){
		width = parseInt($(tipcontent).style.width);
	}
	var classname = new String(($(tipcontent).classNames() && $(tipcontent).classNames() != "")? $(tipcontent).classNames() : "trexglobal");
	if (classname.indexOf("drop_arrow") != -1){
	  posx = PMCommonObj.find_element_x_pos(obj) + (obj.offsetWidth/2) - (width/2)-5;

	  posy = PMCommonObj.find_element_y_pos(obj) + 20;
    if(navigator.appVersion.indexOf("MSIE 7.0") !=-1 ){
      posy-=10;
    };
	}
	
	myWin = new Window({className: classname, top: posy, left: posx, zIndex: 203, width: width, height: height,  minimizable: false, closable: true, maximizable: false, resizable: false, showEffect: show_effect, hideEffect: hide_effect, draggable:false, wiredDrag: false});


	myWin.setContent(tipcontent, false);
$(tipcontent).show();
	$(tipcontent).style.display="block";
	$(tipcontent).style.styleFloat="left";
	$(tipcontent).style.cssFloat="left";


	
	myWin.setCloseCallback(function(){
		$(tipcontent).style.display = "none";
		$('root').appendChild($(tipcontent)); 
		myWin = null;
		return true;
	})
	
	myWin.show();
	
	if($(tipcontent).style.zIndex){
		myWin.setZIndex($(tipcontent).style.zIndex);
	}
	myWin.toFront();
	$(tipcontent).onmouseover = function(){document.onmousedown = function(){}};
	$(tipcontent).onmouseout = function(){document.onmousedown = closeToolTip};
	current_tt_handle = myWin;
	if(isCloseOnMouseOut){
	  $$(".drop_arrow_close").each(function(el){
	    el.style.display = "none";
	  });
	}
}


/**
 * Hides the tip layer
 * @returns - none
 */
function hideTip(){
	document.getElementById('infoContainer').style.display="none";
}


/**
 * Attaches the onclick event to the link element and call a function
 * @returns - false : to prevent the scroll up
 */
function tipLinkClick() {
	var linkList= document.getElementsByTagName("a");
	for (var i=0;i<linkList.length;i++) {
		if(linkList[i].name.indexOf("hover") != -1){
			linkList[i].onmouseover = tipLinkOnClick;  			  
		}else if (linkList[i].className=="infoLink") {
			var tipcontent_obj = $("info" + linkList[i].id.substr(linkList[i].className.length));

			linkList[i].removeAttribute("alt");
			linkList[i].removeAttribute("title");
			linkList[i].href = "javascript://";
			
			linkList[i].onmouseover = function() {
			  tip_el = $("info" + this.id.substr(this.className.length));
				if (this.name == "open-left"){
					tooltip.offsetX = -300;
				}
				if (this.name == "open-top"){
					tooltip.offsetY = (parseInt(tip_el.style.height.replace(/px/g, ""))*-1)-20;
				}
				
				tooltip.show(tip_el);
			};
			linkList[i].onmouseout = function() {
				if (this.name == "open-left"){
					tooltip.offsetX = 0;
				}
				tooltip.hide();
			};
		}  
	}
	return false;	    
}

/**
 * Attaches the onclick event to the link element and call a function
 * @returns - false : to prevent the scroll up
 */
function tipLinkFocus() {
	var linkList= document.getElementsByTagName("input");
	for (var i=0;i<linkList.length;i++) {
		if (linkList[i].className=="infoLinkFocus") {
			linkList[i].onselect = tipLinkOnFocus;
		}  
	}
	return false;	    
}

function tipLinkOnFocus(e){
	if (!e)	{ var e = window.event;	}
	showTip(PMCommonObj.find_element_x_pos(this)+90,PMCommonObj.find_element_y_pos(this),this);
	return false;
}


/**
 * Starts finding the absolute postion of the link
 * @param {Event} e - the event (required for Mozilla)
 * @returns - false : to prevent the scroll up
 */
function tipLinkOnClick(e){
	if (!e)	{ var e = window.event;	}
	setCoordinates(e,this);	
	return false;
}


/**
 * Finds the absolute position of the link
 * @param {Object} obj - the link object
 * @param {Event} e - the event (required for Mozilla)
 * @returns - false : to prevent the scroll up
 */
function setCoordinates(e,obj) {
	var posx = 0;
	var posy = 0;	
	if (!e) { var e = window.event; }
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		// posx and posy contain the mouse position relative to the document
		posx 	= e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy 	= e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
	}
	showTip(posx,posy,obj);
}


/**
 * Attaches the onclick event to the help link element and call a function
 * @returns - none
 */
function helpLinkClick() {
	document.getElementById("helpLink").onclick = helpOnClick;
}


/**
 * Shows the help layer object
 * @returns - false (to prevent scrolling)
 */
function helpOnClick() {
	hideHelpContent ();	
	if (document.getElementById("help").style.display=="" || document.getElementById("help").style.display=="none") {
		document.getElementById("helpLink").style.background = "url('/images/arrow_lg_down.gif') left 2px no-repeat";
		document.getElementById("rightcolhelp").style.width="150px";
		document.getElementById("rightcolhelp").style.height="auto";
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
		 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 		 if (ieversion>=6 && ieversion < 7){
			document.getElementById("rightcolhelp").style.position="absolute";
			document.getElementById("rightcolhelp").style.left=$('main').style.right;
		 }
		}
		document.getElementById("help").style.display="block";
	}
	else if (document.getElementById("help").style.display=="block") {
		document.getElementById("helpLink").style.background = "url('/images/arrow_lg_right.gif') left 2px no-repeat";
		document.getElementById("help").style.display="none";
		document.getElementById("rightcolhelp").style.height="40px";
		document.getElementById("rightcolhelp").style.width="60px";
		document.getElementById("rightcolhelp").style.position = "static";
	}
	return false;
}


/**
 * Attaches the onclick event to the item link element and call a function
 * @returns - none
 */
function helpItemLink() {
	var linkList= document.getElementsByTagName("a");
	for(var i=0;i<linkList.length;i++) {	
		if(linkList[i].className=="helpItem") {
			linkList[i].onclick = helpItemClick;  		    
		}  
	}
}


/**
 * Shows/Hides the item layer
 * @returns - none
 */
function helpItemClick() {
	hideHelpContent();	
	var itemnumber = this.id.substr(this.className.length);
	var helpcontent= document.getElementById("helpContent"+itemnumber);
	if(helpcontent.style.display=="" || helpcontent.style.display=="none") {
		helpcontent.style.display = "block";
	}
	else {
		helpcontent.style.display="none";
	}
	return false;  
}


/**
 * Hides the help layer
 * @returns - none
 */
function hideHelpContent() {
	var i=1;
	while(document.getElementById("helpContent"+i)) {
		document.getElementById("helpContent"+i).style.display="none";
		i++;
	}
}


/* Add new event handlers functions onload */
Event.observe(window,"load",tipLinkClick);
Event.observe(window,"load",tipLinkFocus);
if (typeof tips_only == "undefined"){
  Event.observe(window,"load",function(){if($("rightcolhelp")){helpLinkClick();}});
  Event.observe(window,"load",helpItemLink);
}