
var ie = (document.all) ? true : false;

var zoomBG;
var zoomDIV;
var zoomIMG;
var zoomIndex;

window.onload = init;
window.onresize = redraw;

function redraw() {
    var pageBody = document.getElementById('PageBody');
    var leftColumn = document.getElementById('LeftColumn');
    var mainColumn = document.getElementById('MainColumn');
    
    if (leftColumn && mainColumn) {
        pageBody.style.height = null;
        mainColumn.style.height = null;
        leftColumn.style.height = null;
        var pageBodyHeight = Math.max(
                leftColumn.scrollHeight, 
                mainColumn.scrollHeight,                  
                document.body.clientHeight - pageBody.offsetTop - 75);
        pageBody.style.height = pageBodyHeight + 'px';
    }
}

function init() {
    zoomBG = document.getElementById('ZoomBG');
    zoomDIV= document.getElementById('ZoomDIV');
    zoomIMG = document.getElementById('ZoomIMG');

    redraw();
}


function getSelectionValue(aSelection) {
    return aSelection.options[aSelection.selectedIndex].value;
}

function setOpacity(element, opacity) {
    element.style.opacity = (opacity / 100);
    element.style.MozOpacity = (opacity / 100);
    element.style.KhtmlOpacity = (opacity / 100);
    element.style.filter = "alpha(opacity=" + opacity + ")";
}

function zoom(aImg, aIndex) {
    if (zoomBG) {
        if (ie)
            zoomBG.style.top = document.body.scrollTop + 'px';
        else
            zoomBG.style.top = '0px';
        zoomBG.style.left = '0px';
        zoomBG.style.width = document.documentElement.scrollWidth + 'px';
        zoomBG.style.height = document.documentElement.scrollHeight + 'px';
        
        zoomBG.style.cursor = 'wait';

        zoomIMG.onload = zoomFinish;
        zoomIMG.src = aImg.src.replace(/\/thm-/, '/img-');
        
        setOpacity(zoomBG, 90);
        zoomBG.style.visibility = 'visible';
        
        zoomSelect(aIndex);
    }
}

function zoomFinish() {
    if (zoomBG && zoomDIV && zoomIMG) {
        zoomDIV.style.width = zoomIMG.clientWidth + 24 + 'px';
        zoomDIV.style.height = zoomIMG.clientHeight + 84 + 'px';
        if (ie) {
            zoomDIV.style.top = document.body.parentNode.scrollTop + (document.body.clientHeight - zoomDIV.clientHeight) / 2 + 'px';
            zoomDIV.style.left = (document.body.clientWidth - zoomDIV.clientWidth) / 2 + 'px';
        } else {
            zoomDIV.style.top = window.scrollY + (window.innerHeight - zoomDIV.clientHeight) / 2 + 'px';
            zoomDIV.style.left = (window.innerWidth - zoomDIV.clientWidth) / 2 + 'px';
        }
        zoomBG.style.cursor = 'default';
        zoomDIV.style.visibility = 'visible';
    }
}

function zoomSelect(aIndex) {
    if (zoomList) {
        if (0 <= zoomIndex) {
            var e = document.getElementById('ZoomRef' + zoomIndex);
            e.className = e.className.replace(/Selected/, 'Unselected');
        }
        zoomIndex = aIndex;
        if (zoomList[aIndex] < 10)
            str = '0' + zoomList[aIndex];
        else
            str = zoomList[aIndex];
        zoomIMG.src = zoomIMG.src.replace(/-\d\d\.jpg/, '-' + str + '.jpg');
        var e = document.getElementById('ZoomRef' + aIndex);        
        e.className = e.className.replace(/Unselected/, 'Selected');
        if (9 < aIndex)
            document.getElementById('ZoomDotsLeft').style.display = 'block';
        else
            document.getElementById('ZoomDotsLeft').style.display = 'none';
        for (var i = 0; i < aIndex - aIndex % 10; i++)
                document.getElementById('ZoomRef' + i).style.display = 'none';
        for (var i = aIndex - aIndex % 10; i < Math.min(zoomList.length, aIndex + 10 - aIndex % 10); i++)
            document.getElementById('ZoomRef' + i).style.display = 'block';
        for (var i = aIndex + 10 - aIndex % 10; i < zoomList.length; i++)
            document.getElementById('ZoomRef' + i).style.display = 'none';
        if (aIndex + 10 - aIndex % 10 < zoomList.length - 1)
            document.getElementById('ZoomDotsRight').style.display = 'block';
        else
            document.getElementById('ZoomDotsRight').style.display = 'none';
        
    }
}

function zoomFirst() {
    zoomSelect(0);
}

function zoomPrev() {
    if (zoomList && 0 < zoomIndex) {
        zoomSelect(zoomIndex - 1);
    }
}

function zoomNext() {
    if (zoomList && zoomIndex < zoomList.length - 1) {
        zoomSelect(zoomIndex + 1);
    }
}

function zoomLast() {
    if (zoomList) {
        zoomSelect(zoomList.length - 1);
    }
}

function zoomClose() {
    if (zoomBG && zoomDIV) {
        zoomBG.style.visibility = 'hidden';
        zoomDIV.style.visibility = 'hidden';
    }
}


var dialogTop = 0;
var dialogLeft = 0;

function getTop(aTop, aHeight) {
    if (20 < (screen.height - aTop - aHeight))
        return aTop + 20;
    else
        return 0;
}

function getLeft(aLeft, aWidth) {
    if (20 < (screen.width - aLeft - aWidth))
        return aLeft + 20;
    else
        return 0;
}

function openDialog(aURL, aWinID, aWidth, aHeight) {
    if (! aWinID)
        aWinID = aURL;
    if (! aWidth)
       aWidth = 720;
    if (aWidth < 200)
        aWidth = 200;
    if (! aHeight)
        aHeight = 480;
    if (aHeight < 200)
        aHeight = 200;
    dialogTop = getTop(dialogTop, aHeight);
    dialogLeft = getLeft(dialogLeft, aWidth);
    window.open('../intranet/null/' + aURL, aWinID, 'resizable=yes,scrollbars=no,left=' + dialogLeft + ',top=' + dialogTop + ',width='+ aWidth + ',height=' + aHeight);
}

