// uniTip - written by Nathan Ford for Unit Interactive
//
// uniTip is based on qTip:
// qTip - CSS Tool Tips - by Craig Erskine
// http://qrayg.com

var uniTipTag = "th,a,div,dd"; //Which tag do you want to uniTip-ize? Keep it lowercase. No spaces around commas.//
var uniTipClass = ""; //Which classes do you want to uniTip-ize? If you leave this blank, then all the tags designated above will get uniTip-ized. Match case. No spaces around commas.

var uniTipX = -55; // X offset from cursor//
var uniTipY = 10; // Y offset from cursor//

//______________________________________________There's no need to edit anything below this line//

var offsetX = uniTipX, offsetY = uniTipY, elewidth = null, eleheight = null, tipid = null, tiptop = null, tipbot = null, tipcapin=null, tippointin=null, altText=false;

var x=0, y=0, WinWidth=0, WinHeight=0, TipWidth=0, TipHeight=0, CapHeight=0, PointHeight=0;

var ActiveElements = new Array();

// first, find all the correct elements
unitipInit = function () {
    if(document.getElementById("unitipmid")){
	    document.getElementById("unitipmid").innerHTML = "";
	    document.onmousemove = '';
	    if(tipid){
	        if(tipid.style.display != "none"){
	            document.body.removeChild(tipid);
	            tipid.style.display = "none";
	        }
	    }
	}
	addMouseEventsBool = false;
    if(readCookie("helpOn")==null)createCookie("helpOn", "true", 10000);
    if(readCookie("invHelpOn")==null)createCookie("invHelpOn", "false", 10000);
    // check/uncheck checkbox
    var checkBoxFound = false;
    for(var i=1; i<5; i++){
        if($get(NavID+"_HelpTips"+i)){
                if(UsrAccTp==0){$get("HelpTipsCheckBox"+i).checked = (readCookie("helpOn")=="true")?true:false;}
                else if(UsrAccTp==1){$get("HelpTipsCheckBox"+i).checked = (readCookie("invHelpOn")=="true")?true:false;}
                addMouseEventsBool = $get("HelpTipsCheckBox"+i).checked;
                checkBoxFound = true;
        }
    }
    if(!checkBoxFound && (typeof(showTipsWithoutCheckbox)!="undefined" && !showTipsWithoutCheckbox))
        return false;
    if(typeof(showTipsWithoutCheckbox)!="undefined" && showTipsWithoutCheckbox)addMouseEventsBool = true;
	var elementList = uniTipTag.split(",");
	for(var j = 0; j < elementList.length; j++) {	
		var elements = document.getElementsByTagName(elementList[j]);
		if(elements) {
			for (var i = 0; i < elements.length; i ++) {
				if (uniTipClass != '') {
				
					var elClass = elements[i].className;
					var elClassList = uniTipClass.split(",");
					
					for (var h=0; h < elClassList.length; h++) { if (elClass.match(elClassList[h])) unitipize(elements[i]); }
					
				} else {
				    if(elements[i].getAttribute("helptip")!=null || elements[i].getAttribute("helptip")!='')unitipize(elements[i], addMouseEventsBool);
				}
			}
		}
	}
}

unitipUnInit = function(){
	for(var j = 0; j < ActiveElements.length; j++) {	
		ActiveElements[j].onmouseover = null;
		ActiveElements[j].onmouseout = null;
	}
},

// next, add the tooltip function to those elements
unitipize = function (element, addMouseEventsBool) {
	var a = element;
	altText = (a.alt && a.getAttribute("alt") != '' ) ? true : false;
	var sTitle = (altText == true) ? a.getAttribute("alt") : a.getAttribute("helptip");	
	if(sTitle) {
	    if(addMouseEventsBool){
		    a.onmouseover = function() {buildUnitip(a, sTitle);};
		    a.onmouseout = function() {hideUnitip(a, sTitle);};
		    ActiveElements.push(a);
		}else{
		   a.onmouseover = null;
		   a.onmouseout = null;
		   a.title = "";
		}
	}
}

