//---------------------------------------------------------------------------------------------
// Bloglendar, the calendar navigation JavaScript for Blogger
//
// Created by philringnalda (http://www.philringnalda.com/archivescripts/calendar/):
// - A navigation device for your (monthly) archive pages
//
// Bug fixed by Andy (http://www.cs.cityu.edu.hk/~hwchun/):
// - Fixed the bug of displaying the wrong month when there exists posts of 2 or more months
//
// Code cleaned and enhanced by minghong (http://minghong.dyndns.org:8080/OpenWiki/?MingHong):
// - Added "var"s for every variables
// - Seperate style into CSS
// - Improved stylesheet - the chance of having name conflict with the existing stylesheet should be lowered
//
//---------------------------------------------------------------------------------------------

/*
 * You absolutely, positively must change the value for dateType below to match your
 * "Date Header Format" as set in your blog's Settings
 * If your date format is:
 * "Sunday, June 24, 2001" use 1
 * "6/24/2001" use 2
 * "6.24.2001" use 3
 * "20010624" use 4
 * "2001/06/24" use 5
 * "2001-06-24" use 6
 * "24.6.01" use 7
 * "June 24, 2001" use 8
 * "june 24, 2001" use 9
 * something else, change to one of those and use the right number!
 */
var dateType = "1";

/*
 * ID of the bloglendar placeholder
 * If changed, update the ID in CSS as well
 */
var bloglendarId = "bloglendarHere";

/*
 * Change the lowMonthNames if you use a non-English language
 * and you use one of the date styles with the month name in the date,
 * but be sure they are in lower case (these are used for matching, not displaying)
 */
var lowMonthNames = new Array( "january", "february", "march", "april",
                               "may", "june", "july", "august",
                               "september", "october", "november", "december" );

/*
 * You can change the displayMonthNames and displayWeekdayNames to anything that suits you
 */
var displayMonthNames = new Array( "January", "February", "March", "April",
                                   "May", "June", "July", "August",
                                   "September", "October", "November", "December" );
