//*****************************************************************************
//
// USGS Transparent Layers Viewer Extension 
// Created By:  Chris Rusanowski
// Installation:
//   -Copy USGS_Transparent_Layers.js into the javascript directory of the HTML Viewer
//   -Include the javascript/USGS_Transparent_Layers.js in MapFrame.htm 
//     <script type="text/javascript" language="JavaScript" src="javascript/USGS_Transparent_Layers.js"></script>
//   -Add a call to setLayerTransparency() in afterMapRefresh in MapFrame.htm
//     if (useLayerTransparency) {setLayerTransparency();}
//   -Modify the USGS_OGC.js file to add the style="-moz-opacity:1.0;filter:alpha(opacity=100);" to the 
//     string that creates the background image layers.  (If this does not exist then IE does not create
//     the style parameters for the image, and you will get an error...)  The line below is copied from 
//     a modified USGS_OGC.js file:  (Note this is in the first function AddUSGSOGCLayers.)
//		   var content = '<img name="BackgroundMap' + i + '" alt="Background Map ' + i + '" onload="FinishedLoading();" src="images/pixel.gif" hspace="0" vspace="0" border="0" width="' + mWidth + '" height="' + mHeight + '" style="-moz-opacity:1.0;filter:alpha(opacity=100);"> ';
//   -Add a button to the toolbar.htm that calls parent.MapFrame.chgLayerTransparency().
//     (Actually add a button to USGS_Toolbar.js... The code below is and example from that JS file.)
//      if (parent.MapFrame.useLayerTransparency)  { miscCount++; }
//        if (parent.MapFrame.useLayerTransparency) {
//          LocalString += WriteButton( MakeButton2( "usgs_images/LayerTransparency.gif", 'Layer Transparency Tool', "Layer Transparency Tool", "parent.MapFrame.chgLayerTransparency();", 'status=\'Layer Transparency Tool\'', "", false ) );
//          buttonCol++;
//          LocalString += Table_Structure(buttonCol,miscCount);
//        }
//   -Copy the setLayerTransparency.htm fil to the main directory of the HTML Viewer
//   -Copy the webfx_slider folder to the main viewer folder (This has all the code for doing the slider...)
//
// NOTE: This requires the USGS_OGC.js be installed in the viewer!
// NOTE: To use this w/out sliders, change chgLayerTransparency to uncomment the _noSlider pages
//       and then the webfx_slider folder is not needed.
//
//*****************************************************************************
// useLayerTransparency determines if the toolbar should show this button or not.
var useLayerTransparency = true;

//LayerTransparencySetting holds a value for each background image layer of the transparency
//which is a number between 0 and 1 (1 being opaque, 0 being transparent)
var LayerTransparencySetting = new Array();

//LayerTransparencyInitialized is a boolean check that is used by setLayerTransparency
//to see if the LayerTransparencySetting array has been initialized.  If the 
//array is not initialized, it will make a call to the function that does so.
var LayerTransparencyInitialized = false;

//*****************************************************************************
// Function setLayerTransparency
// This function cycles through all the background image layer objects and 
// sets the style for that layer using the LayerTransparencySetting array
//*****************************************************************************
function setLayerTransparency() {
  if (!LayerTransparencyInitialized) { initLayerTransparency(); }
  Background_Image_Counter = 0;
  Number_OGC_Layers = SRC_Layer_URL.length;
  for (var i=0;i<Number_OGC_Layers;i++) {
    if (SRC_Layer_URL[i]!=null) {
      if (isIE) {
        //Create a string with That sets the IE transparency style  which is 100* the value in the LayerTransparencySetting
        TempString2 = 'document.BackgroundMap' + Background_Image_Counter + '.filters.alpha.opacity = ' + (LayerTransparencySetting[Background_Image_Counter] * 100) + ';';
        //Evaluate that string (execute the JavaScript)
        //alert(TempString2);
        eval(TempString2);
      } else {
        //Create a string with That sets the Mozilla transparency style
        TempString2 = 'document.BackgroundMap' + Background_Image_Counter + '.style.MozOpacity = ' + LayerTransparencySetting[Background_Image_Counter] + ';';
        //Evaluate that string (execute the JavaScript)
        eval(TempString2);
      }
      //LayerTransparencySetting[Background_Image_Counter] = 1.0;
      Background_Image_Counter++;
    }
  }
}

