

var xmlhttp = null;
var xmlhttptype = 0;
var highlighted = "z4";

function loadUp(url)
{
    if( xmlhttp == null ) 
    {
        try { xmlhttp = new ActiveXObject( 'Msxml2.XMLHTTP.3.0' ); xmlhttptype = 2; } catch (e) { xmlhttp = null; };
    }
    if( xmlhttp == null ) 
    {
        try { xmlhttp = new ActiveXObject( 'Msxml2.XMLHTTP' ); xmlhttptype = 3; } catch (e) { xmlhttp = null; };
    }
    if( xmlhttp == null ) 
    {
        try { xmlhttp = new ActiveXObject( 'Microsoft.XMLHTTP' ); xmlhttptype = 4; } catch (e) { xmlhttp = null; };
    }
    // Firefox
    if( xmlhttp == null ) 
    {
        try { xmlhttp = new XMLHttpRequest(); xmlhttptype = 1; } catch (e) { xmlhttp = null; };
    }
    if(xmlhttp != null)
    {
        var timestamp = new Date().getTime();
        var urlfixed = url;
        if (url.indexOf('?') == -1)
        {
           urlfixed = urlfixed + "?ts=" + timestamp;
        }
        else
        {
           urlfixed = urlfixed + "&ts=" + timestamp;
        }

        xmlhttp.onreadystatechange=state_Change;
        xmlhttp.open("GET", urlfixed, true);
        xmlhttp.send(null);
    }
    else
    {
        alert("Sorry, your browser does not support XMLHTTP.")
    }
}

function state_Change()
{
    if(xmlhttp.readyState == 4)
    {
        if(xmlhttp.status == 200)
        {
            if(document.getElementById('confinder') != null)
            {
                parent.document.getElementById('picWindow').contentDocument.location.reload(true);
                document.location.reload(true);
            }
            else
            {
                reloadIFrame('picWindow');
                reloadIFrame('picControl');
            }
        }   
        else    
        {
            alert("There was a problem retrieving the data: " + xmlhttp.statusText);
        }
    }  
}

function picHeight()
{
   document.getElementById("picWindow").style.height = getIFrameDocument('picWindow').getElementById("mainImage").height+"px";
   document.getElementById("picControl").style.height = document.getElementById("picWindow").style.height;
}
                                                                                                                                                        
function switchHigh(whichOne)
{
    if(highlighted != "none")
    {
        document.getElementById(highlighted).style.border = "3px solid #feb601";
    }
    if(whichOne != "none")
    {
        document.getElementById(whichOne).style.border = "3px solid black";
    }
    highlighted = whichOne;                                                                                                                                                                                                                                                                                              
}

function zoomIn(le, tu, win_width, bac_max, job_id)
{
    c = (le + tu) /2;
    doit = 1;
    if(win_width > 80000)
    {
        t = Math.min(c + 40000, bac_max);
        l = t - 80000;
        switchHigh("z1");
    }
    else if(win_width > 60000)
    {
        t = Math.min(c + 30000, bac_max);
        l = t - 60000;
        switchHigh("z2");
    }
    else if(win_width > 40000)
    {
        t = Math.min(c + 20000, bac_max);
        l = t - 40000;
        switchHigh("z3");
    }
    else if(win_width > 30000)
    {
        t = Math.min(c + 15000, bac_max);
        l = t - 30000;
        switchHigh("z4");
    }
    else if(win_width > 20000)
    {
        t = Math.min(c + 10000, bac_max);
        l = t - 20000;
        switchHigh("z5");
    }
    else if(win_width > 10000)
    {
        t = Math.min(c + 5000, bac_max);
        l = t - 10000;
        switchHigh("z6");
    }
    else if(win_width > 5000)
    {
        t = Math.min(c + 2500, bac_max);
        l = t - 5000;
        switchHigh("z7");
    }
    else if(win_width > 1000)
    {
        t = Math.min(c + 500, bac_max);
        l = t - 1000;
        switchHigh("z8");
    }
    else
    {
        doit = 0;
    }
    if(doit == 1)
    {
        changeLeftTo(l, t, job_id, bac_max);
    }
}

