history.navigationMode = 'compatible';
$(document).ready( onPageLoad );

///////////////////////////////////////////////////////
// FUNCTIONS
///////////////////////////////////////////////////////
function onPageLoad() {
  $("#previous").bind('click', function() {
      var enddate = $("#begin_date").val();  // use current begin date as new end date
//      var formURL = $("#controlForm").attr("action");
      var url = window.location.href;
						var tmp = url.split('?');
						var formURL = tmp[0];
						var plotlist = $("#plotlist").val();
						if ( plotlist == null )  plotlist = 'all.all.all';						
						plotlist = removeRedundantPlots(plotlist);
 					plotlist = formatPlotNames(plotlist);
  				var url = formURL + '?action=previous' + '&enddate=' + enddate + '&plotlist=' + plotlist;;
		  		window.location.href = url;
		});
		
  $("#next").bind('click', function() {
      var begindate = $("#end_date").val();  // use current end date as new begin date
      var url = window.location.href;
						var tmp = url.split('?');
						var formURL = tmp[0];
						var plotlist = $("#plotlist").val();
						if ( plotlist == null )  plotlist = 'all.all.all';
						plotlist = removeRedundantPlots(plotlist);
 					plotlist = formatPlotNames(plotlist);
  				var url = formURL + '?action=next' + '&begindate=' + begindate + '&plotlist=' + plotlist;;
		  		window.location.href = url;		});
		
  $(".showRequestedPlotsButton").bind('click', function() {
      var begindate = $("#begindate").val();
      var enddate = $("#enddate").val();
      var url = window.location.href;
						var tmp = url.split('?');
						var formURL = tmp[0];
						var plotlist = $("#plotlist").val();
						if ( plotlist == null )  plotlist = 'all.all.all';
						plotlist = removeRedundantPlots(plotlist);
 					plotlist = formatPlotNames(plotlist);
  				var url = formURL + '?action=View' + '&begindate=' + begindate + '&enddate='+ enddate + '&plotlist=' + plotlist;;
		  		window.location.href = url;
		});		
		
		$("#plotlist").bind('change', function() {
      var plots = $("#plotlist option").get();
						var endIndentLevel = 99;
      for (i=0; i < plots.length; i++) {
        if ( plots[i].selected ) {
  								var indentLevel = getIndentLevel(plots[i].value);
										if ( indentLevel == 2 ) { // site
										  endIndentLevel = 2;
										} else if ( indentLevel == 1 ) { // instrument
										  endIndentLevel = 1;
										} else {
											 continue;
										}
										for (j=i+1; j < plots.length; j++) {
											 if ( getIndentLevel(plots[j].value) <= endIndentLevel ) break;
										  $(plots[j]).attr('selected', 'selected');
										} 
								} 

						}
  });
		
		
		/*****************
		$(".showhide").bind('click', function(event) {
				// get row index																																				
    var id = $(event.target).attr('id');
				var tmp = id.split('_');
				var rowindex = tmp[1];
				
				// show/hide images in row
				if ( $("#showhide_"+rowindex+":checked").val() == 'show' )  {
      $("#row" + rowindex + " img").show();
				} else {
      $("#row" + rowindex + " img").hide();					
				}
		});
		***************/
		
		$("#showPlotMenu").bind('click', function() {
    $("#selectPlotsContainer2").toggle();
  });
		$("#closeButton").bind('click', function() {
    $("#selectPlotsContainer2").toggle();
  });
		
		var newSize = document.getElementById("plotlist").options.length + 2;
		var newHeight = parseInt(newSize + 8, 10) + "em";
		$("#plotlist").attr("size", newSize);
		$("#selectPlotsContainer2").css("height", newHeight);

  $(".dateSelector").datepicker({ 
      showOn: "button", 
      buttonImage: "/webcode/javascript/datepicker-calendar-button.gif", 
      duration: '',
						dateFormat: 'yy-mm-dd',
      changeMonth: true,
      changeYear: true,						
      buttonImageOnly: true 
  }); 
		
		
}

//-------------------------------------------------------------------------
function getIndentLevel(plot) {
	  var n;
	  if (plot == 'all') {
				 n = 0;
			} else {
  	  var a = plot.split('.');		
					n = a.length
			}
  	return n;			
}

//-------------------------------------------------------------------------
function removeRedundantPlots(plotlist) {
  // remove redundant plots from list if a parent instrument or site is listed 
				
		// DEBUG - NOT IMPLEMENTED YET!!!
  
  return plotlist;
}

//-------------------------------------------------------------------------
function formatPlotNames(plotlist) {
	 // add 'all' for instrument names and plot names - i.e., change
		// rig to rig.all.all.  Another example: rig.tower - change to rig.tower.all
				
		var tmp;
		for (i=0; i < plotlist.length; i++) {
		  tmp = plotlist[i].split('.');
				if (tmp.length == 1) { // site name only
				  plotlist[i] = tmp[0] + '.all.all';
				} else if (tmp.length == 2) { // site.instrument
				  plotlist[i] = tmp[0] + '.' + tmp[1] + '.all';				
				}
		}

		return plotlist
}
