/* Copyright Robaviax Design Studio, Madison WI */
/* This is the MorningGloryArt Calendar (and IE Drag) JavaScript text file - filename: calendar.js - approx 11 KBytes total - November 29 2004, added Copyright notice 12/9/2004 */
/* Code originally written Summer 2004 */
/* Changes from original CalendarTest.html file are id attribute of Month and Year were changed to MM and YY (from M and Y) in HTML code and function nowCal() */
/* The attribute id="M" conflicted with existing mgacss.css declaration */
/* Also 8 td cells in HTML code needed a style attribute of font-size:12pt to override 10pt style declaration for td cells in mgacss.css */
/* This file is used by the 6 DPC pages only */
/* Added Presidents Day (3rd Mon in Feb) 2/23/2005 */
/* Fixed Feb leap year reset bug by moving line of code in makeCal() function 2/26/2005 */

// Start Calendar Javascript Code

var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var monthDays = [31,null,31,30,31,30,31,31,30,31,30,31];
var weekDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var sunHol = [0,6,5,4,3,2,1];
var monHol = [1,0,6,5,4,3,2];
var memHol = [8,7,6,5,4,3,9]; // modified monHol array for Memorial Day
var thursHol = [4,3,2,1,0,6,5];
var now=new Date();
var when=new Date();
var mi, yi;
var calCells=new Array(43); calCells[0]=null;
var cC=new Array(43); cC[0]=null;
var calM, calY, calT;

function nowCal(){
if (document.getElementById){
calM=document.getElementById("MM");
calY=document.getElementById("YY");
for(var i=1; i<43; i++){calCells[i]=document.getElementById('c'+i)} // 42 table cells
mi=now.getMonth(); yi=now.getFullYear(); // month index is 0-11, year index is 4 digit
todayIs();
}
}

function todayIs(){
calT=document.getElementById("T");
calT.innerHTML = "Today is " + weekDays[now.getDay()] + " " + months[now.getMonth()] + " " + now.getDate() + " " + now.getFullYear();
makeCal();
}

function changeMonth(deltaM){
if (document.getElementById){
if (deltaM==1){mi++}
else if (deltaM==-1){mi--}
if(mi>11){mi=0; yi++}
if(mi<0){mi=11; yi--}
when.setMonth(mi);
when.setFullYear(yi);
makeCal();
}
}

function changeYear(deltaY){
if (document.getElementById){
if (deltaY==1){yi++}
else if (deltaY==-1){yi--}
when.setFullYear(yi);
makeCal();
}
}

