function checkOrientation()
{
    document.body.className = window.orientation && window.orientation % 180 ? "landscape" : "";
}
onorientationchange = checkOrientation;
function rephraseElement(el)
{
    var childNodes = el.childNodes;
    for(var i = 0; i != childNodes.length; ++i)
    {
        var child = childNodes[i];
        if(child.nodeType == 3)
        {
            var txt = child.nodeValue;
            if(/fi|fl/.test(txt))
                txt = child.nodeValue = txt.replace(/fl/g, "ﬂ").replace(/fi/g, "ﬁ");
        }
        else if(child.localName != null && child.localName.toLowerCase() != "h2")
            rephraseElement(child);
    }
}
function rephrase(el)
{
    if(!self.opera && document.location.pathname.indexOf("/admin/") == -1)
        rephraseElement(el);
}
var doneSetUp = false;
function setUp()
{
    checkOrientation();
    if(doneSetUp)
        return;
    doneSetUp = true;
    var arcDoc;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        arcDoc = new XMLHttpRequest();
        if (arcDoc.overrideMimeType){
            arcDoc.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) { // IE
        try {
            arcDoc = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                arcDoc = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {return;}
        }
    }
    arcDoc.onreadystatechange = function ()
        {
            try
            {
                if (arcDoc && arcDoc.readyState == 4 && arcDoc.status == 200)
                {
                    var el = document.getElementById('archiveList');
                    el.innerHTML = '<h3><a href="/archives">Archives</h3></a>' + arcDoc.responseText;
                    rephrase(el);
                    arcDoc = null;
                }
            }
            catch(e){}
        }
    var uri = "http://smorgasblog.ie/archiveListRoot/";
    var herePath = self.location.pathname;
    var ymMatch = herePath.match(/^\/(\d{4}\/\d{2}\/)/);
    if(ymMatch)
        uri += ymMatch[1];
    else
    {
        var yMatch = herePath.match(/^\/(\d{4}\/)/);
        if(yMatch)
            uri += yMatch[1];
    }
    arcDoc.open('GET', uri, true);
    arcDoc.send(null);
    var twiDoc;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        twiDoc = new XMLHttpRequest();
        if (twiDoc.overrideMimeType){
            twiDoc.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) { // IE
        try {
            twiDoc = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                twiDoc = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {return;}
        }
    }
    twiDoc.onreadystatechange = function ()
        {
            try
            {
                if (twiDoc && twiDoc.readyState == 4)
                {
                    var el = document.getElementById('twitter');
                    switch(Math.floor(twiDoc.status / 100))
                    {
                        case 2:
                            el.innerHTML = twiDoc.responseText + '<a href="http://twitter.com/smorgasblog">Follow Smörgåsblog on Twitter</a>';
                            break;
                        case 4: case 5:
                            el.innerHTML = '<a href="http://twitter.com/smorgasblog">Follow Smörgåsblog on Twitter</a>';
                            rephrase(el);
                    }
                    twiDoc = null;
                }
            }
            catch(e){}
        }
    twiDoc.open('GET', "http://smorgasblog.ie/twitter", true);
    twiDoc.send(null);
    rephrase(document.documentElement)
}
function toggleArchive(item)
{
    item.className = item.className == "open" ? "" : "open";
    return false;
}
function getArchive(item)
{
    item.className = "open";
    var anchor = item.getElementsByTagName("a")[0];
    var href = anchor.href;
    var pathPart = href.substring(href.indexOf('smorgasblog.ie/') + 14);
    var arDoc;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        arDoc = new XMLHttpRequest();
        if (arDoc.overrideMimeType){
            arDoc.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) { // IE
        try {
            arDoc = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                arDoc = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {return;}
        }
    }
    arDoc.onreadystatechange = function ()
        {
            try
            {
                if (arDoc && arDoc.readyState == 4 && arDoc.status == 200)
                {
                    item.innerHTML = '<a href="' + href + '" onclick="return toggleArchive(this.parentNode);">' + anchor.innerHTML + '</a>' + arDoc.responseText;
                    rephrase(item)
                    item.getElementsByTagName("a")[0].focus();
                }
            }
            catch(e){}
        }
    arDoc.open('GET', "http://smorgasblog.ie/archiveList" + pathPart, true);
    arDoc.send(null);
    return false;
}
if(window.addEventListener)
    window.addEventListener("DOMContentLoaded", function(){setUp()}, true);
