var months = new Array(13);
months["january"] = 1;
months["february"] = 2;
months["march"] = 3;
months["april"] = 4;
months["may"] = 5;
months["june"] = 6;
months["july"] = 7;
months["august"] = 8;
months["september"] = 9;
months["october"] = 10;
months["november"] = 11;
months["december"] = 12;
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";

function pad(val, ch, len)
{
    val = "" + val; // coerce to string
    while ( val.length < len )
        val = "" + ch + val; // coerce to string
    return val;
}

function prev_month(year, month)
{
    month--;
    if ( month == 0 ) {
        year--;
        month = 12;
    }
    return year+"_"+pad(month, "0", 2);
}

function next_month(year, month)
{
    month++;
    if ( month == 13 ) {
        year++;
        month = 1;
    }
    return year+"_"+pad(month, "0", 2);
}

function has_prev(year, month)
{
    month--;
    if ( month == 0 ) {
        year--;
        month = 12;
    }
    var string = months[month]+" "+year;
    for (var i=0; i < archive_menu.length;i++) {
        if ( string == archive_menu[i][1] )
            return true;
    }
    return false;
}

function has_next(year, month)
{
    month++;
    if ( month == 13 ) {
        year++;
        month = 1;
    }
    var string = months[month]+" "+year;
    for (var i=0; i < archive_menu.length;i++) {
        if ( string == archive_menu[i][1] )
            return true;
    }
    return false;
}

var search_terms;

function archive_title(month, title)
{
    month = month.substring(title.length + 2);
    var m = month.split(" ");
    var n = months[m[0].toLowerCase()];
    document.write("<h2 class=\"post-title\">");
    var newest = "Newest";
    if ( n >= 1 && n <= 12 ) {
        newest = "Oldest";
        document.write("Archive for " + month);
    } else if ( m[0] == "Search" && m[1] == "results" && m[2] == "for" ) {
        m.shift();
        m.shift();
        m.shift();
        search_terms = m;
        document.write("Search Results");
    } else {
        document.write("Posts with label " + month);
    }
    document.write("</h2><p id=\"small\">("+newest+" First)</p>");
}

function next_prev_months(month, title)
{
    month = month.substring(title.length + 2);
    var m = month.split(" ");
    m[0] = months[m[0].toLowerCase()];
    document.write("<p>");
    if ( has_prev(m[1], m[0]) )
        document.write("<a href=\""+prev_month(m[1], m[0])+"_01_archive.html\">&lt;:: "+months[((m[0]+10)%12)+1]+"</a> ");
    document.write(":: "+month.split(" ")[0]+" ::");
    if ( has_next(m[1], m[0]) )
        document.write(" <a href=\""+next_month(m[1], m[0])+"_01_archive.html\">"+months[(m[0]%12)+1]+" ::&gt;</a>");
    document.write("</p>");
}

function create_archive_link(date)
{
    var bits = date.split(", ");
    var month = bits[1].split(" ")[0];
    var year = bits[2];
    month = months[month.toLowerCase()];
    document.write("<a href=\""+year+"_"+pad(month, "0", 2)+"_01_archive.html\">"+date+"</a>");
}

var labels_menu = new Array();

function labels_menu_add(url, name, count)
{
    labels_menu[labels_menu.length] = new Array(url, name, count);
}

function labels_menu_print()
{
    document.write("<p><select name=\"labelsmenu\" onchange=\"document.location.href=this.options[this.selectedIndex].value;\">\n");
    document.write("<option selected>- Select a label -</option>\n");
    for (var i=0;i<labels_menu.length;i++) {
        document.write("<option value=\"" + labels_menu[i][0] + "\">" + labels_menu[i][1] + " (" + labels_menu[i][2] + ")</option>\n");
    }
    document.write("</select></p><hr id=\"fancy\">\n");
}

var archive_menu = new Array();

function archive_menu_add(url, name, count)
{
    archive_menu[archive_menu.length] = new Array(url, name, count);
}

