var regionTitle;
var nynjregion;
var njregion;
var is_ny = true;
var pr_param;
var pic;
var act;
var action;
var pos_x = 0;
var pos_y = 0;
//------------------------------------------------------

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

// USED
function showhelp() {
	document.getElementById('helptext').style.display='block';
	document.getElementById('helpbutton').style.display='none';
}

// USED
function presentLoadup() {
  set_banner_hide_switch(false);
  set_banner_client_control(null);
  banner_load();
  window.focus();
  loadAllCookies();

  pic=document.getElementById('main_img');
  regionTitle = document.getElementById('regionTitle');
  is_ny = window.prefs.rtregion == 'njreg' ? false : true;
  pr_param = window.prefs.rtparam  ? window.prefs.rtparam : 'WSPD';
  paramCtl = document.getElementById('param');
  for (var i = 0; i< paramCtl.length; i++) {
	  if (paramCtl.options[i].value == pr_param) {
		  paramCtl.options[i].selected=true;
		}
	}
  regionTitle.innerHTML = is_ny ? 'Manhattan Waters' : 'New Jersey Coast';
  changeImage();
  writeSessionCookie('PARAM', pr_param);
  writeSessionCookie('REGION', reg_part);
  writeSessionCookie('mouseY', pos_y);
  writeSessionCookie('mouseX', pos_x);
}
//------------------------------------------------------

// NOT USED
/*function openDesc() {

var desclink=document.getElementById('desclink');
var paramCtl=document.getElementById('param');
var link='desc_'+paramCtl.value + '.html';
var w = window.open(link, "Plot desription", "WIDTH=400,HEIGHT=300");

if (w==null) {
desclink.href='desc_'+paramCtl.value + '.html';
}
}
function openCredit() {
var creditlink=document.getElementById('creditlink');
var paramCtl=document.getElementById('param');
var link='credit_'+paramCtl.value + '.html';
var w = window.open(link, "Acknowledgements", "WIDTH=400,HEIGHT=300");
if (w==null) {
creditlink.href='credit_'+paramCtl.value + '.html';
}
}*/

// USED
function presentChangeRegion() {
  regionTitle.innerHTML = is_ny ? 'Manhattan Waters' : 'New Jersey Coast';
	changeImage();
}


//-------------------------------------------------------------------------------
function changeImage() {
reg_part = is_ny ? 'nynj' : 'nj';
pr_param=document.getElementById('param').value;
d = document.getElementById('map_description');
d.href="desc_" + pr_param + ".pdf";
param_name = { WSPD: 'Winds', WTMP: 'Water Temperature', SALT: 'Salinity', ELEV: 'Water Level', CURR: 'Currents'};
d.innerHTML= 'Map Description: ' + param_name[pr_param];
source='../images/PRESENT/' + reg_part +'_' + pr_param + '.png';
document.getElementById('main_img').src=source;
}
//-------------------------------------------
function presentChangeParam() { 
	changeImage();
}
//----------------------------------------------------------------

// not used
function presentChangeAction() {
	reg_part = is_ny ? 'nynj' : 'nj';
var map_param = pr_param;
if (map_param !== "CURR")
{
	var map_region = reg_part + 'reg';
	var map_mousex = pos_x;
	var map_mousey = pos_y;
	loop_sid_array(map_param, map_region, map_mousex, map_mousey, true);
}
}


// USED
function buttonClick(param) {

for (i=0; i<document.selectOptions.param.length; i++) 
{
if (document.selectOptions.param.options[i].value==param) 
  {
  document.selectOptions.param.selectedIndex=i;
  }
	reg_part = is_ny ? 'nynj' : 'nj';
changeImage();
}


}

// USED
function clearDropDown(d) {
  for (var i=d.options.length-1; i >= 0; i--) {
    d.options[i]=null;
  }
}
var sourceCtl;
var paramCtl;
var Pic;
var startCtl;
var endCtl;
var unitsCtl;
function plotByStationLoadup(id,type, begin, finish, units) {
  sourceCtl = document.getElementById('source');
  paramCtl = document.getElementById('param');
  Pic=document.getElementById('main_pic');
  Pic2=document.getElementById('second_pic');
  sodar_title=document.getElementById('sodar_title');
  startCtl=document.getElementById('start');
  endCtl=document.getElementById('end');
  startCtl.value=begin;
  endCtl.value=finish;

  unitsCtl=document.getElementById('units'); 
  unitsCtl.value=units;
  clearDropDown(sourceCtl);
  for (var i = 0; i < presentSources.length; i++) {
    sourceCtl.options[i] = new Option(sidNames[presentSources[i].name], presentSources[i].name);
    if (presentSources[i].name == id) {
      sourceCtl.options[i].selected = true;
      setParam(i);
    }
  }
  for (var i = 0; i < paramCtl.length; i++) {
  if (paramCtl.options[i].value == type) {
    paramCtl.options[i].selected=true; 
   }
 }
plotImage();
} 

