/*          *****  calendar.js  *****

    Scripts for the RosemaryRideoutPhotography.com daily photo calendar page

    Copyright (c) 2007 - Bruce G. Rideout
*/


StartDate = new Date();
CalDate = new Date(StartDate.getTime());


//  Initialize calendars
function InitCalendars(inidate)
{

    StartDate = inidate;
    CalDate = inidate;
    c1 = document.getElementById("calendar");
    LoadCalendar(c1, CalDate, StartDate, false);
}


//  Load the calendar for the indicated month and selected day
//      tbl  = <table> element
//      cdat = Month for display of calendar
//      dat  = Selected date
//      scrl = Scroll to bottom of calendar if files exist
function LoadCalendar(tbl, cdat, dat, scrl)
{

    //  Load the month into the title
    ths = tbl.getElementsByTagName("th");
    mo = cdat.getMonth();
    ths[1].lastChild.nodeValue = (mo + 1) + " / " + cdat.getFullYear();

    //  Load the day numbers
    tds = tbl.getElementsByTagName("td");
    d1 = new Date(cdat.valueOf());
    d1.setDate(1);
    wd = d1.getDay();

    dd = 0;
    while (dd < wd)
        {
        tds[dd].setAttribute("day", "-1");
        tds[dd].setAttribute("title", "");
        tds[dd].innerHTML = "";
        tds[dd].style.borderWidth = "0";
        tds[dd].style.height = "";
        tds[dd++].style.backgroundColor = "";
        }

    while (d1.getMonth() == mo)
        {
        tds[dd].style.borderWidth = "";
        tds[dd].style.height = "";
        if (d1.getFullYear() == dat.getFullYear()  &&  d1.getMonth() == dat.getMonth()  &&  d1.getDate() == dat.getDate())
            {
            tds[dd].style.backgroundColor = "#ffeada";
            }
        else
            {
            tds[dd].style.backgroundColor = "";
            }
        tds[dd].setAttribute("day", d1.getDate());
        tds[dd].setAttribute("title", d1.getDate());
        tds[dd++].innerHTML = "&nbsp;";
        d1.setDate(d1.getDate() + 1);
        }

    while (dd < 42)
        {
        tds[dd].setAttribute("day", "-1");
        tds[dd].setAttribute("title", "");
        tds[dd].innerHTML = "";
        tds[dd].style.borderWidth = "0";
        tds[dd].style.height = "1px";
        tds[dd++].style.backgroundColor = "";
        }

    spage = "get_thumbnail.php?yr=" + cdat.getFullYear() + "&mo=" + (cdat.getMonth() + 1);
    getThumbnails(wd, spage, scrl);
}


//  Change calendar month
function ChangeMonth(cid, cdat, amt, dat)
{

    //  Find the calendar
    cal = document.getElementById(cid);

    //  Adjust the date
    cdat.setMonth(cdat.getMonth() + amt);

    //  Display the calendar
    LoadCalendar(cal, cdat, dat);
}


//  Select Date
function SelectDate(ele, cdat, dat, scrl)
{

    //  Set the selected day
    day = Number(ele.getAttribute("day"));
    if (day > 0)
    {
        cdat.setDate(day);
        dat.setTime(cdat.getTime());

        //  Display the calendar
        cal = ele.parentNode;
        while (cal.nodeName.toLowerCase() != "table")
            {
            cal = cal.parentNode;
            }
        tds = cal.getElementsByTagName("td");
        for (ii = 0; ii < 42; ii++)
        {
            if (ele == tds[ii])
            {
                tds[ii].style.backgroundColor = "#ffeada";
            }
            else
            {
                tds[ii].style.backgroundColor = "";
            }
        }

        //  Show the images
        var wsz = new WindowSize();
        serverPage = "get_images.php?date=" + DateDirectory(dat) + "&width=" + wsz.winWidth;
        getImages(serverPage, scrl);
    }
}


//  Get the image directory relative path for the selected date
function DateDirectory(dat)
{
    dstr = dat.getFullYear().toString() + "/";
    if (dat.getMonth() < 9)
    {
        dstr += "0";
    }
    dstr += Number(dat.getMonth() + 1).toString() + "/";
    if (dat.getDate() < 10)
    {
        dstr += "0";
    }
    dstr += Number(dat.getDate()).toString();
    return dstr;
}


//  Get the XMLHttpRequest object
function getXMLHttp()
{
    var xmlhttp = false;

    //  Check for IE versions
    try
    {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e1)
    {
        try
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e2)
        {
            xmlhttp = false;
        }
    }

    if (!xmlhttp  &&  typeof XMLHttpRequest != 'undefined')
    {
        xmlhttp = new XMLHttpRequest();
    }

    return xmlhttp;
}


//  Get the thumbnail images for the calendar month
function getThumbnails(idx, serverPage, scrl)
{

    xmlhttp = getXMLHttp();
    xmlhttp.open("GET", serverPage);
    xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4  &&  xmlhttp.status == 200)
            {
                var xml = xmlhttp.responseXML;
                n1 = xml.documentElement;
                n2 = n1.firstChild;
                while (n2)
                {
                    if (n2.nodeType == 1  &&  n2.tagName == "thumb")
                    {
                        day = Number(n2.getAttribute("day"));
                        ix = day + idx - 1;
                        tds[ix].innerHTML = "<img src=\"" + n2.getAttribute("src") +
                                            "\" alt=\"" + day + ": Click for images\"" +
                                            " title=\"" + day + ": Click for images\" />";
                    }
                    n2 = n2.nextSibling;
                }

                //  Load the images for the selected date
                ix = CalDate.getDate() + idx - 1;
                SelectDate(tds[ix], CalDate, StartDate, scrl);
            }
        }
    xmlhttp.send(null);
}

 //  Get the images for the selected day
function getImages(serverPage, scrl)
{

    xmlhttp = getXMLHttp();
    xmlhttp.open("GET", serverPage);
    xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4  &&  xmlhttp.status == 200)
            {
                dpy = document.getElementById("display");
                dpy.innerHTML = xmlhttp.responseText;

                //  Scroll to bottom of calendar
                if (scrl)
                {
                    d1 = new Date(CalDate.valueOf());
                    d1.setDate(1);
                    idx = d1.getDay();
                    d1.setMonth(d1.getMonth() + 1, 1);
                    d1.setDate(0);
                    idx += d1.getDate() -1;
                    itm = tds[idx];
                    yy = itm.offsetTop;
                    while (itm = itm.offsetParent)
                    {
                        yy += itm.offsetTop;
                    }
                    if (yy > 100)
                    {
                        yy -= 100;
                    }
                    scrollTo(0, yy);
                }
            }
        }
    xmlhttp.send(null);
}


//  Request the journal page for the selected date
function journal()
{

    d1 = new Date(CalDate.valueOf());
    d1.setDate(1);
    idx = d1.getDay() + CalDate.getDate() - 1;
    if (tds[idx].innerHTML != "" &&  tds[idx].innerHTML != "&nbsp;")
    {
        window.location = "http://rosemaryr.livejournal.com/" + DateDirectory(CalDate);
    }
    else
    {
        window.location = "http://rosemaryr.livejournal.com/";
    }
}


//  View the large version of the clicked image
function viewLarge(img)
{

    window.location = "view_image.php?image=" + img.src;
}


