var todayDate = "";
var todayYear = "";
var gbDate;
var gbMonNum;
var gbYear;

        CalDate=CalDate.toLowerCase()
        if(CalDate.indexOf(":")==-1){
                dte=CalDate.split("|")
        }else{
                dte=new Array();
                tmpDte=CalDate.split("-");
                dte[0]=tmpDte[0].substring(0,3);
                dte[1]=tmpDte[1];
                dte[3]=tmpDte[2].substring(0,4);
                tmpDte=tmpDte[0].split(", ");
                dte[2]=tmpDte[1];
        }
        if(dte[2].indexOf("0")==0){dte[2]=dte[2].charAt(1)}
        gbDate=parseInt(dte[2]);
        gbMonth=dte[1];
        gbYear=dte[3];
        var ldays = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
        for(i=0;i<ldays.length;i++){
                if(gbMonth.substring(0,3)==ldays[i]){gbMonNum=i};
        }


function renderCal(xDate,xMonNum,xYear){
	var tbl = "";
	var cDate = new Date();var dDate = new Date();
	dDate.setTime(Date.UTC(xYear, xMonNum, xDate+1).toString())
	var back = (Math.ceil((cDate.getTime() - dDate.getTime()) / 86400000))
	if(back<7){
		var initDiff = cDate.getDay();
		var toDay = dDate.getDate() + "-" + dDate.getMonth();
		cDate.setDate(cDate.getDate()-6)
		var diffDate = new Date()
		diffDate.setTime(Date.UTC(xYear, xMonNum, xDate-(5-back)).toString())
		var weekAgo = diffDate.getDate() + "-" + diffDate.getMonth();
		cDate.setDate(cDate.getDate()-(initDiff+1))
		var tmpCalCell = "";
		var tmpLink1 = "";
		var tmpLink2 = "";
		var tpWk = 0;
		var wkCount = false;
	 	for(i=0;i<14;i++){
			var tmpDt = cDate.getDate() + "-" + cDate.getMonth();
			if((tmpDt == weekAgo)||(wkCount)){
				if(tpWk<7){
					tpWk++; tmpCalCell = "calCellWeek";	wkCount = true;
				}else{
					wkCount = false;
				}
			}else{
				tmpCalCell = "";tmpLink1 = tmpLink2 = ""
			}
			if(tmpDt == toDay){
				tmpCalCell = "calCellToday";
				tpWk++;
                                todayTmp = toDay;
                                todaySplit = parseInt(todayTmp.split("-")[1]) + 1;
                                if(todaySplit < 10)
                                  todayDay = "0" + todaySplit;
                                else 
                                  todayDay = todaySplit
                                todayYear = "" + dDate.getYear();
                                todayDate = todayYear.slice(1) + "_" + todayTmp.split("-")[0] + "_" + todayDay;
			}
			if(tmpCalCell!=""){
				var calMonPad = cDate.getMonth() + 1;
				var calDayPad = cDate.getDate();
				if (calMonPad < 10) (calMonPad = "0" + calMonPad);
				if (calDayPad < 10) (calDayPad = "0" + calDayPad);
				tmpLink1 = "<a onclick=\"s_linkType='TodaysGlobe_SevenDay_Archive';\" href='http://www.boston.com/bostonglobe/" + cDate.getFullYear() + "/" + calMonPad + "/" + calDayPad + "/'>";
				tmpLink2 = "</a>";
			}
			tbl += "<td class='" + tmpCalCell + "' title='" + cDate.getDate() + "'>" + tmpLink1 + cDate.getDate() + tmpLink2 + "</td>";
			if(tmpDt == toDay){if(tpWk<8){tmpCalCell = "calCellWeek"}else{tmpCalCell = "";tmpLink1 = tmpLink2 = ""}}
			if(i==6){tbl += "</tr><tr class='calRow'>";}
			cDate.setDate(cDate.getDate()+1)
		}
                document.write("<div class=\"leadIn\">From the past seven days</div>");
		document.write("<table class='calTable' cellpadding='0' cellspacing='0' border='0'><tr><td class='calHed'>Su</td><td class='calHed'>M</td><td class='calHed'>Tu</td><td class='calHed'>W</td><td class='calHed'>Th</td><td class='calHed'>F</td><td class='calHed'>S</td></tr><tr class='calRow'>" + tbl + "</tr></table>");
	}else{
		document.write("<center><a href='http://search.boston.com/local/Historic.do'>Search the Globe Archives</a></center>");
	}
}

function initCal(){
        CalDate=CalDate.toLowerCase()
        if(CalDate.indexOf(":")==-1){
                dte=CalDate.split("|")
        }else{
                dte=new Array();
                tmpDte=CalDate.split("-");
                dte[0]=tmpDte[0].substring(0,3);
                dte[1]=tmpDte[1];
                dte[3]=tmpDte[2].substring(0,4);
                tmpDte=tmpDte[0].split(", ");
                dte[2]=tmpDte[1];
        }
        if(dte[2].indexOf("0")==0){dte[2]=dte[2].charAt(1)}
        gbDate=parseInt(dte[2]);
        gbMonth=dte[1];
        gbYear=dte[3];
        var ldays = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
        for(i=0;i<ldays.length;i++){
                if(gbMonth.substring(0,3)==ldays[i]){gbMonNum=i};
        }

	renderCal(gbDate,gbMonNum,gbYear);
}
function initCoverImage(){
gbMonNum = gbMonNum + 1;
if (gbDate < 10)
  gbDate = "0" + gbDate;
else
  gbDate = gbDate;
if (gbMonNum < 10)
  gbMonNum = "0" + gbMonNum;
else
  gbMonNum = gbMonNum;


var todayPopDate = gbYear + "_" + gbMonNum + "_" + gbDate;

         document.write("<img src=\"http:\/\/cache.boston.com\/globe\/" + gbYear + "\/" + gbMonNum + "\/" + gbDate + "\/" + "today_thumbnail.jpeg\" title=\"Globe Cover Image\" alt=\"Globe Cover Image\" border=\"0\" id=\"globeCov\" \/>");
         document.write("<a href=\"http:\/\/www.boston.com\/news\/globe\/larger_view\/?g_date=" + todayPopDate + "\" onclick=\"openWindow('http:\/\/www.boston.com\/news\/globe\/larger_view\/?g_date=" + todayPopDate + "','largerview','width=650,height=1100,resizable=yes,scrollbars=yes,toolbar=no,location=no,menubar=no,status=no'); return false;\" class=\"globePop\">Enlarge this image</a>");
};