/*****************************************************************
	Time Series Navigation and Animation Functions

	Written by Alex Prusevich - alex.proussevitch@unh.edu

	Updated April, 2010
******************************************************************/

	/* Global Variables */

var day_str = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31'];
var month_str = ['None','January','February','March','April','May','June','July','August','September','October','November','December'];

var month_28 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28];
var month_29 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29];
var month_30 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
var month_31 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];

var URL_img = "/images/explorer/";
var Start_Anm = new Image(110,37); Start_Anm.src = URL_img + "Start_Anm.gif";
var Stop_Anm = new Image(110,37); Stop_Anm.src = URL_img + "Stop_Anm.gif";
var Faster = new Image(55,37); Faster.src = URL_img + "Faster.gif";
var Faster_p = new Image(55,37); Faster_p.src = URL_img + "Faster-p.gif";
var Slower = new Image(55,37); Slower.src = URL_img + "Slower.gif";
var Slower_p = new Image(55,37); Slower_p.src = URL_img + "Slower-p.gif";
var Empty_Im = new Image(48,24); Empty_Im.src = URL_img + "empty.gif";

//////////////////////////////////////////////////////////////////
///////////////   Time Series Navigation Object   ////////////////

function tsNavObj() {
  this.group	= new Array(3);
  this.tsScale	= null;
  this.dates	= null;
  this.date	= {'year':null,'month':null,'day':null};
  this.date_max	= {'year':null,'month':null,'day':null};
  this.strt_date= {'year':null,'month':null,'day':null};
  this.end_date	= {'year':null,'month':null,'day':null};

  this.anim	= false;		/// Animation variables
  this.animList	= null;
  this.animImg	= null;
  this.animSrc	= null;
  this.tm_step	= 0;
  this.i_anim	= 0;
  this.anim_r	= 0;
  this.animTime	= 0;
}

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

tsNavObj.prototype.load = function(tsScale) {
  if (!tsScale) return;

  this.tsScale = tsScale;

  var formObj =	document.date;
  this.group  =	[[formObj.year,formObj.month,formObj.day],
		 [formObj.start_year,formObj.start_month,formObj.start_day],
		 [formObj.end_year,formObj.end_month,formObj.end_day]];

  this.date_max.year  = this.dates.year.length - 1;
  this.date_max.month = this.dates.year[this.date_max.year].month.length - 1;
  this.date_max.day   = this.dates.year[this.date_max.year].month[this.date_max.month].day.length - 1;

  this.strt_date.year = (!tsScale.match('yearly'))?this.date_max.year:0;
  this.strt_date.month= (tsScale == 'daily')?this.date_max.month:0;
  this.strt_date.day  = 0;

  this.end_date.year  = this.date_max.year;
  this.end_date.month = this.date_max.month;
  this.end_date.day   = this.date_max.day;

  if (!this.date)
    this.date 	= {'year':this.date_max.year,'month':this.date_max.month,'day':this.date_max.day};

  this.setYear(this.group[0],this.date);
  this.setYear(this.group[1],this.strt_date);
  this.setYear(this.group[2],this.end_date);
};

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

tsNavObj.prototype.setYear = function(group,ymd) {
  if (!this.tsScale.match('_clim')) {
    group[0].length = this.dates.year.length;
      for (var y=0; y<this.dates.year.length; y++) {
        group[0].options[y].value = this.dates.year[y].val;
        group[0].options[y].text  = this.dates.year[y].val;
      }
    group[0].options[ymd.year].selected = true;
  }

  this.setDates(group,ymd,true);
};


//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

tsNavObj.prototype.changeDate = function(order) {
  if (order == -1) {
    if (this.date.year || this.date.month || this.date.day) {
      if (this.date.day) {this.date.day--;}
      else {
	if (this.date.month) {this.date.month--;}
	else {
	  this.date.year--;
	  this.date.month = this.dates.year[this.date.year].month.length - 1;
	}
	this.date.day = this.dates.year[this.date.year].month[this.date.month].day.length - 1;
      }
      this.setDates(this.group[0],this.date,false);
    }
    else {return false;}
  }
  else if (order == 1) {
    if (this.date.year<this.date_max.year || this.date.month<this.date_max.month || this.date.day<this.date_max.day) {
      if (this.date.day < this.dates.year[this.date.year].month[this.date.month].day.length - 1) {this.date.day++;}
      else {
	if (this.date.month < this.dates.year[this.date.year].month.length - 1) {this.date.month++;}
	else {
	  this.date.year++;
	  this.date.month = this.dates.year[this.date.year].month.length - 1;
	}
	this.date.day = 0;
      }
      this.setDates(this.group[0],this.date,false);
    }
    else {return false;}
  }
  else {
    this.changeDates(this.group[0],this.date);
  }

  load_map();
};

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

