// 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 0.1 November 20, 2009  JimH
//    Release Candidate  1.8 , Feb 1, 2010  JimH 
//    Release 1.0 Feb 2, 2010 by JimH and MikeA
/////////////////////////////////////////////////////////////////////////////////////
//
//  Date Object Description
//
 // Tweekables
 var calstripWidth = 760;
 var caldaysOnStrip = 7; // must show at least one day. come on!!!
 var calpixWidth  = (calstripWidth - 20)/caldaysOnStrip; //150;
 var calpixHeight = 220;
 var caloffsetDisplay = -1; //data collection isn't real for a couple of days.
 var calTitle = "Rel 1.0 ";

 //  bgColors
 var calbgMonthColor = "#cccccc";
 
 //  basic date stuff
 var caltoday = new Date();
 // serves to make a time base, you can change - buffers the latestDate
 var callatestTime = new Date(caltoday.getTime() + (caloffsetDisplay * 1000 * 60 * 60 * 24)); 

 // initialize vars
 var calyear = caltoday.getFullYear(); 
 var calmyDay = caltoday.getDate(); 
 var calmyMo = caltoday.getMonth(); 
 var calmonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
 var caldays = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");  
 var calchild = ""; 
 var calnavFlag = false;
 var calmonthlen = new Array(31,checkLeapYearGCP(calyear),31,30,31,30,31,31,30,31,30,31); 

 function selNumDays(){ // shows 7,14,21,& 28 days
  caldaysOnStrip = document.getElementById('select').value;
  calpixWidth  = (calstripWidth - 20)/ caldaysOnStrip;
  jZcalendarGCP('add', 0); // setup caltoday
  displayStripGCP();
 }
 
 function jZcalendarGCP(whatToDo, parms) { // call this like an exec pgm
  var longDate = new Date();
  longDate.setTime(callatestTime.getTime());
  targetDy = 1;
  if (whatToDo == "set") {// initialize caltoday
   //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() - (caldaysOnStrip * 1000 * 60 * 60 * 24) + (parms * 1000 * 60 * 60 * 24); 
   longDate.setTime(targetDy); 
   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 pictureStripGCP() { // setup (on page entry) a picture in a box... fires on BODY onload
 var targetDy = callatestTime.getTime() - (-1  * 1000 * 60 * 60 * 24); //use an offset to determine 'how old' the cache should be (2-3 days)
 callatestTime.setTime(targetDy);
 jZcalendarGCP('add', 0); // setup caltoday
 displayStripGCP();
}

function displayStripGCP() { // write this into the div tag
 document.getElementById('showMoGCP').innerHTML = makeStripGCP();  
}

function open_windowGCP(url,width, height, resize, scroll) { // popup window
 calchild = window.open(url, "newwindow", "width=" + width + ",height=" + height + ",resizable=" + resize + ",scrollbars=" + scroll + "");
}

function checkLeapYearGCP(theyear) { // checking for leap year
 if ( ((theyear % 4 == 0) && (theyear % 100 != 0)) || (theyear % 400 == 0) ) {
  return("29");
 } 
 else {
  return("28");
 }
}

function moveDisplayGCP(n){ // change the base time for display, then move from that date
 var targDay = 1;
 targDay = callatestTime.getTime() + (n * 1000 * 60 * 60 * 24);
 callatestTime.setTime(targDay);
 jZcalendarGCP('add', 0); // show
 displayStripGCP();
}

function getDisplayMo(){
 var xxX = jZcalendarGCP('add', caldaysOnStrip -1 );
 xxX = xxX.substr(4,2);
 return xxX;
}


function makeStripGCP(){ // 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:"+ calstripWidth +"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'> Global Consciousness Project - Random Network Variance ";
  stripStr += "</div>";
  stripStr += "<div align='center' style='width:100%; background-color:#efefef'>";
  stripStr += "<span style='width:20px'><a href='#' onClick='calnavFlag = true; moveDisplayGCP(-caldaysOnStrip); displayStripGCP(); return false'> << </a><a href='#' onClick='calnavFlag = true; moveDisplayGCP(-1); displayStripGCP(); 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' >"+calmonths[getDisplayMo()-1]+" "+callatestTime.getFullYear()+" </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 id='rightNav' style='width:20px;'>";
  if (calnavFlag) stripStr += "<a href='#' onClick='moveDisplayGCP(1); displayStripGCP(); return false'>> </a><a href='#' onClick='moveDisplayGCP(caldaysOnStrip); displayStripGCP(); return false'>  | >> </a>";
  stripStr += "</span></div>";
  stripStr += "<div style='height:30px; padding-top:10px; background-color:#ffffff; font-size:x-small;'>Click for detailed waveforms.</div>";
  stripStr += "<div style='padding-left: 0px; padding-right: 5px;'>"; // strip Output "master div"
  stripStr += "<span><img src='http://nocc.xxyyzz.com/jimX/leftMarginDisplay.php?d=24%20%20hour%20moving%20sum%20%28Z^2%20-1%20%29' border='0'></span>";
  for(var i = 0; i < caldaysOnStrip ; i++) { // put a picture in the span in master div
   jZcalendarGCP('add', -1);   
   stripStr += "<span id='day6'><a href='#' onclick=\"window.open('http://nocc.xxyyzz.com/jimX/imageDisplay.php?d="+jZcalendarGCP('add', i)+"', 'subWindow', 'height=470, width=770, resizable, scrollbars'); return false;\"'><img src='http://nocc.xxyyzz.com/jimX/stripImageDisplay.php?d="+jZcalendarGCP('add', i)+"' height='"+calpixHeight+"' width='"+calpixWidth+"' border='0'  title='"+jZcalendarGCP('add',i)+"'/></a></span>";
  } 
  stripStr += "</div>"; // close strip output after you build the strip
  // build the number strip under the calendar  
    stripStr += "<br /> ";
    var myStr = "";
    var xxXX = "<div align='center'>";
    for(var i = caldaysOnStrip-1; i >= 0 ; i--) {
    xxXX = jZcalendarGCP('add', i);
       myStr += "<div align='center' style='float:right; width:"+((calstripWidth - 15)/caldaysOnStrip)+"px;'>"+ xxXX.substr(6,8) + "</div>";
     }
     stripStr += "<div><font size='-5'>"+myStr+"</font></div><br />";
  // close the number strip under the calendar display
  stripStr += "<div style='height:25px; padding-top:10px; background-color:"+ calbgMonthColor +"; font-size:x-small;'><span  style='font-weight: bold;color:#0000ff'><font size='-3'>"+ calTitle + "</font></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &copy;Copyright 2009, "+calyear+", Global Coherence Initiative, all rights reserved.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name='select' size='1' id='select' onChange='selNumDays();return false;'><option value='7'>Days to Display</option><option value='7'>7 days</option><option value='14'>14 days</option><option value='21'>21 days</option><option value='28'>28 days</option></select><span> &nbsp;&nbsp; Current: "+caldaysOnStrip+" days.</span></div>"; 
return  stripStr;
}