var fname ;
 var gifdir = "/data/testbed/globalgrids/"
 var looping    = false;
 var paused     = false;
 var displayIdx = 0 ;
 var timerID    = -1;
 var frameDelay  = 500;              // Delay between frames
 var lastDwell   = 750;              // Additional delay after last frame


  function set_levels(element) {
  
        var s = element.form.level;
        switch(element.value) {
        case "mxip":
        case "mnip":
        case "mnctp":
        case "mxctp":
            s.options.length = 5;
            s.options[4].value = "300";
            s.options[4].text = "FL 300";
            s.options[3].value = "400";
            s.options[3].text = "FL 240";
            s.options[2].value = "500";
            s.options[2].text = "FL 180";
            s.options[1].value = "600";
            s.options[1].text = "FL 140";
            s.options[0].value = "700";
            s.options[0].text = "FL 100";
            break;
        case "mxcat":
        case "mncat":
            s.options.length = 5;
            s.options[4].value = "150";
            s.options[4].text = "FL 450";
            s.options[3].value = "200";
            s.options[3].text = "FL 390";
            s.options[2].value = "250";
            s.options[2].text = "FL 340";
            s.options[1].value = "300";
            s.options[1].text = "FL 300";
            s.options[0].value = "400";
            s.options[0].text = "FL 240";
            break;
        case "cb":
            s.options.length = 2;
            s.options[0].value = "tops";
            s.options[0].text = "Tops";
            s.options[1].value = "cover";
            s.options[1].text = "Extent";
            break;
        case "swx":
             s.options.length = 2 ;
             s.options[0].value="midlvl" ;
             s.options[0].text="Mid Level" ;
             s.options[1].value="hilvl" ;
             s.options[1].text="High Level" ;
            break ;
         
        default:
            break;
        }
    }

    function make_filename(product, area, level, ihour, fhour) {
        //ihour + "/" + fhour + "/" +
        var filename =  area +"_" + product + "_" + level  + "_" + fhour + ".gif";
            //+ "_" + ihour + "_" + fhour
            //alert(filename) ;
        return filename;
    }

 

    // initialize selection
   
    // this should depend on availability of the most recent products
//    var last_ihour = '<? print ($last_ihour); ?>';
//    switch (last_ihour) {
//    case '00':
 //       f.ihour[0].checked = true;
 //       break;
 //   case '06':
 //       f.ihour[1].checked = true;
 //       break;
 //   case '12':
 //       f.ihour[2].checked = true;
 //       break;
 //   case '18':
 //       f.ihour[3].checked = true;
  //      break;
  //  default:
 //       break;
 //   }
 //   f.fhour[0].checked = true;
 //   f.area[0].checked = true;1
    // path to GIF files, assumes subdirectories HH/FF (issue and forecast time)
    imagedir = "gif ";
    // popup window sizes, depending on sector
    sizes = new Array();
    sizes['x'] = [1120, 720];
    sizes['h'] = [880, 840];
    sizes['i'] = [880, 800];
    sizes['l'] = [880, 720];
    sizes['b'] = [880, 780];
    

// Set the latest of "00", "06", "12", and "18"
  vtime = '<? print ($HH); ?>';
function btnReset() {
  if (vtime == "00") {
  document.ggGraphic1.vtime[0].checked = document.ggGraphic2.vtime2[0].checked = "true";
  } else if (vtime == "06") {
  document.ggGraphic1.vtime[1].checked = document.ggGraphic2.vtime2[1].checked = "true";
  } else if (vtime == "12") {
  document.ggGraphic1.vtime[2].checked = document.ggGraphic2.vtime2[2].checked = "true"
  } else if (vtime == "18") {
  document.ggGraphic1.vtime[3].checked = document.ggGraphic2.vtime2[3].checked = "true"
  }
}
//ensures that vtime and radio button are consistent on "refresh" and "shift-refresh" [unused?]
function saveSelected() {
  if (eval("document.ggGraphic1.vtime[0].checked") == true) {
    vtime = "00";
  } else if (eval("document.ggGraphic1.vtime[1].checked") == true) {
    vtime = "06";
  } else if (eval("document.ggGraphic1.vtime[2].checked") == true) {
    vtime = "12";
  } else if (eval("document.ggGraphic1.vtime[3].checked") == true) {
    vtime = "18";
  }
}
// Preload and assign map mouseover images

