var dofade=true;
                     
var leftayuda=0;
var topayuda=0;

var clasediv="down";

var IE = (navigator.appName.indexOf("Microsoft")>=0)? true : false;

var w_y, w_x, navayuda, boxheight, boxwidth;
var ishover=false;
var isloaded=false;
var ieop=0;
var op_id=0;

function getWindowDimsAyuda(){

    if(IE)
    {
        var _scrollheight = 0;
        var _scrolltop = 0;
        var _scrollwidth = 0;
        var bodys = document.getElementsByTagName("HTML");
        var count = bodys.length;        
        for(var i =0; i < count; i++){
            var obj = document.getElementsByTagName("HTML")[i];
            _scrollheight = obj.clientHeight;
            _scrolltop = obj.scrollTop;
            _scrollwidth = obj.clientWidth;
        }
        w_y=_scrollheight + _scrolltop;
        w_x=_scrollwidth;
    }
    else
    {
        w_y=window.innerHeight + window.scrollY;
        w_x=window.innerWidth;
    }

}

function getBoxWidthAyuda(){
    if(IE)
        boxwidth=(navayuda.style.pixelWidth)? navayuda.style.pixelWidth : navayuda.offsetWidth;
    else  
        boxwidth=(navayuda.style.width)? parseInt(navayuda.style.width) : parseInt(navayuda.offsetWidth);
}

function getBoxHeightAyuda(){
    if(IE)
        boxheight=(navayuda.style.pixelHeight)? navayuda.style.pixelHeight : navayuda.offsetHeight;
    else
        boxheight=parseInt(navayuda.offsetHeight);
}

function WriteDivAyuda(title, text){
    var _html = '<iframe id="iframetop" frameborder="0" style="position:absolute; left:0; top:0; width:0px; height:0px; margin:0; padding:0; background:transparent; filter:alpha(opacity=0);"></iframe>';
    _html = _html + '<div id="navayuda" class="ac_help">';
    _html = _html + '<div id="navayuda2" class="help_top_' + clasediv + '"></div>';
    _html = _html + '<div class="help_body">';
    _html = _html + '<h5>';
    _html = _html + title;
    _html = _html + '<a href="#"><img src="/IMG/miautocosmos/help_box_close.gif" alt="cerrar" onclick="WriteAyuda(0, 0, 0, 0, 0); return false;" /></a></h5>';
    _html = _html + text;
    _html = _html + '</div>';
    _html = _html + '<div id="navayuda3" class="help_bottom_' + clasediv + '"></div>';
    _html = _html + '</div>';
    navayuda.innerHTML=_html;
}

function WriteAyuda(e, imgayu, title, text, divcontenedor){
    if(!isloaded){definirAyuda();}
    if(text!=0){
        ishover=true;
        WriteDivAyuda(title, text);
        DefinirPosicionAyuda(e, imgayu, title, text);
        if (divcontenedor != '')
        {
            var _divcontenedor = document.getElementById(divcontenedor);
            var _scrollStart = window.pageXOffset || _divcontenedor.scrollLeft || document.body.scrollLeft || 0;
            leftayuda = leftayuda - _scrollStart;
        }
        MoveAyuda(leftayuda, topayuda);
        if(dofade)
        {
            ieop=0;
            IncropacityAyuda();
            var iframe = document.getElementById('iframetop');
            if (iframe != null)
            {
                iframe.style.width = navayuda.offsetWidth + 'px';
                iframe.style.height = navayuda.offsetHeight + 'px';    
            }
        }
    }
    else
    {
        var iframe = document.getElementById('iframetop');
        if (iframe != null) iframe.style.visibility="hidden";
        OutcropacityAyuda();
    }
}

function IncropacityAyuda(){
    if(ieop<=100){
        ieop+=10;
        if(IE)
            navayuda.style.filter="alpha(opacity="+ieop+")";
        else
            navayuda.style.MozOpacity=ieop/100;
        op_id=setTimeout('IncropacityAyuda()', 40);
    }
}

function OutcropacityAyuda(){
    if(ieop>0){
        ieop-=10;
        if(ieop<0){ieop=0;}
        if(IE)
            navayuda.style.filter="alpha(opacity="+ieop+")";
        else
            navayuda.style.MozOpacity=ieop/100;
        op_id=setTimeout('OutcropacityAyuda()', 40);
    }
    else{
        if(dofade)clearTimeout(op_id);
        navayuda.style.visibility="hidden";
        WriteDivAyuda('', '');
        ishover=false;
    }
}

function findPosBotonAyuda(obj) {
	leftayuda = 0;
	topayuda = 0;
	if (obj.offsetParent) {
		do {
			leftayuda += obj.offsetLeft;
			topayuda += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
}


function DefinirPosicionAyuda(e, imgayu, title, text) {	       
    
    var targ;
    if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3)
		targ = targ.parentNode;
    
    findPosBotonAyuda(targ);    
    
    getWindowDimsAyuda();
    getBoxWidthAyuda();
    getBoxHeightAyuda();
    
    if (topayuda + boxheight > w_y)
    {
       leftayuda = leftayuda - boxwidth + 30;
       topayuda = topayuda - boxheight;       
       clasediv = "up" ;
    }    
    else
    {
       leftayuda = leftayuda - boxwidth + 30;
       topayuda = topayuda + 10;
       clasediv = "down" ;
    }
    
    WriteDivAyuda(title, text);
        
}

function MoveAyuda(ayuleft, ayutop){
    if(isloaded && ishover){
        navayuda.style.left=ayuleft+'px';
        navayuda.style.top=ayutop+'px';
        navayuda.style.visibility="visible";
    }
}

function definirAyuda()
{
  navayuda=document.getElementById('navayuda');
  if(navayuda)
  {
      isloaded=true;
      if(!IE)navayuda.style.padding='4px';
      if(IE && dofade)navayuda.style.filter="alpha(opacity=0)";
   }
}

window.onload=function(){ definirAyuda(); }
window.onresize=getWindowDimsAyuda;

