/*
Sweet Titles (c) Creative Commons 2005
http://creativecommons.org/licenses/by-sa/2.5/
Author: Dustin Diaz | http://www.dustindiaz.com
*/

var sweetTitles = {                                                     //left offset of the tooltip relative to to its parrent node;
	tipLeftOffset: 197,
	tipMapOffset: [20,-20],
	tipClass: 'tt',
	xCord : 0,								// @Number: x pixel value of current cursor position
	yCord : 0,								// @Number: y pixel value of current cursor position
	tipElements : ['a','div'],              // @Array: Allowable elements that can have the toolTip
	obj : Object,							// @Element: That of which you're hovering over
	tip : Object,							// @Element: The actual toolTip itself
	active : 0,								// @Number: 0: Not Active || 1: Active
	
	init : function() {
		if ( !document.getElementById ||
			!document.createElement ||
			!document.getElementsByTagName ) {
			return;
		}

		var i,j;
		this.tip = document.createElement('div');
		this.tip.id = 'toolTip';
		document.getElementsByTagName('body')[0].appendChild(this.tip);
		this.tip.style.top = '0';
		this.tip.style.visibility = 'hidden';
		var tipLen = this.tipElements.length;
		for ( i=0; i<tipLen; i++ ) {
			var current = getElementsByClass(this.tipClass,null,this.tipElements[i]);
			var curLen = current.length;
			for ( j=0; j<curLen; j++ ) {
				addEvent(current[j],'mouseover',this.tipOver);
				addEvent(current[j],'mouseout',this.tipOut);
				current[j].setAttribute('tip',current[j].title);

				if (current[j].tagName == 'DIV') {
				}
				else {					
					crd = findPos(current[j]);
					current[j].setAttribute('top',crd[1]);
				}
				
				current[j].removeAttribute('title');
			}
		}
	},
	
	updateXY : function(e) {
		if ( document.captureEvents ) {
			sweetTitles.xCord = e.pageX;
			sweetTitles.yCord = e.pageY;
		} else if ( window.event.clientX ) {
			sweetTitles.xCord = window.event.clientX+document.documentElement.scrollLeft;
			sweetTitles.yCord = window.event.clientY+document.documentElement.scrollTop;
		}
	},
	
	tipOut: function() {
		if ( window.tID ) {
			clearTimeout(tID);
		}
		if ( window.opacityID ) {
			clearTimeout(opacityID);
		}
		sweetTitles.tip.style.visibility = 'hidden';
	},
	
	checkNode : function() {
		var trueObj = this.obj;
		if ( this.tipElements.inArray(trueObj.nodeName.toLowerCase()) ) {
			return trueObj;
		} else {
			return trueObj.parentNode;
		}
	},
	
	tipOver : function(e) {
		sweetTitles.obj = this;
		tID = window.setTimeout("sweetTitles.tipShow()",0);
		sweetTitles.updateXY(e);
	},
	
	tipShow : function() {		
		var anch = this.checkNode();
		var addy = '';
		var access = '';
		if (anch.tagName == 'DIV') {			
			var crd = findPos(anch);
			this.tip.style.top = String(crd[1]*1+this.tipMapOffset[1]*1)+'px';
			
			this.tip.style.left = String(crd[0]*1+this.tipMapOffset[0]*1)+'px';
		} else {
			this.tip.style.left = this.tipLeftOffset+'px';
			this.tip.style.top = anch.getAttribute('top')+'px';
		}

		this.tip.innerHTML = "<p>"+anch.getAttribute('tip')+"</p>";
		this.tip.style.visibility = 'visible';
		this.tip.style.opacity = '.1';
		this.tipFade(10);
	},
	
	tipFade: function(opac) {
		var passed = parseInt(opac);
		var newOpac = parseInt(passed+10);
		this.tip.style.opacity = '1';
		this.tip.style.filter = "alpha(opacity:100)";

/* 2007-11-01
		if ( newOpac < 80 ) {
			this.tip.style.opacity = '.'+newOpac;
			this.tip.style.filter = "alpha(opacity:"+newOpac+")";
			opacityID = window.setTimeout("sweetTitles.tipFade('"+newOpac+"')",20);
		}
		else { 
			this.tip.style.opacity = '.80';
			this.tip.style.filter = "alpha(opacity:80)";
		}
		*/
		
	}
};

function pageLoader() {
	sweetTitles.init();
	objects = document.getElementsByTagName("object");
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function findXPos(obj) {
     var curtop = 0;
     if (obj.offsetParent) {
          curtop = obj.offsetTop
          while (obj = obj.offsetParent) {
               curtop += obj.offsetTop
          }
     }
     return curtop;
}

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
		}
	}
	return [curleft,curtop];
}

function getAreaCoords(id) {
  var xCoord = "";
  var yCoord = "";
  var areaObj = document.getElementById(id); 
  var pointCoords = areaObj.coords.split(',');


  var els = document.getElementsByTagName('img');
  var i = 0;
  var mapId;
  var mapImg;
  while (i < els.length) {
	mapId = els[i].getAttribute('usemap');
	if (mapId) {
		mapImg = els[i];
		break;}
	i=i+1;

  }    
	var imgCoords = findPos(mapImg);
	xCoord = pointCoords[0]*1 + imgCoords[0];
	yCoord = pointCoords[1]*1 + imgCoords[1];
    return [xCoord,yCoord];
}

function showDot(id) {
	var dotCoords = getAreaCoords(id);
	var dotId = 'dot-'+id;
	var dotObj = document.getElementById(dotId);
	dotObj.style.left = (dotCoords[0]-5)+'px';
	dotObj.style.top = (dotCoords[1]-5)+'px';
	dotObj.style.visibility = 'visible';
}

function hideDot(Id) {
	document.getElementById(Id).style.visibility = 'hidden';
}

function hideDot(Id) {
    els = getElementsByClass('tt');
	for (i=0; i<els.length; i++) {
		els[i].style.visibility = 'hidden';
	}
}

addEvent(window,'load',pageLoader);
