//////////////////////////////////////////////////////////////////
// qTip - CSS Tool Tips - by Craig Erskine
// http://qrayg.com | http://solardreamstudios.com
//
// Inspired by code from Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com
//////////////////////////////////////////////////////////////////



var qTipTag = "a"; //Which tag do you want to qTip-ize? Keep it lowercase!//
var qTipX = 7; //This is qTip's X offset//
var qTipY = 22; //This is qTip's Y offset//

//There's No need to edit anything below this line//
tooltip = {
  name : "qTip",
  offsetX : qTipX,
  offsetY : qTipY,
  tip : null
}

tooltip.init = function () {
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
	if(!tipContainerID){ var tipContainerID = "qTip";}
	var tipContainer = document.getElementById(tipContainerID);

	if(!tipContainer) {
	  tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);
	  document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}
	
	if (!document.getElementById) return;
	this.tip = document.getElementById (this.name);
	//if (this.tip) document.onmousemove = function (evt) {tooltip.move (evt)};

	var a, sTitle;

	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "event")){
			anchor.onclick = function() {popupTip(this.id); return false;};
			anchor.onfocus = function() {popupTip(this.id); return false;};
			//anchor.onblur = function() {tooltip.hide()};
		}
	}
	// Get the URL
    var currentURL = unescape(window.location);
	if (currentURL.indexOf('#')>-1)  {
        // Highlight the target
		var selDate = currentURL.substring(currentURL.indexOf('#')+1,currentURL.length);
		var strClassName = "date";
		strClassName = strClassName.replace(/\-/g, "\\-");
		var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
		if(oRegExp.test(selDate)) popupTip(selDate);
	}
	//document.getElementById('staffPhoto').className = 'on';
}

function popupTip(date) {
		date = document.getElementById(date);
		var cal_events = date.parentNode.parentNode.getElementsByTagName("dd");
		var cal_detail;
		for (var k=0; k<cal_events.length; k++){
			var this_event = cal_events[k];
			if(!cal_detail) cal_detail = "<div>"+this_event.innerHTML+"</div>";
			else cal_detail = cal_detail+"<div>"+this_event.innerHTML+"</div>";
		}
		//alert(cal_month+" "+cal_date+", "+cal_year+" - "+cal_detail);
		tooltip.align (date,"qTip");
		
		// get date into from table caption
		var cal_caption = document.getElementById("cal-date");
		cal_caption = cal_caption.childNodes[1].nodeValue.split(" ");
		var cal_month = cal_caption[0];
		var cal_year = cal_caption[1];
		var cal_date = date.id.split('-')[1];
		
		tooltip.show("<h4>"+cal_month+" "+cal_date+", "+cal_year+"</h4>"+cal_detail);
		return false;
}

tooltip.align = function (obj,lyr)
{
	var coors = findPos(obj);
	if (lyr == 'testP') coors[1] -= 50;
	var x = document.getElementById(lyr);
	x.style.top = coors[1] + 'px';
	x.style.left = coors[0] + 'px';
}
function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
		curleft = curleft + qTipX;
		curtop = curtop + qTipY;
	}
	return [curleft,curtop];
}

tooltip.show = function (text) {
	if (!this.tip) return;
	var callout = '<img class="callout" src="/_images/event_callout.gif" alt="Callout" />'; // callout box image
	var closebutton = '<a href="#close-this" class="close-button" onclick="tooltip.hide()"><img src="/_images/event_close.gif" alt="Close" /></a>'; // close button
	this.tip.innerHTML = text+callout+closebutton;
	this.tip.style.display = "block";
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}

function initTooltip() {
	tooltip.init ();
}
addLoadEvent(initTooltip);
