var allThumbsSelected = "";
var thumbSelectedList = new String();
var responseAarfnbrs = "";
var justRotated = false;
var actualImagesLoaded = 0;
var firstLoad = true;
var sortType = 3;
var imageSortType = 0;
var albumsXML;
var popupClass = new String();
var closeGraphic = new String();
var theHtml = new String();
var favoritesSelectedList = new String();
var masterAlbumID = 0;
var imageID = 0;
var encAlbumIDs = "";
var enc_Album = 0;
var advancedImage = 0;
var medSize = 300;
var maxSize = 600;
var parAlbumCtr = 0;

// album data structure
function AlbumData()
{
	this.id;				    // unique identifier for fetching this album
	this.name;				    // UI name that shows with the thumbnail for this album
	this.date;				    // date shown with thumbnail
	this.sortDate;              // Date to sort by
	this.numPics;			    // number of pictures in the album
	this.thumbnailPath;		    // small image for showing this album in the UI
	this.thumbnailOrientation;  // thumbnail orientation, landscape or portrait
	this.parentAlbum;           // ID of this album's parent
	this.hasChildren;           // has child albums
}

function formatDate(theDate) {
    theDate = theDate.split(' ');
    var justDate = theDate[0];
    return justDate;
}

function GetProPublicEvents() {
    var soaprequest, param;
    param = AddXMLParameter("ProID", ProShopperID);
    param = WrapXML("GetProPublicEvents", "xmlns='http://webservice.lifepics.com'", param);

    document.body.style.cursor="wait";

	soaprequest = CreateSOAPRequestXML(param);
	SubmitSOAPRequest(soaprequest, "http://webservice.lifepics.com/GetProPublicEvents", GetProPublicEventsResponse);
}

function GetProPublicEventsResponse() 
{
    if(GotGoodResponse())
    { 
        var dom = http.responseXML;
        var body = dom.getElementsByTagName("Body");
        
        if (body != null) 
        {
            var usersAlbums = dom.getElementsByTagName("UsersAlbums");
            if(usersAlbums) 
            {
                parAlbumCtr = 0;
                var albums = dom.getElementsByTagName("album");
                albumsXML = albums;
                var totalNumAlbums = albums.length;
                lpAlbums = new Array();
                for (i=0; i<totalNumAlbums; ++i)
                {    
                    if(albums[i].getAttribute("parent") == "0") {
                        parAlbumCtr++;
                    }                    
				    lpAlbums[i] = new AlbumData();
				    lpAlbums[i].name = albums[i].getAttribute("name");
				    lpAlbums[i].date = albums[i].getAttribute("date");
				    zeDate = formatDate(lpAlbums[i].date);
				    zeDate = zeDate.split('/');
				    zeSDate = zeDate[2] + " " + (zeDate[0].length == 1 ? "0" + zeDate[0] : zeDate[0]) + " " + (zeDate[1].length == 1 ? "0" + zeDate[1] : zeDate[1]);
				    lpAlbums[i].sortDate = zeSDate;
				    lpAlbums[i].numPics = albums[i].getAttribute("numImages");
				    lpAlbums[i].id = albums[i].getAttribute("id");
				    lpAlbums[i].thumbnailPath = albums[i].getAttribute("path");
				    lpAlbums[i].thumbnailOrientation = albums[i].getAttribute("orientation");
				    lpAlbums[i].parentAlbum = albums[i].getAttribute("parent");
				    lpAlbums[i].hasChildren = albums[i].getAttribute("hasChildren");
                }
				lpAlbumsFull = lpAlbums;
				if(parAlbumCtr <= showNumAlbums)
				{
				    if(document.getElementById("showall")) {
				        document.getElementById("showall").style.display = 'none';
				    }
				}
				
				// Get the sort order for this user
				/*var SortTypeNode = dom.getElementsByTagName("SortType")[0];
				if(SortTypeNode) 
				{
			        switch(SortTypeNode.getAttribute("type")) 
			        {
                        case "title":
                            document.getElementById("ddlSortBy").selectedIndex = 0;
                            break;
				        case "titleRev":
				            document.getElementById("sortDDL").selectedIndex = 1;
				            break;
				        case "dateNewOld":
				            document.getElementById("sortDDL").selectedIndex = 2;
				            break;
				        case "dateOldNew":
				            document.getElementById("sortDDL").selectedIndex = 3;
				            break;
                    }
                }*/
				// Render the albums page
				document.body.style.cursor='default';
			    RenderAlbums();		
			}
		}
	}
}