if (document.images) {
    npimg = new Image();
		npimg.src = '/static/images/sat/intl/npole.gif';
    npimg_h = new Image();
		npimg_h.src = '/static/images/sat/intl/npole_H.gif';
    npimg_i = new Image();
		npimg_i.src = '/static/images/sat/intl/npole_I.gif';
    npimg_g = new Image();
		npimg_g.src = '/static/images/sat/intl/npole_G.gif';
    mercimg = new Image();
		mercimg.src = '/static/images/sat/intl/merc.gif';
    mercimg_a = new Image();
		mercimg_a.src = '/static/images/sat/intl/merc_A.gif';
    mercimg_b1 = new Image();
		mercimg_b1.src = '/static/images/sat/intl/merc_B1.gif';
    mercimg_c = new Image();
		mercimg_c.src = '/static/images/sat/intl/merc_C.gif';
    mercimg_d = new Image();
		mercimg_d.src = '/static/images/sat/intl/merc_D.gif';
    mercimg_e = new Image();
		mercimg_e.src = '/static/images/sat/intl/merc_E.gif';
    mercimg_f = new Image();
		mercimg_f.src = '/static/images/sat/intl/merc_F.gif';
    mercimg_m = new Image();
		mercimg_m.src = '/static/images/sat/intl/merc_M.gif';
		spimg = new Image();
		spimg.src = '/static/images/sat/intl/spole.gif';
    spimg_j = new Image();
		spimg_j.src = '/static/images/sat/intl/spole_J.gif';
    spimg_k = new Image();
		spimg_k.src = '/static/images/sat/intl/spole_K.gif';
    
    dot = new Image();
		dot.src = '/static/images/sat/intl/dots/radio.gif';
    dotgld = new Image();
		dotgld.src = '/static/images/sat/intl/dots/radiogld.gif';
    dotgldsel = new Image();
		dotgldsel.src = '/static/images/sat/intl/dots/radiogldmd.gif';
    dotglddel = new Image();
		dotglddel.src = '/static/images/sat/intl/dots/radiogldsm.gif';

// Debugging 7200 secs = 2hr
//document.write("diff00 ="+diff00+" | ");
//document.write(diff00+diff06+diff12+diff18);
//document.write(buChk);
initNorm() ;
//if (buChk == 'false') {initNorm();}
//  else if (Chk[0] == vtime || Chk[1] == vtime || Chk[2] == vtime || Chk[3] == vtime) {initGray();}
//else {initNorm();}
//}
// If "buChk" exists and Tm is a back-up time ... will allow for color change
//function checkBack(Tm) {
//if (buChk != 'false') {
//  if (Tm == Chk[0] || Tm == Chk[1] || Tm == Chk[2] || Tm == Chk[3])
//   {
//    backUp();
//    bwOnly = "True";
//    }else{
//    backNorm();
//    bwOnly = "";
//    }
//  }
}

