//gobal var used to store the current x/y coords of the mouse
var x,y;
var x_min=0;
var x_max=680;//720;
var y_min=0;
var y_max=305;//525;

//log the mousemovement to handle the positioning of the popups
$(document).mousemove(
	function(event) {
		x = event.clientX;
		y = event.clientY;
	}
);

$(document).ready(function () { 	
	$('.popupContainer').each(function () {
		// setup options    
		var distance = 0;		//the distance the popup will float on show/hide
		var time = 150;		    //time to show
		var hideDelay = 1000;     //time after mouseout before the popup hides
		var hideDelayTimer = null;    
		
		// tracker    
		var beingShown = false;    
		var shown = false;        
		var popup = $('.popup', this);//.css('opacity', 0);   
		//var container = $(this); 
		
		//activate every help tag on page
		$('.glossary-term').each(function() {
			var trigger = $(this);
			// set the mouseover and mouseout on both element    		
			$([trigger.get(0), popup.get(0)]).mouseover(function () {      
				// stops the hide event if we move from the trigger to the popup element      
				if (hideDelayTimer) clearTimeout(hideDelayTimer);      
							
				// don't trigger the animation again if we're being shown, or already visible      
				if (beingShown || shown) {        
					return;      
				} else {        
					beingShown = true;   
					
					//set the popup class to be specific for this glossary term
					popup.attr('class','popup ' + trigger.text().replace(/ /g,'-'));	
					
														
					
					// get glossary key
					var glossaryKey;
					glossaryKey = $(this).text();
					if (glossaryKey.charAt(0) == '(')
						glossaryKey = glossaryKey.substring(1,glossaryKey.length);
					
					// set pop html from glossary dictionary using glossary key, if we're dealing with the proven glossary output though we need to detect which
					// version we want to output.
					if(glossaryKey == 'Proven') {
						var parentContainer = trigger.parents('dd.pe-glossary-finder');
						if(parentContainer.hasClass('warranty-2')) {
							glossaryKey = 'ProvenExtended';
						}
					}
					
					if($.glossary.getValue(glossaryKey)){
						var closeHtml = "<div class=close style = 'display: block; margin-left: 380px;'><img src='/assets/images/form_close.png' alt='close' /></div>";
						popup.html(closeHtml + $.glossary.getValue(glossaryKey));
					}	

					//ok we need to do some calculation now to subtract the height from the bottom coord of the iframe
					var y_coord, x_coord;
					if(trigger.html() == 'Approved') {
						x_coord = 280;
						y_coord = 198;
					}
					else {
						x_coord = ((x > x_min && x < x_max) ? x : (x > x_max) ? x_max : x_min)-140;
						y_coord = (y > y_min && y < y_max) ? y : (y > y_max) ? y_max : y_min;
					}								
												
					// reset position of popup box        
					popup.css({          
						top: y_coord,// - 120,
						left: x_coord,// - 140,
						display: 'block'
					});
				}    
			})
			
			$('.popup').click(function(){							       						      
						popup.css({
							display: 'none'
						});  
						shown = false;  
						beingShown = false;  					
			})		
			
				.mouseout(function () {      		
					// reset the timer if we get fired again - avoids double animations      
					if (hideDelayTimer) clearTimeout(hideDelayTimer);            
					
					// store the timer so that it can be cleared in the mouseover if required      
					hideDelayTimer = setTimeout(function () {        
						hideDelayTimer = null;        
						popup.css({
							display: 'none'
						});  
						shown = false;  
						beingShown = false;  
					}, hideDelay); 
				});																			
		});  
	});			
});

//function used to log ty
function SetValues()
{
    x = window.event.clientX;
    y = window.event.clientY ;
}  

//function used to get the y-coord of the help tip being requested
function getY( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) {
	iReturnValue += oElement.offsetTop;
	oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

//function used to get the x-coord of the help tip being requested
function getX( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) {
	iReturnValue += oElement.offsetLeft;
	oElement = oElement.offsetParent;
	}
	return iReturnValue;
}