﻿var obj;
var clickOpen = false;
var tagname = new Array('div', 'img');

var tooltip = {
    
    init: function(){        
        if (!obj) {
			obj = document.createElement('div');
			obj.setAttribute('id', 'tooltip');                        
			obj.onclick = function(){			
				clickOpen = false;
				
				obj.innerHTML = '';
				obj.style.display = 'none';                        
				obj.style.backgroundColor = '';
				obj.style.color = '';
			}
			document.body.appendChild(obj);
		}
        
        window.document.onmousemove = this.move;        
        
        for(var i = 0; i < tagname.length; i++){
            
            var ancora = document.getElementsByTagName(tagname[i]);
            
            for(var j = 0; j < ancora.length; j++){
                
                var a = ancora[j];
                texttitle = a.getAttribute('title');                
                
                if(texttitle){
                    
                    var objText = document.getElementById(texttitle);
                    if (objText) {
						texttitle = objText.innerHTML;
						a.setAttribute('sBackgroundColor', objText.style.backgroundColor);
						a.setAttribute('sColor', objText.style.color);
                    }
                    
                    a.setAttribute('sTitle', texttitle);
                    a.removeAttribute('title');                                        
                    
                    a.onmouseover = function(){
                        
                        if (!clickOpen) {                        
							t = this.getAttribute('sTitle');
							b = this.getAttribute('sBackgroundColor');
							c = this.getAttribute('sColor');
	                        
							obj.innerHTML = t;
							obj.style.display = 'block';
	                        
							obj.style.backgroundColor = b;
							obj.style.color = c;							
							clickOpen = true;
						}
                        
                    };// end function
                    
                    a.onmouseout = function(){
						//clickOpen = true;
					};
                    
                    /*a.onmouseout = function(){
                        
                        if (!clickOpen) {
							obj.innerHTML = '';
							obj.style.display = 'none';
	                        
							obj.style.backgroundColor = '';
							obj.style.color = '';
						}
                        
                    };*/// end function
                    
                    a.onclick = function(){
						clickOpen = true;
                    } // end function                                                       
                }//end if
                
            }//end for
            
        }//end for
        
    },
    
    move: function(e){
        
        if (!clickOpen) {
        
			e = e || window.event;
	        
			if(e.pageX || e.pageY){
	            
				x = e.pageX;
				y = e.pageY;
	            
			}else{
	            
				x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
				y = e.clientY + (document.documentElement.scrollTop ||  document.body.scrollTop) -  document.documentElement.clientTop;
	            
			}//end if
	        
			obj.style.left = (x-150)+'px';
			obj.style.top = (y-35)+'px';			 
			//alert(obj.style.width);
			//alert(obj.style.height);
			obj.style.position = 'absolute';									
		}
		
		var divTitle = document.getElementById('tooltipTitle');
			
		if (!divTitle) {
			divTitle = document.createElement('div');				
			divTitle.setAttribute('id', 'tooltipTitle');				
			divTitle.innerHTML = '<span id="tooltipTitleText">Informação' + obj.style.height + '</span>';
			
			var divButton = document.createElement('button');
			divButton.setAttribute('id', 'tooltipTitleButton');
			divButton.setAttribute('title', 'Fechar');
			divTitle.appendChild(divButton);				
			
			html = obj.innerHTML;
			obj.innerHTML = '';				
			obj.appendChild(divTitle);				
			obj.innerHTML += html;								
		}
        
        return true;
        
    }
    
}