toggleUnitip = function(){
    if(UsrAccTp==0) // PLAYER
    {
        if(readCookie("helpOn")==null)createCookie("helpOn", "true", 10000);
        if(readCookie("helpOn")=="true"){createCookie("helpOn", "false", 10000)}
        else{ createCookie("helpOn", "true", 10000);}
    }
    else if(UsrAccTp==1) // INVESTOR
    {
        if(readCookie("invHelpOn")==null)createCookie("invHelpOn", "true", 10000);
        if(readCookie("invHelpOn")=="true"){ createCookie("invHelpOn", "false", 10000);}
        else{createCookie("invHelpOn", "true", 10000);}
    }
    unitipInit();
}


findParentElement = function(element, nodeType){

    if(element == null) return null;

    var tag = element.tagName.toLowerCase() ;

    if(tag == "html")return null;//top of the DOM tree

    if(tag == nodeType)return element;

    return findParentElement(element.parentNode, nodeType);

}

// now, we build the tooltip
buildUnitip = function (a, sTitle) {
	
	if (a.title) a.title = "";
	if (altText==true) a.alt = "";
	
	var tipContainer = document.createElement("div");
	tipContainer.setAttribute("id", "unitip");
	document.body.appendChild(tipContainer);
	
	var tipContainerTop = document.createElement("div");
	tipContainerTop.setAttribute("id", "unitippoint");
	tipContainer.appendChild(tipContainerTop);
	
	var tipContainerMid = document.createElement("div");
	tipContainerMid.setAttribute("id", "unitipmid");
	tipContainer.appendChild(tipContainerMid);
	
	var tipContainerBot = document.createElement("div");
	tipContainerBot.setAttribute("id", "unitipcap");
	tipContainer.appendChild(tipContainerBot);

	tipid = document.getElementById("unitip");
	tippoint = document.getElementById("unitippoint");
	tipmid = document.getElementById("unitipmid");
	tipcap = document.getElementById("unitipcap");
	
	var helpTipTitle = "";
	var helpTipMessage = "";
	helpTipArr = sTitle.split(":::");
	helpTipTitle = helpTipArr[0];
	if(helpTipArr[1])helpTipMessage = helpTipArr[1];
	tipmid.innerHTML = "<div id='unitiptitle'>"+helpTipTitle+"</div>";
	tipmid.innerHTML += "<div id='unitipmessage'>"+helpTipMessage+"</div>";
	tipid.style.display = "block";
	
	elewidth = document.getElementById("unitipmid").offsetWidth;
	eleheight = document.getElementById("unitip").offsetHeight;
	
	WinWidth = document.body.offsetWidth;
	WinHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;	
	
	CapHeight = document.getElementById('unitipcap').offsetHeight;
	PointHeight = document.getElementById('unitippoint').offsetHeight;
	
	if (typeof pngfix=="function") { // if IE, rebuilds wraps unitippoint and unitipcap in outer div
		if (tippoint.currentStyle.backgroundImage.match(/\.png/gi)) {
			var tipP = tippoint.innerHTML;
			
			tippoint.id = 'unitipP'; // switch unitippoint to outer div
			
			tippoint.style.overflow = "hidden";
			tippoint.style.height = PointHeight + "px";
			tippoint.style.width = elewidth + "px";
			tippoint.style.position = "absolute";
			tippoint.style.display = "block";
			
			tippoint.innerHTML = '<div id="unitippoint">' + tipP + '</div>'; // inject unitippoint
			
			tippointin = document.getElementById("unitippoint");  // redefine styles for unitippoint to fit filter image
			tippointin.style.width = (elewidth * 2) + "px";
			tippointin.style.height = (PointHeight * 2) + "px";
			tippointin.style.position = "absolute";
			tippointin.style.backgroundImage = tippoint.style.backgroundImage;
			
			tippoint.style.backgroundImage = "none";
		}
		if (tipcap.currentStyle.backgroundImage.match(/\.png/gi)) {
			var tipC = tipcap.innerHTML;
			
			tipcap.id = 'unitipC';
			
			tipcap.style.overflow = "hidden";
			tipcap.style.height = CapHeight + "px";
			tipcap.style.width = elewidth + "px";
			tipcap.style.position = "relative";
			tipcap.style.display = "block";
			
			tipcap.innerHTML = '<div id="unitipcap">' + tipP + '</div>';
			
			tipcapin = document.getElementById("unitipcap");
			tipcapin.style.height = (CapHeight * 2) + "px";
			tipcapin.style.position = "absolute";
			tipcapin.style.backgroundImage = tipcap.style.backgroundImage;
			
			tipcap.style.backgroundImage = "none";
		}
		
		pngfix(); // png fix
	}
	document.onmousemove = function (evt) {moveUnitip (evt, a)};
}

