////////////////////////////////////////////////

function CPhotoManager()
{
    this.m_oPageRenderer = null;
    this.m_oAlbumManager = new CAlbumManager();
    this.m_listPhoto = new CList();

    this.m_sRequestID = null;
    this.m_sInitalPhotoID = null;
}

CPhotoManager.ALBUM_REQUEST = 'GetAlbums.xml';

CPhotoManager.prototype.Initialise = function(oPageRenderer)
{
    this.m_oPageRenderer = oPageRenderer;
    
    var sType = this.m_oPageRenderer.GetType();
    if((sType == 'Album') ||
       (sType == 'AlbumLikeTest') ||
       (sType == 'PhotoOfTheDay'))
    {
        this._ReadAddressBar();
    }
    else
    {
        this.m_sRequestID = this.m_oPageRenderer.GetAlbum();
    }

    this._LoadAlbumInformation();
    return SW_OK;
}

CPhotoManager.prototype._LoadAlbumInformation = function()
{
    var pMe = this;
    var sRequest = CPhotoManager.ALBUM_REQUEST + '?ID=' + this.m_sRequestID;

    new Ajax.Request(sRequest,
    {
        method: 'get',
        onSuccess: function(transport)
        {
            pMe._OnSuccess(transport.responseText);
        },
        onFailure: function() { pMe._OnFailure(); }
    });
    return SW_OK;
}

CPhotoManager.prototype._OnSuccess = function(sAlbums)
{
    var nodeAlbums = null;
    try
    {
        nodeAlbums = XML.StringToNode(sAlbums);
        if(nodeAlbums != null)
        {
            this.m_oAlbumManager.Initialise(nodeAlbums);
        }
    }
    catch(e)
    {

    }

    this.m_oPageRenderer.Draw();
    return SW_OK;
}

CPhotoManager.prototype._OnFailure = function()
{
    this.m_oPageRenderer.Draw();
    return SW_OK;
}
     
CPhotoManager.prototype._ReadAddressBar = function()
{
    var sAddress = String(window.location);
    var nIndex = sAddress.indexOf('#ID=');
    if(nIndex != -1)
    {
        this.m_sRequestID = sAddress.substring(nIndex + 4);  // .php#ID=123
    }
    else
    {
        this.m_sRequestID = this.m_oPageRenderer.GetID(); // .php?ID=123
    }
    
    if(this.m_sRequestID != null)    
    {
        if(this.m_sRequestID.length > 4) // Check if Album and Initial Photo
        {
          this.m_sInitalPhotoID = this.m_sRequestID; // Use this as the initial photo
        }
    }
  return SW_OK;
}
    
CPhotoManager.prototype.GetAlbumManager = function()
{
    return this.m_oAlbumManager; 
}

CPhotoManager.prototype.GetInitialPhotoID = function()
{
    return this.m_sInitalPhotoID; 
}

////////////////////////////////////////////////

function CAlbumManager()
{
    this.m_listAlbum = new CList();
}

CAlbumManager.prototype.Initialise = function(nodeAlbums)
{
    var nodeAlbumList = nodeAlbums.childNodes;
    var nCount = nodeAlbumList.length;
    for(var nCursor = 0; nCursor < nCount; nCursor++)
    {
        var nodeAlbum = nodeAlbumList[nCursor];
        if(nodeAlbum.nodeName == 'ALBUM')
        {
            var oAlbum = new CAlbum();
            oAlbum.Initialise(nodeAlbum);

            this.m_listAlbum.Add(oAlbum);
        }
    }
    return SW_OK;
}

CAlbumManager.prototype.GetAlbums = function()
{
    return this.m_listAlbum;
}

CAlbumManager.prototype.GetAllPhotos = function()
{
    var listAllPhotos = new CList();

    var nCount = this.m_listAlbum.GetCount();
    for(var nCursor = 0; nCursor < nCount; nCursor++)
    {
        var oAlbum = this.m_listAlbum.Get(nCursor);
        var nPhotoCount = oAlbum.GetPhotos().GetCount();
        for(var nPhotoCursor = 0; nPhotoCursor < nPhotoCount; nPhotoCursor++)
        {
            listAllPhotos.Add(oAlbum.GetPhotos().Get(nPhotoCursor));
        }
    }
    return listAllPhotos;
}

////////////////////////////////////////////////

function CAlbum()
{
    this.m_listPhoto = new CList();
    this.m_sAlbumName = '';
}

CAlbum.prototype.Initialise = function(nodeAlbum)
{
    this.m_sAlbumName = nodeAlbum.getAttribute('Name');

    var nodePhotoList = nodeAlbum.childNodes;
    var nCount = nodePhotoList.length;
    for(var nCursor = 0; nCursor < nCount; nCursor++)
    {
        var nodePhoto = nodePhotoList[nCursor];
        if(nodePhoto.nodeName == 'PHOTO')
        {
            var oPhoto = new CPhoto();
            oPhoto.Initialise(nodePhoto, this);

            this.m_listPhoto.Add(oPhoto);
        }
    }
    return SW_OK;
}

CAlbum.prototype.GetName = function()
{
    return this.m_sAlbumName;
}

CAlbum.prototype.GetPhotos = function()
{
    return this.m_listPhoto;
}

////////////////////////////////////////////////
