var g_bCatToggle = true;
var g_bCanHideCats = false;
var g_bKillGetThumbs = false; // Had to add this so we stop getting thumbs if the set changes.
var g_sApiKey = "54b174653cc1c836fe32010f63fdbe8c";
var g_sUserAccToUse = "kycphotos";
var g_sNsid = null;
var g_xmlHttp = null;
var g_xmlHttpThumb = null;
var g_xmlHttpPhoto = null;
var g_xmlHttpPhotoNav = null;
var g_ThumbSet = null;
var g_sCurPhotoSet = null;
var g_iPhotoTimer = 0;

function ThumbSet(element, i)
{
    this.m_PhotoSetElements = element;
    this.m_iIndex = i;
}

function XmlHttpObjectMgr(obj)
{
    this.m_obj = obj;
    this.bInUse = false;
}

function getXmlHttpObject()
{
    var xmlHttp = null;
    
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    
    return xmlHttp;
}

function showCats()
{
    if(g_bCanHideCats)
    {
        if(g_bCatToggle)
        {
            document.getElementById("expand_img").setAttribute("src", "images/retract.GIF");
            document.getElementById("expand_img").setAttribute("alt", "Retract");
            heightFolderExpand();
            g_bCatToggle = false;
        }
        else
        {
            document.getElementById("expand_img").setAttribute("src", "images/expand.GIF");
            document.getElementById("expand_img").setAttribute("alt", "Expand");
            heightFolderRetract();
            g_bCatToggle = true;
        }
    }
}

function photoTimeout()
{
    if(document.getElementById("photo_img").offsetHeight > 40)
    {
        heightPhotoChange();
    }
    else
    {
        g_iPhotoTimer = setTimeout(photoTimeout, 500);
    }
}

function receivePhotoSets()
{
    try
    {
        if(g_xmlHttp.m_obj.readyState == 1)
        {
            document.getElementById("expand_img").setAttribute("src", "images/loader.GIF");
            document.getElementById("expand_img").setAttribute("alt", "Loading...");
        }
        else if(g_xmlHttp.m_obj.readyState == 4)
        {
            if(g_xmlHttp.m_obj.status == 200)
            {
                element = g_xmlHttp.m_obj.responseXML.documentElement.getElementsByTagName("photoset");
                innerHtml = "<table cellspacing=\"25\" cellpadding=\"0\">";
                CONST_TBLWDTH = 4;
                rowIsOpen = false;
                
                for(i = 0; i < element.length; i++)
                {
                    if(i % CONST_TBLWDTH == 0)
                    {
                        innerHtml += "<tr>\n";
                        rowIsOpen = true;
                    }
                    
                    innerHtml += "<td valign=\"top\" align=\"center\" width=\"200px\">";
                    try
                    {
                        subHtml = "<a href=\"javascript:getSet('" + element[i].getAttribute("id") + "')\">";
                        subHtml += "<img src=\"images/folder.PNG\" border=\"0\" alt=\"\" /><br />";
                        subHtml += element[i].getElementsByTagName("title")[0].firstChild.nodeValue;
                        subHtml += "</a>";
                        innerHtml += subHtml;
                    }
                    catch(e_sub)
                    {
                    }
                    innerHtml += "</td>";
                    
                    if((i + 1) % CONST_TBLWDTH == 0)
                    {
                        innerHtml += "\n</tr>\n";
                        rowIsOpen = false;
                    }
                }
                
                if(rowIsOpen)
                {
                    innerHtml += "\n</tr>\n";
                }
                innerHtml += "</table>"
                document.getElementById("photosets").innerHTML = innerHtml;
                
                g_bCanHideCats = true;
                setTimeout(
                    function()
                    {
                        showCats();
                    },
                    100);
            }
            else
            {
                document.getElementById("thumbs").innerHTML =
                    "<h2>Sorry, I wasn't able to get the Photo Sets.</h2>";
            }
        }
    }
    catch(e)
    {
        document.getElementById("thumbs").innerHTML =
            "<h2>Sorry, something went wrong while I was trying to find Photo Sets: " + e + "</h2>";
    }
    finally
    {
        if(g_xmlHttp.m_obj.readyState == 4)
        {
            g_xmlHttp.bInUse = false;
        }
    }
}

