
// -----------------------	UTILITY FUNCTIONS ---------------------------- //

function removeSpaces(txt){
txt = txt.replace(/[^a-zA-Z0-9]+/g,'');
return txt;
}


function toUpper(name) { //capitalize name
       var firstLetter = name.substring(0, 1).toUpperCase();
        var restOfWord = name.substring(1, name.length).toLowerCase();

        name = firstLetter + restOfWord; 
  return name;
}

 

function nameDays(myDate,days) { //set date into month/day/year format
 //  return new Date(myDate.getTime() + days*24*60*60*1000);
 
	var dte = new Date(myDate.getTime() + (days*1000*60*60*24));
	dte.setHours(dte.getHours()-5);
var d2  = dte.getDate();
var dy = (d2 < 10) ? '0' + d2 : d2;
var mnth = dte.getMonth() + 1;
//var mnth = (m < 10) ? '0' + m : m;
var yy = dte.getYear();
var yr = (yy < 1000) ? yy + 1900 : yy;
return mnth + "/" + dy + "/" + yr;
}


function dayToDays(inTime) { //convert time since Jan 1 1970 to days
	return (Math.floor(inTime.getTime()/(1000*60*60*24)));
}


function daysFrom(inDate) { //subtract days to find difference
	return dayToDays(d) - dayToDays(startDate);
}



// -----------------------	SET START DATE ---------------------------- //



var d = new Date(new Date().getTime());
	d.setHours(d.getHours()-5);
//d = new Date();
this.month = d.getMonth();
this.date = d.getDate();
this.day = d.getDay();
this.year = d.getFullYear();

var months = new Array('01','02','03','04','05','06','07','08','09','10','11','12');
var days = new Array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');


//var d = new Date (2007,0,18); //TEMP variable: use this with a date in the future to see image-to-date placements

startDate = new Date(2006,9,31); //start date was Oct 31, 2006
//startDate = new Date(2006,9,10);  //change to test images



var baseURL = "/Media/jigsaws/"




// -----------------------	WRITE TODAY'S LINK ---------------------------- //


var thisDaysLink = months[this.month] + this.date + this.year; //today
var which = daysFrom(startDate); //how many days is today away from start date
var todaysFile = fileArray[which][1]; //set file path from fileArray
var fileTextTitle = fileArray[which][0]; //set file title from fileArray
var pastFiles;

function writeDateLink(){
	var w = 900;
	var h = 700;
	if (screen.width< 801) { w =695; h=590; } else { w=900; h=700; }<!--nameDays(new Date(),0) + -->
	document.write("<a href=\"javascript:;\" onClick=\"window.open('" + baseURL + "jigsaw.cfm?" + which + "','jigsaw','resizable=yes,width=" + w + ",height=" + h + "');\"><h3>Today's puzzle: " + fileTextTitle + "</h3><img src=\"/Media/jigsaws/puzzles/" + todaysFile + ".jpg\" alt=\"" + fileTextTitle + "\" class=\"banner\" style=\"margin:10px auto; max-width:570px;\"></a>");
}



// -----------------------	WRITE PAST WEEK'S LINKS TABLE ---------------------------- //

var filePath;
var fileTitle;
var dayName = new Date();
//var dayName = d; //TEMP variable: use this with a date in the future to see image-to-date placements

function writeWeeksLinks(){
document.write('<table width="520" class="defaultTable plain"><tr><td style="text-align:center;" colspan=4"');
document.write("\"><h3>Previous Puzzles</h3></td></tr><tr>");
for (i = 1;i <= which && i < 9; i++) {
	filePath = fileArray[which - i][1];
	fileTitle = fileArray[which-i][0];
	document.write("<td style=\"text-align:center;\" valign=\"top\">");
	document.write("<span class=\"pastDate\">" + nameDays(dayName,-i) + "</span>");
	document.write("<br><a href=\"javascript:;\" onClick=\"window.open('" + baseURL + "jigsaw.cfm?" + (which-i) + "','jigsaw','resizable=yes,width=900,height=700');\">" + fileTitle+ "<br><img src=\"/Media/jigsaws/puzzles/sm/" + filePath + ".jpg\" alt=\"" + fileTitle + "\" class=\"banner\"></a></td>");
	if (i == 4) {
		document.write("</tr><tr>");
	}
}
document.write("</tr></table>");
}


// -----------------------	WRITE GALLERY TABLE ---------------------------- //

var thisCategory;

function makeGalleryTable() {
	
	var holderArray = new Array();
	
	
	document.write('<table class="defaultTable plain" style="width:600px;float:none; margin:0 auto;"><tr><td valign="top" id="galleryCategories" colspan="4" style="text-align:center;"><h3>Gallery/Archives</h3></td></tr><tr>');
	
	for(k=1;k<= which;k++){ // for as many days as it's been since start date, cycle through the used files and create the gallery table from the ones that have been used.
	
	
	
	
	for(m=2;m<fileArray[which-k].length;m++) { //cycle through sub array
	
		thisCategory = fileArray[which-k][m]; // thisCategory is the MAIN category for each particular day (also folder where image is located)
		
		isThere = false;
		
		for (n=0;n<holderArray.length;n++) { //go through the holder array
	
			if (thisCategory == holderArray[n] || thisCategory == "reused") { // if thisCategory is listed in the holder array...
			isThere = true; // set variable to true
			break;
			}
		}
		
			if (!isThere &&  thisCategory != "reused") { // if thisCategory was not listed in the holder array add it to end of array and write its link
					 holderArray[holderArray.length] = thisCategory;
					capName = toUpper(thisCategory);
				   document.write('<td style="text-align:center;"><a href="galleries.cfm" onClick="this.href=\'galleries.cfm?'+ thisCategory + '\'" class="current">' + capName + '<br /><img src="/Media/jigsaws/images/gallery_' + thisCategory + '.gif" alt="' + capName + ' Gallery" width="100" height="83" /></a></td>');	
				 	if ( n==3 || n==7 || n==11) {
					document.write("</tr><tr>");
				}
			}  
			
		}
			
			
		}
	document.write('<br class="clear:both;"></td></tr></table>');
}