function archive_menu_print()
{
    document.write("<p><select name=\"archivemenu\" onchange=\"document.location.href=this.options[this.selectedIndex].value;\">\n");
    document.write("<option selected>- Select a month -</option>\n");
    for (var i=archive_menu.length-1;i>=0;i--) {
        document.write("<option value=\"" + archive_menu[i][0] + "\">" + archive_menu[i][1] + " (" + archive_menu[i][2] + ")</option>\n");
    }
    document.write("</select></p><hr id=\"fancy\">\n");
}

var archive_page = new Array();

function archive_append_date(date)
{
    var o = new Object;
    o.type="date";
    o.date=date;
    archive_page[archive_page.length] = o;
}

function archive_append_post(postid, url, title, comments)
{
    var o = new Object;
    o.type="post";
    o.postid=postid;
    o.url=url;
    o.title=title;
    o.comments=comments;
    archive_page[archive_page.length] = o;
}

function archive_print_reversed()
{
    var posts = "";
    
    for (var i=archive_page.length-1;i>=0;i--) {
        var o = archive_page[i];
        if ( o.type == "date" ) {
            document.write("<dt>"+o.date+"</dt>");
            document.write(posts);
            posts = "";
        } else if ( o.type == "post" ) {
            posts += "<dd><a href=\""+o.url+"\">"+o.title+"</a>";
            if ( o.comments != "" && o.comments != "0" ) {
                posts += " [<a href=\""+o.url+"#comments\">"+o.comments+" "+(o.comments==1?"comment":"comments")+"</a>]";
                posts += " <a href=\"http://www.blogger.com/feeds/17292364/"+o.postid+"/comments/default\"><img id=\"rss\" src=\"http://yasmar.net/blogger/rss.png\"></a>";
            }
            posts += "</dd>";
        }
    }
}

function show_edit_controls()
{
    var spans = document.getElementsByTagName("span"); 
    for (var i = 0; i < spans.length; i++) { 
        var theclass = spans[i].getAttribute("class"); 
        if ( theclass.match(RegExp(/item-control/)) ) {
            spans[i].setAttribute("style", "display:inline!important");
        }
    }
}

var search_post_ids = new Array();

function search_posts_add(id)
{
    search_post_ids[search_post_ids.length] = id;
}

function search_toggle(id)
{
    var block = document.getElementById("t_"+id);
    showhide = document.getElementById(id);
    if ( block.getAttribute("style") == "display:block!important" ) {
        block.setAttribute("style", "display:none!important");
        showhide.innerHTML = "show";
    } else {
        block.setAttribute("style", "display:block!important");
        showhide.innerHTML = "hide";
    }
}

function search_highlight()
{
    // This also gets called for label pages... jump out if there's no search terms!
    if ( !search_terms ) return;
    // Make the post titles toggle the text on and off
    for (var i = 0; i < search_post_ids.length; i++) { 
        var link = document.getElementById(search_post_ids[i]);
        link.href = "javascript:search_toggle('"+search_post_ids[i]+"');";
    }
    // Hide some bits search terms
    var spans = document.getElementsByTagName("span"); 
    for (var i = 0; i < spans.length; i++) { 
        var theclass = spans[i].getAttribute("class"); 
        if ( theclass == "searchhide" ) {
            spans[i].setAttribute("style", "display:none!important");
        } else if ( theclass == "searchshow" ) {
            spans[i].setAttribute("style", "display:inline!important");
        }
    }
    // Highlight the search terms
    var spans = document.getElementsByTagName("span"); 
    for (var i = 0; i < spans.length; i++) { 
        var theclass = spans[i].getAttribute("class"); 
        if ( theclass == "searchonly" ) {
            spans[i].setAttribute("class", "searchonly dark");
            //spans[i].setAttribute("style", "display:block!important");
            var theHtml = spans[i].innerHTML;
            spans[i].innerHTML = "";
            var tokens = theHtml.split(" "); // FIXME split this up better
            for (var j = 0; j < tokens.length; j++) {
                for ( var s = 0; s < search_terms.length; s++ ) {
                    if ( tokens[j].match(search_terms[s]) ) {
                        tokens[j] = "<span class=\"highlight\">"+tokens[j]+"</span>";
                    }
                }
            }
            theHtml = tokens.join(" ");
            spans[i].innerHTML = theHtml;
        }
    }
}

