/* fool tooltip.js - based in part on lightbox.js by huddletogether.com */

Event.observe(window, 'load', requestToolTips, false);

// create tooltip class
var tooltip = Class.create();
tooltip.prototype = {

	initialize: function(ctrl) {
		this.trigger = ctrl.id;
		if (this.trigger == '') { alert('A tooltip trigger link has no ID.'); }
		if (ctrl.rel == null) {
			this.content = ctrl.alt;
			Event.observe(ctrl, 'focus', this.activate.bindAsEventListener(this), false);
			Event.observe(ctrl, 'blur', this.deactivate.bindAsEventListener(this), false);
			}
		else {
			this.content = ctrl.rel;
			Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
			}
		ctrl.onclick = function(){return false;};
		},

	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
  	var isIE = false;
    var IEVersion = 0;
    var detect = navigator.userAgent.toLowerCase();
    
    if(detect.indexOf("msie") >= 0) {
      isIE = true;
      IEVersion = parseInt(detect.substring(detect.indexOf("msie")+5));
    }
    
    if (isIE && IEVersion < 7)
    {
		  selects = document.getElementsByTagName('select');
		  for(i = 0; i < selects.length; i++) {
			  selects[i].style.visibility = visibility;
		  }
		}
	},

	getTriggerPosition: function() {
		triggerPosition = Position.positionedOffset($(this.trigger));
		// horiz
		this.triggerPositionX = triggerPosition[0];
		// alert(this.triggerPositionX);
		// vert
		this.triggerPositionY = triggerPosition[1];
		// alert(this.triggerPositionY);
		},

	actions: function(){
		ttActions = document.getElementsByClassName('ttAction');
		for(i = 0; i < ttActions.length; i++) {
			Event.observe(ttActions[i], 'click', this[ttActions[i].rel].bindAsEventListener(this), false);
			ttActions[i].onclick = function(){return false;};
		}
	},		

	activate: function(direction) {
		this.getTriggerPosition();
		$(this.content).style.position = "absolute";
		$(this.content).style.width = "300px";
		leftClass = "LeftSide";
		rightClass = "RightSide";
		
		windowWidth = document.body.clientWidth;
		windowMaximum = this.triggerPositionX+320;
		
		if(windowMaximum > windowWidth) {
			direction = "left";
			}
		
		switch(direction) {
			case "left":
				// left of link
				$(this.content).style.left = (this.triggerPositionX-315)+"px";
				$(this.content).style.top = (this.triggerPositionY-8)+"px";
				$(this.content).addClassName(leftClass);
				break;
			default:
				// right of link
				$(this.content).style.left = (this.triggerPositionX+20)+"px";
				$(this.content).style.top = (this.triggerPositionY-8)+"px";
				$(this.content).addClassName(rightClass);
				break;
			}
		hideAll();
		this.hideSelects('hidden');
		$(this.content).style.display = "block";
		this.actions();		
		},

	deactivate: function() {
		$(this.content).style.display = "none";
		this.hideSelects('visible');
		}
	}
	
// Onload, make all links that need to trigger a lightbox active
function initializeToolTips(){
	extraMarkup();

	triggers = $$('.ttTrigger');
	for(i = 0; i < triggers.length; i++) {
		valid = new tooltip(triggers[i]);
		triggers[i].style.display = 'block';
	}
}

function hideAll() {
	tooltips = $$('div.GlossaryTooltip');
	for(i = 0; i < tooltips.length; i++) {
		tooltips[i].style.display = "none";
		}
	}

function extraMarkup(){
	}
	

function requestToolTips()
{
  var pagepath = location.pathname;

  new Ajax.Request("/AJAX/LazyTooltips.aspx", {
    method: "get", 
    parameters: {path: escape(pagepath)},
    onSuccess: parseTootltipData});
}

function parseTootltipData(transport)
{
  var response = transport.responseText
  var tooltips = eval("(" + response + ")");
  
  var glossaryDiv;
  var anchor;
  
  
  
  for (i=0; i < tooltips.length; i++)
  {
  
    glossaryDiv = document.createElement("div");
    glossaryDiv.id = tooltips[i].Id;
    glossaryDiv.className = "Tooltip GlossaryTooltip";
    
    glossaryDiv.innerHTML = '<div class="TooltipInternal"><p>' + tooltips[i].Text + '</p><p class="CloseLink"><a href="#" class="ttAction" rel="deactivate">Close</a></p></div>';
    
    anchor = $(tooltips[i].Anchor);
    if (anchor) {
    anchor.appendChild(glossaryDiv);
    }
  }
  
  if (window.initializeToolTips)
  {
    initializeToolTips();
  }
}
