// Gerenciador de eventos
function addEvent(element, eventType, lamdaFunction, useCapture) {
  if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}
// Recupera a referência do div( cross-browser )
function fnc_referencia_div( p_div_id ) {
  //Netscape layers	
  if( document.layers ) 		return document.layers[ p_div_id ]; 
  //DOM; IE5, NS6, Mozilla, Opera
  if( document.getElementById ) return document.getElementById( p_div_id );
  //Proprietary DOM; IE4
  if( document.all ) return document.all[ p_div_id ]; 
  //Netscape alternative
  if( document[p_div_id] ) return document[p_div_id]; 

  return ( false );
}
// oculta o div
function fnc_mostra_div( p_div_id ) {
  if( !fnc_referencia_div( p_div_id ) ) return;
  
  fnc_referencia_div( p_div_id ).style.display = '';
}
// mostra o div
function fnc_oculta_div( p_div_id ) {
  if( !fnc_referencia_div( p_div_id ) ) return;	
  fnc_referencia_div( p_div_id ).style.display = 'none';
}

function getAbsolutePos(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

// Altera a cor da linha
function swapBG( el, BG1, BG2 ) {
  el.style.backgroundColor = ( el.style.backgroundColor == BG1 ) ? BG2 : BG1;
}

function fnc_mostrar_tooltip(p_objeto, p_texto, p_alinhamento, p_cor_fundo) {
 if( !p_texto ) return; // se o hint não estiver disponível não executa o tooltip
 
 var deslocamento;
 var size		= ( !p_objeto.style.width.split('px')[0] )?p_objeto.size * 4:p_objeto.style.width.split('px')[0];;
 var posicao    = getAbsolutePos( p_objeto );
 var tip    	= document.createElement('div');
	 tip.id 	= 'tip';
	 tip.style.backgroundColor 	 = '#ffffcc';
	 tip.style.color 	 		 = '#000000';
	 tip.style.border 		 	 = 'solid black 1px';    
	 tip.style.fontFamily 	 	 = 'Verdana';
	 tip.style.fontSize 		 = '10px';
	 tip.style.paddingLeft 		 = '10px';
	 tip.style.paddingTop 		 = '7px';
	 tip.style.zIndex			 = '551';
	 tip.style.display			 = 'none';
	 tip.style.position			 = 'absolute';
	 tip.style.width			 = eval( p_texto.length * 6.9) + "px";
	 tip.style.height			 = ( p_texto.length > 100)?40  + "px":30+ "px";
	 tip.innerHTML 				 = String.fromCharCode(32,32) + p_texto;

 switch ( p_alinhamento.toLowerCase() ){ 
	case "top" : 
		tip.style.top  = eval(posicao.y - tip.offsetHeight) + "px";
		tip.style.left = posicao.x + "px"; 
	break;
	case "left" :
	    deslocamento = parseInt( posicao.x - tip.offsetWidth) + parseInt( size ) + 35;
		tip.style.top  = posicao.y - 40 + "px";
		tip.style.left = posicao.x - 15 + "px";
	break;
	case "bottom" :
		tip.style.top  = eval(posicao.y + tip.offsetHeight) + "px";
		tip.style.left =  posicao.x + "px";
	break;
	case "right" :
		tip.style.top  = posicao.y - 40 + "px";
		tip.style.left = eval( posicao.x + tip.offsetWidth - 15) + "px";
	break;
 }
 document.body.appendChild( tip );
  tip.style.display ='';
}

function fnc_ocultar_tooltip(){ 
  if(!fnc_referencia_div('tip')) return;
  document.body.removeChild(  fnc_referencia_div('tip') );
}
// Alterna a cor de fundo do elemento
function setBackgroundColor( e ) {
  var target;
  if (window.event) { // IE5.5 and below
    target = window.event.srcElement;
  } else if (e) {     // N4+ && DOM compliant
    target = e.target;
  } else {
    target = "unsupported";
  }
  if( target.style ) {
    target.style.backgroundColor = cor_primaria;

	fnc_mostrar_tooltip( target ,target.getAttribute('hint'),'left', target.style.backgroundColor );
  }
 
}
// Limpa a cor de fundo do elemento
function clearBackgroundColor( e ) {
  var target;
  if (window.event) { // IE5.5 and below

    target = window.event.srcElement;
  } else if (e) {     // N4+ && DOM compliant
    target = e.target;
  } else {
    target = "unsupported";
  }
  
  if( target.style ) {
    target.style.backgroundColor = '';
	fnc_ocultar_tooltip();
  }
}