function makeCal(){
when.setFullYear(yi); // moved here to fix Feb leap year reset bug
var leapYear=(((when.getFullYear()%4==0)&&(when.getFullYear()%100!=0))||(when.getFullYear()%400==0))
if(leapYear){monthDays[1]=29}
else {monthDays[1]=28}
calM.innerHTML=months[mi];
calY.innerHTML=yi;
// when.setFullYear(yi);
when.setMonth(mi);
when.setDate(1);
var di = when.getDay(); // 0 - 6
var firstDay = di + 1; // 1 - 7 (cell number of first day of month)
for(var j=1; j<43; j++){ // Start of 42 cell for loop
if((j-firstDay>=0) && ((j-firstDay) < monthDays[mi])){
calCells[j].innerHTML=j-firstDay+1;
if((now.getDate()==(j-firstDay+1))&&(now.getFullYear()==when.getFullYear())&&(now.getMonth()==when.getMonth())){calCells[j].className="calToday"}
else {calCells[j].className="calD"}
}
else {calCells[j].className="calB"; calCells[j].innerHTML='&nbsp\;'} // blank cells
// assign style to cells
if(calCells[j].className=="calToday"){calCells[j].style.color="blue"; calCells[j].style.border="1px solid black"; calCells[j].style.backgroundColor="#ddeeff"}
else if(calCells[j].className=="calD"){calCells[j].style.color="black"; calCells[j].style.border="1px solid silver"; calCells[j].style.backgroundColor="#0099ff"}
else if(calCells[j].className=="calB"){calCells[j].style.color="black"; calCells[j].style.border="1px solid silver"; calCells[j].style.backgroundColor="#9999ff"}
// initialize all cells to empty string for title property
calCells[j].title="";
// Holidays
var wgm=when.getMonth();
cC[j]=calCells[j].className;
// Holidays with Fixed Calendar Date - Total of 13
// Sunday (2) Monday (5) and Thursday (1) Holidays - Total of 8
switch (wgm) {
   case 0 :
     if(j==di+1) {cC[j]="calH"; calCells[j].title="New Years Day"}// Jan 1
     if(j==firstDay+14+monHol[di]) {cC[j]="calH"; calCells[j].title="MLK Jr Day"}// 3rd Monday in Jan
     break;
   case 1 :
     if(j==di+2) {cC[j]="calH"; calCells[j].title="Groundhog Day"}// Feb 2
     if(j==di+14) {cC[j]="calH"; calCells[j].title="Valentines Day"}// Feb 14
     if(j==firstDay+14+monHol[di]) {cC[j]="calH"; calCells[j].title="Presidents Day"}// 3rd Monday in Feb
     break;
   case 2 :
     if(j==di+17) {cC[j]="calH"; calCells[j].title="St. Patricks Day"}// March 17
     break;
   case 3 :
     if(j==di+1) {cC[j]="calH"; calCells[j].title="April Fools Day"}// April 1
     if(j==di+22) {cC[j]="calH"; calCells[j].title="Earth Day"}// April 22
     break;
   case 4 :
     if(j==di+5) {cC[j]="calH"; calCells[j].title="Cinco de Mayo"}// May 5
     if(j==firstDay+7+sunHol[di]) {cC[j]="calH"; calCells[j].title="Mothers Day"}// 2nd Sunday in May
     if(j==firstDay+21+memHol[di]) {cC[j]="calH"; calCells[j].title="Memorial Day"}// Last Monday in May
     break;
   case 5 :
     if(j==di+14) {cC[j]="calH"; calCells[j].title="Flag Day"}// June 14
     if(j==firstDay+14+sunHol[di]) {cC[j]="calH"; calCells[j].title="Fathers Day"}// 3rd Sunday in Jun
     break;
   case 6 :
      if(j==di+4) {cC[j]="calH"; calCells[j].title="Independence Day"}// July 4
     break;
   case 7 :
     break;
   case 8 :
     if(j==firstDay+monHol[di]) {cC[j]="calH"; calCells[j].title="Labor Day"}// 1st Monday in Sep
     break;
   case 9 :
     if(j==firstDay+7+monHol[di]) {cC[j]="calH"; calCells[j].title="Columbus Day"}// 2nd Monday in Oct
     if(j==di+31) {cC[j]="calH"; calCells[j].title="Halloween"}// Oct 31
     break;
   case 10 :
     if(j==di+11) {cC[j]="calH"; calCells[j].title="Veterans Day"}// Nov 11
     if(j==firstDay+21+thursHol[di]) {cC[j]="calH"; calCells[j].title="Thanksgiving"}// 4th Thursday in Nov
     break;
   case 11 :
     if(j==di+25) {cC[j]="calH"; calCells[j].title="Christmas"}// Dec 25
     if(j==di+31) {cC[j]="calH"; calCells[j].title="New Years Eve"}// Dec 31
     break;
   default :
     break;
}
// if(wgm==7 && j==di+7) {cC[j]="calH"; calCells[j].title="TEST"} //TEST - Set to Month and Day
// Easter and Good Friday section
if(wgm==2 || wgm==3){ // Start of Easter if statement - do Easter / Good Friday only if month is March or April
if(j==1){ // Start of Oudin if statement - do Oudin Easter algorithm only once per calendar
// Easter algorithm credited to J.M. Oudin (1940) - Reference U.S. Naval Observatory website
// Easter (and Good Friday) calculation using Javascript implementation of Oudin's algorithm:
// Input to algorithm is Y, output is M and D
var Y,C,N,K,I,J,L,M,D;
Y=yi;
   C = Math.floor(Y/100);
   N = Y % 19;
   K = Math.floor((C-17)/25);
   I = C - Math.floor(C/4) - Math.floor((C-K)/3) + 19 * N + 15;
   I = I % 30;
   I = I - Math.floor(I/28) * (1 - Math.floor(I/28) * Math.floor(29/(I+1)) * Math.floor((21-N)/11));
   J = Y + Math.floor(Y/4) + I + 2 - C + Math.floor(C/4);
   J = J % 7;
   L = I - J;
   M = 3 + Math.floor((L+40)/44);
   D = L + 28 - 31 * Math.floor(M/4);
// Y, M, D are Year, Month, Day of Easter (M=3 is March, M=4 is April, D=1 thru 31 inclusive)
} // End of Oudin if statement
if(wgm==M-1 && j==di+D) {cC[j]="calH"; calCells[j].title="Easter"}// Easter date is March 22 to April 25 inclusive
// Good Friday is 2 days before Easter
if(M==3 && wgm==M-1 && j==di+D-2) {cC[j]="calH"; calCells[j].title="Good Friday"}//Good Friday if Easter in March
if(M==4 && D>2 && wgm==M-1 && j==di+D-2) {cC[j]="calH"; calCells[j].title="Good Friday"}//Good Friday if Easter in April on/after April 3
if(M==4 && D==2 && wgm==M-2 && j==di+31) {cC[j]="calH"; calCells[j].title="Good Friday"}//Good Friday is March 31 if Easter is April 2
if(M==4 && D==1 && wgm==M-2 && j==di+30) {cC[j]="calH"; calCells[j].title="Good Friday"}//Good Friday is March 30 if Easter is April 1
// Special Cases for title property: Good Friday or Easter is April 1 or April 22
if(M==4 && D==1 && wgm==M-1 && j==di+1) {cC[j]="calH"; calCells[j].title="Easter & April Fools Day"}// Easter is April 1
if(M==4 && D==22 && wgm==M-1 && j==di+22) {cC[j]="calH"; calCells[j].title="Easter & Earth Day"}// Easter is April 22
if(M==4 && D==3 && wgm==M-1 && j==di+1) {cC[j]="calH"; calCells[j].title="Good Friday & April Fools Day"}// Good Friday is April 1 (Easter is April 3)
if(M==4 && D==24 && wgm==M-1 && j==di+22) {cC[j]="calH"; calCells[j].title="Good Friday & Earth Day"}// Good Friday is April 22 (Easter is April 24)
} // End of Easter if statement
if(cC[j]=="calH"){calCells[j].style.color="red"} // replaces previous cell color
} // End of 42 cell for loop
if(document.getElementById("Calendar").style.display=="none") {document.getElementById("Calendar").style.display="block"}
return false;
}

