// lookup.js - generates html code for a link element to load the correct liturgical calendar color scheme
//    - litcal.js should be executed before lookup.js and both should be within the html head section
//    - defines and sets the value for the globals "litcal_headertext" and "litcal_scripttext" which should be written into
//      the appropriate div element
//    - lookup_date: if not defined, today's date is used, else a date string in yyyymmdd format specified

var litcal_headertext = "(default)";
var litcal_scripttext = "(default)";

function get_lookup_date()  // extracts the date from the location URL string; format is website.com?date=yyyymmdd
{
   var litcal_date; // date string in the format yyyymmdd, if undefined then today's date used instead for litcal_lookup()
   var datestr = location.search.substring(1);
//   document.write("datestr="+datestr+"<br/>");
   var pos = datestr.indexOf('=');
//   document.write("pos="+pos.toString()+", date:"+datestr.substring(0,4)+"<br/>");
   if (pos == 4 && (datestr.substring(0,4) == "date"))
     {
	 litcal_date = datestr.substring(5);
//    document.write("litcal_date="+litcal_date+"<br/>");
	 }
   return litcal_date;
}

function litcal_lookup(lookup_date)
{
   var cssfile = "lit_default.css";
   litcal_headertext = "";
   litcal_scripttext = "";
   if (!litcal)
 	  document.write("litcal.js not loaded <br />");
  else
      {
      var ldt = lookup_date?lookup_date.match(/(\d{4})(\d{2})(\d{2})/):undefined;
	  var today = ldt?new Date(parseInt(ldt[1],10), parseInt(ldt[2],10)-1, parseInt(ldt[3],10)):new Date();
//	  document.write("lookup_date=" + today.toLocaleString()+"<br />");
      for (var i=0; i<litcal.length; i++)
	     {
         var dt = litcal[i].d.match(/(\d{4})(\d{2})(\d{2})/);
         if (!dt)
	        {
//			document.write("bad date: " + litcal[i].d+"<br />");
            litcal_headertext = "";
			litcal_scripttext = "";
			cssfile = "lit_default.css"
			}
         else
		    {
            var litcal_date = new Date(parseInt(dt[1],10), parseInt(dt[2],10)-1, parseInt(dt[3],10));
//            document.write("i="+i.toString()+" litcal_date="+litcal_date.toLocaleString()+"<br />");
            if ((today.getFullYear() < litcal_date.getFullYear()) || 
				((today.getFullYear() == litcal_date.getFullYear()) && ((today.getMonth() < litcal_date.getMonth()) ||
				((today.getMonth() == litcal_date.getMonth()) && (today.getDate() < litcal_date.getDate())))))
		       break; // previous litcal item to be used
	        else {
	           if (litcal[i].lc != "*")
			      cssfile = "scripts/lit_" + litcal[i].lc + ".css";
	           if (litcal[i].ht != "*")
			      litcal_headertext = litcal[i].ht;
	           if (litcal[i].st != "*") {
			      var str = litcal[i].st;
				  var pos = str.search(/\s+\(/); 
//				  document.write("pos="+pos.toString()+"<br/>");
				  if (pos > 0)
				     litcal_scripttext = '"' + str.substring(0,pos) + '"'+ str.substring(pos,str.length);
			      else
				     litcal_scripttext = str;
			   }
//            document.write("header:"+litcal_headertext+", scripture:"+litcal_scripttext+"<br />");
            }
         }
      }
	  }
// comment out the next line for testing, the second line when working
document.write('<link rel="stylesheet" type="text/css" href="'+cssfile+'" />\n');

//   document.write('link rel="stylesheet" type="text/css" href="'+cssfile+'"<br />\n');
}

litcal_lookup(get_lookup_date());