function initNorm() 
{

    btn_a = new Image();
        btn_a.src = '/static/images/sat/intl/btns/btn_A.gif';
    btn_aem = new Image();
        btn_aem.src = '/static/images/sat/intl/btns/btn_A_em.gif';
    btn_b1 = new Image();
        btn_b1.src = '/static/images/sat/intl/btns/btn_B1.gif';
    btn_b1em = new Image();
        btn_b1em.src = '/static/images/sat/intl/btns/btn_B1_em.gif';
    btn_c = new Image();
        btn_c.src = '/static/images/sat/intl/btns/btn_C.gif';
    btn_cem = new Image();
        btn_cem.src = '/static/images/sat/intl/btns/btn_C_em.gif';
    btn_d = new Image();
        btn_d.src = '/static/images/sat/intl/btns/btn_D.gif';
    btn_dem = new Image();
        btn_dem.src = '/static/images/sat/intl/btns/btn_D_em.gif';
    btn_e = new Image();
        btn_e.src = '/static/images/sat/intl/btns/btn_E.gif';
    btn_eem = new Image();
        btn_eem.src = '/static/images/sat/intl/btns/btn_E_em.gif';
    btn_f = new Image();
        btn_f.src = '/static/images/sat/intl/btns/btn_F.gif';
    btn_fem = new Image();
        btn_fem.src = '/static/images/sat/intl/btns/btn_F_em.gif';
    btn_g = new Image();
        btn_g.src = '/static/images/sat/intl/btns/btn_G.gif';
    btn_gem = new Image();
        btn_gem.src = '/static/images/sat/intl/btns/btn_G_em.gif';
    btn_h = new Image();
        btn_h.src = '/static/images/sat/intl/btns/btn_H.gif';
    btn_hem = new Image();
        btn_hem.src = '/static/images/sat/intl/btns/btn_H_em.gif';
    btn_i = new Image();
        btn_i.src = '/static/images/sat/intl/btns/btn_I.gif';
    btn_iem = new Image();
        btn_iem.src = '/static/images/sat/intl/btns/btn_I_em.gif';
    btn_j = new Image();
        btn_j.src = '/static/images/sat/intl/btns/btn_J.gif';
    btn_jem = new Image();
        btn_jem.src = '/static/images/sat/intl/btns/btn_J_em.gif';
    btn_k = new Image();
        btn_k.src = '/static/images/sat/intl/btns/btn_K.gif';
    btn_kem = new Image();
        btn_kem.src = '/static/images/sat/intl/btns/btn_K_em.gif';
    btn_m = new Image();
        btn_m.src = '/static/images/sat/intl/btns/btn_M.gif';
    btn_mem = new Image();
        btn_mem.src = '/static/images/sat/intl/btns/btn_M_em.gif';   
}
// re-Initializes normal images
function backNorm() 
{
    initNorm();
    document.images.albl.src=btn_a.src;
    document.images.blbl.src=btn_b1.src;
    document.images.flbl.src=btn_f.src;
    document.images.hlbl.src=btn_h.src;
    document.images.ilbl.src=btn_i.src;
    document.images.jlbl.src=btn_j.src;
    document.images.mlbl.src=btn_m.src;
}

