/* truth @ publicpress */
/* http://creativecommons.org/licenses/by-sa/2.5/ */
/* inspired and lots of code from dustian diaz sweetTitles */
/* modified to remove fade, supports a tipDivId that points to a hidden div containing popup contents */
/* fixed a bunch of flip bugs in IE */
/* changed to track the cursor */


/*
 * 
Sweet Titles (c) Creative Commons 2005
http://creativecommons.org/licenses/by-sa/2.5/
Author: Dustin Diaz | http://www.dustindiaz.com
*/
var sweetTitles = { 
	xCord : 0,								// @Number: x pixel value of current cursor position
	yCord : 0,								// @Number: y pixel value of current cursor position
	tipElements : ['a','input','abbr','acronym'],	// @Array: Allowable elements that can have the toolTip
	tipClasses : ['contribute','userLink'],
	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
	addTitle : function(element,divId) {
		addEvent(element,'mouseover',this.tipOver);
		element.setAttribute('tip',element.title);
		element.removeAttribute('title');
		if(divId!=null) {
			var thetip = document.getElementById(divId);
			element.tipDiv = thetip;
		}
		if(element.getAttribute('tipWidthUnbound')=='true') {
			element.tipWidthUnbound = true;
		}
		
		if(true || !(element.getAttribute('hideTip')=='false')) {
			addEvent(element,'mouseout',this.tipOut);
			addEvent(element,'mousemove',this.tipMouseMove);
		} else {
			addEvent(element,'mouseout',this.tipTimeout);
		}
	},
	checkAndAdd : function(element) {
		var tipIt = false;

		if(element!=null && element.getAttribute) {
			var divId = element.getAttribute('tipDivId');
			
			if((divId!=null && divId!='') || (element.getAttribute('title')!=null && element.getAttribute('title')!='')) {
				tipIt = true;
			} else {
				for(k=0;k<this.tipClasses.length;k++) {
					if(this.tipClasses[k]==element.className && element.getAttribute('title')!=null) {
						tipIt = true;
						break;
					}
				}
			} 
	
			if(tipIt) {
				this.addTitle(element,divId);
			}
		}

	},
	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);
		addEvent(document.getElementsByTagName('body')[0],'click',this.tipOut);
		addEvent(this.tip,'mouseout',this.tipTimeout);
		addEvent(this.tip,'mouseover',this.tipClearTimeout);
		this.tip.style.top = '0';
		this.tip.style.visibility = 'hidden';
		var tipLen = this.tipElements.length;
		for ( i=0; i<tipLen; i++ ) {
			var current = document.getElementsByTagName(this.tipElements[i]);
			var curLen = current.length;
			for ( j=0; j<curLen; j++ ) {
				this.checkAndAdd(current[j]);
			}
		}
	},
	findInnerSize : function() {
		var x,y;
		if (self.innerHeight) // all except Explorer
		{
			x = self.innerWidth;
			y = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}		
		return [x,y];
	},
	findScrollingOffset : function() {
		var x,y;
		if (self.pageYOffset) // all except Explorer
		{
			x = self.pageXOffset;
			y = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
			// Explorer 6 Strict
		{
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}		
		return [x,y];
	},
	findPage : function() {
		var x,y;
		var test1 = document.body.scrollHeight;
		var test2 = document.body.offsetHeight
		if (test1 > test2) // all but Explorer Mac
		{
			x = document.body.scrollWidth;
			y = document.body.scrollHeight;
		}
		else // Explorer Mac;
		     //would also work in Explorer 6 Strict, Mozilla and Safari
		{
			x = document.body.offsetWidth;
			y = document.body.offsetHeight;
		}		
		return [x,y];
	},
	findPos : function(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];
	},
	updateXY : function(e) {
		var pos = sweetTitles.getCursorPosition(e);
		sweetTitles.xCord = pos.x;
		sweetTitles.yCord = pos.y;
	},
	tipOut: function() {
		if ( window.tID ) {
			clearTimeout(tID);
		}
		if ( window.opacityID ) {
			clearTimeout(opacityID);
		}
		if(sweetTitles!=null) {
//			tipGoByeBye = window.setTimeout("sweetTitles.tip.style.visibility = 'hidden'",100);
			sweetTitles.tip.style.visibility = 'hidden';
		}
	},
	tipTimeout: function() {
		if ( window.tipTimer ) {
			clearTimeout(tipTimer);
		}
		tipTimer = window.setTimeout("sweetTitles.tipOut()",1000);
	},
	tipClearTimeout: function() {
		if ( window.tipTimer ) {
			clearTimeout(tipTimer);
		}
	},
	tipKeepAlive: function() {
	
	},
	checkNode : function() {
		var trueObj = this.obj;
		if (this.tipElements.inArray(trueObj.nodeName.toLowerCase()) ) {
			return trueObj;
		} else {
			return trueObj.parentNode;
		}
	},
	tipOver : function(e) {
		if ( window.tipTimer ) {
			clearTimeout(tipTimer);
		}
		if (window.tipGoByeBye) {
			clearTimeout(tipGoByeBye);
		}
		z = e;
		if(sweetTitles!=null) {
			sweetTitles.obj = sweetTitles.getEventSrc(e);
			//tID = window.setTimeout("sweetTitles.tipShow()",20);
			sweetTitles.updateXY(z);
			sweetTitles.tipShow(z);

		}
	},
	getCursorPosition : function(e) {
	    e = e || window.event;
	    var cursor = {x:0, y:0};
	    if (e.pageX || e.pageY) {
	        cursor.x = e.pageX;
	        cursor.y = e.pageY;
	    } 
	    else {
	        var de = document.documentElement;
	        var b = document.body;
	        cursor.x = e.clientX + 
	            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
	        cursor.y = e.clientY + 
	            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
	    }
	    return cursor;	
	},
	tipMouseMove : function(e) {
		z = e;
		if (window.tipGoByeBye) {
			clearTimeout(tipGoByeBye);
		}
		if(sweetTitles!=null) {
			sweetTitles.obj = sweetTitles.getEventSrc(e);
			sweetTitles.updateXY(z);
			if(sweetTitles.tip.style.visibility == 'hidden') {
				sweetTitles.tipShow(z);
			} else {
				//tID = window.setTimeout("sweetTitles.tipShow()",20);
				sweetTitles.tipMove(e);
			}

		}
	},
	tipMove : function(e) {
		var pos = sweetTitles.getCursorPosition(e);
		var scrX = pos.x; //Number(this.xCord);
		var scrY = pos.y; //Number(this.yCord);
		var xSpacer = 15;		
		var tp = parseInt(scrY+10);
		var lt = parseInt(scrX+xSpacer);

		// determine flip for x direction
		cW = parseInt(this.findInnerSize()[0] + this.findScrollingOffset()[0]);	

		if ( cW>0 && cW < (parseInt(this.tip.offsetWidth+lt+xSpacer+16)) && (this.tip.offsetWidth)<=lt) {
			this.tip.style.left = parseInt((lt-(this.tip.offsetWidth))-20)+'px';
		} else {
			this.tip.style.left = (parseInt(lt))+'px';
		}
		
		// determine flip for y direction favor above mouse position
		cH = parseInt(this.findInnerSize()[1] + this.findScrollingOffset()[1]);	
		if ( (this.tip.offsetHeight - 20) < (tp - this.findScrollingOffset()[1]) /* cH>0 && cH < (parseInt(this.tip.offsetHeight+tp)) && (this.tip.offsetHeight)<=tp */) {
			this.tip.style.top = parseInt((tp-(this.tip.offsetHeight))-15)+'px';
		} else {
			this.tip.style.top = (parseInt(tp))+'px';
		}
		
	},
	getEventSrc : function(e) {
		
        if (!e) e = window.event;
        if(!e) return;
        
        if (e.originalTarget)
        return e.originalTarget;
        else if (e.srcElement)
        return e.srcElement;
	},	
	tipShow : function(e) {		
		var anch = this.checkNode();
		var addy = '';
		var access = '';
		if ( anch.nodeName.toLowerCase() == 'a' ) {
			addy = (anch.href.length > 25 ? anch.href.toString().substring(0,25)+"..." : anch.href);
			var access = ( anch.accessKey ? ' <span>['+anch.accessKey+']</span> ' : '' );
		} else if(anch.firstChild!=null) {
			addy = anch.firstChild.nodeValue;
		} else {
			addy = '';
		}
		if(anch.getAttribute('showhref')!='true') {
			addy = '';
		}
		if(anch.getAttribute('tipWidthUnbound')=='true') {
			this.tip.style.width = '';
		} else {
			if(anch.tipDiv!=null) {
				this.tip.style.width = '350px';
			} else {
				this.tip.style.width = '250px';
			}
		}
		if(anch.tipDiv!=null && anch.tipDiv.innerHTML!='') {
			
			this.tip.innerHTML = anch.tipDiv.innerHTML;//"<p>hey there</p>";
			if(anch.tipDiv.className=='thumbPop') {
				this.tip.className = 'toolWithNoBorder';
				thumbStuff = anch.tipDiv.getElementsByTagName("div");
							
				for(i=0;i<thumbStuff.length;i++) {
					// make image popup captions same width as image
					if(thumbStuff[i].className=='thumbStuff') {
						var image = this.tip.getElementsByTagName('IMG')[0];
						if(image!=null) {
							if(image.src.indexOf('_400.')!=-1 || image.src.indexOf('400x300')!=-1) {
								this.tip.style.width = '400px';
							} else if(image.src.indexOf('_300.')!=-1) {
								this.tip.style.width = '300px';
							}
						}
					}
				}
				
			} else {
				this.tip.className = 'toolWithBorder';
			}
		} else if(anch.getAttribute('tip')!=null && anch.getAttribute('tip')!='') {
			this.tip.innerHTML = "<p>"+anch.getAttribute('tip')+"<em>"+access+addy+"</em></p>";
			this.tip.className = 'toolNoDiv';
		} else {
			return;
		}
		// flip depending on mouse location
		sweetTitles.tipMove(e);
		this.tip.style.visibility = 'visible';
		this.tip.style.opacity = '.1';
		this.tip.style.opacity = '1.00';
		this.tip.style.filter = "alpha(opacity:100)";

	},
	tipFade: function(opac) {
		var passed = parseInt(opac);
		var newOpac = parseInt(passed+10);
		if ( newOpac < 100 ) {
			this.tip.style.opacity = '.'+newOpac;
			this.tip.style.filter = "alpha(opacity:"+newOpac+")";
			opacityID = window.setTimeout("sweetTitles.tipFade('"+newOpac+"')",30);
		}
		else { 
			this.tip.style.opacity = '1.00';
			this.tip.style.filter = "alpha(opacity:100)";
			/*tID = window.setTimeout("sweetTitles.tipOut()",2000); */
		}
	}
};
function pageLoader() {
	sweetTitles.init();
}
addEvent(window,'load',pageLoader);