var displayWeekdayNames = new Array( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" );

//---------------------------------------------------------------------------------------------
// Time to stop changing things, unless you are sure you know what you're doing!
//---------------------------------------------------------------------------------------------

/*
 * The links to blog posts
 */
var links = new Array();

// [Andy 1-Sep-2003]
var currentMonth = null;
var firstPostDate = null;

function datesplitter( date, datetype )
{
    var year, month, day;
    switch ( datetype )
    {
    case "1" :
        date = date.substring( date.indexOf(",")+2 );
        year = date.substring( date.length-4 );
        day = date.substring( date.indexOf(" ")+1, date.indexOf(",") );
        var wordMonth = date.substring( 0, date.indexOf(" ") );
        wordMonth = wordMonth.toLowerCase();
        for ( var i = 0; i < 12; i++ )
        {
            if ( lowMonthNames[i] == wordMonth )
            {
                month = i;
                i = 12;
            }
        }
        break;
    case "2" :
        year = date.substring( date.length-4 );
        month = date.substring( 0, date.indexOf("/") )-1;
        day = date.substring( date.indexOf("/")+1, date.lastIndexOf("/") );
        break;
    case "3" :
        year = date.substring( date.length-4 );
        month = date.substring( 0, date.indexOf(".") )-1;
        day = date.substring( date.indexOf(".")+1, date.lastIndexOf(".") );
        break;
    case "4" :
        year = date.substring( 0, 4 );
        month = date.substring( 4, 6 );
        if ( month.charAt(0) == "0" )
            month = month.substring( 1 );
        month = month-1;
        day = date.substring( 6 );
        if ( day.charAt(0) == "0" )
            day = day.substring( 1 );
        break;
    case "5" :
        year = date.substring( 0, 4 );
        month = date.substring( date.indexOf("/")+1, date.lastIndexOf("/") );
        if ( month.charAt(0) == "0" )
            month = month.substring( 1 );
        month = month-1;
        day = date.substring( date.lastIndexOf("/")+1 );
        if ( day.charAt(0) == "0" )
            day = day.substring( 1 );
        break;
    case "6" :
        year = date.substring( 0, 4 );
        month = date.substring( date.indexOf("-")+1, date.lastIndexOf("-") );
        if ( month.charAt(0) == "0" )
            month = month.substring( 1 );
        month = month-1;
        day = date.substring( date.lastIndexOf("-")+1 );
        if ( day.charAt(0) == "0" )
            day = day.substring( 1 );
        break;
    case "7" :
        year = date.substring( date.length-2 );
        if ( year.charAt(0) == "0" )
            year = year.charAt( 1 );
        year = parseInt( year );
        if ( year < 50 )
            year = 2000 + year;
        else
            year = 1900 + year;
        month = date.substring( date.indexOf(".")+1, date.lastIndexOf(".") )-1;
        day = date.substring( 0, date.indexOf(".") );
        break;
    case "8" :
        year = date.substring( date.length-4 );
        day = date.substring( date.indexOf(" ")+1, date.indexOf(",") );
        wordMonth = date.substring( 0, date.indexOf(" ") );
        wordMonth = wordMonth.toLowerCase();
        for ( var i = 0; i < 12; i++ )
        {
            if ( lowMonthNames[i] == wordMonth )
            {
                month = i;
                i = 12;
            }
        }
    break;
        case "9" :
        year = date.substring( date.length-4 );
        day = date.substring( date.indexOf(" ")+1, date.indexOf(",") );
        wordMonth = date.substring( 0, date.indexOf(" ") );
        for ( var i = 0; i < 12; i++ )
        {
            if ( lowMonthNames[i] == wordMonth )
            {
                month = i;
                i = 12;
            }
        }
        break;
    default :
        year = 1980;
        month = 1;
        day = 1;
    }
    var oDate = new Date( year, month, day );
    return oDate;
}

function countDays( date )
{
    // given a date object, return number of days in that month
    var monthcount = new Array ( 31, 28, 31, 30,
                                 31, 30, 31, 31,
                                 30, 31, 30, 31 );
    var year = date.getFullYear();
    if ( year % 4 == 0 )
    {
        if ( year % 100 == 0 )
        {
            if ( year % 400 == 0 )
                monthcount[1]++;
        }
        else
            monthcount[1]++;
    }
    return monthcount[date.getMonth()];
}

function drawCalendar()
{
    // only executes in a DOM compliant browser
    if ( document.getElementById )
    {
        if ( document.createElement )
        {
            // IE is just barely compliant, if you use a deprecated style of attribute setting
            var isIE;
            navigator.appVersion.indexOf("MSIE") > 0 ? isIE = true : isIE = false;

            // Create DOM objects
            var calStart = firstPostDate;
            calStart.setDate( 1 );
            var dayCount = countDays( calStart );
            var leadblanks = calStart.getDay();
            var oTable = document.createElement( "table" );
            var oTHead = document.createElement( "thead" );
            var oTBody = document.createElement( "tbody" );
            var oRow = document.createElement( "tr" );
            var oCell = document.createElement( "th" );
            var oDateLabel = document.createTextNode(
                displayMonthNames[calStart.getMonth()]
                + " " + calStart.getFullYear());

            // Create month
            oCell.colSpan = "7";
            oCell.className = "month";
            oCell.appendChild( oDateLabel );
            oRow.appendChild( oCell );
            oTHead.appendChild( oRow );

            // Create weekdays
            oRow = document.createElement( "tr" );
            for ( var i = 0; i < 7 ;i++ )
            {
                oCell = document.createElement( "th" );
                oCell.className = "weekday"; 
                var oDay = document.createTextNode( displayWeekdayNames[i] );
                oCell.appendChild( oDay );
                oRow.appendChild( oCell );
            }
            oTHead.appendChild(oRow);

            // Create days
            var dayindex = 1;
            while ( dayindex <= dayCount )
            {
                oRow = document.createElement( "tr" );
                
                // Create empty days
                for ( var i = 0; i < leadblanks; i++ )
                {
                    oCell = document.createElement( "td" );
                    var oSpace = document.createTextNode(" ");
                    oCell.appendChild( oSpace );
                    oRow.appendChild( oCell );
                }

                // Create days
                for ( var b = leadblanks; b < 7; b++ )
                {
                    oCell = document.createElement( "td" );
                    
                    // Create active days
                    if ( links[dayindex] != null )
                    {
                        // Hyperlink
                        var oLink = document.createElement( "a" );
                        if ( isIE )
                            oLink.href = links[dayindex];
                        else
                            oLink.setAttribute( "href", links[dayindex] );
                        var oLinkText = document.createTextNode( dayindex );

                        // Cell link (not available to IE yet)
                        oCell.setAttribute( "onclick", "javascript: document.location.href = '"
                            + document.location.protocol + "//" + document.location.hostname
                            + document.location.pathname + links[dayindex] + "';" );

                        // Done
                        oLink.appendChild( oLinkText );
                        oCell.appendChild( oLink );
                        oCell.className = "activeday";
                    }
                    // Create inactive days
                    else if ( dayindex <= dayCount )
                    {
                        var oDayText = document.createTextNode( dayindex );
                        oCell.appendChild( oDayText );
                        oCell.className = "day";
                    }
                    // Create empty days
                    else
                    {
                        var oSpace = document.createTextNode( " " );
                        oCell.appendChild( oSpace );
                    }
                    oRow.appendChild( oCell );
                    oTBody.appendChild( oRow );
                    dayindex++;
                }
                leadblanks = 0;
            }
            oTable.appendChild( oTHead );
            oTable.appendChild( oTBody );

            // Add the bloglendar to its holder finally
            document.getElementById( bloglendarId ).appendChild( oTable );
        }
    }
}