//////////////////////////////////////////////////////////////////
// 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 = -145; //This is qTip's X offset//
var qTipY = 35; //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 (evt) {
				var cal_date = this.firstChild.innerHTML;
				var cal_events = this.getElementsByTagName("span");
				var cal_detail;
				for (var k=0; k<cal_events.length; k++){
					var this_event = cal_events[k];
					if(!cal_detail) cal_detail = "<p>"+this_event.innerHTML+"</p>";
					else cal_detail = cal_detail+"<p>"+this_event.innerHTML+"</p>";
				}
				//alert(cal_month+" "+cal_date+", "+cal_year+" - "+cal_detail);
				tooltip.align (this,"qTip");
				tooltip.show("<h4>"+cal_month+" "+cal_date+", "+cal_year+"</h4>"+cal_detail);
				return false;
			}*/
			anchor.onclick = function() {popupTip(this.id)};
			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);
		if(selDate != "content" && selDate != "open") popupTip(selDate);
	}
	//document.getElementById('staffPhoto').className = 'on';
}

function popupTip(date) {
		date = document.getElementById(date);
		var cal_date = date.firstChild.innerHTML;
		var cal_events = date.getElementsByTagName("span");
		var cal_detail;
		for (var k=0; k<cal_events.length; k++){
			var this_event = cal_events[k];
			if(!cal_detail) cal_detail = "<p>"+this_event.innerHTML+"</p>";
			else cal_detail = cal_detail+"<p>"+this_event.innerHTML+"</p>";
		}
		//alert(cal_month+" "+cal_date+", "+cal_year+" - "+cal_detail);
		tooltip.align (date,"qTip");
		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";
}

window.onload = function () {
	tooltip.init ();
}

