/**
 * $Id$
 */
addEvent(window, 'load', enableInfoTip);
addEvent(document, 'click', hideInfoTip);

function enableInfoTip()
{
    for (var i = 0; i < document.links.length; i ++)
    {
        if (document.links[i].rel.substr(0, 5).toLowerCase() == 'info_')
        {
            var params = document.links[i].rel.substr(5).split('_');
            var type = params.shift();
            
            document.links[i].infoType = type;
            document.links[i].infoParams = params;
            document.links[i].className += ' info_' + type;
            document.links[i].rel = '';
            document.links[i].href = '#oubk';
            document.links[i].onclick = showInfoTip;
        }
    }
    var tips = document.getElementById('info_tip');
    if (!tips)
    {
        var tips = document.createElement('div');
        tips.id = 'info_tip';
        tips.className = 'info_tip';
        
        document.body.appendChild(tips);
    }
}

function showInfoTip(e)
{
    e = e || window.event;
    var target   = e.target || e.srcElement;
    adjustPosition(target);
    currentShow = target;
    //if (target.infoType == 'sdk')
    var params = target.infoParams;
    showSdk(params,target.infoType);
    return false;
}

function showSdk(id,infoType)
{
    var tip = document.getElementById('info_tip');
    tip.className = 'user_tip';
    tip.innerHTML = '<div style="padding: 5px 8px" class="small">loading...</div>';
    $.ajax({
            url:"/Practice/GetMenu.ashx?types="+ id +"&infoType="+ infoType,type:"GET",async:false, cache: false,timeout:2000,
            success: function(msg){
                tip.innerHTML = '';
                tip.innerHTML = msg;
                adjustPosition(currentShow);
            }
         });
}


function adjustPosition(target)
{
    var tip = document.getElementById('info_tip');  
    tip.style.display = 'block';
    
    var pos = getPosition(target);
    
    var x = pos.x + target.offsetWidth;
    var y = pos.y;
    
    if (pos.y - document.documentElement.scrollTop + tip.clientHeight > document.documentElement.clientHeight)
    {
        y = pos.y + target.offsetHeight - tip.clientHeight;
    }
    else
    {
        y = pos.y;
    }

    if (pos.x + tip.offsetWidth > document.documentElement.offsetWidth)
    {
        x = pos.x - tip.clientWidth;
    }

    tip.style.top = y + 'px';
    tip.style.left = x + 'px';
}

function hideInfoTip(e)
{
    e = e || window.event;

    var target   = e.target || e.srcElement;

    var tip = document.getElementById('info_tip');

    if (typeof target.infoType != 'undefined')
    {
        //showInfoTip(e);
        return false;
    }

    if (target.id == 'info_tip')
    {
        return false;
    }
    
    var isLink = target.tagName.toLowerCase() == 'a' ? true : false;

    while (target.parentNode)
    {
        if (target.parentNode.id == 'info_tip')
        {
            return isLink ? true : false;
        }

        target = target.parentNode;
    }
	
	if (tip)
	{
		tip.style.display = 'none';
	}
    
    //return false;
}