function receiveSet()
{
    try
    {
        // Allow thumbs to download.
        g_bKillGetThumbs = false;
    
        if(g_xmlHttp.m_obj.readyState == 1)
        {
            document.getElementById("thumbs").innerHTML = 
                "<img src=\"images/loader.GIF\" border=\"0\" alt=\"Loading...\" />";
        }
        else if(g_xmlHttp.m_obj.readyState == 4)
        {
            if(g_xmlHttp.m_obj.status == 200)
            {
                g_ThumbSet = new ThumbSet(g_xmlHttp.m_obj.responseXML.documentElement.getElementsByTagName("photo"), 0);
                innerHtml = "<table cellspacing=\"1\" cellpadding=\"0\">";
                CONST_TBLWDTH = 7;
                rowIsOpen = false;
                
                for(i = 0; i < g_ThumbSet.m_PhotoSetElements.length; i++)
                {
                    if(i % CONST_TBLWDTH == 0)
                    {
                        innerHtml += "<tr>\n";
                        rowIsOpen = true;
                    }
                    
                    innerHtml += "<td id=\"" + g_ThumbSet.m_PhotoSetElements[i].getAttribute("id") + 
                        "\" title=\"" + g_ThumbSet.m_PhotoSetElements[i].getAttribute("title") + "\">";
                    innerHtml += "</td>";
                    
                    if((i + 1) % CONST_TBLWDTH == 0)
                    {
                        innerHtml += "\n</tr>\n";
                        rowIsOpen = false;
                    }
                }
                
                if(rowIsOpen)
                {
                    innerHtml += "\n</tr>\n";
                }
                innerHtml += "</table>";
                document.getElementById("thumbs").innerHTML = innerHtml;
                
                document.getElementById("slideshow").innerHTML = 
                    "<a href=\"http://www.flickr.com/photos/" + g_sUserAccToUse + "/sets/" + g_sCurPhotoSet + 
                    "/show/\" target=\"_blank\"><img src=\"images/slideshow.gif\" border=\"0\" alt=\"Slideshow\" /></a>";
                
                setTimeout(
                    function()
                    {
                        getThumb(g_ThumbSet);
                    },
                    100);
            }
            else
            {
                document.getElementById("thumbs").innerHTML =
                    "<h2>Sorry. I couldn't find the set you requested.</h2>";
            }
        }
    }
    catch(e)
    {
        document.getElementById("thumbs").innerHTML =
            "<h2>Sorry, something went wrong while I was finding the set you requested: " + e + "</h2>";
    }
    finally
    {
        if(g_xmlHttp.m_obj.readyState == 4)
        {
            g_xmlHttp.bInUse = false;
        }
    }
}

function receiveThumb()
{
    try
    {
        // Stop populating thumbs. A new set is being loaded.
        if(g_bKillGetThumbs)
        {
            g_xmlHttpThumb.m_obj.abort();
            g_xmlHttpThumb.bInUse = false;
            return;
        }
    
        innerHtml = "";
        
        if(g_xmlHttpThumb.m_obj.readyState == 1)
        {
            document.getElementById(g_ThumbSet.m_PhotoSetElements[g_ThumbSet.m_iIndex].getAttribute("id")).innerHTML = 
                "<img src=\"images/loader.GIF\" alt=\"Loading...\" />";
        }
        else if(g_xmlHttpThumb.m_obj.readyState == 4)
        {
            sSource = getThumbSource(g_xmlHttpThumb.m_obj.responseXML.documentElement.getElementsByTagName("size"), "Square");            
            sId = g_ThumbSet.m_PhotoSetElements[g_ThumbSet.m_iIndex].getAttribute("id");
            
            innerHtml += "<a href=\"javascript:showPhoto('" + sId + "')\">";
            innerHtml += "<img src=\"" + sSource[0] + "\" alt=\"\" style=\"height:" + sSource[1] + "px;" +
                "opacity:0;filter:alpha(opacity=0);\" onload=\"fadeIn(this, 5, 20)\" />";
            innerHtml += "</a>";
                        
            document.getElementById(sId).innerHTML = innerHtml;
            
            if(g_ThumbSet.m_iIndex == 0)
            {
                setTimeout(
                    function()
                    {
                        showPhoto(g_ThumbSet.m_PhotoSetElements[0].getAttribute("id"));
                    },
                    100);
            }
            
            g_ThumbSet.m_iIndex++;
            
            if(g_ThumbSet.m_iIndex < g_ThumbSet.m_PhotoSetElements.length)
            {
                setTimeout(
                    function()
                    {
                        getThumb(g_ThumbSet);
                    },
                    10);
            }
        }
    }
    catch(e)
    {
    }
    finally
    {
        if(g_xmlHttpThumb.m_obj.readyState == 4)
        {
            g_xmlHttpThumb.bInUse = false;
        }
    }
}

