//<!--

/**
 * Archive calendar for Blogger, copyright Romans Kasperovics, 2005.
 * For details visit http://romkas.blogspot.com/2005/07/blogger_calendar.html
 *
 * You can freely use this script for your blog. I'd appreciate if you keep
 * this comment with my name and a link to my blog. If find any errors, please
 * put them as a comment to my post about blogger calendar. This is especially
 * a case for different browsers - I didn't check IE and Opera. Feature requests
 * also are welcome. If you make some improvements yourself, show me,
 * so I can use them too :) Ragards, Romans.
 */


/**
 * TODO: disallow to go forward and to go back if there's no more records
 * TODO: react also to the date in the item page ;)
 */


//var isIE = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );
//var isIE5 = ( isIE && /msie 5\.0/i.test(navigator.userAgent) );
var isOpera = /opera/i.test(navigator.userAgent);
//var isKHTML = /Konqueror|Safari|KHTML/i.test(navigator.userAgent);
var isMozilla = /Mozilla\/[45]/i.test(navigator.userAgent);
var isGoodBrowser = isOpera || isMozilla;

var vCurDate = new Date();
var vURLDate = "";
var vHTML;
var reg = /.*([0-9]{4})_([0-9]{2})_([0-9]{2}).*/;
if (reg.test(document.URL)) {
  vURLDate = document.URL.replace(reg, "$1_$2_$3");
  vCurDate.setDate(document.URL.replace(reg, "$3"));
  vCurDate.setMonth(document.URL.replace(reg, "$2") - 1);
  vCurDate.setFullYear(document.URL.replace(reg, "$1"));
}

var reg2 = /.*\/([0-9]{4})\/([0-9]{2})\/.*/;
if (reg2.test(document.URL)) {
  vURLDate = document.URL.replace(reg2, "$1_$2_01");
}

var vParent;
var DAYS = new Array('Su','Mo','Tu','We','Th','Fr','Sa');
var MONTHS = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var vArcDates = new Array(0);
var vArcLinks = new Array(0);

function drawURLBack(pBlogHome) {
  document.write("<p>Press <a href=\"" + pBlogHome + "#" + vURLDate + "\">here</a> to enter Posts-By-Date view</p>");
}

function getElement(aID){
  var rv = (document.getElementById) ? document.getElementById(aID) : document.all[aID];
  return rv;
}

function formatDate(pDate, pMonth, pYear) {
  return MONTHS[pMonth] + " " + ("0" + pDate).slice(-2) + ", " + pYear;
}

function createElement(type, parent) {
  var el = null;
  if (document.createElementNS) {
    // use the XHTML namespace; IE won't normally get here unless
    // _they_ "fix" the DOM2 implementation.
    el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
  } else {
    el = document.createElement(type);
  }
  if (typeof parent != "undefined") {
    parent.appendChild(el);
  }
  return el;
};

function prevMonth () {
  if (vCurDate.getMonth() == 0) {
    vCurDate.setYear(vCurDate.getFullYear()-1);
    vCurDate.setMonth(11);
  } else {
    vCurDate.setMonth(vCurDate.getMonth()-1);
  }
}

function nextMonth () {
  if (vCurDate.getMonth() == 11) {
    vCurDate.setYear(vCurDate.getFullYear()+1);
    vCurDate.setMonth(0);
  } else {
    vCurDate.setMonth(vCurDate.getMonth()+1);
  }
}

function drawCalendar () {

  var vDate = new Date(vCurDate.toString());
  vDate.setDate(1);    // Start the calendar day at '1'
  var vWeek = vDate.getDay(); // Day of week of the first day of a month
  var vYear = vDate.getFullYear(); // Returns year
  var vMonth = vDate.getMonth(); // Returns month (0-11)
  var vWeekIndex = 0;
  if (vParent == null) vParent = getElement("div-archive-calendar");
  //TABLE HEADER WITH MONTH AND YEAR
  var vDayHTML;
  vHTML  = '<table width="100%"><thead><tr>\n';
  vHTML += '<td class="cellArrow" onClick="prevMonth();drawCalendar()">&lt;&lt;</td>\n';
  vHTML += '<td colspan="5" class="cellMonth">' + MONTHS[vMonth] + ' / ' + vYear + '</td>\n';
  vHTML += '<td class="cellArrow" onClick="nextMonth();drawCalendar()">&gt;&gt;</td>\n';
  //TABLE HEADER WITH DAY NAMES
  vHTML += '</tr><tr>\n';
  for (var i=0;i<7;i++) {
    vHTML += '<td class="cellWeekDays">' + DAYS[i] + '</td>\n';
  }
  //EMPTY CELLS BEFORE
  vHTML += '</tr></thead><tbody><tr>\n';
  for (var i=0;i<vWeek;i++) {
    vHTML += '<td class="cellEmpty">&nbsp;</td>\n';
    vWeekIndex++;
  }
  //DAYS OF THE MONTH
  for (var i=0; i<31; i++) {
    if (i>vDate.getDate()) break;
    if(vWeekIndex==7) {
      vWeekIndex = 0;
      vHTML += '</tr><tr>';
    }
    vDayHTML = '<td class="cellDayEmpty">' + (i+1) + '</td>\n';
    for (var j=0; j<vArcDates.length; j++) {
      if (vArcDates[j] == formatDate(i+1,vMonth,vYear)) {
        vDayHTML = '<td class="cellDayFilled"><a href="' + vArcLinks[j] + '">' + (i+1) + '</a></td>\n';
        break;
      }
    }
    vHTML += vDayHTML;
    vDate.setDate(vDate.getDate()+1);
    vWeekIndex++;
  }
  //EMPTY CELLS AFTER
  for (var i=vWeekIndex;i<7;i++) {
    vHTML += '<td class="cellEmpty">&nbsp;</td>\n';
  }
  vHTML += '</tr></tbody></table>\n';
  vParent.innerHTML = vHTML;
  delete vDate;
}

function drawDebugInfo() {
  var lDebugParent = getElement("div-debug-info");
  var lDatesHTML = '<ul>';
  for (var i=0; i<vArcDates.length; i++) {
    lDatesHTML += '<li>' + vArcDates[i] + ', ' + vArcLinks[i] + '</li>';
  }
  lDatesHTML += '</ul>';
  if (lDebugParent != null) {
    lDebugParent.innerHTML = "<p>Dates and links to be displayed by calendar: </p>" + lDatesHTML + "<p>User Agent: "+navigator.userAgent+"</p>" + "<xmp>" + vHTML + "</xmp>";
  }
}
// -->