//function setFcstHr(hour)
//{
//    document.getElementById("fcstHr").value = hour ;
//}
// Pops up gg image window
function ggWindow(area) 
{

document.ggGraphic1.area.value = area ;
document.ggGraphic1.submit() ;
return ;
var selectedProduct = document.getElementById("product") ;
var selectedProductText = selectedProduct.options[selectedProduct.selectedIndex].text ;
var level = document.getElementById("level").value ;

    //if (window.bwOnly) { alert(bwOnly); var mypage = "/static/images/awclogos/jump/jumpback.jpg"}
    if ((window.bwOnly) && (clr == "CL")) 
    { 
    //4  alert(bwOnly);
      var mypage = "/static/images/awclogos/jump/jumpback.jpg";
      var w = "400";
      var h = "400";
      var titlepos = "30";
      var textLabel = "<div align=\"center\">The "+vtime+"Z Color Images are not Current<BR><BR>This will typically occur during our Quarterly Back-Up test<BR>with WAFC London where they produce and transmit our High Level product<BR><BR>Please use "+vtime+"Z B&W for Current product</div>";
    } else {
      var mypage = gifdir + make_filename(selectedProduct.value,area,level,"00","06") ;
      alert (mypage) ;
      var textLabel = "Double Click to Close";
      var titlepos = (w / 2) - 122;
      var textLabel = "Double Click to Close";
    }
    var winl = (screen.width - w) / 2;
    alert (winl) ;
    var wint = (screen.height - h) / 2;
    var clkh = (h - 19);
    var clkw = (w - 20);
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
    var ggWin = window.open(mypage, selectedProductText, winprops)
    ggWin.document.open();
    ggWin.document.write("<html><\head\><title>"+selectedProductText+"</title>");
    ggWin.document.write("<\SCRIPT LANGUAGE=\"JavaScript\">");
    ggWin.document.write("\<\!\-\-");
    ggWin.document.write("   if (document.images) \{");
    ggWin.document.write("");
    ggWin.document.write("ggimg = new Image\(\);");
    ggWin.document.write("ggimg.src = \'"+mypage+"\'; \}");
    ggWin.document.write("\/\/\-\-\>");
    ggWin.document.write("<\/SCRIPT>");
    ggWin.document.write("<\/head><body bgcolor=\"black\" ondblclick=\"javascript\:window.close()\"><SPAN STYLE=\"position\: absolute; LEFT\:0px; TOP\:0px; z-Index\:0\"><IMG SRC=\""+mypage+"\" HEIGHT="+h+" WIDTH="+w+" BORDER=0></SPAN><SPAN style=\"position\:absolute; LEFT\:"+titlepos+"px; TOP\:30px; z-Index\:1; color:ff3300; font-size:20pt;\">"+textLabel+"</SPAN></body></html>")
    ggWin.document.close();
    if (parseInt(navigator.appVersion) >= 4) 
    { 
        ggWin.window.focus(); 
    }
}
function setVtime(vt) 
{
  vtime = vt;
}
function shoVtime() 
{
  alert(vtime);
}
// Ensures selection in one form is reflected in the other
function changeRadio(org) 
{
  if (org == '1') 
  {
    for (i=0, n=document.ggGraphic1.elements.length; i<n; i++) 
    {
      if (document.ggGraphic1.elements[i].checked) 
      {
        document.ggGraphic2.elements[i].checked = document.ggGraphic1.elements[i].checked;
        break;
      } 
    }
  } else {
    for (i=0, n=document.ggGraphic2.elements.length; i<n; i++) 
    {
      if (document.ggGraphic2.elements[i].checked) 
      {
        document.ggGraphic1.elements[i].checked = document.ggGraphic2.elements[i].checked;
        break;
      } 
    }
  }  
}
// Status Bar msg
function setStatus(msg) 
{
    window.status = msg;
    return true;
}

function setValues(area,product,fcst_hr,level)
{

    
    if (fcst_hr == "")
    {
        fcst_hr = "06" ;
    }
    if (product == "")
    {
        product = "mxip" ;
    }
    if ( level == "")
    {
        level = "700" ;
    }
    document.getElementById("selectProduct").value = product ;
    //document.getElementById("area").value = area ;
    setArea(area) ;
    setFcstHr(fcst_hr) ;
    //document.getElementById("selectFcstHr").value= fcst_hr ;
    
    set_levels(document.getElementById("selectProduct")) ;
    document.getElementById("level").value = level ;
    document.getElementById("displayImage").className = area ;
    changeImage() ;
}   

