/*
 * Generates css controlled hovertooltip 
 */


// set up Argos hovertooltip namespace
if (!argos) var argos = {};
if (!argos.tooltipmessage) argos.tooltipmessage = {};
if (!argos.tooltipmessage.hovertooltips) argos.tooltipmessage.hovertooltips = {};

argos.tooltipmessage.hovertooltipdisplay = {	
	
	init : function() {
	
		// shorten namespace
		var tooltipmessage = argos.tooltipmessage;
		var hovertooltipdisplay = argos.tooltipmessage.hovertooltipdisplay;
		var hovertooltips = argos.tooltipmessage.hovertooltips;
		var activeLink = null;
		var timeoutShowTooltip, timeoutHideTooltip;
		var tooltipContentExists;
		var prevLink, tempRel, tempAlt, tempTitle,tempParentAlt, tempParentTitle;
		
		
		
		// set tooltipGroupList to relevant bubble group object from jsp
		var tooltipGroupList = hovertooltips;
		
		//set content up, specify relevant tooltip group 
		tooltipmessage.setupTooltip(tooltipGroupList);
		

		
		$("." + tooltipmessage.SHOW_OBJECT).hover(
		
			// grab content to display	
			function(){
				// clear them if timeouts exist
				clearTimeout(timeoutShowTooltip);
				clearTimeout(timeoutHideTooltip);
				
				if (this != prevLink){
					prevLink = this;
					//failsafe - hide any existing hovertooltip if hitting different link
					$("div.lastViewedTooltip").hide();
					$("div.lastViewedTooltip").removeClass("lastViewedTooltip");
					
					//locate assign matching identifier (rel value) - remove tooltip content if changing
					if (tempRel != $(this).attr("rel")) {
						tempRel = $(this).attr("rel");
					}
				}

				// check page is supplying Rel values and tooltip id is correct form
				if ((typeof tooltipGroupList[tempRel] === "object") && (tooltipGroupList[tempRel].TOOLTIP_ID != null || undefined)) {
				
					// check if tooltip container exists
					tooltipContentExists = document.getElementById(tooltipGroupList[tempRel].TOOLTIP_ID);
			
					// if matching temp content container exists then proceed with position/display
					if (tooltipContentExists) {

						var newPosition = new tooltipmessage.tooltipPosition(this,tooltipGroupList,tempRel);
						// set last link clicked for re-positioning on window resize
						activeLink = this;
						
						tempAlt = $(this).attr("alt");
						tempTitle = $(this).attr("title");
						tempParentAlt = $(this).parent().attr("alt");	
						tempParentTitle = $(this).parent().attr("title");
						tempChildAlt = $(this).children().attr("alt");	
						tempChildTitle = $(this).children().attr("title");
						
						//remove
						$(this).attr("alt","");
						$(this).attr("title","");
						$(this).parent().attr("alt","");
						$(this).parent().attr("title","");
						$(this).children().attr("alt","");
						$(this).children().attr("title","");
						
						//set timeouts for tooltip display
						timeoutShowTooltip = setTimeout(
							function(){
								$("#" + tooltipGroupList[tempRel].TOOLTIP_ID).show();
								$("#" + tooltipGroupList[tempRel].TOOLTIP_ID).addClass("lastViewedTooltip");
								// if hovering on tooltip, reset display timeouts (keep alive)
								$("#" + tooltipGroupList[tempRel].TOOLTIP_ID).hover(
									function(){
									//	alert($("#" + tooltipmessage.TOOLTIP_ID).height());
										clearTimeout(timeoutShowTooltip);
										clearTimeout(timeoutHideTooltip);
										$("#" + tooltipGroupList[tempRel].TOOLTIP_ID).show();
									},
									function(){
									
										clearTimeout(timeoutShowTooltip);
										clearTimeout(timeoutHideTooltip);
										timeoutHideTooltip = setTimeout(function(){
											$("#" + tooltipGroupList[tempRel].TOOLTIP_ID).hide();
					   						
									
										},300);
									
									}
								);	
						},300);
					}
				}
			},
	
			function(){
				// if matching temp content container exists then proceed with hides
				if (tooltipContentExists) {
					//clear them if timeouts exist
   					clearTimeout(timeoutShowTooltip);
   					timeoutHideTooltip = setTimeout(function(){
   						$("#" + tooltipGroupList[tempRel].TOOLTIP_ID).hide();
   						
   					
						// only remove the temp content exists reference at this point when user is off the object for over 300ms
						// clear timeouts on tooltip message hover will keep the reference alive   
						tooltipContentExists = null;
   					},300);
   					//re-instate
   					$(activeLink).attr("alt",tempAlt);
   					$(activeLink).attr("title",tempTitle);
   					$(activeLink).parent().attr("alt",tempParentAlt);
	   				$(activeLink).parent().attr("title",tempParentTitle);
   					$(activeLink).children().attr("alt",tempChildAlt);
   					$(activeLink).children().attr("title",tempChildTitle);
				}
			}
		);
	}
}
