var globalYear;
var globalFilename;
var globalVizType;

  $(document).ready(function(){
    document.getElementById("fileSelect").selectedIndex=0;
    document.getElementById("pluginSelect").selectedIndex=0;
    document.getElementById("dataType").selectedIndex=0;
    document.getElementById("year").style.display='none';
    document.getElementById("year").style.visibility="hidden";
    $("#fileSelect").change(function () {
      $("#fileSelect option:selected").each(function () {
        if($(this).hasClass('elevations')){
          document.getElementById("dataType").style.display='';
          document.getElementById("dataType").style.visibility="visible";
          document.getElementById("year").style.visibility='hidden';
          document.getElementById("year").style.display='none';
          $(".earthPlugin").css("display","");
          $(".earthPlugin").css("visibility","visible");
          $(".mapsPlugin").css("display","none");
          $(".mapsPlugin").css("visibility","hidden");
          document.getElementById("pluginSelect").selectedIndex=0;
          document.getElementById("dataType").selectedIndex=0;
          globalVizType="elevations";
        }
        else if($(this).hasClass('pies')){
          document.getElementById("pluginSelect").style.display="";
          document.getElementById("pluginSelect").selectedIndex=0;
          document.getElementById("dataType").style.display='none';//??
          $(".earthPlugin").css("display","");
          $(".earthPlugin").css("visibility","visible");
          $(".mapsPlugin").css("display","");
          $(".mapsPlugin").css("visibility", "visible");
          globalVizType="pies";          
        }
        else{
          document.getElementById("pluginSelect").style.display="";         
          document.getElementById("dataType").style.display="none";
          document.getElementById("dataType").selectedIndex=0;
          document.getElementById("year").style.display="none";
          document.getElementById("year").style.visibility="hidden";
          
          $(".earthPlugin").css("display","");
          $(".mapsPlugin").css("display","");
        }
        if($(this).hasClass('pies')){
          globalVizType="pies";
        }
        if($(this).hasClass('elevations')){
          globalVizType="elevations";
        }          
      });
    });
    $("#pluginSelect").change(function () {
      $("#pluginSelect option:selected").each(function () {
        if($(this).hasClass('earthPlugin')){
          document.getElementById("year").style.display='none';
          document.getElementById("year").style.visibility="hidden";
        }
        else if($(this).hasClass('mapsPlugin')){
          document.getElementById("year").style.display='';
          document.getElementById("year").style.visibility="visible";
        }
      });
    });
  });








   function checkYear(year){
      if((!isNumeric(year))||year>2063||year<2004){
        alert("please enter a number between 2004 and 2063");
        document.getElementById("year").value="Please select desired year";
        return 0;
      }
      else
        return 1;
    }


    function drawMaps(){
        document.getElementById("keyDiv").innerHTML="<img src='Screenshot-1.png'\\>";
        map = new google.maps.Map2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(0,0), 2);
        map.setUIToDefault();
//      alert("mapppapappa");
//        alert("http://cimearth.org/tools/utils/netlink.php?fname="+globalFilename+"&earth=0&year="+globalYear);
        if(globalVizType=="pies"){
            if(checkYear(globalYear)){
                var geoXml=new google.maps.GeoXml("http://cimearth.org/tools/utils/maps.php?fname="+globalFilename+"&earth=0&year="+globalYear+"&type=pie");
                //alert("http://cimearth.org/tools/utils/maps.php?fname="+globalFilename+"&earth=0&year="+globalYear+"&type=pie");
                map.addOverlay(geoXml);
            }
        }
        else
            alert("globalVizType is "+globalVizType);
   }
    function drawRegions(){        
        map = new google.maps.Map2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(0,0), 2);
        map.setUIToDefault();
        var geoXml=new google.maps.GeoXml("http://cimearth.org/tools/utils/maps.php?fname=noData.php&earth=0&year="+globalYear);
//            alert("http://128.135.219.194/utils/netlink.php?fname="+filename+"&earth=0&year="+year);
            map.addOverlay(geoXml);
     
    }

    function drawEarth(){
//      if(checkYear(globalYear)){
//        google.earth.createInstance('map_canvas', function(instance){
//          ge = instance;
//          ge.getWindow().setVisibility(true);
//          ge.getNavigationControl().setVisibility(ge.VISIBILITY_SHOW);
//          var url = "http://128.135.219.194/utils/netlink.php?fname="+file+"&earth=0&year=+"year;
//          google.earth.fetchKml(ge, url, finished);
//        }, failureCB);
          document.getElementById("keyDiv").innerHTML="";
          google.earth.createInstance('map_canvas', initCB, failureCB);
//      }
   }


    function finished(object)
    {
//      if (!object) {
        // wrap alerts in API callbacks and event handlers
        // in a setTimeout to prevent deadlock in some browsers
//        setTimeout(function() {
//          alert('Bad or null KML.');
//        }, 0);
//        return;
//      }
      ge.getFeatures().appendChild(object);
//      var la = ge.createLookAt('');
//      la.set(0, 0, 25, ge.ALTITUDE_RELATIVE_TO_GROUND, 180, 60, 500);
//      ge.getView().setAbstractView(la);         
    }

    function initCB(instance){
      ge = instance;
      ge.getWindow().setVisibility(true);
      ge.getNavigationControl().setVisibility(ge.VISIBILITY_SHOW);
      ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, false);
      alert(globalFilename);
      if(globalVizType=="pies")
          var url = "http://cimearth.org/tools/utils/maps.php?fname="+globalFilename+"&earth=1";
      if(globalVizType=="elevations"){
          var url= "http://cimearth.org/tools/utils/elevatedEarthNetLink.php?fname="+globalFilename;
      }
      //alert(url);
      google.earth.fetchKml(ge, url, finished);
    }




    function failureCB(errorCode) {
        alert("failure to make a google earth has occurred.");
        alert(errorCode);
    }



    function init(filename, dataType, pluginType, year, fileIndex){
      if(!verifyForm()){
          return;   
      }
      if(globalVizType=="elevations")
          filename = dataType+"means_"+filename;
      globalFilename=filename;
      globalYear=year;
      clearDiv(document.getElementById("map_canvas"));
      if(pluginType=='maps')
         google.load("maps", "2.x", {"callback" : drawMaps});
      else if(pluginType=='standalone_maps')
        window.location = getWindowLocation(globalVizType);
      else
         google.load("earth", "1", {"callback" : drawEarth});
    }
    function clearDiv(fullDiv){
      while(child=fullDiv.firstChild)
        fullDiv.removeChild(child);
    }
    function drawRegionMap(){
        google.load("maps", "2", {"callback" : drawRegions});
    }
    function getWindowLocation(vizType){
        if(vizType=="elevations")
            return "http://www.cimearth.org/tools/utils/elevatedEarthNetLink.php?fname="+globalFilename;
        if(vizType=="pies")
            return "http://www.cimearth.org/tools/utils/maps.php?fname="+globalFilename+"&earth=1"

    }
    function verifyForm(){
        if(document.getElementById("fileSelect").selectedIndex==0){
            alert("Please Select a File");
            return false;
        }
        if(globalVizType=="elevations"){
            if(document.getElementById("dataType").selectedIndex==0){
                alert("Please select a data type");
                return false;
            }
        }
        if(document.getElementById("pluginSelect").selectedIndex==0){
            alert("Please Select a Plugin");
            return false;
        }
        return true;
    }