 // client-side code for countdown clocks.
 // Author. Brenden West. 

 // initial variables
 var txtFmt = 1;

 var aDays = new Array ("Sun.", "Mon.", "Tue.", "Wed.", "Thur.", "Fri.", "Sat.");
 var aMonths = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");

function displayClock(oData,dir) {
	if (!oData.ID) { oData.ID = "clock"; }
	if (!oData.BoxStyle && oData.appFmt==2) { oData.BoxStyle = 1; }
	if (!oData.copyMargin) { oData.copyMargin = 9; }
	if (!oData.sBodyFont) { oData.sBodyFont = ""; }
	oData.sBodyFont = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; line-height: 140%;font-weight:bold;" + oData.sBodyFont;


	var sBody = "<table style=width:100% border=0 cellpadding=0 cellspacing=0>";

	sBody += "<tr class=textSmall><td align=center>Days</td><td align=center>Hours</td><td align=center>Mins</td><td align=center>Secs</td></tr>";
	if (document.all || document.getElementById) {
		sBody += "<tr style=\"" +oData.sBodyFont+ "\"><td id=" +oData.ID+ "_days align=center height=15>&nbsp;0&nbsp;</td><td id=" +oData.ID+ "_hrs align=center>&nbsp;0&nbsp;</td><td id=" +oData.ID+ "_mins align=center>&nbsp;0&nbsp;</td><td id=" +oData.ID+ "_secs align=center>&nbsp;0&nbsp;</td></tr>";
	} else {
		sBody += "<tr><td colspan=4><form name=clock><input type=text size=26 name=countbox></form></td></tr>";
	}
	sBody += "</table>";

	if (oData.appFmt == 2) { // MSN9 component style
		if (oData.BoxStyle) {
			if (!window.cssList) { cssList = new Array(); }
			if (window.getCSS) { getCSS(oData.BoxStyle); }
		}

		// main table structure and anchor tag
		document.write("<a name=anc_" +oData.ID+ "><table border=0 cellpadding=0 cellspacing=0 width=" +oData.appWidth+ "><tr><td>");

		//creates header
		if (oData.appHeader) {
			getAppHeader(oData);
			document.write(oData.appHeader);
		}

		document.write("<table cellspacing=0 cellpadding=0 class=boxBody_" +oData.BoxStyle+ " width=100%>");

		if (oData.mainArt) { document.write("<TR valign=top><TD colspan=2 width=100%>" + oData.mainArt + "</TD></TR>"); }
		
		if (oData.appDeck) { document.write("<TR valign=top><TD colspan=2 width=100% style=padding:0px," +oData.copyMargin+ "px,0px," +oData.copyMargin+ "px;><font class=textMed color=666666><b>" +oData.appDeck+ "</b></font></TD></TR>"); }

		document.write("<tr><td class=boxBColor_" +oData.BoxStyle+ ">" + sBody + "</td></tr></table>"); // end box

		if (oData.appFooter) {
			document.write("<span class=credit>"+oData.appFooter+"</span>");
		}
		document.write("</td></tr></table></a>");
	} else {
		if (oData.appDeck) { document.write("<font class=textMed color=666666 style=padding:0px," +oData.copyMargin+ "px,0px," +oData.copyMargin+ "px;><b>" +oData.appDeck+ "</b></font>"); }

		document.write(sBody);
	}

	if (oData.buttons && document.images) {
		num = new Array();
		for (i=0;i<10;i++){
			num[i] = new Image()
			num[i].src = oData.buttons + i + ".gif"
		}
	}

	if (oData.dateEnd) { oData.dDate = oData.dateEnd; }
	if (dir) { oData.Dir = dir; }

	var dNow = new Date();
	var dEnd = new Date(oData.dDate);

	if (oData.elapsedTime) { 
	  dEnd.setMilliseconds(dEnd.getMilliseconds() - getElapsed(oData.elapsedTime));
	}
	var dd = (oData.elapsedTime && oData.stop) ? getElapsed(oData.elapsedTime) : dEnd - dNow;

	if (dEnd > dNow) {
	   setTimeout("updateClock("+oData.ID+"," + dd +");",990)
	}
}

