// JavaScript Calendaring Functions For Display of Date/Time
//  Copyright Institute of HeartMath Creation Date: Nov 4, 2009, all rights reserved.
//  Creator Jim Huffman for GCMP
//  History: Release 1.0 November 20, 2009  JimH
//     Release 2.0 January  09, 2009  JimH
/////////////////////////////////////////////////////////////////////////////////////
//
//  Date Object Description
//
 // Tweekables
 var stripWidth = 776;
 var daysOnStrip = 30; // must show at least one day. come on!!!
 var pixWidth  = 24;
 var pixHeight = 223;
 var offsetDisplay = 0; //data collection isn't real for a couple of days.
 var Title = "Release 2.0 ";
 //  bgColors
 var bgMonthColor = "#cccccc";
 
 //  basic date stuff
 var today = new Date();
 var latestTime = new Date(today.getTime() + (offsetDisplay * 1000 * 60 * 60 * 24)); // serves to make a time base, you can change - buffers the latestDate

 // initialize vars
 var year = today.getFullYear(); 
 var myDay = today.getDate(); 
 var myMo = today.getMonth(); 
 var outMonth = "";
 var outNum = ""; 
 var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
 var monthlen = new Array(31,checkLeapYear(year),31,30,31,30,31,31,30,31,30,31); 
 var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");  
 var child = ""; 
 var x = 0;
 var latestMo = myMo;
 var latestDy = myDay;
 var latestYr = year;

 function jZcalendar(whatToDo, parms) { // call this like and exec pgm
  var longDate = new Date();
  longDate.setTime(latestTime.getTime());
  targetDy = 1;
  if (whatToDo == "set") {// initialize today
   //set (dateInFuture) return year mo day in future, past, etc
  }
  if (whatToDo == "add") {// make a new date by adding or subtracting (by sign) by parms (number of days)
   var myAddy = "";
   // add numberOfDays to today's initial date, cover the offset too
   var targetDy = longDate.getTime() - (daysOnStrip * 1000 * 60 * 60 * 24) + (parms * 1000 * 60 * 60 * 24); 
   //targetDy = longDate.getDate(targetDy);
   longDate.setTime(targetDy); /// this is the problem for now... jhh
   targetDy = longDate.getDate();
   var targetMo = longDate.getMonth();
   targetMo++;
   var targetYr = longDate.getFullYear();
  }
  myAddy = targetYr + "";  
  if (targetMo <= 9) myAddy += "0";
  myAddy += targetMo + "";
  if (targetDy <= 9) myAddy += "0";
  myAddy += targetDy ;
 return myAddy;  
 }

function pictureStrip() { // setup (on page entry) a picture in a box
 var targetDy = latestTime.getTime() - (-1  * 1000 * 60 * 60 * 24); //use an offset to determine 'how old' the cache should be (2-3 days)
 latestTime.setTime(targetDy);
 jZcalendar('add', 0); // setup today
 displayStrip();
}

function displayStrip() { // write this into the div tag
 document.getElementById('showMo').innerHTML = makeStrip();  
}

function open_window(url,width, height, resize, scroll) { // popup window
 child = window.open(url, "newwindow", "width=" + width + ",height=" + height + ",resizable=" + resize + ",scrollbars=" + scroll + "");
}

function checkLeapYear(theyear) { // checking for leap year
 if ( ((theyear % 4 == 0) && (theyear % 100 != 0)) || (theyear % 400 == 0) ) {
  return("29");
 } 
 else {
  return("28");
 }
}

function moveDisplay(n){ // change the base time for display, then move from that date
 var targDay = 1;
 targDay = latestTime.getTime() + (n * 1000 * 60 * 60 * 24);
 latestTime.setTime(targDay);
 jZcalendar('add', 0); // show
 displayStrip();
}
 
function makeStrip(){ // the display strip, goes back numdaysonstrip, then starts showing date for daysOnStrip - brings us to the spot
  stripStr = "";
  stripStr += "<div align='center'>";
  stripStr += "<div id='calBody' style='width:"+ stripWidth +"px; border-width: medium; border-style: solid; border-color: #666666;'>";
  stripStr += "<div id='menu'><div align='center' style='height:35px; padding-top:10px; font-size:medium; background-color:#cccccc; font-style:bold'> - <span  style='font-weight: bold;color:#0000ff'>Solar Wind</span>, <span style='font-weight: bold;color:#ff0000'>GOES 11</span> and <span  style='font-weight: bold;color:#00ff00'>GOES 12 </span>";
  stripStr += "</div>";
  stripStr += "<div align='center' style='width:100%; background-color:#efefef'></div>";
  //stripStr += "<span style='width:20px'><a href='#' onClick='moveDisplay(-daysOnStrip); displayStrip(); return false'> << </a><a href='#' onClick='moveDisplay(-1); displayStrip(); return false'> | < </a></span>";
  //stripStr += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  //stripStr += "<span style='font-size:medium' align='center' > blah blsh </span>"; // </tr></table>";
  //stripStr += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  //stripStr += "<span style='width:20px'><a href='#' onClick='moveDisplay(1); displayStrip(); return false'>> </a><a href='#' onClick='moveDisplay(daysOnStrip); displayStrip(); return false'>  | >> </a></span></div>";
  stripStr += "<div style='height:30px; padding-top:10px; background-color:#ffffff; font-size:x-small;'>These waveforms have amplification and compression. Click for detailed waveforms.</div>";
  stripStr += "<div style='height:30px; padding-top:10px; background-color:#ffffff; font-size:small;'>Previous Week &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Previous Week &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Last Week &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Current Week &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>";
  stripStr += "<div>"; // strip Output "master div" 
  for(var i = 0; i < daysOnStrip ; i++) { // put a picture in the span in master div
   jZcalendar('add', -1);   
   stripStr += "<span id='day6'><a href='#' onclick=\"window.open('http://nocc.glcoherence.org/dailyImage/em11/imageDisplay.php?d="+jZcalendar('add', i)+"', 'subWindow', 'height=470, width=770, resizable, scrollbars'); return false;\"'><img src='http://nocc.glcoherence.org/Xfeed/monthDisplay.php?d="+jZcalendar('add', i)+"' height='"+pixHeight+"' width='"+pixWidth+"' border='0'  title='"+jZcalendar('add', i)+"'/></a></span>";
  }
  stripStr += "<br /> ";
  var myStr = "";
  var xxXX = "";
  for(var i = 0; i < daysOnStrip ; i++) {
      xxXX = jZcalendar('add', i);
   myStr += xxXX.substr(6,8)+'&nbsp;&nbsp;&nbsp;&nbsp;';
  }
  stripStr += "<div><font size='-3'>"+myStr+"</font></div>";
  stripStr += "<div><font size='-4'>"+Title+"</font></div>";
  stripStr += "<div style='height:20px; padding-top:10px; background-color:"+ bgMonthColor +"; font-size:x-small;'>&copy;Copyright "+year+", Global Coherence Initiative, all rights reserved.</div></div>"; 
  stripStr += "</div>"; // close strip output after you build the strip
return  stripStr;
}
