/*
 * Generates css controlled tooltip 
 */

if (!argos) var argos = {};

argos.tooltipmessage = {

	//properties
	TOOLTIP_ID : "defaultToolTip", 
	TOOLTIP_CLASS : "defaultTooltip",
	SUB_CONTAINER : "tooltipsub",
	DEFAULT_ALIGNED_CLASS : "tooltipright",
	ALT_ALIGNED_CLASS : "tooltipleft",
	SHOW_OBJECT : "tooltipmessageobject", 
	MOVE_EXISTING_CONTAINER : false, //should be set as false by default
	HTML_INCLUDE : "/includedHTML/tooltipmessages/tooltipMessages.htm",
	TOOLTIPS_CONTAINER : "tooltipMessages",
	SHIFT_LEFT : 0, 	
	SHIFT_RIGHT : 0,	
	SHIFT_VERTICAL  : 0,	
	BASE_POSITION : "below",
	TOOLTIP_HTML : null,
		
	setupTooltip : function(obj) {
	
		// shorten namespace
 		var tooltipmessage = argos.tooltipmessage;
		var tooltipGroupList = obj;
		var strHTML;
		
		var containerExists = $("body").find("#"+tooltipmessage.TOOLTIPS_CONTAINER)[0];
		if(!containerExists){
			$("body").append('<div id="'+tooltipmessage.TOOLTIPS_CONTAINER+'"></div>');
		}	
		
		
		
		//locate relevant tooltip links on page
		for(var relValue in tooltipGroupList){
			
			if($("body").find("." + tooltipmessage.SHOW_OBJECT + "[rel="+relValue+"]")[0]) {
			
				if(!$("#"+tooltipmessage.TOOLTIPS_CONTAINER).find("#"+tooltipGroupList[relValue].TOOLTIP_ID)[0]){
			
					var newTooltip = new function() {
						
						// set construct options
						this.TOOLTIP_ID = !(tooltipGroupList[relValue].TOOLTIP_ID  == null || undefined) ? tooltipGroupList[relValue].TOOLTIP_ID : tooltipmessage.TOOLTIP_ID;	
						this.TOOLTIP_CLASS = !(tooltipGroupList[relValue].TOOLTIP_CLASS == null || undefined) ? tooltipGroupList[relValue].TOOLTIP_CLASS : tooltipmessage.TOOLTIP_CLASS;	
						this.TOOLTIP_HTML = !(tooltipGroupList[relValue].TOOLTIP_HTML == null || undefined) ? tooltipGroupList[relValue].TOOLTIP_HTML : tooltipmessage.TOOLTIP_HTML;	
						this.MOVE_EXISTING_CONTAINER = !(tooltipGroupList[relValue].MOVE_EXISTING_CONTAINER  == null || undefined) ? tooltipGroupList[relValue].MOVE_EXISTING_CONTAINER : tooltipmessage.MOVE_EXISTING_CONTAINER ;		

						// if we  not using an HTML file
						if(this.TOOLTIP_HTML == null) {
						
							if(this.MOVE_EXISTING_CONTAINER) {
								$("#"+this.TOOLTIP_ID).addClass(this.TOOLTIP_CLASS);
								//append html 
								$("#"+this.TOOLTIP_ID).appendTo("#"+tooltipmessage.TOOLTIPS_CONTAINER);
							
							}	else {
								// use the default bubble html							
							
								strHTML = '<div id="'+this.TOOLTIP_ID+'" class="'+this.TOOLTIP_CLASS+'">';
								strHTML += '<div class="'+tooltipmessage.SUB_CONTAINER + ' ' + tooltipmessage.DEFAULT_ALIGNED_CLASS +'" >';
								strHTML += '<div class="top"></div><div class="base"></div>';
								strHTML += '<div class="body"><div class="content"></div></div>';
								strHTML += '</div></div>';
							
								//append html 
								$("#"+tooltipmessage.TOOLTIPS_CONTAINER).append(strHTML);
							
								//loop through / insert content for each tooltip if provided
								for(var textValue in tooltipGroupList[relValue].DISPLAY_TEXT){
									$("#" + this.TOOLTIP_ID + " div.content").append(tooltipGroupList[relValue].DISPLAY_TEXT[textValue]);
								}	
							}
						}	else {
							//take file name and get content
							$.get(this.TOOLTIP_HTML, function(data){
								$("#"+tooltipmessage.TOOLTIPS_CONTAINER).append(data);
							});
					
						}			
	
					} 
				}
			}
		}	
		
	},
	

	tooltipPosition : function(link,obj,tempRel) {
 		
		// shorten namespace
 		var tooltipmessage = argos.tooltipmessage;
 		
 		var tooltipGroupList = obj;
 		 		
 		this.TOOLTIP_ID = tooltipGroupList[tempRel].TOOLTIP_ID;
 		this.BASE_POSITION = !(tooltipGroupList[tempRel].BASE_POSITION == null || undefined) ? tooltipGroupList[tempRel].BASE_POSITION : tooltipmessage.BASE_POSITION;
		this.SHIFT_VERTICAL = !(tooltipGroupList[tempRel].SHIFT_VERTICAL == null || undefined) ? tooltipGroupList[tempRel].SHIFT_VERTICAL : tooltipmessage.SHIFT_VERTICAL;
		this.SHIFT_LEFT = !(tooltipGroupList[tempRel].SHIFT_LEFT == null || undefined) ? tooltipGroupList[tempRel].SHIFT_LEFT : tooltipmessage.SHIFT_LEFT;
		this.SHIFT_RIGHT = !(tooltipGroupList[tempRel].SHIFT_RIGHT == null || undefined) ? tooltipGroupList[tempRel].SHIFT_RIGHT : tooltipmessage.SHIFT_RIGHT;
	 		
		//establish base coords
		var posX = tooltipmessage.getPageX(link);
 		var posY = tooltipmessage.getPageY(link);
 		var posW = $(link).width();
 		var posH = $(link).height();
		var tooltipObject = document.getElementById(this.TOOLTIP_ID);
 		var tooltipWidth = $(tooltipObject).width();
 		var tooltipHeight = $(tooltipObject).height();
 		
 		//force the css height so that it remains inline
 		$(tooltipObject).css('height',tooltipHeight);
 			
 		posX += (posW/2);
 		posY += posH;
 		
 		if(this.BASE_POSITION == "above") {
			posY = (tooltipmessage.getPageY(link) - tooltipHeight);	
 		}

 		posY += this.SHIFT_VERTICAL;
 		
 		// x position - two tests for IE6 vs all other browsers 
 		var leftClearance = document.documentElement.clientWidth||window.innerWidth - posX;
 		
 		// if tooltip will not fit rightwards
		if ((leftClearance - posX) < tooltipWidth) {
			// set the tooltip to the left of link
			tooltipmessage.toggleLeftRight(tooltipmessage.ALT_ALIGNED_CLASS,tooltipmessage.DEFAULT_ALIGNED_CLASS,this.TOOLTIP_ID);
			tooltipObject.style.left = posX + this.SHIFT_RIGHT - tooltipWidth + "px";
 		} else  { 
			// default - set tooltip rightwards
			tooltipmessage.toggleLeftRight(tooltipmessage.DEFAULT_ALIGNED_CLASS,tooltipmessage.ALT_ALIGNED_CLASS,this.TOOLTIP_ID);
			
			//explicitly compensate for ff posX alignement bug in comparison table view only (
		/*	if (($.browser.mozilla) && ($("body").hasClass("productcompare"))) {
			
				//adjust X		
				var ffWidthAdjust = ($(window).width() - $("#outerwrap").width());
				if (ffWidthAdjust >= 2) {
					posX = (posX+(ffWidthAdjust/2));
				}
			}
		*/
			tooltipObject.style.left = posX - this.SHIFT_LEFT + "px";
		}
		
		// y position
		//explicitly compensate for ff posY alignement bug in comparison table view only
		/*if(($.browser.mozilla) && ($("body").hasClass("productcompare"))) {
			//adjust y		
			posY = (posY+15);				 				 
		}
	*/
		tooltipObject.style.top = posY + "px"
		
		//apply iframe if IE6 - reinforce check
		var IE6 = ($.browser.msie && parseInt($.browser.version) == 6);
		
		if(IE6) {
			$("#" + this.TOOLTIP_ID).bgiframe(); 
		}
		/*
		console.group("POS");
		console.log("psoX: " + posX);
		console.log("psoY: " + posY);
		console.log("psoW: " + posW);
		console.log("psoH: " + posH);
		console.log("width: " + tooltipWidth);
		console.log("height: " + tooltipHeight);
		console.groupEnd();
			*/	
	
	},
	
	toggleLeftRight : function(aclass,rclass,tooltip) {
		// shorten namespace
 		var tooltipmessage = argos.tooltipmessage;
 		var tooltipObject = document.getElementById(tooltip);
 		
		$("#" + tooltip + " ." + tooltipmessage.SUB_CONTAINER).addClass(aclass);
		$("#" + tooltip + " ." + tooltipmessage.SUB_CONTAINER).removeClass(rclass);
	},
  	
  	//PPK's find element position (sets offset relative to page, not container)
	getPageX : function(obj) {
		var curleft = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {curleft += obj.offsetLeft;obj = obj.offsetParent;}
		}
		else if (obj.x) curleft += obj.x;
		return curleft;
	},

	getPageY : function(obj) {
		var curtop = 0;
		if (obj.offsetParent) {
			while (obj.offsetParent) {curtop += obj.offsetTop;obj = obj.offsetParent;}
		}
		else if (obj.y) curtop += obj.y;
		return curtop;
	}
	

}