function zoomOut(le, tu, win_width, bac_max, job_id)
{
    c = (le + tu) /2;
    doit = 1;
    if(win_width < 1000)
    {
        l = Math.max(c - 500, 0);
        t = l + 1000;
        switchHigh("z8");
    }
    else if(win_width < 5000)
    {
        l = Math.max(c - 2500, 0);
        t = l + 5000;
        switchHigh("z7");
    }
    else if(win_width < 10000)
    {
        l = Math.max(c - 5000, 0);
        t = l + 10000;
        switchHigh("z6");
    }
    else if(win_width < 20000)
    {
        l = Math.max(c - 10000, 0);
        t = l + 20000;
        switchHigh("z5");
    }
    else if(win_width < 30000)
    {
        l = Math.max(c - 15000, 0);
        t = l + 30000;
        switchHigh("z4");
    }
    else if(win_width < 40000)
    {
        l = Math.max(c - 20000, 0);
        t = l + 40000;
        switchHigh("z3");
    }
    else if(win_width < 60000)
    {
        l = Math.max(c - 30000, 0);
        t = l + 60000;
        switchHigh("z2");
    }
    else if(win_width < 80000)
    {
        l = Math.max(c - 40000, 0);
        t = l + 80000;
        switchHigh("z1");
    }
    else
    {
        doit = 0;
    }
    if(doit == 1)
    {
        w = t - l;
        if(t > bac_max)
        {
            t = bac_max;
            l = t - w;
        }
        changeLeftTo(l, t, job_id, bac_max);
    }
}
        
function clicketyClick(which, am, job_id, bm)
{
    switchHigh(which); 
    var l = parseInt(document.getElementById('left').value); 
    var t = parseInt(document.getElementById('to').value); 
    var nl = Math.max(0, ((t+l)/2 - am/2)); 
    var nt = Math.min(bac_max, ((t+l)/2 + am/2)); 
    if(nl <= 0)
    { 
        nl = 0;
        nt = am;
    }
    if(nt >= bac_max)
    {
        nt = bac_max;
        nl = Math.max(0, nt-am);
    }
    changeLeftTo(nl, nt, job_id, bac_max);
}

function updateLeftTo(l, t)
{
    left = parseInt(l);
    to = parseInt(t);
    win_width = to - left;
    document.getElementById('to').value = to;
    document.getElementById('left').value = left;
}

function changeLeftTo(l, t, job_id, bm)
{
    error = 0;
    if(isNaN(parseInt(l)) || isNaN(parseInt(t)))
    {
        error = 1;
    }
    
    if(parseInt(t) > parseInt(bm))
    {
        t = bm;
        l = Math.max(0, t - win_width);
    }
    if(parseInt(l) < 0)
    {
        l = 0;
        t = Math.min(parseInt(bm), l + win_width);
    }
    if(parseInt(l) > parseInt(t) || parseInt(l) == parseInt(t))
    {
        l = 0;
        t = parseInt(bm);
    }
    w = t - l;
    if(w < 0 || error == 1)
    {
        alert('The values you entered don\'t make sense.\nPlease try some other values.\n From = '+l+' To = '+t);
    }
    else
    {
        //setIFrameInnerHtml( 'picControl', '');
        //setIFrameInnerHtml( 'picWindow', 'Redrawing Image.<br><br>Please wait...<br>');
        var url = "CWL.pl?W="+w.toString()+"&L="+l.toString()+"&job_ID="+job_id.toString();
        loadUp(url);
        updateLeftTo(l, t);
    }
}

function getIFrame( elementId )
{
    var oIframe = document.getElementById(elementId);
    return oIframe;
}

function getIFrameWindow( elementId )
{
    var oIframe = getIFrame(elementId).window;
    return oIframe;
}

function getIFrameContentDocument( elementId )
{
    var oIframe = getIFrame(elementId);
    var oDoc = oIframe.contentWindow || oIframe.contentDocument;
    return oDoc;
}

function getIFrameDocument( elementId )
{
    var oDoc = getIFrameContentDocument( elementId );
    if (oDoc.document) {
        oDoc = oDoc.document;
    }
    return oDoc;
}

function setIFrameInnerHtml( elementId, htmlContent )
{
    getIFrameDocument(elementId).innerHTML = htmlContent;
}

function doIfFirst()
{
    if(first == 1)
    {
        first = 0;
        changeLeftTo('0', '30000', job_id, bac_max);
    }
}

function reloadIFrame( elementId )
{
    getIFrameDocument(elementId).location.reload(true);
    getIFrame(elementId).src = getIFrame(elementId).src;
}

function toggleIface(whichOne, j_id)
{  
    //document.body.innerHTML="<div id='confinder' style='visibility: hidden;'></div>";
    //setIFrameInnerHtml('picWindow', "Redrawing Image.<br><br>Please wait...<br>" );
    var url = "controller.pl?job_ID="+j_id+"&toggle="+whichOne;
    loadUp(url);
}