function RenderAlbums() 
{
    var rowCount = 0;
    var albumCounter = 0;
    var hasAlbums = false;
    var curRow = 0;
    parAlbumCtr = 0;

    switch(sortType) 
    {
        case "1":
            lpAlbums.sort(sortNameAsc);
            break;
        case "2":
            lpAlbums.sort(sortNameDesc);
            break;
        case "3":
            lpAlbums.sort(sortDateDesc);
            break;
        case "4":
            lpAlbums.sort(sortDateAsc);
            break;
    }
    
    var upperLimit = curAlbumPage * showNumAlbums;
    var lowerLimit = upperLimit - showNumAlbums;
    //Display thumbnail style
    var albumsTable = document.getElementById("thumbsTable");

    if (albumsTable)
    {
        for(var row = 0; row < albumsTable.rows.length; row++) 
        {
            albumsTable.removeChild(albumsTable.lastChild);
        }

        var tableRows = albumsTable.rows.length;
        if(lpAlbums.length == 0) 
        {
            var noAlbumsRow = albumsTable.insertRow(tableRows);
            var noAlbumsCell = noAlbumsRow.insertCell(0);
            if(document.getElementById("recentPublicEvents")) {
                document.getElementById("recentPublicEvents").style.display='none';
            }
        } 
        else  //album.length > 0 
        {
            for(albumKey=lowerLimit; albumKey < upperLimit; albumKey++) 
            {
                if(lpAlbums[albumKey] != null) 
                {
                    //only draw if parent album.....WHY??  NMS taking this out.
                    //if(lpAlbums[albumKey].parentAlbum == "0") 
                    //{
                        hasAlbums = true;

                        //if the number of albums is less than the number per page
                        if(parAlbumCtr < showNumAlbums) 
                        {
                            if(rowCount == 0)
                            {
                                if(navigator.userAgent.indexOf('Safari') != -1) 
                                {
                                    curRow = document.createElement('tr');
                                    albumsTable.appendChild(curRow);
                                } 
                                else 
                                {
                                    curRow = albumsTable.insertRow(-1);
                                } 
                            } //if(rowCount == 0)
                            curRow.setAttribute("id", "row" + tableRows);
                            tableRows++;
                          
                            var newCell = document.createElement('td');
                            curRow.appendChild(newCell);
                            newCell.className = "thumbtd1";
                            newCell.setAttribute("valign", "top");
                            newCell.setAttribute("align", "center");
                        
                            var scaleDown = "";
                            var divClass = "";
                            if(lpAlbums[albumKey].thumbnailOrientation == "landscape") 
                            {
                                scaleDown = "width='100'";
                                divClass = "event_imgs";
                            } 
                            else 
                            {
                                scaleDown = "height='100'";
                                divClass = "event_imgs";
                            }
                            
                            var imageName = lpAlbums[albumKey].thumbnailPath.toLowerCase();
                            if(imageName.indexOf('albumempty') > -1) 
                            {
                                scaleDown = "";
                                divClass = "thumbdiv_empty";
                            }

                            var albumLink = "";
                            albumLink = "EventInfo.aspx?id=" + lpAlbums[albumKey].id;
                            
                            var thumbPath = "";
                            if(imageName.indexOf('albumempty') > -1) {
                                thumbPath = imageName;
                            } else {
                                if(imageName.indexOf("maxsize") == -1) {
                                    thumbPath = imageName + "&maxSize=" + thumbnailSize + "&matte=252525";
                                } else {
                                    thumbPath = lpAlbums[albumKey].thumbnailPath.replace("=100", "=" + thumbnailSize) + "&matte=252525";
                                }
                            }
                                            
                            var theHTML = "<div id='" + lpAlbums[albumKey].id + "'><div class='event_imgs' onclick=\"document.location='" + albumLink + "'\" onmouseover=\"this.style.cursor='pointer'\">";
                            if(imageName.indexOf('albumempty') > -1) {
                                theHTML += "<img src=\"" + thumbPath + "\" alt='" + lpAlbums[albumKey].name + "' class='recevtImage' style='padding:25px 0 25px 0;' /><br />";
                            }else
                                 theHTML += "<img src=\"" + thumbPath + "\" alt='" + lpAlbums[albumKey].name + "' class='recevtImage' /><br />";
                            theHTML += "<a href='" + albumLink + "'>" + lpAlbums[albumKey].name + "</a><br />";
                            theHTML += formatDate(lpAlbums[albumKey].date) + "</div>";
                            
                            newCell.innerHTML = theHTML;

                            rowCount++;
                            parAlbumCtr++;
                            
                            //rowcount is at number of albums/row - need to add new row
                            if(rowCount == albumsPerRow) 
                            {
                                rowCount = 0;
                                //add another row if albumCounter is less than total albums shown
                                if(parAlbumCtr != showNumAlbums) 
                                {
                                    if(navigator.userAgent.indexOf('Safari') != -1) 
                                    {
                                        var rowDivider = document.createElement('tr');
                                        albumsTable.appendChild(rowDivider);
                                    } 
                                    else 
                                    {
                                        var rowDivider = albumsTable.insertRow(-1);
                                    }
                                    
                                    rowDivider.setAttribute("id", "row" + tableRows);
                                    var cellDivider = rowDivider.insertCell(0);
                                    cellDivider.colSpan = albumsPerRow;
                                    cellDivider.innerHTML = "<div class='horz_whitedivider'><!-- &nbsp; --></div>";
                                    tableRows++;
                                }//if(parAlbumCtr != showNumAlbums)
                            }//if(rowCount == albumsPerRow)
                         }//if(parAlbumCtr < showNumAlbums)                   
                     //}//if(lpAlbums[albumKey].parentAlbum == "0")
                }//if(lpAlbums[imageKey] != null)
            }//  for(imageKey=lowerLimit; imageKey < upperLimit; imageKey++)
            
            if (lowerLimit != upperLimit)
            {
                //only one image being shown - don't show paging
                //BuildAlbumPaging();
            } 
        } //album.length > 0
    }//if (albumsTable)
}