function receivePhoto()
{
    try
    {
        innerHtml = "";
        
        if(g_xmlHttpPhoto.m_obj.readyState == 1)
        {
            document.getElementById("photo").innerHTML = 
                "<img id=\"photo_img\" src=\"images/loader.GIF\" alt=\"Loading...\" />";
        }
        else if(g_xmlHttpPhoto.m_obj.readyState == 4)
        {
            sSource = getThumbSource(g_xmlHttpPhoto.m_obj.responseXML.documentElement.getElementsByTagName("size"), "Medium");
            
            innerHtml += "<img id=\"photo_img\" src=\"" + sSource[0] + "\" alt=\"\" style=\"height:" + sSource[1] + "px;" +
                "opacity:0;filter:alpha(opacity=0);\" onload=\"fadeIn(this, 10, 20)\" />";
            document.getElementById("photo").innerHTML = innerHtml;
            
            clearTimeout(g_iPhotoTimer);
            g_iPhotoTimer = setTimeout(photoTimeout, 100);
        }
    }
    catch(e)
    {
        document.getElementById("thumbs").innerHTML =
            "<h2>Sorry. Something bad happened when I tried to display the photo: " + e + "</h2>";
    }
    finally
    {
        if(g_xmlHttpPhoto.m_obj.readyState == 4)
        {
            g_xmlHttpPhoto.bInUse = false;
        }
    }
}

function receivePhotoNav()
{
    try
    {
        if(g_xmlHttpPhotoNav.m_obj.readyState == 4)
        {
            prevElement = "0";
            nextElement = "0";
            
            try
            {
                prevElement = g_xmlHttpPhotoNav.m_obj.responseXML.documentElement.getElementsByTagName("prevphoto")[0].getAttribute("id");
            }
            catch(e_prev)
            {
                prevElement = "0";
            }
            try
            {
                nextElement = g_xmlHttpPhotoNav.m_obj.responseXML.documentElement.getElementsByTagName("nextphoto")[0].getAttribute("id");
            }
            catch(e_next)
            {
                nextElement = "0";
            }
            
            if(prevElement != "0")
            {
                document.getElementById("nav_prev").innerHTML = 
                    "<a href=\"javascript:showPhoto('" + prevElement + "')\"><img src=\"images/prev.gif\" border=\"0\" alt=\"Previous\" /></a>";
            }
            if(nextElement != "0")
            {
                document.getElementById("nav_next").innerHTML = 
                    "<a href=\"javascript:showPhoto('" + nextElement + "')\"><img src=\"images/next.gif\" border=\"0\" alt=\"Next\" /></a>";
            }
        }
    }
    catch(e)
    {
    }
    finally
    {
        if(g_xmlHttpPhotoNav.m_obj.readyState == 4)
        {
            g_xmlHttpPhotoNav.bInUse = false;
        }
    }
}