function setArea(area)
{
    idx = 0 ;
    found = false ;
    var tmpArea = document.getElementById("selectArea").options ;
    while ((idx < (tmpArea.length -1)) && !found)
    {
        if(tmpArea[idx].value == area)
        {
        
            document.getElementById("selectArea").selectedIndex = idx ;
            found = true ;
        }
        idx = idx + 1 ;
    }
    if(!found)
    {
        document.getElementById("selectArea").selectedIndex = 1 ;
    }
}
function setIndexFcstHr(fcst_hr)
{
    document.getElementById("fcstHr").value= fcst_hr ;
}
function setFcstHr(fcst_hr)
{
    idx = 0 ;
    found = false ;
    var tmpHr = document.getElementById("selectFcstHr") ;
    
    
    while ((idx < (tmpHr.length -1)) && !found)
    {
    
        if(tmpHr.options[idx].value == fcst_hr)
        {
            document.getElementById("selectFcstHr").selectedIndex = idx ;
            found = true ;
        }
        idx = idx + 1 ;
    }
    if (!found)
    {
        document.getElementById("selectFcstHr").selectedIndex = 1;
    }
    
}
function changeImage()
{

    var fname = make_filename(document.getElementById("selectProduct").value,document.getElementById("selectArea").value,
                    document.getElementById("level").value,"00",document.getElementById("selectFcstHr").value) ;                
    document.getElementById("displayImage").src = gifdir + fname;
    document.getElementById("displayImage").className = document.getElementById("selectArea").value ;
}

function imageBigger()
{
   var width = document.getElementById("displayImage").width ;
   var height = document.getElementById("displayImage").height ;
   document.getElementById("displayImage").width = width + 100 ;
   document.getElementById("displayImage").height = height + 100;
   
}

function changeFcstHr(direction)
{
    idx = document.getElementById("selectFcstHr").selectedIndex ;
    var total = document.getElementById("selectFcstHr").length - 1 ;
    if(direction == "+")
    {
        
        if (idx < total)
        {
            displayIdx = document.getElementById("selectFcstHr").selectedIndex = idx + 1 ;
            changeImage() ;
        }
        else if (looping == 1)
        {
            displayIdx = document.getElementById("selectFcstHr").selectedIndex = 0 ;
            changeImage() ;
        }
    }
    else if (direction == "-")
    {
        if( idx > 0)
        {
            displayIdx = document.getElementById("selectFcstHr").selectedIndex = idx - 1 ;
            changeImage() ;
        }
    }
    else if (direction == "first")
    {
        displayIdx = document.getElementById("selectFcstHr").selectedIndex = 0 ;
        changeImage() ; 
    }
    else if (direction == "last")
    {
        displayIdx = document.getElementById("selectFcstHr").selectedIndex = total ;
        changeImage() ;
    }
}

function changeLevel(direction)
{
    idx = document.getElementById("level").selectedIndex ;
    if(direction == "+")
    {
        var total = document.getElementById("level").length - 1 ;
        if (idx < total)
        {
            document.getElementById("level").selectedIndex = idx + 1 ;
            changeImage() ;
        }
        
    }
    else
    {
        if( idx > 0)
        {
            document.getElementById("level").selectedIndex = idx - 1 ;
            changeImage() ;
        }
    }
}
function imageSmaller()
{
   var width = document.getElementById("displayImage").width ;
   var height = document.getElementById("displayImage").height ;
   document.getElementById("displayImage").width = width - 100 ;
   document.getElementById("displayImage").height = height - 100;

}

function loopImages() {
        var delay = frameDelay;

        timerID = -1;
        if (looping == 1) {
            changeFcstHr("+");

            if (displayIdx == (document.getElementById("selectFcstHr").length - 1)) {
                delay += lastDwell;
            }

            var newID = setTimeout('loopImages()', delay);
            if (timerID == -1) {
                timerID = newID;
            } else {
                clearTimeout(newID);
            }
        }
    }

    function stopLoop() {
        if (looping == 1) {
            looping = 0;
            if (timerID != -1) {
                clearTimeout(timerID);
                timerID = -1;
            }
        }

        return true;
    }

    function startLoop() {
        if (looping != 1) {
            // be sure to set looping first or loopImages() won't start
            looping = 1;
            if (timerID != -1) {
                clearTimeout(timerID);
                timerID = -1;
            }
            loopImages();
        }
        return true;
    }

    function toggleLoop() {
        if (looping == 1) {
            stopLoop();
            updateDisp();
        } else {
            if (displaySet.length != 0) {
                startLoop();
            }
            if (paused) {
                paused = false;
                document.time.loop.value = 'Play';
            }
        }
        return true;
    }

