/**********************************************************
feel free to use this wherever
always include www.umaniac.com
how to use:
this.toolTip();
**********************************************************/

toolTip = {
// initShow
				div: undefined,
				newdiv: undefined,
				newdivT: undefined,
				content: '',
				offsetX: 20,
				offsetY: 20,
				fade: 0,
				fadeStart: 0,
				fadeEnd: 1,
				opacity: 100,
				timer: 0,
				deleteD: 0,
				follow: 'yes',
				width: undefined,
				topMenu: false,
				id: undefined,
				css: '',
/* initialize */
			  initToolTip: function (element, content, options) {
						switch ( options.topMenu ) {
									 case true:
									 			toolTip.topMenu = true;
												break;
									 default:
									 			toolTip.topMenu = false;
												break;
						} 

						var newDivExists = false;
/**********************
						if ( toolTip.newdiv ) {
							 newDivExists = true;
							 if ( toolTip.newdiv.ParentNode ) {
							 		alert('parent exists: ');
							 } else {
							 	  alert('parent does not exist');
							 }
						} else {
							 alert('no div');
						}
**********************/						
						if ( ( toolTip.newdiv ) && ( toolTip.fadeEnd == 0 ) && ( toolTip.newdiv.parentNode != null ) ) {
						  toolTip.fadeEnd = 1;
							toolTip.newdiv.parentNode.removeChild(toolTip.newdiv);
						} 

						clearTimeout(toolTip.deleteD);

						if ( parseInt(options.timer) == options.timer ) {
							 toolTip.timer = options.timer;
						}

						if (typeof element == 'string') {
						  toolTip.div = document.getElementById(element);
						} else { 
							toolTip.div = element;
						}

						if ( options.fixC == true ) {
							 toolTip.content = unescape(content);
						} else {
							 toolTip.content = content;
						}
						toolTip.fixContent();
						// var css = 'position: absolute;';

						if ( options.css ) 
							 toolTip.css = options.css;
						else 
							 toolTip.css = '';

						var newdiv = document.createElement('div');
      			// newdiv.style.cssText = options.css;
						if ( options.z > 0 ) {
							 z = options.z;
						} else {
							 z = 1024;
						}

						switch ( options.follow ) {
									 case 'no':
									 		toolTip.follow = 'no';
											break;
									 default:					
									 		toolTip.follow = 'yes';
											break;
						}

						switch ( options.fade ) {
									 case 'stay':
									 		toolTip.fade = 1;
									 		break;
									 case 'fade':
									 		toolTip.fade = 2;
											break;
									 default:
									 		toolTip.fade = 0;
											break;
						}
						if ( ( parseInt(options.width) == options.width ) && ( options.width > '0' ) ) {
							 toolTip.width = options.width;  
						} 

						newdiv.style.zIndex  = z;

						if ( ( ( options.bubble == false ) && ( ( toolTip.fade == 0 ) || ( toolTip.fade == 2 ) ) ) && ( !newDivExists ) ) {

							 if ( (newdiv.style.backgroundColor == '') && (toolTip.css == '')) {
							 		newdiv.style.backgroundColor = '#4b6f87';
							 		newdiv.style.color = '#fff';
							 }

							 if ( toolTip.css != '' ) {
							 		newdiv.style.cssText = toolTip.css;
							 		/* newdiv.style.backgroundColor = 'red;'; // color: #fff;'; //toolTip.css; */
									/* newdiv.style.color = "#adeaae;"; */
							 }

							 newdiv.style.width = toolTip.width;
							 newdiv.style.overflow = 'auto';
							 newdiv.style.padding = '4px'

							 newdiv.style.zIndex  = z;
							 newdiv.style.position = 'absolute';
							 newdiv.innerHTML = toolTip.content;
      				 document.body.appendChild(newdiv);

							 // alert('parent now exists');
		 		 			 toolTip.newdiv = newdiv;
						} else if ( ( ( toolTip.fade == 0 ) || ( toolTip.fade == 2 ) ) && ( !newDivExists ) ) {
							 newdiv.style.width = '200px';
							 newdiv.style.zIndex  = z;
							 newdiv.style.position = 'absolute';
      			   newdiv.innerHTML = '<span class="tooltip"><span class="top"></span><span class="middle">'+toolTip.content+'</span><span class="bottom"></span></span>';
      				 document.body.appendChild(newdiv);
							 // alert('parent now exists');
		 		 			 toolTip.newdiv = newdiv;
						}

						if ( ( toolTip.fade == 1 ) && ( toolTip.topMenu ) ) {
							  createWindow(options.id, {dragging: 'drag', icon:'images/icons/famfam/silk/icons_magnify.png', title: 'Info'});
							  document.getElementById(options.id).innerHTML = '<div class="contentBlack">'+toolTip.content+'</div>';
								toolTip.newdiv = newdiv;
						} else {

								if ( toolTip.follow == 'no' ) { 
										 toolTip.div.onmousemove = toolTip.checkLocation;
										 toolTip.div.onmouseout  = toolTip.timeMouseOut;

										 toolTip.div.onkeydown = toolTip.checkLocation;
										 toolTip.div.onkeyup   = toolTip.timeMouseOut;
								} else {
										 toolTip.div.onmousemove = toolTip.checkLocation;
										 toolTip.div.onmouseout = toolTip.timeMouseOut;								
								}
						}
				},
/* fixContent */
				fixContent: function() {
    				var string = "";
            var i = 0;
						var utftext = toolTip.content;
            var c = c1 = c2 = 0;
						utftext = utftext.replace(/\+/g, " ");
            while ( i < utftext.length ) {
    
                c = utftext.charCodeAt(i);
    
                if (c < 128) {
                    string += String.fromCharCode(c);
                    i++;
                }
                else if((c > 191) && (c < 224)) {
                    c2 = utftext.charCodeAt(i+1);
                    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                    i += 2;
                }
                else {
                    c2 = utftext.charCodeAt(i+1);
                    c3 = utftext.charCodeAt(i+2);
                    string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                    i += 3;
                }
    				} // end while
						toolTip.content = string;
				},
/* checkLocation */
				checkLocation: function (e) {
						// alert('checkLocation');
						// alert(toolTip.newdiv);
						var e = e||window.event;
						if ( toolTip.follow == 'yes' ) {
							  // toolTip.newdiv.style.width = toolTip.width + 'px';
    						toolTip.newdiv.style.left = e.clientX + toolTip.offsetX + 'px';
    						toolTip.newdiv.style.top  = e.clientY + toolTip.offsetY + 'px';
						} else if ( ( toolTip.newdiv.style.left == '' ) || ( toolTip.fadeEnd == 0 ) ) {
								if ( ( e.clientX == undefined ) || ( e.clientY == undefined ) ) {
									toolTip.newdiv.style.left = toolTip.offsetX + 'px';
      						toolTip.newdiv.style.top  = toolTip.offsetY + 'px';
								} else {  
  								toolTip.newdiv.style.left = e.clientX + toolTip.offsetX + 'px';
      						toolTip.newdiv.style.top  = e.clientY + toolTip.offsetY + 'px';
								}
						} else {
							
						}
				},
/* timeMouseOut */
	 			timeMouseOut: function() {
						// alert('timeMouseOut');
						toolTip.fadeEnd = 0;
						toolTip.deleteD = setTimeout("toolTip.deleteDiv()", toolTip.timer*1000);
				},
/* deleteDiv */
	 			deleteDiv: function (e) {
						if ( toolTip.fade == 0 ) {
							 toolTip.newdiv.parentNode.removeChild(toolTip.newdiv);
						} else if ( toolTip.fade == 2 ) {
						/* courtesy of brainerror.com */
            	  // speed for each frame
								var speed = 10;
                var timer = 0;
                //determine the direction for the blending, if start and end are the same nothing happens
/*
                if(opacStart > opacEnd) {
                    for(i = opacStart; i >= opacEnd; i--) {
                        setTimeout("toolTip.changeOpac(" + i + ",'" + id + "')",(timer * speed));
                        timer++;
                    }
                } else if(opacStart < opacEnd) {
*/
                    for(i = 100; i >= 0; i--) {
												if ( toolTip.fadeEnd == 0 ) {
                          setTimeout("toolTip.changeOpac(" + i + ")",(timer * speed));
                          timer++;
												}
                    }
//                }
						} else if ( toolTip.fade == 1 ) {
							 toolTip.newdiv.onmouseout = toolTip.changeOpac(0);
						}
				},
/* opacity - brainerror.com */
        changeOpac: function(opacity) {
						// var t = document.getElementById('coding').innerHTML ;
						// var d = '';
						// alert(opacity + ' ' + toolTip.fadeEnd + ' ' + toolTip.newdiv.parentNode);
						if ( toolTip.fadeEnd == 1 ) { return; } else { 
               toolTip.newdiv.style.opacity = (opacity / 100);
               toolTip.newdiv.style.MozOpacity = (opacity / 100);
               toolTip.newdiv.style.KhtmlOpacity = (opacity / 100);
               toolTip.newdiv.style.filter = "alpha(opacity=" + opacity + ")";
						}
						if ( opacity <= 1 ) {
							 if (toolTip.newdiv.parentNode == null ) {
							 		// toolTip.fadeEnd = 1;
									// d = ' IN HERE ' + toolTip.newdiv.nodeType + ' end ' ;
							 } else {
							    d = 'kill';
							 		toolTip.newdiv.parentNode.removeChild(toolTip.newdiv);
									toolTip.fadeEnd = 1;
							 }
							 // toolTip.fadeEnd = 1;
						}

						// document.getElementById('coding').innerHTML = d + t;
						// + opacity + ' ' + toolTip.fadeEnd + ' ' + toolTip.newdiv.parentNode;

        }
} // end toolTip Object