tsNavObj.prototype.changeDates = function(group,ymd) {
  if (!this.tsScale.match('_clim'))
	ymd.year  = group[0].selectedIndex;
  if (!this.tsScale.match('yearly'))
	ymd.month = group[1].selectedIndex;
  if (this.tsScale.match('daily'))
	ymd.day   = group[2].selectedIndex;

  this.setDates(group,ymd,true);
};

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

tsNavObj.prototype.setDates = function(group,ymd,chng) {

  if (!this.tsScale.match('_clim')) {						// year
    if (group[0].selectedIndex != ymd.year) {
      group[0].options[ymd.year].selected  = true;
      chng = true;
    }
  }

  if (!this.tsScale.match('yearly')) {						// month
    if (chng || group[1].selectedIndex != ymd.month) {
      if (chng) {
	group[1].length = this.dates.year[ymd.year].month.length;
	for (var m=0; m<this.dates.year[ymd.year].month.length; m++) {
	  group[1].options[m].value = day_str[this.dates.year[ymd.year].month[m].val];
	  group[1].options[m].text  = month_str[this.dates.year[ymd.year].month[m].val];
	}
	if (ymd.month >= group[1].length) ymd.month = group[1].length - 1;
      }
      group[1].options[ymd.month].selected  = true;
      chng = true;
    }
  }

  if (this.tsScale.match('daily')) {							// day
    if (chng) {
      group[2].length = this.dates.year[ymd.year].month[ymd.month].day.length;
      for (var d=0; d<this.dates.year[ymd.year].month[ymd.month].day.length; d++) {
	group[2].options[d].value = day_str[this.dates.year[ymd.year].month[ymd.month].day[d]];
	group[2].options[d].text  = this.dates.year[ymd.year].month[ymd.month].day[d];
      }
      if (ymd.day >= group[2].length) ymd.day = group[2].length - 1;
    }
    group[2].options[ymd.day].selected  = true;
  }
};

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

tsNavObj.prototype.setRange = function(arg) {
  if (arg) this.changeDates(this.group[1],this.strt_date);
  else     this.changeDates(this.group[2],this.end_date);

  var jd_strt = this.strt_date.year*365 + this.strt_date.month*30 + this.strt_date.day;
  var jd_end  = this.end_date.year*365  + this.end_date.month*30  + this.end_date.day;

  if (arg) {
    if (jd_strt > jd_end) {
      this.end_date.year  = this.strt_date.year;
      this.end_date.month = this.strt_date.month;
      this.end_date.day   = this.strt_date.day;
      this.setDates(this.group[2],this.end_date,true);
    }
  }
  else {
    if (jd_strt > jd_end) {
      this.strt_date.year  = this.end_date.year;
      this.strt_date.month = this.end_date.month;
      this.strt_date.day   = this.end_date.day;
      this.setDates(this.group[1],this.strt_date,true);
    }
  }
};

//////////////////////////////////////////////////////////////////
/****************************************************************/
	/* Animation Functions */

tsNavObj.prototype.Anim_onclick = function(Data) {
  if (!this.anim) {
    this.anim = true;
    this.i_anim = 1;
    Data.clear();
				/// Build Animation list
    this.animList = new Array();
    this.animImg  = new Array();
    this.animSrc  = new Array();
    for (var y=this.strt_date.year; y<=this.end_date.year; y++) {
      for (var	m=((y==this.strt_date.year)?this.strt_date.month:0);
		m<=((y==this.end_date.year)?this.end_date.month:(this.dates.year[y].month.length-1)); m++) {
	for (var d=((y==this.strt_date.year && m==this.strt_date.month)?this.strt_date.day:0);
		 d<=((y==this.end_date.year && m==this.end_date.month)?this.end_date.day:(this.dates.year[y].month[m].day.length-1)); d++) {
	  var dateStr = this.dates.year[y].val + '-' +
	  		day_str[this.dates.year[y].month[m].val] + '-' +
	  		day_str[this.dates.year[y].month[m].day[d]];
	  this.animList.push({'year':y,'month':m,'day':d});
	  this.animImg.push(new Image());
	  this.animSrc.push(img_0.src.replace(/\d{4}-\d{2}-\d{2}/,dateStr));
	}
      }
    }

    if (this.anim_length_check(this.animList.length)) {
	document.images["ss_anim"].src = Stop_Anm.src;
	this.tm_step = 100;
	this.get_anim();
    }
    else { this.stop_anim(); }
  }
  else { this.stop_anim(); }
};

