// Common functions for KM
//
// showHideTR, used to allow inline forms and deal with <tr> elements

var timeout_handle = 0; //global used for settimeout
var show_cloud = true;  // to avoid last show

function showHideTR(panel) {
    if (navigator.appName.indexOf('Microsoft') != -1) {
        //alert('Hello')
        // If using IE, use the block/none attrib's
        if ($(panel).style.display != 'block')
            $(panel).style.display = 'block';
        else
            $(panel).style.display = 'none';
    }
    else {
        // assume we're using fierfox, this should be updated 
        // use table-row/none attrib's
        if ($(panel).style.display != 'table-row')
            $(panel).style.display = 'table-row';
        else
            $(panel).style.display = 'none';
    }
    
}

function showHidePro(panel) {
    if (($(panel).style.display == 'block') || ($(panel).style.display == 'table-row'))
        $(panel).hide();
    else
        $(panel).show();
}


function showHide(panel) {
    if ($(panel).style.display != 'block')
        $(panel).style.display = 'block';
    else
        $(panel).style.display = 'none';
}


function showPanel(panel) {
    if ($(panel)) {
        $(panel).style.display = 'block';
    }
}

function hidePanel(panel) {
    if ($(panel)) {
        $(panel).style.display = 'none';
    }
}




function loading() {
    showHide('loading');
}
function showLoading() {
    $('loading').style.display = 'block';
}
function hideLoading() {
    $('loading').style.display = 'none';
}



/// SHOWBALLOON
function showBalloon(e, id, size) {
    var posx = 0;
    var posy = 0;
    var pnt = 1;
            
    // first hide other div's
    while ($('balloon'+pnt)) {
        if (pnt != id) {
            var tmpel = $('balloon'+pnt);
            Element.hide(tmpel);
        }
        pnt = pnt + 1;
    }
    
    // second we obtain the Y pos of the HANDLER element based en viewport position + height
    posx = getXY(e)[0];
    posy = getXY(e)[1];
        
    var element = $('balloon'+id+'-handler');
    if (element) 
      menuYpos = Position.page(element)[1] + Element.getHeight(element);
    else
      menuYpos = posy;
    
    // we determine if left sided or right sided and then position the pyramid left or right 
    var element = $('balloon'+id+'-topbar');
     
    if ((posx + size + 10) > screen.height) {
        posx = posx - (size - 40);
        // 28 is to properly position the pyramid
        element.style.marginLeft = (size - 28) + 'px'; 
    }
    else {
        element.style.marginLeft = '15px';
    }
   
    // Finally we move the balloon to the W/Y position and display
    
    var element = $('balloon'+id);
    // -38 is to position the arrow (not the left corner) under the click
    // Only in left-sided mode
    element.style.left = (posx - 38) + 'px';
    element.style.top = menuYpos + 'px';
    element.style.width = size + 'px';
    element.style.display='block';
    
}

function closeBalloon(balloonId) {
    var d = $(balloonId);
    Element.hide(d);
}

function createCloud(e, divId, width) {
    var YC = $('YCloud');
    var YCT = $('YCText');
    
    //If we receive a DIV, put the inner text into, else, put the text passed
    ST = $(divId);
    if (ST != null)
        YCT.innerHTML = ST.innerHTML;
    else
        YCT.innerHTML = divId;
    
    posx = getXY(e)[0];
    posy = getXY(e)[1];
    
    if (width) 
        YC.style.width = width + 'px';
    YC.style.left = posx + 'px';
    YC.style.top = posy + 18 + 'px'; // just below the pointer
    if (timeout_handle != 0) {
        clearTimeout(timeout_handle);
        timeout_handle = 0;
    }
    
    show_cloud = true;    
    var timeout_handle=setTimeout('showCloud()',850)  //850 miliseconds of delay
}

function showCloud() {
    var YC = $('YCloud');

    if (show_cloud)
        YC.style.display = 'block';
    
    clearTimeout(timeout_handle);
}

function hideCloud() {
    var YC = $('YCloud');
    clearTimeout(timeout_handle);
    show_cloud = false;

    YC.style.display = 'none';
}

function getXY(e) {
    retval = new Array()
    
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        posx = e.clientX + document.body.scrollLeft
            + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
            + document.documentElement.scrollTop;
    }
    
    retval[0] = posx
    retval[1] = posy
    return(retval)
}

function showGridPanel(id, handler) {
    var GP = $(id)
    
    if (handler)
        var element = $(handler);
    else
        var element = $('table-handler'); 
    
    panelXpos = Position.page(element)[0] + element.clientWidth;

    GP.style.display = 'block';
    // Little margin between table & panel
    GP.style.left = panelXpos + 30 + 'px';
    GP.style.top = '138' + 'px'; 
}


function KMonLoadHandler() {
    adjustHeight();
}

function adjustHeight() {
    // That makes the body content tall as the navtree if is too short 
    // Need some improvment and retormating classes
    version=0
    if (navigator.appVersion.indexOf("MSIE")!=-1){
        temp=navigator.appVersion.split("MSIE")
        version=parseFloat(temp[1])
        //alert(version);
        if (version == 7) {
            body_h = parseInt($('KMBODY').offsetHeight);
            nav_h = parseInt($("kmnavtree").offsetHeight);
            
            str = 'body_h:'+body_h+'   naw_h:'+nav_h;
            //alert(str)
            $('KMBODY').style.minHeight=nav_h + 30 + 'px';
            return
        }
    }

    if ($('KMBODY') && $("kmnavtree")) {
        
        body_h = parseInt($('KMBODY').offsetHeight);
        nav_h = parseInt($("kmnavtree").offsetHeight);
        
        str = 'navtree='+nav_h+'   body='+body_h;
        //alert(str)
        if (body_h > nav_h)
            $('KMBODY').style.height = body_h+"px";
        else
            $('KMBODY').style.height = nav_h+"px";
    }
}