// USED
function plotByTypeLoadup(id,type, begin, finish, units) {
	alert(2);
  sourceCtl = document.getElementById('source');
  paramCtl = document.getElementById('param');
  Pic=document.getElementById('main_pic');
  Pic2=document.getElementById('second_pic');
  //sodar_title=document.getElementById('sodar_title');
  startCtl=document.getElementById('start');
  endCtl=document.getElementById('end');
  startCtl.value=begin;
  endCtl.value=finish;
  sodar_title.innerHTML=' ';
  unitsCtl=document.getElementById('units');
  unitsCtl.value=units;
  clearDropDown(paramCtl);
  for (var i = 0; i < presentParams.length; i++) {
    paramCtl.options[i] = new Option(paramNames[presentParams[i].name], presentParams[i].name);
    if (presentParams[i].name == type) {
      paramCtl.options[i].selected = true;
      setSources(i);
    }
  }
  for (var i = 0; i < sourceCtl.length; i++) {
  if (sourceCtl.options[i].value == id) {
    sourceCtl.options[i].selected=true;
   }
 } 

plotImage();
}

// USED
function setParam(index) {
  
  var params = presentSources[index].params;
  clearDropDown(paramCtl); 
  for (var i = 0; i < params.length; i++) {
    paramCtl.options[i] = new Option(paramNames[params[i].name], params[i].name);    
  }
 plotImage();
}

// USED
function changeSource() {
 setParam(sourceCtl.selectedIndex);
 //plotImage();
}

// USED
function plotImage() {
var id=sourceCtl.options[sourceCtl.selectedIndex].value;
var type=paramCtl.options[paramCtl.selectedIndex].value;
var start=startCtl.value;
var end=endCtl.value;
var units=unitsCtl.options[unitsCtl.selectedIndex].value;
var time=document.getElementById('time').value;
if ((id=='STHOWE1')&&(type=='WSPD')) {
Pic2.src='http://hudson.dl.stevens-tech.edu/maritimeforecast/images/PRESENT/sodar.jpg';
sodar_title.innerHTML='Time is EST (GMT-5) with no Daylight Savings';
}
else{
Pic2.src='http://hudson.dl.stevens-tech.edu/maritimeforecast/images/grey.jpg';
sodar_title.innerHTML='';
}
if ((type=='WSPD')||(type=='WDIR')||(type=='WD')) {
Pic.src='winds.php?type='+type+'&id='+id+'&units='+units+'&start='+start+'&end='+end+'&location='+sourceCtl.options[sourceCtl.selectedIndex].text+'&parameter='+ paramCtl.options[paramCtl.selectedIndex].text+'&time='+time;
}
else {
Pic.src='plot.php?type='+type+'&id='+id+'&units='+units+'&start='+start+'&end='+end+'&location='+sourceCtl.options[sourceCtl.selectedIndex].text+'&parameter='+ paramCtl.options[paramCtl.selectedIndex].text+'&time='+time;
}
}

// USED
function changeParam() {
var id=sourceCtl.options[sourceCtl.selectedIndex].value;
 setSources(paramCtl.selectedIndex, id);
 plotImage();
}

// USED
function setSources(index,id) {
//alert(id);
  //var id=sourceCtl.options[sourceCtl.selectedIndex].value;
  var sources = presentParams[index].stations;
  clearDropDown(sourceCtl);
  for (var i = 0; i < sources.length; i++) {
    sourceCtl.options[i] = new Option(sidNames[sources[i].name], sources[i].name);
	  if (sourceCtl.options[i].value == id) {
	  sourceCtl.options[i].selected=true;
	  }
				 
  }
}

// USED
function QueryData() {
var id=sourceCtl.options[sourceCtl.selectedIndex].value;
var type=paramCtl.options[paramCtl.selectedIndex].value;
var start=startCtl.value;
var end=endCtl.value;
var units=unitsCtl.options[unitsCtl.selectedIndex].value;
var time=document.getElementById('time').value;
var url;
if ((paramCtl.options[paramCtl.selectedIndex].value=='WSPD')||(paramCtl.options[paramCtl.selectedIndex].value=='WDIR')) {
url='query_wind_dir.php?units='+units+'&id='+id+'&d1='+start+'&d2='+end+'&action=data&time='+time+'/data.xls';
}
else {
url='query.php?type='+type+'&id='+id+'&units='+units+'&start='+start+'&end='+end+'&location='+sourceCtl.options[sourceCtl.selectedIndex].text+'&parameter='+ paramCtl.options[paramCtl.selectedIndex].text+'&time='+time+'/data.xls';
}
//querywindow=window.open(url, "Query Data", "height=600, width=150");
var x = document.getElementById('query');
x.href=url;
//alert(x.href);
}