// End Calendar Javascript Code

// Start Drag Javascript Code

var XY = (document.all && document.getElementById && !window.opera);

function DMP(obj) {   //mouseover
if(XY){
obj.style.color='lime';
obj.style.borderColor='lime';
obj.style.backgroundColor='#000000';
obj.innerHTML='Drag Me Please . . .';
obj.style.cursor='move';
}
}

function MGA(obj) {   //mouseout
if(XY){
obj.style.color='white';
obj.style.borderColor='#7788cc';
obj.style.backgroundColor='#7788cc';
obj.innerHTML='MorningGloryArt';
}
}

var dragOK=false;
var Cal,CalX,CalY,CurX,CurY,scrollW,scrollH,clientW,clientH;

function initDrag() { //mousedown on td
if(XY){
if (window.event.button==1){
dragOK=true;
Cal=document.getElementById('CalTable');
// initial Calendar and cursor positions, initial client size and scroll settings
CalX=parseInt(Cal.style.left);
CalY=parseInt(Cal.style.top);
CurX=window.event.clientX + document.body.scrollLeft;
CurY=window.event.clientY + document.body.scrollTop;
scrollH=document.body.scrollHeight;
scrollW=document.body.scrollWidth;
clientH=document.body.clientHeight;
clientW=document.body.clientWidth;
document.onmousemove=Drag;
document.onmouseup=stopDrag;
return false;
}
}
}

function Drag() { //mousemove
if(XY){
if (window.event.button==1 && dragOK==true){
Cal.style.left=(CalX + window.event.clientX + document.body.scrollLeft - CurX) + "px";
Cal.style.top=(CalY + window.event.clientY + document.body.scrollTop - CurY) + "px";
// scroll window if necessary
if((document.body.scrollTop > 0) && (window.event.clientY < 100)){
window.scrollBy(0,-40);
Cal.style.top=(parseInt(Cal.style.top) - 40) + "px";
}
else if ((window.event.clientY + document.body.scrollTop < scrollH) && (clientH - window.event.clientY) < 100){
window.scrollBy(0,40);
Cal.style.top=(parseInt(Cal.style.top) + 40) + "px";
}
if((document.body.scrollLeft > 0) && (window.event.clientX < 100)){
window.scrollBy(-40,0);
Cal.style.left=(parseInt(Cal.style.left) - 40) + "px";
}
else if ((window.event.clientX + document.body.scrollLeft < scrollW) && (clientW - window.event.clientX) < 100){
window.scrollBy(40,0);
Cal.style.left=(parseInt(Cal.style.left) + 40) + "px";
}
return false;
}
}
}

function stopDrag() {   //mouseup
if(XY){
if (window.event.button==1 && dragOK==true){
dragOK=false;
document.onmousemove=null;
document.onmouseup=null;
return false;
}
}
}

// End Drag Javascript Code