//*****************************************************************************
// Function chgLayerTransparency
// This function presents the user with a list of all the layers in a Drop
// down, and allows the user to set each one to a percentage value.
//*****************************************************************************
function chgLayerTransparency() {
  //Change to the commented out _noSlider references if you wish to use a version of this w/out the webfx slider.
  if ((useExternalWindow) || (!useTextFrame)) {
    window.open("http://" + hostName + appDir + "setLayerTransparency.htm","Set_Layer_Transparency","width=425,height=300,scrollbars=yes,resizable=yes");
    //window.open("http://" + hostName + appDir + "setLayerTransparency_noSlider.htm","Set_Layer_Transparency","width=425,height=300,scrollbars=yes,resizable=yes");
	} else {
    parent.TextFrame.document.location = "http://" + hostName + appDir + "setLayerTransparency.htm";
    //parent.TextFrame.document.location = "http://" + hostName + appDir + "setLayerTransparency_noSlider.htm";
	}
}

//*****************************************************************************
// Function initLayerTransparency
// This function will initialize the transparency array and set the check to true
//*****************************************************************************
function initLayerTransparency() {
  Background_Image_Counter = 0;
  Number_OGC_Layers = SRC_Layer_URL.length;
  for (var i=0;i<Number_OGC_Layers;i++) {
    if (SRC_Layer_URL[i]!=null) {
      LayerTransparencySetting[Background_Image_Counter] = 1.0;
      Background_Image_Counter++;
    }
  }
  LayerTransparencyInitialized = true;
}

//*****************************************************************************
// Function getLayersTransparency
// This function return the layers for transparency as a selection field
//*****************************************************************************
function getLayersTransparency() {
  Temp_String = "";
  Background_Image_Counter = 0;
  //Get the Current Scale from USGS Scale Tool
  var CenterAdjust = Math.abs(eRight - eLeft);
  var CurrentMapScale = HaversineScale((eLeft + CenterAdjust), eBottom, (eLeft + CenterAdjust), eTop, mHeight, ScaleToolDPI);
  //Cycle through the Categories from the Category_Order array
	for (var i=Category_Order.length-1;i>=0;i--) {
    if (Category_Layer_Order[i].length>0) {
      //For each category, get the Layer_Order and Layer_Visibility and split into some temp arrays.
      TempArray = Category_Layer_Order[i].split(',');
      TempArray2 = Category_Layer_Visibility[i].split(',');
      //Cycle through the layers for this current category in Layer_Order
	    for (var j=TempArray.length-1;j>=0;j--) {
        //Check the layer's scale factor and extent to see if we should display it
        if (Check_Layer_Extent_and_Scale(TempArray[j], eLeft, eBottom, eRight, eTop, CurrentMapScale)) {
          //Passes Extent/Scale check
          //Now display the information from that layer if it is visible 
          if (TempArray2[j]=='true') {
            Temp_String += '<option value="' + Background_Image_Counter + '">' + SRC_Layer_Name[TempArray[j]] + '</option>';
          }
        }
        Background_Image_Counter++;
      }
    }
  }
  return Temp_String;
}

//*****************************************************************************
// Function modifyLayersTransparency
// This function sets the actual value for transparency and modifies the layer style
//*****************************************************************************
function modifyLayersTransparency(OpacityPercent, BackgroundLayer) {
  LayerTransparencySetting[BackgroundLayer] = OpacityPercent/100;
}