function updateClock(oData,dd){
	if (!oData.stop) {
		dd -= 1000; 
	}
	var sID = oData.ID;
	
	var dday=dd/(60*60*1000*24)*1;
	var dhour=(dd%(60*60*1000*24))/(60*60*1000)*1;
	var dmin=((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1;
	var dsec=(((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1;
	
	if (dd > 0) {
		dday = Math.floor(dday);
		dhour = Math.floor(dhour);
		dmin = Math.floor(dmin);
		dsec = Math.floor(dsec);
	} else {
		dday = Math.abs(Math.ceil(dday));
		dhour = Math.abs(Math.ceil(dhour));
		dmin = Math.abs(Math.ceil(dmin));
		dsec = Math.abs(Math.ceil(dsec));	
	}

	if (document.getElementById || document.all) {
		if (document.getElementById) {
			eval("document.getElementById(\"" + sID + "_days\").innerHTML = " + dday);
			eval("document.getElementById(\"" + sID + "_hrs\").innerHTML = " + dhour);
			eval("document.getElementById(\"" + sID + "_mins\").innerHTML = " + parseInt(dmin));
			eval("document.getElementById(\"" + sID + "_secs\").innerHTML = " + parseInt(dsec));
		} else {
			eval("document.all." + sID + "_days\").innerHTML = " + dday);
			eval("document.all." + sID + "_hrs.innerHTML = " + dhour);
			eval("document.all." + sID + "_mins.innerHTML = " + parseInt(dmin));
			eval("document.all." + sID + "_secs.innerHTML = " + parseInt(dsec));
		}
	} else if (txtFmt==2) {
		var sDay = (dday!=1) ? "days" : "day";
		var sHour = (dhour!=1) ? "hours" : "hour";
		var sMins = (dmin!=1) ? "mins" : "min";
		var sSecs = (dsec!=1) ? "secs" : "sec";
		document.clock.countbox.value = "  y" + dday + " " + sDay + "  " + dhour + " " + sHour+ ", "+parseInt(dmin)+" " +sMins+ ", "+parseInt(dsec)+" " + sSecs;
	} else {
		document.clock.countbox.value = "   " + dday + "          " + dhour + "        "+dmin+" : "+dsec;
	}
	if (!oData.stop) {
		setTimeout("updateClock("+sID+"," + dd+");",990);
	}
}
  

function getDateString(gmtOffset,bWorld,bDay,bYear) {
	var dDate = new Date();
	var sTmp = "";
	// set client time indicator to desired timezone
	dDate.setMinutes(dDate.getMinutes()+dDate.getTimezoneOffset()+gmtOffset);

	var nHours = dDate.getHours();
	var sLabel = (nHours < 12) ? " a.m." : " p.m.";
	if (!bWorld && nHours > 12) { nHours -= 12; }
	if (nHours == 0) { nHours = 12; }

	var minutes = dDate.getMinutes();
	minutes = (minutes < 10) ? "0"+minutes : minutes;

	sTmp += nHours + ":" + minutes+ sLabel;
	if (bDay != 0) { sTmp += " " + aDays[dDate.getDay()] + ","; }
	sTmp += " " + aMonths[dDate.getMonth()] + " " + dDate.getDate();
	if (bYear) { sTmp +=  ", " + dDate.getYear(); }
	return sTmp;
}

function getElapsed(sTmp) { // calculate milliseconds of elapsed time
  var aTmp = sTmp.split(":");
  var dTmp = new Date();
  var nDiff = 0;
  if (aTmp[0]) { nDiff = aTmp[0]*24*60*60*1000; }
  if (aTmp[1]) { nDiff += aTmp[1]*60*60*1000; }
  if (aTmp[2]) { nDiff += aTmp[2]*60*1000; }
  if (aTmp[3]) { nDiff += aTmp[3]*1000; }
  return nDiff;
}


// COMMON FUNCTIONS
// same as for other story components
function getAppHeader(oData) {
	sTmp = "";
	if (oData.appHeader) {

	var hdrIcon = (!oData.HeaderIcon) ? "" : (oData.HeaderIcon.indexOf("/") == 0 || oData.HeaderIcon.indexOf("http://") == 0) ? "<img src=\"" + oData.HeaderIcon + "\"  vspace=0 hspace=0 align=absmiddle>" : oData.HeaderIcon;

	var aHeader = (!oData.appHeader) ? new Array("","") : (oData.appHeader.indexOf("|") > -1) ? oData.appHeader.split("|") : new Array("",oData.appHeader);
	
	var bImgHdr = (aHeader[1].toLowerCase().indexOf("<img") > -1);
	if (bImgHdr) { aHeader = new Array(aHeader[1],""); }
	if (!oData.sTitle) { oData.sTitle = aHeader[1]; }
	if (oData.BoxStyle) {
		var header2 = (aHeader[1]) ? "<td nowrap=1 class=boxH2C_" +oData.BoxStyle+ " style=width:" +100*((oData.appWidth-25)/oData.appWidth)+ "%;><span class=\"textSmall\">" +aHeader[1]+ "</span></td>" : "";
			
		var iconWid = (hdrIcon) ? 25 : 0;
		var strHeader1 = (bImgHdr) ? aHeader[0] : "<div class=\"textSmall\">" +aHeader[0]+ "</div>";

		sTmp = "<table width=" +oData.appWidth+ " cellspacing=0 cellpadding=0 class=boxHeader_" +oData.BoxStyle+ "><tr><td class=boxHI_" +oData.BoxStyle+ " width=1%>" +hdrIcon+ "</td><td width=* nowrap=true class=boxHColor_" +oData.BoxStyle+ ">" +strHeader1+ "</td>" +header2+ "</tr></table>";
		
	} else if ((aHeader[0] || aHeader[1]) && !oData.sHedStyle && !bImgHdr && aHeader[1].toLowerCase().indexOf("<table") == -1) { // blue/grey interactives

		var brdr = "1px solid #a4abc1";
		var hfs = (oData.appWidth <= 200) ? 85 : 100;
		oData.sHedStyle = (aHeader[1]) ? "color:#26386b;border-top:" +brdr+ ";border-left:" +brdr+ ";border-right:" +brdr+ ";font-family: Arial, Helvetica, sans-serif; font-size: " +hfs+ "%;font-weight:bold;padding-left:4px;width:90%;height:25px;" : "height:20px;";

		var hedAlign = "align=center";
		if (aHeader[0]) {
			var hedAlign = "";
			aHeader[0] = "&nbsp;&nbsp;" + aHeader[0];
			oData.labelStyle = "font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 80%;font-weight:bold; color: #FFFFFF; text-transform:uppercase;padding-right:10px;";
		}

		sTmp = "<table width=100% cellspacing=0 cellpadding=0><tr><td nowrap=1 class=label"+oData.ID+">" +hdrIcon +aHeader[0]+ "</td><td nowrap=1 class=hed"+oData.ID+" " +hedAlign+ ">" +aHeader[1]+ "</td></tr></table>";

	} else if (oData.appHeader) {
		sTmp = "<DIV class=hed"+oData.ID+">" +oData.appHeader+ "</DIV>";
	}
	}
	oData.appHeader = sTmp;
}
