// The purpose of this function is to automatically change the color of announcements
// as they get older: when they show up, they're bright and easy to notice, if 
// they're more than a week old, they 'cool' somewhat, and stuff that's expired is a dead
// color.  
//
// The point is that I can leave all the postings on the web site for archival purposes,
// yet the most recent postings will be easy to notice.
//
// Notes:
//	Jan == 0, Dec == 11
//	Don't forget that if an event happens on day X, the notice should expire the next day
//
var foo = 10;
function f( arg1, arg2 )
{
	alert("yo, what up?");
}
var newPostingColor = "red"
var moreThanOneWeekOldColor = "darkgreen"
var expiredColor = "black"
function StartAutoColorText(yearPosted, monthPosted, dayPosted, yearExp, monthExp, dayExp)
{
    var today = new Date();
    
    // TODO: If we're within a week of the end of the month, this is smart enough
    // to do the right thing, and fall into the next month, right?
    var oneWeekLater = new Date(yearPosted, monthPosted, dayPosted + 7);
    
//	alert("YP: " + yearPosted + " oldY: " + oneWeekLater.getFullYear() + " curYear:" + today.getFullYear() + " MP: " + monthPosted + " oldM: " + oneWeekLater.getMonth() + " curMon: " + today.getMonth() + " DP: " + dayPosted + " oldDay: " + oneWeekLater.getDate() + " curDay: " + today.getDate() + "\ntypeof yearExp: " + typeof yearExp + " today: " + today.getTime() + " +1week: " + oneWeekLater.getTime() + " new? " + (today.getTime() <= oneWeekLater.getTime()));

    var expired = new Date(yearExp, monthExp, dayExp);
    if ( !(typeof yearExp == "undefined") && today.getTime() > expired.getTime())
    {
        document.write( "<FONT COLOR=" + expiredColor +">");
        return;
    }

    if (today.getTime() <= oneWeekLater.getTime())
    {
        document.write( "<FONT COLOR=" + newPostingColor +">");
    }
    else // today.getTime() > oneWeekLater.getTime()
    {
        document.write( "<FONT COLOR=" + moreThanOneWeekOldColor +">");
    }
}

// Wrapper function
// In case I decide to change SPAN to something else, etc.
function EndAutoColorText()
{
    document.write("</FONT>");
}

function PrintAutoColorExplanation()
{
    document.write("<FONT SIZE=\"-1\">Using JavaScript, the colors of the announcements should automatically change as they get older<BR>");
    document.write("<FONT COLOR=\""+newPostingColor+"\">Brand new announcements are in "+newPostingColor+"</FONT><BR>");
    document.write("<FONT COLOR=\""+moreThanOneWeekOldColor+"\">Announcements more than 1 week old, but still relevant, are in "+moreThanOneWeekOldColor+"</FONT><BR>");
    document.write("<FONT COLOR=\""+expiredColor+"\">Announcements that have expired (but are left in the page for archival purposes), are in "+expiredColor+"</FONT><BR>");
}


// TableGradient: Create a bunch of TDs that are leftColor on the left edge, and rightColor on the right
//
// leftColor, righColor are specified in RGB hex. 
// Width is a suggestion on how wide to make the whole gradient
// numBands is how many separate TD's to make.
//
// NOTES: Remember that any TD's above this will probably have to use the COLSPAN to span these gradient columns
function TableGradient(leftColor, rightColor, width, numBands)
{
    var RED_MASK = 0xFF0000;
    var GREEN_MASK = 0x00FF00;
    var BLUE_MASK = 0x0000FF;
    var redL = (leftColor & RED_MASK) >> 16;
    var redR = (rightColor & RED_MASK) >> 16;
    var redIncr = (redL - redR) / (numBands-1);
    redIncr*=-1;
    
    var greenL = (leftColor & GREEN_MASK) >> 8;
    var greenR = (rightColor & GREEN_MASK) >> 8;
    var greenIncr = (greenL - greenR) / (numBands-1);
    greenIncr*=-1;

    var blueL = (leftColor & BLUE_MASK);
    var blueR = (rightColor & BLUE_MASK);
    var blueIncr = (blueL -blueR ) / (numBands-1);
    blueIncr*=-1;
//	alert("redL:" + redL + "\nredR:" + redR + "\nredIncr: " + redIncr + "\ngreenL:" + greenL + "\ngreenR:" + greenR + "\ngreenIncr: " + greenIncr + "\nblueL:" + blueL + "\nblueR:" + blueR + "\nblueIncr: " + blueIncr);
    
//    document.write("<TABLE cellpadding=\"0\" cellspacing=\"0\" height=\"100%\">");
    var dbgStr = "";
    for(var i = 0; i < numBands; i++)
    {
        var redNum = Math.round(redL + redIncr * i);
        var greenNum = Math.round(greenL + greenIncr*i);
        var blueNum =Math.round(blueL + blueIncr*i);
        var colorNum = redNum + greenNum + blueNum ;
        dbgStr  += "\n\nredNum:" + redNum + "/0x" + redNum.toString(16) + "\ngreenNum: " + greenNum + "/0x" + greenNum.toString(16) + "\nblueNum: " + blueNum + "/0x" + blueNum.toString(16) + "\ncolorNum:" + colorNum+ "/0x" + colorNum.toString(16) ;
        redNum = ("0" + redNum.toString(16)).slice(-2); // get me the last 2 characters, with a padding 0, if needed
        greenNum = ("0" + greenNum.toString(16)).slice(-2); 
        blueNum = ("0" + blueNum.toString(16)).slice(-2); 
        var outputStr = "<TD WIDTH=\"" + width + "px\" bgcolor=\"#" + redNum + greenNum + blueNum + "\"></TD>";
        dbgStr += "\"" + outputStr ;
        document.write(outputStr);
    }
//    alert(dbgStr);
//    document.write("</TABLE>");
}