// -----------------------	WRITE GALLERY PAGE ---------------------------- //

function makeGallery(galleryName) {
	if (capName == "Misc") { capName = "Miscellaneous"} else { capName = capName};
	document.write("<h1>Daily Jigsaw " + capName + " Gallery</h1><a href=\"" + baseURL + "\">Back to Daily Jigsaw page</a><br /><br />");
	
		
	switch (galleryName)
{
   case "animals" :
      document.write ("Learn more about Colonial Williamsburg's <a href=\"/Almanack/life/animals/pr_rare.cfm\">animals.</a>");
      break;
   case "spring" :
      document.write ("Learn more about Colonial Williamsburg's <a href=\"/history/CWLand/index.cfm\">gardens.</a>");
      break;
   case "people" :
      document.write ("Learn more about the <a href=\"/Almanack/people/people.cfm\">people</a> of Williamsburg.");
      break;
   case "military" :
      document.write ("Learn more about Colonial Williamsburg's <a href=\"/history/fife&drum/about.cfm\">Fifes and Drums.</a>");
      break;
   case "christmas" :
      document.write ("Learn more about <a href=\"http://www.history.org/christmas/\" target=\"_blank\">Christmas in Williamsburg.</a><br>");
      document.write ("Learn about <a href=\"/visit/eventsAndExhibits/specialEvents/\">holiday events</a> in Williamsburg.");
      break;
   case "people" :
      document.write ("Learn more about colonial <a href=\"/history/clothing/intro/index.cfm\">fashion.</a>");
      break;
   case "trades" :
      document.write ("Learn more about Colonial Williamsburg's <a href=\"/Almanack/life/trades/tradehdr.cfm\">trades.</a>");
      break;
   case "sites" :
      document.write ("Learn more about Colonial Williamsburg's Historic Area with our interactive <a href=\"/visit/tourTheTown/index.cfm\">Tour the Town</a> feature.");
      break;
   case "thanksgiving" :
      document.write ("Learn about <a href=\"/visit/eventsAndExhibits/specialEvents/\">holiday events</a> in Williamsburg.");
      break;
   default :
      document.write ("");
} 
	
	
	document.write('<br><br><table class="defaultTable plain"><tr>');

	rowtime = 0;
for (j=0; j<=which;j++) {  //as long as j is less than the difference between the current date and the start date

for(n=2;n<fileArray[j].length;n++) { //cycle through sub array


	
		galleryFile = fileArray[j][1];
		fileTxtTtle = fileArray[j][0];
		
	
	
					if (galleryName == fileArray[j][n]) {
						document.write("<td style=\"text-align:center;\" valign=\"top\"><a href=\"javascript:;\" onClick=\"window.open('" + baseURL + "jigsaw.cfm?" + j + "','jigsaw','resizable=yes,width=900,height=700');\">" + fileTxtTtle + "<img src=\"/Media/jigsaws/puzzles/sm/" + galleryFile + ".jpg\" alt=\"" + fileTxtTtle + "\" class=\"banner\"></a></td>");
					if (rowtime == 0) {
						rowtime = 1;
					} else if (rowtime == 1) {
						document.write("</tr><tr>");
						rowtime = 0;
					}
				} 
	
	
	
	
	
	
			}

	}
	
	
	document.write("</tr></table><BR /><br />");
}	

// -----------------------	WRITE TABLE WITH ALL IMAGES FOR INHOUSE REVIEW ---------------------------- //

var filePath;
var fileTitle;
var dayName2 = new Date(2006,10,1);
var countNum= (fileArray.length-1);//0;
//var dayName = d; //TEMP variable: use this with a date in the future to see image-to-date placements

function writeAllImages(){
document.write("<table width=\"550\" cellpadding=\"5\" cellspacing=\"0\" border=\"0\" align=\"center\" bgcolor=\"#FFFFFF\"><tr>");
for (i = 0;i <= (fileArray.length/5) ; i++) {
	for(m=0;m<5;m++) {
	
	
	filePath = fileArray[countNum][1];
	fileTitle = fileArray[countNum][0];
	document.write("<td align=\"center\" valign=\"top\">");
	document.write("<span class=\"pastDate\">" + nameDays(dayName2,countNum) + "</span>");
	document.write("<br><span style=\"font-size:10px;font-family:Verdana,Arial,sans;\">" + fileTitle+ "</span><br><a href=\"" + baseURL + "puzzles/" + filePath + ".jpg\" target=\"_blank\"><img src=\"" + baseURL + "puzzles/sm/" + filePath + ".jpg\" alt=\"" + fileTitle + "\" border=0></a></td>");
	countNum = countNum-1;
	}
		document.write("</tr><tr>");
	
}
document.write("</tr></table>");
}