onload = function()
{
    checkOrientation();
    setUp();
}
function setDateOpts(en)
{
    var frm = document.forms[0];
    frm["publishOnYear"].disabled =
        frm["publishOnMonth"].disabled =
        frm["publishOnDay"].disabled =
        frm["publishOnHour"].disabled =
        frm["publishOnMinute"].disabled = 
        frm["publishOnSecond"].disabled = !en;
}
var monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
function isLeap(year)
{
    return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
}
function dateCheckYear(inp, monthName, dayName)
{
    var year = parseInt(inp.value);
    if(!isLeap(year))
    {
        var frm = inp.form;
        var mnth = frm[monthName];
        if(mnth.selectedIndex == 1)
        {
            var day = frm[dayName];
            if(day.selectedIndex == 28)
            {
                mnth.selectedIndex = 2;
                day.selectedIndex = 0;
            }
        }
    }
}
function dateCheckMonth(inp, yearName, dayName)
{
    var month = inp.selectedIndex;
    var md = monthDays[month];
    if(md != 31)
    {
        var frm = inp.form;
        var dayInp = frm[dayName];
        var day = dayInp.selectedIndex + 1;
        if(month == 1 && isLeap(parseInt(frm[yearName].value)))
            md = 29;
        if(day > md)
            dayInp.selectedIndex = md - 1;
    }
}
function dateCheckDay(inp, yearName, monthName)
{
    var day = inp.selectedIndex + 1;
    if(day > 28)
    {
        var frm = inp.form;
        var mnInp = frm[monthName];
        var month = mnInp.selectedIndex;
        if(month == 1 && day == 29)
        {
            if(!isLeap(parseInt(frm[yearName].value)))
                mnInp.selectedIndex = 2;
        }
        else
        {
            while(monthDays[month] < day)
                ++month;
            mnInp.selectedIndex = month;
        }
    }
}
function togglePostImages(anchor)
{
    var el = document.getElementById("singlePost");
    if(el.className == "noimages")
    {
        el.className = "";
        anchor.innerHTML = "Hide Images";
    }
    else
    {
        el.className = "noimages";
        anchor.innerHTML = "Show Images";
    }
    return false;
}
function togglePostComments(anchor)
{
    var el = document.getElementById("comments");
    if(el.className == "noneYet")
    {
        el.className = "";
        anchor.innerHTML = "Hide Comments";
    }
    else
    {
        el.className = "noneYet";
        anchor.innerHTML = "Show Comments";
    }
    return false;
}
function showFull(anchor, width, height)
{
    var el = document.createElement("div");
    el.className="imgView";
    el.innerHTML = '<p><a href="#" onclick="hideFull();return false">Close (or press Escape)</a></p><div><a href="' + anchor.href + '"><img src="' + anchor.href + '" alt="" /></a></div>';
    var elIn = el.getElementsByTagName("div")[0];
    var y = (typeof(innerHeight) == "number" ? innerHeight : document.body.clientHeight) / 2 -45 - height/2;
    if(y < 0)
        y = 0;
    el.style.top = (typeof(pageYOffset)=='number' ? pageYOffset : document.documentElement.scrollTop) + y + "px"
    el.style.left = (document.body.clientWidth / 2 -25 - width/2) + "px";
    elIn.style.width = width + "px";
    elIn.style.height = height + "px";
    while(document.body.getElementsByTagName("div")[0].id != "main")
        hideFull();
    document.body.insertBefore(el, document.getElementById("main"));
    document.onkeydown = function(e)
        {
            if((e ? e : window.event).keyCode == 27)
                hideFull();
        };
    return false;
}
function hideFull()
{
    var div = document.body.getElementsByTagName("div")[0];
    if(div.id != "main")
        document.body.removeChild(div);
    document.onkeydown = null;
}
function getImageHints(item, id)
{
    item.className = "open";
    var anchor = item.getElementsByTagName("a")[0];
    var imDoc;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        imDoc = new XMLHttpRequest();
        if (imDoc.overrideMimeType){
            imDoc.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) { // IE
        try {
            imDoc = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                imDoc = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {return;}
        }
    }
    imDoc.onreadystatechange = function ()
        {
            try
            {
                if (imDoc && imDoc.readyState == 4 && imDoc.status == 200)
                {
                    item.innerHTML = '<a href="#" onclick="return toggleImageHint(this.parentNode);">' + anchor.innerHTML + '</a>' + imDoc.responseText;
                }
            }
            catch(e){}
        }
    imDoc.open('GET', "http://smorgasblog.ie/imgHints?id=" + id, true);
    imDoc.send(null);
    return false;
}
function toggleImageHint(item)
{
    item.className = item.className == "open" ? "closed" : "open";
    return false;
}

