﻿// JavaScript code for Pastore's Delly
// Will Fastie, 27 Jul 08

/*global PASTORES, document, Date */
PASTORES = {};

// Detect holidays by date range. Return holiday object.
PASTORES.getholidaystyles = function () {
  // Generic Date Functions (thanks to Martin Webb @IRT)
  DayOfWeek = function(day,month,year) {
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
             Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d + 1;
  };
  LeapYear = function(year) {
    if ((year/4)   != Math.floor(year/4))   { return false; };
    if ((year/100) != Math.floor(year/100)) { return true;  };
    if ((year/400) != Math.floor(year/400)) { return false; };
    return true;
  };
  makeArray = function() {
      this[0] = makeArray.arguments.length;
      for (i = 0; i<makeArray.arguments.length; i++) { this[i+1] = makeArray.arguments[i]; };
  };
  var daysofmonth   = new makeArray( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  var daysofmonthLY = new makeArray( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  // definitions for calling NthDay
  var sun=1,mon=2,tue=3,wed=4,thu=5,fri=6,sat=7;
  var jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12;
  var first=1,second=2,third=3,fourth=4,fifth=5,last=-1;
  
  NthDay = function(nth,weekday,month,year) {
    if (nth > 0) return (nth-1)*7 + 1 + (7 + weekday - DayOfWeek((nth-1)*7 + 1,month,year))%7;
    if (LeapYear(year)) {
      var days = daysofmonthLY[month];
    } else {
      var days = daysofmonth[month];
    }
    return days - (DayOfWeek(days,month,year) - weekday + 7)%7;
  };
  Easter = function(Y) {
    var C = Math.floor(Y/100);
    var N = Y - 19*Math.floor(Y/19);
    var K = Math.floor((C - 17)/25);
    var I = C - Math.floor(C/4) - Math.floor((C - K)/3) + 19*N + 15;
    I = I - 30*Math.floor((I/30));
    I = I - Math.floor(I/28)*(1 - Math.floor(I/28)*Math.floor(29/(I + 1))*Math.floor((21 - N)/11));
    var J = Y + Math.floor(Y/4) + I + 2 - C + Math.floor(C/4);
    J = J - 7*Math.floor(J/7);
    var L = I - J;
    var M = 3 + Math.floor((L + 40)/44);
    var D = L + 28 - 31*Math.floor(M/4);
    return new Date(Y, M-1, D, 0, 0, 0);
  };
  var today = new Date();
  //var today = new Date(2009,jan-1,1); // for testing only
  var curyear = today.getFullYear();
  var ts = new Date();
  var te = new Date();
  // Do special calculations
  var tg = NthDay(fourth, thu, nov, curyear);
  var pday = NthDay(third, mon, feb, curyear);
  var columbus = NthDay(second, mon, oct, curyear);
  var lbday = NthDay(first, mon, sep, curyear);
  var memday = NthDay(last, mon, may, curyear);
  var EasterDate = Easter(curyear);
  var eMon = EasterDate.getMonth()+1;
  var eDay = EasterDate.getDate();
  var LeadEasterDate = new Date(EasterDate.getFullYear(), EasterDate.getMonth(), EasterDate.getDate()-14);
  var lMon = LeadEasterDate.getMonth()+1;
  var lDay = LeadEasterDate.getDate();

  // The holiday periods must be unique, cannot overlap, do not have to be in order.
  // The months in this list are numbered 1-12; the code does the proper conversion.
  // (need to change this to use actual dates for the start and end items)
  var holidays = [
    // New Year's Day
    { start:{mn:jan, dy:1}, end:{mn:jan, dy:1}, styles:{im:'/img/holidays/newyears.gif', bg:'transparent', ti:"Happy New Year!"} }, 
    // Valentine's Day - Feb 14
    { start:{mn:feb, dy:1}, end:{mn:feb, dy:14}, styles:{im:'/img/holidays/valentines.gif', bg:'transparent', ti:"Valentine's Day"} },
    // Presidents Day - 3rd Monday in February
    { start:{mn:feb, dy:15}, end:{mn:feb, dy:pday}, styles:{im:'/img/holidays/presidents.gif', bg:'transparent', ti:"Presidents' Day"} },
    // Easter - calculated above with a 14 day lead time and extended to Easter Monday
    { start:{mn:lMon, dy:lDay}, end:{mn:eMon, dy:eDay+1}, styles:{im:'/img/holidays/easter.jpg', bg:'transparent', ti:"Easter"} },
    // Memorial Day - last Monday in May, 14-day lead
    { start:{mn:may, dy:memday-14}, end:{mn:may, dy:memday}, styles:{im:'/img/holidays/memday.gif', bg:'transparent', ti:'Memorial Day'} },
    // Independence Day - Jul 4, 10-day lead
    { start:{mn:jun, dy:25}, end:{mn:jul, dy:05}, styles:{im:'/img/holidays/usa7.gif', bg:'transparent', ti:'Independence Day'} },
    // Labor Day - 1st Monday September, 10-day lead
    { start:{mn:aug, dy:27}, end:{mn:sep, dy:lbday}, styles:{im:'/img/holidays/laborday.gif', bg:'transparent', ti:'Labor Day'} },
    // Columbus Day - 2nd Monday in October
    { start:{mn:oct, dy:1}, end:{mn:oct, dy:columbus}, styles:{im:'/img/holidays/columbus.gif', bg:'transparent', ti:'Columbus Day'} },
    // Veteran's Day - Nov 11
    { start:{mn:nov, dy:3}, end:{mn:nov, dy:11}, styles:{im:'/img/holidays/vetsday.gif', bg:'transparent', ti:"Veterans' Day"} },
    // Thanksgiving - 4th Thursday in November
    { start:{mn:nov, dy:tg-10}, end:{mn:nov, dy:tg+1}, styles:{im:'/img/holidays/cornucopia.gif', bg:'transparent', ti:'Thanksgiving Day'} },
    // Christmas - Dec 25
    { start:{mn:dec, dy:10}, end:{mn:dec, dy:25}, styles:{im:'/img/holidays/xmastree1.gif', bg:'white', ti:'Christmas'} },
    // New Year's Day
    { start:{mn:dec, dy:26}, end:{mn:dec, dy:31}, styles:{im:'/img/holidays/newyears.gif', bg:'transparent', ti:"New Year's Eve"} } 
  ];
  var foundholidaystyles = null;
  for (var i = 0; i < holidays.length; i=i+1) {
    ts.setFullYear(curyear, holidays[i].start.mn - 1, holidays[i].start.dy);
    ts.setHours(0, 0, 0, 0);
    te.setFullYear(curyear, holidays[i].end.mn - 1, holidays[i].end.dy);
    te.setHours(23, 59, 59, 999);
    if ( ts.getTime() <= today.getTime() && today.getTime() <= te.getTime() ) {
      foundholidaystyles = holidays[i].styles;
      break; // Holidays are unique in the array, so if we found one we're done
    } 
  }
  return foundholidaystyles;
};

// Show Holiday displays the apropriate holiday slug if the date is within the range
// Called only from the home page and specific to that page
PASTORES.showholiday = function () {
  var hi;
  var hn;
  var holidaystyles = PASTORES.getholidaystyles();
  if (holidaystyles !== null) {
    hn = document.getElementById('holidaynotice');
    hn.style.display = 'block';
    hi = document.getElementById('holidayimg');
    hi.setAttribute('src', holidaystyles.im);
    hi.style.backgroundColor = holidaystyles.bg;
    hi.setAttribute('title', holidaystyles.ti);
  }
  return false;
};

// Setup - runs when loaded, requries jQuery to be loaded
//$(document).ready(function() {
	// Create hover behaviors for submenus in left menu - adds "hasfocus" class (this code from NAVH)
	// $(".leftmenu li").hover(
	//   function() { this.className = this.className + " hasfocus"; },
	//   function() { this.className = this.className.replace( " hasfocus", "" ); }
	// );
//});

