/*This is adapted from a SitePoint script called "create_new_style_sheet.js" from the book "The Javascript Anthology"

The adaptations, by Christopher Werby of Pipsqueak Productions, LLC (March 23, 2007) are so that a selector and property combination are returned for the current day.  The CSS equivalent (for March 23, 2007) would look like:


table#march2007 td.day23
{
	background-color: #0000CC;
	background-image: url(images/BG_Cell_Today.jpg);
	background-repeat: no-repeat;
	background-position: center center;
}

The day should be padded with a 0 if less than 10.

Then I'm adding a bunch of new selectors and properties based on the current date.  These are to make active the "Tonight!" "Today!" and "Tomorrow" tags in the Features section.  If the current date is March 30, 2007, the equivalent CSS would look like:

 	div#feature div.tag20070330TonightF1 {background-image: url(images/Tag_Tonight_Feature_1.gif); }
 	div#feature div.tag20070330TonightF2 {background-image: url(images/Tag_Tonight_Feature_2.gif); }
 	div#feature div.tag20070330TonightF3 {background-image: url(images/Tag_Tonight_Feature_3.gif); }
 	div#feature div.tag20070330TodayF1 {background-image: url(images/Tag_Today_Feature_1.gif); }
 	div#feature div.tag20070330TodayF2 {background-image: url(images/Tag_Today_Feature_2.gif); }
 	div#feature div.tag20070330TodayF3 {background-image: url(images/Tag_Today_Feature_3.gif); }
 	div#feature div.tag20070331TonightF1 { background-image: url(images/Tag_Tomorrow_Feature_1.gif); }
 	div#feature div.tag20070331TonightF2 { background-image: url(images/Tag_Tomorrow_Feature_2.gif); }
 	div#feature div.tag20070331TonightF3 { background-image: url(images/Tag_Tomorrow_Feature_3.gif); }
  	div#feature div.tag20070331TodayF1 { background-image: url(images/Tag_Tomorrow_Feature_1.gif); }
 	div#feature div.tag20070331TodayF2 { background-image: url(images/Tag_Tomorrow_Feature_2.gif); }
 	div#feature div.tag20070331TodayF3 { background-image: url(images/Tag_Tomorrow_Feature_3.gif); }


2007-04-05 Modified by pulling several functions out of here and into a standard function set.  
The standard_functions.js file is now required to be loaded as well for this script to work.

*/


addLoadEvent(initNewStyleSheet);

function initNewStyleSheet()
{
var currentDate = new Date();
fixDate(currentDate);

var oneMinute = 60 * 1000;  // milliseconds in a minute
var oneHour = oneMinute * 60;
var oneDay = oneHour * 24;
var oneWeek = oneDay * 7;
 
var cellSelector = getCellSelector(currentDate);
var properties = getProperties();
var styleSheet = createNewStyleSheet();

addHeadStyleRule(styleSheet, cellSelector, properties); //This one is for the cell that should light up for the current day
 
var featureItems = new makeArray("F1", "F2", "F3");
var featureTags = new makeArray("Tonight", "Today");
var featureImageItems = new makeArray("Tonight", "Today", "Tomorrow");

var tomorrowDate = new Date(currentDate.getTime() + oneDay);
var DateString = new Array(currentDate.getISODate(), tomorrowDate.getISODate())
var selectorString = '';
var propertyString = '';
var imageString = '';

//This loop defines the 12 css rules for the Today, Tomorrow, Tonight! flags.
for (var i = 0; i<2; i++) { //Selector Date String Today or Tomorrow
	for (var s = 0; s<2; s++) { //Selector Today or Tonight
		for (var r = 0; r<3; r++) { //Individual Features 1, 2, or 3
			if (i == 0) { //Today
				imageString = "images/Tag_" + featureImageItems[s] + "_Feature_" + (r + 1) + ".gif" ; 
				 }
			else { //Tomorrow
				imageString = "images/Tag_"  + featureImageItems[2] + "_Feature_" + (r + 1) + ".gif" ; 
				}
			selectorString = "div#feature div.tag" + DateString[i] + featureTags[s] + featureItems[r] ;
			propertyString = "background-image: url(" + imageString + ");" ;
			addHeadStyleRule(styleSheet, selectorString, propertyString) ;
			}
		}
	 }
  return true;
}



function makeArray() {
  var args = makeArray.arguments;
  for (var i = 0; i < args.length; i++) {
    this[i] = args[i];
  }
  this.length = args.length;
}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}
function getProperties() {
//return 'background-color:#00C;';

	return 'background-image: url(images/BG_Cell_Today.jpg);' + 'background-repeat: no-repeat;' + 'background-position: center center;';
}

function getCellSelector(date) {
  var months = new makeArray("january", "february",
"march", "april", "may", "june", "july", "august",
"september", "october", "november", "december");
var dayString = date.getDate();
if (dayString<10)
	dayString = "0" + dayString;
  return "table#" + months[date.getMonth()] + date.getFullYear() + " td.day" + dayString ;
}


function createNewStyleSheet(media)
{
  var isSafari = /safari/.test(identifyBrowser());

  var styleSheet = document.createElement("style");
  styleSheet.setAttribute("type", "text/css");

  if (typeof media == "undefined")
  {
    styleSheet.setAttribute("media", "all");
  }
  else
  {
    styleSheet.setAttribute("media", media);
  }

  styleSheet = document.getElementsByTagName("head")[0].appendChild(styleSheet);

  if (typeof document.styleSheets != "undefined" && document.styleSheets.length > 0 && !isSafari)
  {
    styleSheet = document.styleSheets[document.styleSheets.length - 1];
  }

  return styleSheet;
}

function addHeadStyleRule(styleSheet, selector, properties)
{
  var isSafari = /safari/.test(identifyBrowser());

  if (typeof styleSheet.addRule != "undefined" && !isSafari)
  {
    styleSheet.addRule(selector, properties);
  }
  else if (typeof styleSheet.insertRule != "undefined" && !isSafari)
  {
    styleSheet.insertRule(selector + " {" + properties + "}", styleSheet.cssRules.length);
  }
  else
  {
    styleSheet.appendChild(document.createTextNode(selector + " {" + properties + "}"));
  }

  return true;
}