// now, we track the mouse and make the tooltip follow
moveUnitip = function (evt, a) {
	
	if (window.event) {
        //parentDiv = findParentElement(a, "div");
		x = window.event.clientX;
		y = window.event.clientY;
		
		if (document.documentElement.scrollLeft) tipid.style.left = (TipWidth >= WinWidth ) ? ((x - offsetX - elewidth) + document.documentElement.scrollLeft) + "px" :  (x + offsetX + document.documentElement.scrollLeft) + "px";
		else tipid.style.left = (TipWidth >= WinWidth ) ? ((x - offsetX - elewidth) + document.body.scrollLeft) + "px" :  (x + offsetX + document.body.scrollLeft) + "px";
		
		if (document.documentElement.scrollTop) tipid.style.top = (TipHeight >= WinHeight) ? ((y - offsetY - eleheight) + document.documentElement.scrollTop) + "px" : (y + offsetY + document.documentElement.scrollTop) + "px";
		else tipid.style.top = (TipHeight >= WinHeight) ? ((y - offsetY - eleheight) + document.body.scrollTop) + "px" : (y + offsetY + document.body.scrollTop) + "px";
		
		
	} else {
		x = evt.clientX;
		y = evt.clientY;	
		
		tipid.style.left = (TipWidth >= WinWidth ) ? ((x - offsetX - elewidth) + window.scrollX) + "px" :  (x + offsetX + window.scrollX) + "px";
		tipid.style.top = (TipHeight >= WinHeight) ? ((y - offsetY - eleheight) + window.scrollY) + "px" : (y + offsetY + window.scrollY) + "px";
	}
	
	TipWidth = x + elewidth + 20;
	TipHeight = y + eleheight + 20;
	
	if (TipHeight >= WinHeight ) { // rearrange the inner divs [123 to 321]
		tipid.removeChild(tippoint);
		tipid.removeChild(tipmid);
		tipid.removeChild(tipcap);
		tipid.appendChild(tipcap);
		tipid.appendChild(tipmid);
		tipid.appendChild(tippoint);
	} else {  // rearrange the inner divs [321 to 123]
		tipid.removeChild(tippoint);
		tipid.removeChild(tipmid);
		tipid.removeChild(tipcap);
		tipid.appendChild(tippoint);
		tipid.appendChild(tipmid);
		tipid.appendChild(tipcap);
	}
	
	if (TipHeight >= WinHeight) {
		
		if (document.getElementById('uniTipP')) {
			tippointin.style.left = (TipWidth >= WinWidth ) ? "-" + elewidth + "px" : "0px";
			tippointin.style.top = "-" + PointHeight + "px";
		} else tippoint.style.backgroundPosition = (TipWidth >= WinWidth ) ? "right bottom" : "left bottom";
		
		if (document.getElementById('uniTipC')) tipcapin.style.top = "-" + CapHeight + "px";
		else tipcap.style.backgroundPosition = "0 -" + CapHeight + "px";
		
	} else {
		
		if (document.getElementById('uniTipP')) {
			tippointin.style.left = (TipWidth >= WinWidth ) ? "-" + elewidth + "px" : "0px";
			tippointin.style.top = "0px";
		} else tippoint.style.backgroundPosition = (TipWidth >= WinWidth ) ? "right top" : "left top";
		
		if (document.getElementById('uniTipC')) tipcapin.style.top = "0px";
		else tipcap.style.backgroundPosition = "0 0";
		
	}
}

// lastly, hide the tooltip
hideUnitip = function (a, sTitle) {
    if(!tipid || !document.getElementById("unitipmid"))return false;
	document.getElementById("unitipmid").innerHTML = "";
	document.onmousemove = '';
	if(tipid.style.display != "none"){
	    document.body.removeChild(tipid);
	    tipid.style.display = "none";
	}
	if (altText==false) a.setAttribute("title", sTitle);
	else a.setAttribute("alt", sTitle);
	altText=false;
}

readCookie = function(name)

{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;

}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


// add the event to the page
if (window.addEventListener) window.addEventListener("load", unitipInit, false);
if (window.attachEvent) window.attachEvent("onload", unitipInit);