function getNsid()
{
    var xmlHttp = getXmlHttpObject();
    var sNsid = "NULL";
    
    try
    {
        if(xmlHttp != null) 
        {
            xmlHttp.open("GET", 
                "proxy.aspx?Url=" +
                escape("http://api.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key=" + g_sApiKey + "&username=" + g_sUserAccToUse), 
                false);
            xmlHttp.send(null);
        
            if(xmlHttp.status == 200)
            {
                sNsid = 
                    xmlHttp.responseXML.documentElement.getElementsByTagName("user")[0].getAttribute("nsid");
            }
            else
            {
                document.getElementById("thumbs").innerHTML =
                    "<h2>Oh no! I couldn't find the Photo Account data. I will not be able to show you any photos.</h2>";
            }
        }
        else
        {
            document.getElementById("thumbs").innerHTML =
                "<h2>Sorry. Your browser does not support dynamic scripting. You will not be able to view this page properly.</h2>";
        }
    }
    catch(e)
    {
        document.getElementById("thumbs").innerHTML =
            "<h2>Oh no! I couldn't find the Photo Account data. I will not be able to show you any photos: " + e + "</h2>";
    }
    
    return sNsid;
}

function getPhotoSets()
{
    if(g_xmlHttp != null)
    {
        if(g_xmlHttp.bInUse)
        {
            g_xmlHttp.m_obj.abort();
            setTimeout(getPhotoSets, 500);
            return;
        }
    }

    g_xmlHttp = new XmlHttpObjectMgr(getXmlHttpObject());
    
    try
    {
        if(g_xmlHttp.m_obj != null) 
        {
            if(g_sNsid == null)
            {
                g_sNsid = getNsid();
            }
        
            g_xmlHttp.m_obj.onreadystatechange = receivePhotoSets;
            g_xmlHttp.m_obj.open("GET", 
                "proxy.aspx?Url=" +
                escape("http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=" + g_sApiKey + "&user_id=" + g_sNsid), 
                true);
            g_xmlHttp.bInUse = true;
            g_xmlHttp.m_obj.send(null);
        }
        else
        {
            document.getElementById("thumbs").innerHTML =
                "<h2>Sorry. Your browser does not support dynamic scripting. You will not be able to view this page properly.</h2>";
        }
    }
    catch(e)
    {
        document.getElementById("thumbs").innerHTML =
            "<h2>Oh no! I couldn't request the Photo Set: " + e + "</h2>";
    }
}

function getSet(photoset_id)
{
    // If thumbs are still downloading from the previous set, stop the download.
    g_bKillGetThumbs = true;

    if(g_xmlHttp != null)
    {
        if(g_xmlHttp.bInUse)
        {
            g_xmlHttp.m_obj.abort();
            setTimeout("getSet('" + photoset_id + "')", 500);
            return;
        }
    }

    g_xmlHttp = new XmlHttpObjectMgr(getXmlHttpObject());
    
    try
    {
        if(g_xmlHttp.m_obj != null)
        {
            showCats();
            g_sCurPhotoSet = photoset_id;
                        
            g_xmlHttp.m_obj.onreadystatechange = receiveSet;
            g_xmlHttp.m_obj.open("GET",
                "proxy.aspx?Url=" +
                escape("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=" + g_sApiKey + "&photoset_id=" + photoset_id),
                true);
            g_xmlHttp.bInUse = true;
            g_xmlHttp.m_obj.send(null);
        }
        else
        {
            document.getElementById("thumbs").innerHTML =
                "<h2>Sorry. Your browser does not support dynamic scripting. You will not be able to view this page properly.</h2>";
        }
    }
    catch(e)
    {
        document.getElementById("thumbs").innerHTML =
            "<h2>Oh no! I couldn't request the set: " + e + "</h2>";
    }
}

function getThumb(thumbset)
{
    // Stop downloading thumbs. New set is being loaded.
    if(g_bKillGetThumbs)
    {
        return;
    }
    
    if(g_xmlHttpThumb != null)
    {
        if(g_xmlHttpThumb.bInUse)
        {
            g_xmlHttpThumb.m_obj.abort();
            setTimeout("getThumb('" + thumbset + "')", 500);
            return;
        }
    }

    g_xmlHttpThumb = new XmlHttpObjectMgr(getXmlHttpObject());
    
    try
    {
        if(g_xmlHttpThumb.m_obj != null) 
        {
            g_xmlHttpThumb.m_obj.onreadystatechange = receiveThumb;
            g_xmlHttpThumb.m_obj.open("GET", 
                "proxy.aspx?Url=" +
                escape("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=" + g_sApiKey + "&photo_id=" + thumbset.m_PhotoSetElements[thumbset.m_iIndex].getAttribute("id")), 
                true);
            g_xmlHttpThumb.bInUse = true;
            g_xmlHttpThumb.m_obj.send(null);
        }
        else
        {
            document.getElementById("thumbs").innerHTML =
                "<h2>Sorry. Your browser does not support dynamic scripting. You will not be able to view this page properly.</h2>";
        }
    }
    catch(e)
    {
    }
}