tsNavObj.prototype.get_anim = function() {
  if (this.i_anim>1 && !this.animImg[this.i_anim - 2].complete) return false;
  if (this.i_anim   &&  this.i_anim<=this.animList.length) {
    this.animImg[this.i_anim - 1].src = this.animSrc[this.i_anim - 1];
    img_0.src = this.animImg[this.i_anim - 1].src;
    if (this.i_anim>1) this.date = this.animList[this.i_anim - 2];
    this.setDates(this.group[0],this.date,true);

    this.i_anim++;
    if (!this.animTime) {this.animTime = setInterval("dateObj.get_anim()",this.tm_step);}
  }
  else {
    if (this.animTime) {clearInterval(this.animTime); this.animTime = 0;}
    this.tm_step = 1000;
    this.anim_r = 1;
    document.images["ss_anim"].src = Stop_Anm.src;
    document.images["faster"].src = Faster.src;
    document.images["slower"].src = Slower.src;
    this.run_anim();
  }
};

tsNavObj.prototype.run_anim= function() {
  if (this.anim_r) {
    if (this.animTime) {clearTimeout(this.animTime); this.animTime = 0;}
    img_0.src = this.animImg[this.anim_r - 1].src;
    this.date = this.animList[this.anim_r - 1];
    this.setDates(this.group[0],this.date,true);

    this.anim_r = (this.anim_r%(this.animList.length)) + 1;
    this.animTime = setTimeout("dateObj.run_anim()",this.tm_step);
  }
};

tsNavObj.prototype.Slow_onmousedown = function() {
  if (this.anim_r) {
    document.images["slower"].src = Slower_p.src;
    this.tm_step *= 1.5;
    tmSlow = setTimeout("document.images['slower'].src = Slower.src;",200);
  }
};

tsNavObj.prototype.Fast_onmousedown = function() {
  if (this.anim_r) {
    document.images["faster"].src = Faster_p.src;
    this.tm_step /= 1.5;
    tmFast = setTimeout("document.images['faster'].src = Faster.src;",200);
  }
};

tsNavObj.prototype.stop_anim = function() {
  if (!this.anim) return false;
  this.anim = false; this.i_anim = 0;

  if (document.images["ss_anim"]) {
    document.images["ss_anim"].src = Start_Anm.src;
    document.images["faster"].src = Empty_Im.src;
    document.images["slower"].src = Empty_Im.src;
  }
  if (this.animTime) {
    this.anim_r = 0; clearTimeout(this.animTime); clearInterval(this.animTime); this.animTime = 0;
  }
  this.changeDate(0);
};

tsNavObj.prototype.anim_length_check = function(len) {
  if (len <= 31) return true;
  return confirm("An animation you have requested is too long ("+len+" frames/data layers).\nIt might take up to "+len*3/60+" minutes to make.\n\nDo you really want to run it?");
};

//////////////////////////////////////////////////////////////////
/****************************************************************/
	/* Time Series Graph Functions */

var ts_graph_win = 0;
var windowName,params,ts_graph_url;

tsNavObj.prototype.Get_TS_Graph = function(Data,Point) {

		// Get Start and End Dates for the Graph
  var Dates =	pad_num(this.dates.year[this.strt_date.year].val,4) + '-' +
  		day_str[this.dates.year[this.strt_date.year].month[this.strt_date.month].val] + '-' +
  		day_str[this.dates.year[this.strt_date.year].month[this.strt_date.month].day[this.strt_date.day]] + '_' +
  		pad_num(this.dates.year[this.end_date.year].val,4) + '-' +
  		day_str[this.dates.year[this.end_date.year].month[this.end_date.month].val] + '-' +
  		day_str[this.dates.year[this.end_date.year].month[this.end_date.month].day[this.end_date.day]];

		// Open a window for the Graph
  windowName   = "TS_Graph";
  params = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=700,height=700";
  ts_graph_url = "/cgi-bin/graph_page.pl?type="+Data+"&date="+Dates+'&point='+Point;

  if ((ts_graph_win) && (!ts_graph_win.closed)) ts_graph_win.focus();
  setTimeout("ts_graph_win = window.open(ts_graph_url, windowName , params);",200);

  return false;
};

/****************************************************************/

function pad_num(num,dig) {
  var str = String(num);
  while (str.length < dig) {
    str = '0' + str;
  }
  return str;
}

