/*
 * Metoda steruje wyświetlaniem wybranego elementu na stronie. Jeśli element jest widocznym ukrywa go, jeśli jest niewidoczy, pokazuje go.
 * Przekazanie elementu do metody następuje przez jego ID.
 *
 * author: s.wilk@amg.net.pl
 */ 
function DXMLToggleElementDisplay(elementId) {
    el = document.getElementById(elementId);
    if ( el ) {
        if ( el.style.display == "none" ) {
            el.style.display = "";
        } else {
            el.style.display = "none";
        } 
    }   
}


/*
 * Metoda automatycznie dokonuje przeskalowania obrazka do wybranej szerokości.
 * Obrazek podawany jest do metody jako obiekt HTML (znaczik IMG).
 *
 * author: s.wilk@amg.net.pl
 */ 
function DXMLImgXRescale(img,size) {
    try {
        if ( img.width && ( img.width > size ) ) {
            img.width = size;
        }
    } catch (e) {}
}

/*
 * Metoda wyświetla popup wyśrodkowany na ekranie i ustawia na nim focus.
 * Maksymalne wymiary popup są hardcodowane w metodzie.
 *
 * author: s.wilk@amg.net.pl
 */ 
function DXMLPopupWin(winURL, winName, width, height, customProps) {

    var maxWidowWidth = 1600;
    var maxWindowHeight = 1200;

    winWidth = eval(width) > maxWidowWidth ? maxWidowWidth : eval(width);
    winHeight = eval(height) > maxWindowHeight ? maxWindowHeight : eval(height);

    var winLeft = (screen.width - winWidth) / 2;
    var winTop = (screen.height - winHeight) / 2;
    
    winProps = 'height=' + winHeight + ',width=' + winWidth + ',top=' + winTop + ',left=' + winLeft + ',status' + customProps;
    win = window.open(winURL, winName, winProps);
    if ( parseInt(navigator.appVersion) >= 4 ) { win.window.focus(); }
    return win;
}


/* Funkcja generująca loader na stronie - zakrywa wskazany element 
 * Generuje DIV o zadanym stylu i pozycjonuje go nad elementem podanym w parametrze parentId
 */

function DXMLLaunchLoader(loaderId,parentElemId,className) {
    _PARENT = document.getElementById(parentElemId);
    coords = getElementCoords(_PARENT.getElementsByTagName("table").item(0));
    
    _DXML_LOADER = document.createElement("div");
        _DXML_LOADER.id = "loaderId";
        _DXML_LOADER.style.height = coords.h  + "px";
        _DXML_LOADER.style.width = coords.w  + "px";
        _DXML_LOADER.style.left = coords.x + "px";
        _DXML_LOADER.style.top = coords.y  + "px";
        _DXML_LOADER.className = className;

    _BODY = document.getElementsByTagName("body").item(0);
        _BODY.appendChild(_DXML_LOADER);
    
    _DXML_LOADER.style.display = "block";
}



/* Funkcja zwraca koordynaty dowolnie wybranego elementu HTML na stronie
 * Dane zwracane są w postaci tablicy: [ X , Y, HEIGHT, WIDTH ]
 *
 * author: s.wilk@amg.net.pl
 */

function getElementCoords(element) {
    var coords = {x: 0, y: 0, h: 0, w: 0};
    coords.w = element.offsetWidth;
    coords.h = element.offsetHeight;
    while (element) {
        if ( element != null ) {
            coords.x += element.offsetLeft;
            coords.y += element.offsetTop;
            element = element.offsetParent;
        }
    }
    return coords;
}  