function getThumbSource(element, size)
{
    try
    {
        for(i = 0; i < element.length; i++)
        {
            if(element[i].getAttribute("label") == size)
            {
                return [element[i].getAttribute("source"), element[i].getAttribute("size")];
            }
        }
    }
    catch(e)
    {
    }
    
    // TODO: Maybe include a default thumb here for error case.
    return "NULL";
}

function showPhoto(photo_id)
{
    if(g_xmlHttpPhoto != null)
    {
        if(g_xmlHttpPhoto.bInUse)
        {
            g_xmlHttpPhoto.m_obj.abort();
            setTimeout("showPhoto('" + photo_id + "')", 500);
            return;
        }
    }

    g_xmlHttpPhoto = new XmlHttpObjectMgr(getXmlHttpObject());
    
    try
    {
        if(g_xmlHttpPhoto.m_obj != null) 
        {
            document.getElementById("nav_prev").innerHTML = "";
            document.getElementById("nav_next").innerHTML = "";
        
            g_xmlHttpPhoto.m_obj.onreadystatechange = receivePhoto;
            g_xmlHttpPhoto.m_obj.open("GET", 
                "proxy.aspx?Url=" +
                escape("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=" + g_sApiKey + "&photo_id=" + photo_id), 
                true);
            g_xmlHttpPhoto.bInUse = true;
            g_xmlHttpPhoto.m_obj.send(null);
            
            document.getElementById("headline").innerHTML = 
                "<h2>" + document.getElementById(photo_id).getAttribute("title") + "</h2>";
            
            destYPos = findTopPosition(document.getElementById("photo"));
            if(document.documentElement.scrollTop > destYPos)
            {
                animateScroll(
                    document.documentElement.scrollTop,
                    destYPos,
                    30,
                    10,
                    false);
            }
            
            setTimeout(
                function()
                {
                    getPhotoNav(photo_id, g_sCurPhotoSet);
                },
                100);
        }
        else
        {
            document.getElementById("thumbs").innerHTML =
                "<h2>Sorry. Your browser does not support dynamic scripting. You will not be able to view this page properly.</h2>";
        }
    }
    catch(e)
    {
        document.getElementById("thumbs").innerHTML =
            "<h2>Oh no! I couldn't request the photo: " + e + "</h2>";
    }
}

function getPhotoNav(photo_id, photoset_id)
{
    if(g_xmlHttpPhotoNav != null)
    {
        if(g_xmlHttpPhotoNav.bInUse)
        {
            g_xmlHttpPhotoNav.m_obj.abort();
            setTimeout("getPhotoNav('" + photo_id + "', '" + photoset_id + "')", 500);
            return;
        }
    }

    g_xmlHttpPhotoNav = new XmlHttpObjectMgr(getXmlHttpObject());
    
    try
    {
        if(g_xmlHttpPhotoNav.m_obj != null) 
        {
            g_xmlHttpPhotoNav.m_obj.onreadystatechange = receivePhotoNav;
            g_xmlHttpPhotoNav.m_obj.open("GET", 
                "proxy.aspx?Url=" +
                escape("http://api.flickr.com/services/rest/?method=flickr.photosets.getContext&api_key=" + g_sApiKey + "&photo_id=" + photo_id + "&photoset_id=" + photoset_id), 
                true);
            g_xmlHttpPhotoNav.bInUse = true;
            g_xmlHttpPhotoNav.m_obj.send(null);
        }
        else
        {
            document.getElementById("thumbs").innerHTML =
                "<h2>Sorry. Your browser does not support dynamic scripting. You will not be able to view this page properly.</h2>";
        }
    }
    catch(e)
    {
    }
}